fix(cli/coder): de-duplicate validateBinary to avoid ESM redeclare error under Bun/Node 23

This commit is contained in:
James Peter
2025-08-30 14:15:21 +10:00
parent 64416f8d0d
commit 703e080d26
2 changed files with 68 additions and 31 deletions
+1 -28
View File
@@ -293,34 +293,7 @@ if (existsSync(binaryPath)) {
}
// Lightweight header validation to provide clearer errors before spawn
const validateBinary = (p) => {
try {
const st = statSync(p);
if (!st.isFile() || st.size === 0) {
return { ok: false, reason: "empty or not a regular file" };
}
const fd = openSync(p, "r");
try {
const buf = Buffer.alloc(4);
const n = readSync(fd, buf, 0, 4, 0);
if (n < 2) return { ok: false, reason: "too short" };
if (platform === "win32") {
if (!(buf[0] === 0x4d && buf[1] === 0x5a)) return { ok: false, reason: "invalid PE header (missing MZ)" };
} else if (platform === "linux" || platform === "android") {
if (!(buf[0] === 0x7f && buf[1] === 0x45 && buf[2] === 0x4c && buf[3] === 0x46)) return { ok: false, reason: "invalid ELF header" };
} else if (platform === "darwin") {
const isMachO = (buf[0] === 0xcf && buf[1] === 0xfa && buf[2] === 0xed && buf[3] === 0xfe) ||
(buf[0] === 0xca && buf[1] === 0xfe && buf[2] === 0xba && buf[3] === 0xbe);
if (!isMachO) return { ok: false, reason: "invalid Mach-O header" };
}
} finally {
closeSync(fd);
}
return { ok: true };
} catch (e) {
return { ok: false, reason: e.message };
}
};
// Reuse the validateBinary helper defined above in the bootstrap section.
const validation = validateBinary(binaryPath);
if (!validation.ok) {
+67 -3
View File
@@ -4778,11 +4778,15 @@ pub(crate) fn new_running_browser_tool_call(
tool_name: String,
args: Option<String>,
) -> RunningToolCallCell {
// Parse args JSON and format like completed cells
// Parse args JSON and use compact humanized form when possible
let mut arg_lines: Vec<Line<'static>> = Vec::new();
if let Some(args_str) = args {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&args_str) {
arg_lines.extend(format_browser_args_line(&json));
if let Some(lines) = format_browser_args_humanized(&tool_name, &json) {
arg_lines.extend(lines);
} else {
arg_lines.extend(format_browser_args_line(&json));
}
}
}
RunningToolCallCell {
@@ -5004,6 +5008,62 @@ fn format_browser_args_line(args: &serde_json::Value) -> Vec<Line<'static>> {
lines
}
// Attempt a compact, humanized one-line summary for browser tools.
// Returns Some(lines) when a concise form is available for the given tool, else None.
fn format_browser_args_humanized(
tool_name: &str,
args: &serde_json::Value,
) -> Option<Vec<Line<'static>>> {
use serde_json::Value;
let text = |s: String| Span::styled(s, Style::default().fg(crate::colors::text()));
// Helper: format coordinate pair as integers (pixels)
let fmt_xy = |x: f64, y: f64| -> String {
let xi = x.round() as i64;
let yi = y.round() as i64;
format!("({xi}, {yi})")
};
match (tool_name, args) {
("browser_click", Value::Object(map)) => {
// Expect optional `type`, and x/y for absolute. Only compact when both x and y provided.
let ty = map
.get("type")
.and_then(|v| v.as_str())
.unwrap_or("click")
.to_lowercase();
let (x, y) = match (
map.get("x").and_then(|v| v.as_f64()),
map.get("y").and_then(|v| v.as_f64()),
) {
(Some(x), Some(y)) => (x, y),
_ => return None,
};
let msg = format!("└ {ty} at {}", fmt_xy(x, y));
Some(vec![Line::from(text(msg))])
}
("browser_move", Value::Object(map)) => {
// Prefer absolute x/y → "to (x, y)"; otherwise relative dx/dy → "by (dx, dy)".
if let (Some(x), Some(y)) = (
map.get("x").and_then(|v| v.as_f64()),
map.get("y").and_then(|v| v.as_f64()),
) {
let msg = format!("└ to {}", fmt_xy(x, y));
return Some(vec![Line::from(text(msg))]);
}
if let (Some(dx), Some(dy)) = (
map.get("dx").and_then(|v| v.as_f64()),
map.get("dy").and_then(|v| v.as_f64()),
) {
let msg = format!("└ by {}", fmt_xy(dx, dy));
return Some(vec![Line::from(text(msg))]);
}
None
}
_ => None,
}
}
fn new_completed_browser_tool_call(
tool_name: String,
args: Option<String>,
@@ -5041,7 +5101,11 @@ fn new_completed_browser_tool_call(
let mut arg_lines: Vec<Line<'static>> = Vec::new();
if let Some(args_str) = args {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&args_str) {
arg_lines.extend(format_browser_args_line(&json));
if let Some(lines) = format_browser_args_humanized(&tool_name, &json) {
arg_lines.extend(lines);
} else {
arg_lines.extend(format_browser_args_line(&json));
}
}
}