Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(artifacts): extract docx text via python3 so panel shows content

Sanil045a041c2325
diff --git a/crates/openhuman-app/src/artifact_commands.rs b/crates/openhuman-app/src/artifact_commands.rs
index 1ba2cb2658..3021488949 100644
--- a/crates/openhuman-app/src/artifact_commands.rs
+++ b/crates/openhuman-app/src/artifact_commands.rs
@@ -90,21 +90,27 @@ pub async fn read_artifact_body(artifact_id: String) -> Result<ArtifactBodyResul
// Truly binary files stay as paths — the panel shows metadata + download.
if kind == "file" && !body.is_empty() {
let file_path = PathBuf::from(&body);
+ // First pass: try the native UTF-8 read (text files like .md, .txt)
match tokio::fs::read(&file_path).await {
Ok(bytes) => {
- // Heuristic: treat as text if it decodes as UTF-8 and has no
- // NUL bytes in the first 8 KiB (binary signatures).
let probe = &bytes[..bytes.len().min(8192)];
- let looks_text = !probe.contains(&0)
- && std::str::from_utf8(probe).is_ok();
- if looks_text {
+ if !probe.contains(&0) && std::str::from_utf8(probe).is_ok() {
if let Ok(text) = String::from_utf8(bytes) {
body = text;
is_text = true;
}
}
}
- Err(_) => { /* leave the path as the body */ }
+ Err(_) => {}
+ }
+ // If still no text and it's a .docx, try to extract text via Python
+ if !is_text && file_path.extension().and_then(|e| e.to_str()) == Some("docx") {
+ if let Ok(text) = crate::docx::extract_text(&file_path) {
+ if !text.is_empty() {
+ body = text;
+ is_text = true;
+ }
+ }
}
}
Ok(ArtifactBodyResult { kind, title, version: versions, body, is_text })
diff --git a/crates/openhuman-app/src/docx.rs b/crates/openhuman-app/src/docx.rs
new file mode 100644
index 0000000000..e706998fc2
--- /dev/null
+++ b/crates/openhuman-app/src/docx.rs
@@ -0,0 +1,41 @@
+//! Minimal `.docx` text extraction for artifact previewing.
+//!
+//! A `.docx` is a ZIP container whose `word/document.xml` holds the text.
+//! Rather than pulling the whole `zip` crate into the shell binary, we
+//! delegate to the platform `python3` (present on macOS and most Linux
+//! systems). If python3 is missing (or the file is not a real docx),
+//! extraction fails cleanly and the panel falls back to the file viewer.
+
+use std::path::Path;
+
+/// Extract plain text from a `.docx` file. Returns empty string on failure.
+pub fn extract_text(path: &Path) -> Result<String, String> {
+ use std::process::Command;
+
+ let script = r#"
+import sys, zipfile, re
+try:
+ with zipfile.ZipFile(sys.argv[1]) as z:
+ xml = z.read('word/document.xml').decode('utf-8')
+ text = re.sub(r'<[^>]+>', ' ', xml)
+ text = re.sub(r'\s+', ' ', text).strip()
+ sys.stdout.write(text)
+except Exception:
+ sys.exit(1)
+"#;
+
+ let output = Command::new("python3")
+ .arg("-c")
+ .arg(script)
+ .arg(path.as_os_str())
+ .output()
+ .map_err(|e| format!("python3 unavailable: {e}"))?;
+
+ if !output.status.success() {
+ return Err(format!(
+ "docx extraction failed: {}",
+ String::from_utf8_lossy(&output.stderr).trim()
+ ));
+ }
+ Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
+}
diff --git a/crates/openhuman-app/src/lib.rs b/crates/openhuman-app/src/lib.rs
index edf11feaaf..583a1c454a 100644
--- a/crates/openhuman-app/src/lib.rs
+++ b/crates/openhuman-app/src/lib.rs
@@ -69,6 +69,7 @@ mod app_update;
// Artifact export command (#2779) — cross-platform Downloads copy. The `rfd`
// Save-As dialog that used to sit in front of it was removed with the crate.
mod artifact_commands;
+mod docx;
mod claude_code;
mod core_process;
mod core_rpc;