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
169 changes: 111 additions & 58 deletions crabport-agent/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ use crate::tunnel::{AgentTunnel, AgentTunnelState};
pub struct ToolOutcome {
/// The text handed back to the model (the approval card renders it too).
pub text: String,
/// The tabular view of the result, when the result is an enumerable
/// list. The model's copy (in `text`) is the same cells as JSON; the card
/// renders the table natively instead of letting the model re-render
/// markdown over it.
pub table: Option<ToolTable>,
/// Exit status of a captured command, when the backend reported one.
pub exit_code: Option<u32>,
/// A captured command outlived its deadline; the output is what was
Expand All @@ -40,6 +45,15 @@ pub struct ToolOutcome {
pub cancelled: bool,
}

/// A list-style tool result as labelled cells: what the panel renders as a
/// native table, and what the model receives as JSON objects keyed by the
/// headers.
#[derive(Clone, Debug)]
pub struct ToolTable {
pub headers: Vec<String>,
pub rows: Vec<Vec<String>>,
}

impl ToolOutcome {
/// An outcome that is plain text and nothing else.
pub fn text(text: impl Into<String>) -> Self {
Expand All @@ -48,6 +62,35 @@ impl ToolOutcome {
..Default::default()
}
}

/// A list result: the model's text is the rows as a JSON array of
/// objects (headers as keys), the panel renders the table.
pub fn table(headers: &[&str], rows: Vec<Vec<String>>) -> Self {
let headers: Vec<String> = headers.iter().map(|header| header.to_string()).collect();
let text = table_json(&headers, &rows);
Self {
text,
table: Some(ToolTable { headers, rows }),
..Default::default()
}
}
}

/// Serialize labelled rows as a compact JSON array of objects, one per row.
/// Derived from the same cells the card's table renders, so what the model
/// reasons over and what the user sees cannot drift.
fn table_json(headers: &[String], rows: &[Vec<String>]) -> String {
let objects: Vec<Value> = rows
.iter()
.map(|row| {
let mut object = serde_json::Map::new();
for (header, cell) in headers.iter().zip(row) {
object.insert(header.clone(), Value::String(cell.clone()));
}
Value::Object(object)
})
.collect();
serde_json::to_string(&objects).unwrap_or_default()
}

/// How a tool call is running.
Expand Down Expand Up @@ -189,6 +232,7 @@ impl Agent {
exit_code: out.exit_code,
timed_out: out.timed_out,
cancelled: out.cancelled,
..Default::default()
});
}),
);
Expand Down Expand Up @@ -261,6 +305,7 @@ impl Agent {
exit_code: None,
timed_out,
cancelled,
..Default::default()
})
.await;
})
Expand Down Expand Up @@ -317,15 +362,15 @@ impl Agent {
let is_file = name == "read_file";
let (tx, rx) = bounded(1);
smol::spawn(async move {
let text = smol::unblock(move || {
let outcome = smol::unblock(move || {
if is_file {
crate::local_fs::read_local_file(&path)
} else {
crate::local_fs::read_local_directory(&path)
}
})
.await;
let _ = tx.send(ToolOutcome::text(text)).await;
let _ = tx.send(outcome).await;
})
.detach();
Executed::Pending {
Expand Down Expand Up @@ -354,25 +399,26 @@ impl Agent {
let (tx, rx) = bounded(1);
smol::spawn(async move {
let started = Instant::now();
let mut text = None;
let mut outcome = None;
while started.elapsed() < SFTP_LIST_TIMEOUT {
smol::Timer::after(OUTPUT_POLL).await;
// The backend logs a failed navigation silently; the timeout
// below is what reports it.
let Some((cwd, entries)) = session.sftp_listing() else {
let Some((_cwd, entries)) = session.sftp_listing() else {
continue;
};
let fresh = match &before {
None => true,
Some(old) => !Arc::ptr_eq(old, &entries),
};
if fresh {
text = Some(format_remote_listing(&cwd, &entries));
outcome = Some(format_remote_listing(&entries));
break;
}
}
let text = text.unwrap_or_else(|| t!("ai_panel.tool_sftp_list_timeout").to_string());
let _ = tx.send(ToolOutcome::text(text)).await;
let outcome =
outcome.unwrap_or_else(|| ToolOutcome::text(t!("ai_panel.tool_sftp_list_timeout")));
let _ = tx.send(outcome).await;
})
.detach();
Executed::Pending {
Expand Down Expand Up @@ -477,7 +523,7 @@ impl Agent {
args: &Value,
) -> Executed {
match name {
"tunnel_list" => Executed::Done(ToolOutcome::text(self.tunnel_list_text(session))),
"tunnel_list" => Executed::Done(self.tunnel_list_outcome(session)),
"tunnel_create" => self.tunnel_create(args),
"tunnel_open" | "tunnel_close" | "tunnel_delete" => {
let Some(token) = args.get("tunnel_id").and_then(|id| id.as_str()) else {
Expand All @@ -501,62 +547,49 @@ impl Agent {
}

/// The tunnel list: the agent's own first (their ids are the actionable
/// ones), then the Tunnels-page configs for this host.
fn tunnel_list_text(&self, session: &Arc<dyn AgentSession>) -> String {
/// ones), then the Tunnels-page configs for this host. The model receives
/// the rows as JSON (a `tool` message), the card renders the same cells
/// as a table — see [`ToolOutcome::table`].
fn tunnel_list_outcome(&self, session: &Arc<dyn AgentSession>) -> ToolOutcome {
let manager = session.tunnel_manager();
let mut lines: Vec<String> = Vec::new();
let mut rows: Vec<Vec<String>> = Vec::new();
{
let tunnels = self.tunnels.lock().unwrap_or_else(|e| e.into_inner());
if !tunnels.is_empty() {
lines.push(t!("ai_panel.tool_tunnel_list_mine").to_string());
lines.extend(
tunnels
.iter()
.map(|tunnel| tunnel.describe(manager.as_ref())),
);
}
rows.extend(
tunnels
.iter()
.map(|tunnel| tunnel.list_row(manager.as_ref())),
);
}
let configured: Vec<String> = session
.registry_tunnels()
.into_iter()
.map(|view| {
let source = if view.borrowed {
t!("ai_panel.tunnel_source_borrowed")
} else {
t!("ai_panel.tunnel_source_owned")
};
// Both ends of a local/remote tunnel go into the result — the
// model needs host *and* port to use or talk about it.
let mut addresses = format!("{}:{}", view.bind_addr, view.bind_port);
if !view.target_host.is_empty() && view.target_port != 0 {
addresses.push_str(&format!(" -> {}:{}", view.target_host, view.target_port));
}
let state = if view.running {
t!("ai_panel.tunnel_running_at", bind = addresses)
} else {
t!("ai_panel.tunnel_stopped_at", bind = addresses)
};
// The id token leads the line: it is what the open / close /
// delete calls take.
format!(
"t{} {} ({}, {}) — {}",
view.id,
view.name,
view.kind.as_str(),
source,
state
)
})
.collect();
if !configured.is_empty() {
lines.push(t!("ai_panel.tool_tunnel_list_host").to_string());
lines.extend(configured);
for view in session.registry_tunnels() {
let source = if view.borrowed {
t!("ai_panel.tunnel_source_borrowed")
} else {
t!("ai_panel.tunnel_source_owned")
};
// Both ends of a local/remote tunnel go into one cell — the model
// needs host *and* port to use or talk about it.
let mut address = format!("{}:{}", view.bind_addr, view.bind_port);
if !view.target_host.is_empty() && view.target_port != 0 {
address.push_str(&format!(" -> {}:{}", view.target_host, view.target_port));
}
let status = if view.running {
t!("ai_panel.tunnel_state_running")
} else {
t!("ai_panel.tunnel_state_stopped")
};
rows.push(vec![
format!("t{}", view.id),
view.name,
view.kind.as_str().to_string(),
address,
format!("{status} · {source}"),
]);
}
if lines.is_empty() {
t!("ai_panel.tool_tunnel_list_empty").to_string()
} else {
lines.join("\n")
if rows.is_empty() {
return ToolOutcome::text(t!("ai_panel.tool_tunnel_list_empty"));
}
ToolOutcome::table(&["id", "name", "kind", "address", "status"], rows)
}

/// Define a session-local tunnel. Not started, never persisted.
Expand Down Expand Up @@ -934,3 +967,23 @@ fn split_token(token: &str) -> (char, Option<u64>) {
token[1..].parse::<u64>().ok(),
)
}

#[cfg(test)]
mod tests {
use super::*;

/// A list result reaches the model as a JSON array of objects keyed by
/// the headers — the same cells the card renders as a table.
#[test]
fn table_outcome_serializes_rows_as_objects() {
let outcome =
ToolOutcome::table(&["id", "status"], vec![vec!["a1".into(), "running".into()]]);
assert_eq!(outcome.text, r#"[{"id":"a1","status":"running"}]"#);
let table = outcome.table.expect("table present");
assert_eq!(table.headers, ["id", "status"]);
assert_eq!(
table.rows,
vec![vec!["a1".to_string(), "running".to_string()]]
);
}
}
2 changes: 1 addition & 1 deletion crabport-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod text;
pub mod tools;
pub mod tunnel;

pub use agent::{Agent, Executed, ToolOutcome};
pub use agent::{Agent, Executed, ToolOutcome, ToolTable};
pub use compaction::{
COMPACTED_HEADER, COMPACTION_PROMPT, CompactCall, CompactTurn, TurnRole, compaction_cut,
compaction_transcript,
Expand Down
55 changes: 29 additions & 26 deletions crabport-agent/src/local_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,41 @@ use std::io::Read as _;

use rust_i18n::t;

use crate::agent::ToolOutcome;
use crate::limits::{READ_DIR_LIMIT, READ_FILE_LIMIT};
use crate::text::cap_tool_result_head;

/// Read up to [`READ_FILE_LIMIT`] bytes of a local file; binary content is
/// reported rather than dumped. Never fails — every problem becomes text.
pub fn read_local_file(path: &str) -> String {
pub fn read_local_file(path: &str) -> ToolOutcome {
let file = match std::fs::File::open(path) {
Ok(file) => file,
Err(err) => return format!("cannot open {path}: {err}"),
Err(err) => return ToolOutcome::text(format!("cannot open {path}: {err}")),
};
let size = file.metadata().map(|meta| meta.len()).unwrap_or(0);
let mut reader = file.take(READ_FILE_LIMIT);
let mut buf = Vec::new();
if let Err(err) = reader.read_to_end(&mut buf) {
return format!("reading {path} failed: {err}");
if reader.read_to_end(&mut buf).is_err() {
return ToolOutcome::text(format!("reading {path} failed: I/O error"));
}
if buf.contains(&0) {
return t!("ai_panel.tool_read_file_binary", bytes = size.to_string()).to_string();
return ToolOutcome::text(t!(
"ai_panel.tool_read_file_binary",
bytes = size.to_string()
));
}
cap_tool_result_head(&String::from_utf8_lossy(&buf))
ToolOutcome::text(cap_tool_result_head(&String::from_utf8_lossy(&buf)))
}

/// List a local directory: one line per entry, directories marked with a
/// trailing slash, capped at [`READ_DIR_LIMIT`]. Never fails — every problem
/// becomes text.
pub fn read_local_directory(path: &str) -> String {
/// List a local directory: one table row per entry (name / type / size,
/// as the model sees them — directories say `dir`), capped at
/// [`READ_DIR_LIMIT`]. Never fails — every problem becomes text.
pub fn read_local_directory(path: &str) -> ToolOutcome {
let dir = match std::fs::read_dir(path) {
Ok(dir) => dir,
Err(err) => return format!("cannot list {path}: {err}"),
Err(err) => return ToolOutcome::text(format!("cannot list {path}: {err}")),
};
let mut rows: Vec<String> = Vec::new();
let mut rows: Vec<Vec<String>> = Vec::new();
let mut overflow = 0usize;
for entry in dir {
let Ok(entry) = entry else { continue };
Expand All @@ -44,23 +48,22 @@ pub fn read_local_directory(path: &str) -> String {
}
let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
let name = entry.file_name().to_string_lossy().into_owned();
if is_dir {
rows.push(format!("{name}/"));
} else {
let size = entry
.metadata()
.map(|meta| meta.len().to_string())
.unwrap_or_else(|_| "?".to_string());
rows.push(format!("{name} {size}"));
}
let kind = if is_dir { "dir" } else { "file" }.to_string();
let size = entry
.metadata()
.map(|meta| meta.len().to_string())
.unwrap_or_else(|_| "?".to_string());
rows.push(vec![name, kind, size]);
}
if rows.is_empty() {
return t!("ai_panel.tool_dir_empty").to_string();
return ToolOutcome::text(t!("ai_panel.tool_dir_empty"));
}
let mut out = rows.join("\n");
let mut outcome = ToolOutcome::table(&["name", "type", "size"], rows);
if overflow > 0 {
out.push('\n');
out.push_str(&t!("ai_panel.tool_dir_truncated", more = overflow));
outcome.text.push_str(&format!(
"\n{}",
t!("ai_panel.tool_dir_truncated", more = overflow)
));
}
out
outcome
}
Loading
Loading