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
41 changes: 24 additions & 17 deletions cmux-tui/crates/acpmux/src/cloud_repo_mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ pub async fn run() -> Result<()> {

fn ok(result: Value) -> Value {
let text = result_text(&result);
let code = result["code"].as_str().or(result["error"]["code"].as_str());
let code = result
.get("code")
.and_then(Value::as_str)
.or_else(|| result.pointer("/error/code").and_then(Value::as_str));
let is_error = code.is_some_and(|code| !matches!(code, "repo.needs_github" | "repo.ambiguous"));
json!({"content":[{"type":"text","text":text}],"structuredContent":result,"isError":is_error})
}
Expand All @@ -75,15 +78,19 @@ fn result_text(result: &Value) -> String {
result.get("error").filter(|v| v.is_object()).unwrap_or(&Value::Null)
};
if error.get("code").and_then(Value::as_str).is_some() {
let mut text = error["message"].as_str().unwrap_or("Repository action failed").to_owned();
if error["code"].as_str() == Some("repo.needs_github") {
let mut text = error
.get("message")
.and_then(Value::as_str)
.unwrap_or("Repository action failed")
.to_owned();
if error.get("code").and_then(Value::as_str) == Some("repo.needs_github") {
text.push_str(
" Connect GitHub; this add will be retried automatically when it finishes.",
);
}
return text;
}
if let Some(repos) = result["repos"].as_array() {
if let Some(repos) = result.get("repos").and_then(Value::as_array) {
return if repos.is_empty() {
"No repositories match. Connect GitHub or use a local project and try again.".to_owned()
} else {
Expand All @@ -94,11 +101,11 @@ fn result_text(result: &Value) -> String {
)
};
}
if result["state"].as_str() == Some("done") {
if result.get("state").and_then(Value::as_str) == Some("done") {
return format!(
"Attached {} at {}.",
result["repo"].as_str().unwrap_or("the repository"),
result["folder"].as_str().unwrap_or("the chat folder")
result.get("repo").and_then(Value::as_str).unwrap_or("the repository"),
result.get("folder").and_then(Value::as_str).unwrap_or("the chat folder")
);
}
serde_json::to_string(result).unwrap_or_else(|_| "Repository action completed.".to_owned())
Expand Down Expand Up @@ -130,15 +137,15 @@ async fn run_cli_with(
if action == REPO_LIST_ACTION { "list" } else { "add" },
"--session",
]);
let session = request["session"].as_str().unwrap_or_default();
let session = request.get("session").and_then(Value::as_str).unwrap_or_default();
command.arg(session);
if action == REPO_LIST_ACTION {
if let Some(query) = request["query"].as_str() {
if let Some(query) = request.get("query").and_then(Value::as_str) {
command.args(["--query", query]);
}
} else {
command.args(["--repo", request["repo"].as_str().unwrap_or_default()]);
if let Some(reference) = request["ref"].as_str() {
command.args(["--repo", request.get("repo").and_then(Value::as_str).unwrap_or_default()]);
if let Some(reference) = request.get("ref").and_then(Value::as_str) {
command.args(["--ref", reference]);
}
}
Expand Down Expand Up @@ -168,8 +175,8 @@ async fn run_cli_with(
// restore the shared flat typed action result for the agent.
if value.get("error_code").is_some() {
return Ok(json!({
"code": value["error_code"].as_str().unwrap_or("repo.unavailable"),
"message": value["error"].as_str().unwrap_or("Reconnect to the Cloud machine and retry.")
"code": value.get("error_code").and_then(Value::as_str).unwrap_or("repo.unavailable"),
"message": value.get("error").and_then(Value::as_str).unwrap_or("Reconnect to the Cloud machine and retry.")
}));
}
}
Expand All @@ -195,7 +202,7 @@ pub async fn call(_daemon: &Arc<Client>, session: &str, params: &Value) -> Value
Ok(v) => v,
Err(v) => return v,
};
let name = params["name"].as_str().unwrap_or_default();
let name = params.get("name").and_then(Value::as_str).unwrap_or_default();
match name {
"list_repos" => {
let mut req = json!({"session": session});
Expand Down Expand Up @@ -240,12 +247,12 @@ pub async fn call(_daemon: &Arc<Client>, session: &str, params: &Value) -> Value
fn enrich_retry(result: &mut Value, session: &str, repo: &str, reference: Option<&Value>) {
let error = if result.get("code").and_then(Value::as_str).is_some() {
result
} else if result["error"].is_object() {
&mut result["error"]
} else if let Some(error) = result.get_mut("error").filter(|error| error.is_object()) {
error
} else {
return;
};
if error["code"].as_str() != Some("repo.needs_github") {
if error.get("code").and_then(Value::as_str) != Some("repo.needs_github") {
return;
}
let mut args = json!({"session": session, "repo": repo});
Expand Down
2 changes: 1 addition & 1 deletion cmux-tui/crates/acpmux/src/hub/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl Hub {
// their first names, and its running background commands (`running_work`).
let work = running_work(
session,
summary["turn"].is_object(),
summary.get("turn").is_some_and(Value::is_object),
self.running_processes_read(session),
);
summary.set("subagentsRunning", json!(work.subagents));
Expand Down
6 changes: 4 additions & 2 deletions cmux-tui/crates/acpmux/src/subagents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,10 @@ impl SubagentTree {
.map(|t| truncate_label(&t));
match at {
Some(at) => {
if self.running[at].1.is_none() {
self.running[at].1 = label;
if let Some(slot) = self.running.get_mut(at)
&& slot.1.is_none()
{
slot.1 = label;
}
}
None => self.running.push((id.to_owned(), label)),
Expand Down
11 changes: 8 additions & 3 deletions cmux-tui/crates/cmux-host/src/cloud/github_repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn failure_with_install(
get: &mut impl FnMut(&str) -> Answer,
) -> Value {
let mut reply = failure(answer);
if reply["error"]["code"] == "repo.no_access"
if reply.pointer("/error/code").and_then(Value::as_str) == Some("repo.no_access")
&& let Answer::Http { status: 200, body } = get(&team_path(INSTALL_PATH, team))
&& let Some(url) =
body.get("url").and_then(Value::as_str).filter(|url| url.starts_with("https://"))
Expand Down Expand Up @@ -158,7 +158,10 @@ pub fn answer(ask: &ReposAsk, team: &str, mut get: impl FnMut(&str) -> Answer) -
let state = body
.get("connectors")
.and_then(Value::as_array)
.and_then(|rows| rows.iter().find(|row| row["provider"] == "github"))
.and_then(|rows| {
rows.iter()
.find(|row| row.get("provider").and_then(Value::as_str) == Some("github"))
})
.and_then(|row| row.get("state"))
.and_then(Value::as_str);
match state {
Expand Down Expand Up @@ -212,7 +215,9 @@ pub fn answer(ask: &ReposAsk, team: &str, mut get: impl FnMut(&str) -> Answer) -
reply
}
Answer::Http { status: 404, .. } => connect(team, &mut get),
Answer::Http { status: 409, body } if body["_tag"] == "ConnectorNeedsAuth" => {
Answer::Http { status: 409, body }
if body.get("_tag").and_then(Value::as_str) == Some("ConnectorNeedsAuth") =>
{
connect(team, &mut get)
}
_ => failure_with_install(&listed, team, &mut get),
Expand Down
37 changes: 20 additions & 17 deletions cmux-tui/crates/cmux-tui-core/src/cloud_repos/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,13 @@ fn choose(matches: Vec<Repo>) -> Result<String, Failure> {
}

pub(crate) fn add(backend: &impl Backend, args: &Value) -> Result<Value, Failure> {
let session = args["session"].as_str().filter(|s| valid_name(s)).ok_or_else(|| {
Failure::new("seed.bad_request", "Open a Cloud chat and retry with its chat ID.")
})?;
let input = args["repo"]
.as_str()
let session =
args.get("session").and_then(Value::as_str).filter(|s| valid_name(s)).ok_or_else(|| {
Failure::new("seed.bad_request", "Open a Cloud chat and retry with its chat ID.")
})?;
let input = args
.get("repo")
.and_then(Value::as_str)
.ok_or_else(|| Failure::new("seed.bad_request", "Choose a repository to add."))?;
let reference = match args.get("ref") {
None | Some(Value::Null) => None,
Expand All @@ -216,15 +218,13 @@ pub(crate) fn add(backend: &impl Backend, args: &Value) -> Result<Value, Failure
};
let result = (|| {
let repo = resolve(backend, input)?;
if let Some(existing) = backend
.attached(session)?
.into_iter()
.find(|r| r["repo"].as_str().is_some_and(|n| n.eq_ignore_ascii_case(&repo)))
{
if let Some(existing) = backend.attached(session)?.into_iter().find(|r| {
r.get("repo").and_then(Value::as_str).is_some_and(|n| n.eq_ignore_ascii_case(&repo))
}) {
return Ok(existing);
}
let attached = backend.clone_repo(session, &repo, reference).map_err(|mut failure| {
if failure.0["code"] == "repo.no_access" {
if failure.0.get("code").and_then(Value::as_str) == Some("repo.no_access") {
failure.0.set("org", json!(repo.split('/').next().unwrap_or_default()));
}
failure
Expand All @@ -233,12 +233,12 @@ pub(crate) fn add(backend: &impl Backend, args: &Value) -> Result<Value, Failure
Ok(attached)
})();
result.map_err(|mut failure: Failure| {
if failure.0["code"] == "repo.needs_github" {
if failure.0.get("code").and_then(Value::as_str) == Some("repo.needs_github") {
let mut retry_args = json!({"session":session,"repo":input});
if let Some(reference) = reference { retry_args.set("ref", json!(reference)); }
failure.0.set("retry", json!({"action":"cloud.repo.add", "args":retry_args, "wait_action":"cloud.repo.wait_github"}));
}
if failure.0["code"] == "repo.no_access"
if failure.0.get("code").and_then(Value::as_str) == Some("repo.no_access")
&& let Ok(name) = normalize(input)
&& let Some((org, _)) = name.split_once('/')
{
Expand All @@ -249,12 +249,15 @@ pub(crate) fn add(backend: &impl Backend, args: &Value) -> Result<Value, Failure
}

pub(crate) fn seed_failure(value: &Value) -> Failure {
let code = value["error_code"]
.as_str()
.or_else(|| value["code"].as_str())
let code = value
.get("error_code")
.and_then(Value::as_str)
.or_else(|| value.get("code").and_then(Value::as_str))
.unwrap_or("seed.clone_failed");
if code == "seed.needs_github" || code == "repo.needs_github" {
return Failure::needs_github(value["install_url"].as_str().unwrap_or(""));
return Failure::needs_github(
value.get("install_url").and_then(Value::as_str).unwrap_or(""),
);
}
if code == "repo.no_access" || code == "seed.no_access" {
let mut result = Failure::new(
Expand Down
7 changes: 4 additions & 3 deletions cmux-tui/crates/cmux-tui-core/src/cloud_repos/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ pub(crate) fn local_catalog(mux: &Mux) -> anyhow::Result<Value> {
let projects = mux.read_registry_state(|connection| {
crate::state::projects_store::list_value(connection, false, None, None)
})?;
let paths: Vec<&str> = projects["projects"]
.as_array()
let paths: Vec<&str> = projects
.get("projects")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|project| project["path"].as_str())
.filter_map(|project| project.get("path").and_then(Value::as_str))
.collect();
let deadline = Instant::now() + CATALOG_DEADLINE;
Ok(catalog(&paths, |path| read_remote(path, deadline)))
Expand Down
Loading
Loading