Skip to content

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

chore(tools): remove custom html-to-markdown converter in favour of tinyjuice

Steven Enamakel64a3485b7bc5
diff --git a/crates/openhuman-core/src/tools/impl/network/html_markdown.rs b/crates/openhuman-core/src/tools/impl/network/html_markdown.rs
deleted file mode 100644
index b5f62db59e..0000000000
--- a/crates/openhuman-core/src/tools/impl/network/html_markdown.rs
+++ /dev/null
@@ -1,468 +0,0 @@
-//! HTML → Markdown for `web_fetch`.
-//!
-//! `web_fetch` used to hand the model — and, worse, the payload
-//! summarizer — the raw response body: minified JS, inline CSS, SVG path
-//! data and nav chrome included. A wire capture of one research turn
-//! showed seven summarizer calls at 68k–197k prompt tokens each and
-//! 1,083,069 input tokens for the turn, nearly all of it markup the
-//! model had no use for.
-//!
-//! Reference agents don't pay that. Hermes' `web_extract` budgets 15k
-//! chars of *clean markdown* (its vendors do the extraction); Codex
-//! never fetches a page client-side at all. Extraction is the cheap half
-//! of the gap: dropping markup typically takes a page an order of
-//! magnitude under the summarizer's 16 KB trigger, so the expensive path
-//! stops firing rather than being made faster.
-//!
-//! Markdown rather than flat text because the structure is the part the
-//! model acts on: headings tell it which section answers the question,
-//! and link targets are what it feeds the next `web_fetch`. A text
-//! extractor throws both away.
-//!
-//! ## Why this isn't TinyJuice's extractor
-//!
-//! `vendor/tinyjuice/src/compressors/html.rs` does something close, but
-//! it is (a) html→text, keeping no headings or hrefs, and (b) behind the
-//! loadable-module boundary: OpenHuman links only `tinyjuice-bus`, and
-//! `inference/tokenjuice/README.md` forbids adding the engine crate
-//! back. Reaching it needs the `modules` feature, a loaded cdylib and
-//! `compaction_enabled` (default `false`). A core tool's default output
-//! must not depend on whether an optional module loaded, so extraction
-//! lives here. If the two ever need to agree, this is the host-side one.
-//!
-//! This is a scanner, not a parser: the tree shape doesn't matter, only
-//! "which bytes would a reader see, and what role did they play".
-//! `html5ever` and its `markup5ever`/`string_cache` retinue is a large
-//! dependency for a crate that ships no HTML parser today, in a repo
-//! that actively sheds dependencies (`scripts/assert-shed.sh`).
-//! Malformed markup degrades to "some tags show up as text" — the same
-//! failure the raw body already had.
-
-/// Elements whose *content* is machinery, not prose. Everything up to
-/// the matching close tag is dropped, nested markup included.
-const DROPPED_ELEMENTS: &[&str] = &[
- "script", "style", "noscript", "template", "svg", "canvas", "iframe", "object", "embed",
- "math", "form", "select",
-];
-
-/// Elements whose body is taken verbatim and re-emitted as a fenced
-/// block, so indentation and line breaks in code survive.
-const VERBATIM_ELEMENTS: &[&str] = &["pre"];
-
-/// Elements that force a line break when they open or close, so that
-/// stripping tags doesn't run a heading into the paragraph after it.
-const BLOCK_ELEMENTS: &[&str] = &[
- "address", "article", "aside", "blockquote", "dd", "div", "dl", "dt", "fieldset", "figcaption",
- "figure", "footer", "header", "hr", "main", "nav", "ol", "p", "section", "table", "tbody",
- "td", "tfoot", "th", "thead", "tr", "ul",
-];
-
-/// Upper bound on a single extracted link target. A tracking URL can run
-/// to several KB of query string, which is pure cost in the transcript.
-const MAX_HREF_CHARS: usize = 300;
-
-/// Does this look like HTML at all? Cheap sniff over the first few KB:
-/// a doctype, an `<html`/`<head`/`<body` root, or enough tag-shaped runs
-/// to outweigh coincidence in prose or JSON.
-pub fn looks_like_html(body: &str) -> bool {
- let head = &body[..crate::util::floor_char_boundary(body, 4096)];
- let lower = head.to_ascii_lowercase();
- if lower.contains("<!doctype html")
- || lower.contains("<html")
- || lower.contains("<head")
- || lower.contains("<body")
- {
- return true;
- }
- // A JSON document quoting "<b>" shouldn't trip this; a fragment of
- // real markup should.
- ["<div", "<p>", "<p ", "<span", "<a ", "<table", "<li>", "<script", "<meta"]
- .iter()
- .filter(|needle| lower.contains(*needle))
- .count()
- >= 3
-}
-
-/// Convert an HTML document to Markdown.
-pub fn html_to_markdown(body: &str) -> String {
- Converter::new(body).run()
-}
-
-struct Converter<'a> {
- body: &'a str,
- out: String,
- /// Open `<a>` elements: where their text began in `out`, and the
- /// href to wrap it with on close.
- links: Vec<(usize, String)>,
- /// Nesting depth of ordered/unordered lists, for indenting `-`.
- list_depth: usize,
-}
-
-impl<'a> Converter<'a> {
- fn new(body: &'a str) -> Self {
- Self {
- body,
- out: String::with_capacity(body.len() / 4),
- links: Vec::new(),
- list_depth: 0,
- }
- }
-
- fn run(mut self) -> String {
- let bytes = self.body.as_bytes();
- let mut i = 0usize;
-
- while i < bytes.len() {
- if bytes[i] != b'<' {
- let start = i;
- while i < bytes.len() && bytes[i] != b'<' {
- i += 1;
- }
- let text = decode_entities(&self.body[start..i]);
- self.out.push_str(&text);
- continue;
- }
-
- // `<!-- comment -->`, `<!doctype …>`, `<![CDATA[…]]>`
- if self.body[i..].starts_with("<!--") {
- i = find_after(self.body, i + 4, "-->").unwrap_or(bytes.len());
- continue;
- }
- if self.body[i..].starts_with("<!") {
- i = find_after(self.body, i + 2, ">").unwrap_or(bytes.len());
- continue;
- }
-
- let Some(tag_end) = find_after(self.body, i + 1, ">") else {
- // Unterminated `<` — the rest is text, not a dropped tail.
- let text = decode_entities(&self.body[i..]);
- self.out.push_str(&text);
- break;
- };
- let inner = &self.body[i + 1..tag_end - 1];
- let closing = inner.starts_with('/');
- let self_closing = inner.ends_with('/');
- let name = element_name(inner);
-
- if !closing && !self_closing && DROPPED_ELEMENTS.contains(&name.as_str()) {
- // An unterminated <script> swallowing the remainder is
- // the correct read of malformed markup; a browser agrees.
- match find_close_tag(self.body, tag_end, &name) {
- Some(after) => i = after,
- None => break,
- }
- continue;
- }
-
- if !closing && !self_closing && VERBATIM_ELEMENTS.contains(&name.as_str()) {
- i = self.emit_verbatim(tag_end, &name).unwrap_or(bytes.len());
- continue;
- }
-
- self.emit_tag(&name, inner, closing);
- i = tag_end;
- }
-
- normalize(&self.out)
- }
-
- /// `<pre>` and friends: take the body as-is, strip any inner tags,
- /// and fence it so whitespace survives the normalizer.
- fn emit_verbatim(&mut self, from: usize, name: &str) -> Option<usize> {
- let close = find_close_tag_start(self.body, from, name)?;
- let raw = &self.body[from..close];
- let text = decode_entities(&strip_tags(raw));
- self.break_line();
- self.out.push_str("```\n");
- self.out.push_str(text.trim_matches('\n'));
- self.out.push_str("\n```");
- self.break_line();
- find_after(self.body, close, ">")
- }
-
- fn emit_tag(&mut self, name: &str, inner: &str, closing: bool) {
- match name {
- "br" => self.out.push('\n'),
- "title" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => {
- self.break_line();
- if !closing {
- let level = match name {
- "title" | "h1" => 1,
- other => other[1..].parse::<usize>().unwrap_or(1),
- };
- self.out.push_str(&"#".repeat(level));
- self.out.push(' ');
- }
- }
- "li" => {
- if !closing {
- self.break_line();
- let indent = " ".repeat(self.list_depth.saturating_sub(1));
- self.out.push_str(&indent);
- self.out.push_str("- ");
- }
- }
- "ul" | "ol" => {
- if closing {
- self.list_depth = self.list_depth.saturating_sub(1);
- } else {
- self.list_depth += 1;
- }
- self.break_line();
- }
- "b" | "strong" => self.push_emphasis("**"),
- "i" | "em" => self.push_emphasis("*"),
- "code" => self.push_emphasis("`"),
- "a" => {
- if closing {
- self.close_link();
- } else if let Some(href) = attribute(inner, "href") {
- self.links.push((self.out.len(), href));
- }
- }
- "img" => {
- if let Some(alt) = attribute(inner, "alt").filter(|a| !a.trim().is_empty()) {
- // Keep the caption, drop the source: an `img` src is
- // either a URL the model can't read or, worse, a
- // multi-KB base64 data: blob.
- self.out.push_str(&format!("[IMAGE: {}]", alt.trim()));
- }
- }
- other if BLOCK_ELEMENTS.contains(&other) => self.break_line(),
- _ => {}
- }
- }
-
- /// Emphasis markers are only worth emitting around real text; an
- /// empty `<b></b>` would otherwise leave `****` behind.
- fn push_emphasis(&mut self, marker: &str) {
- if self.out.ends_with(marker) {
- self.out.truncate(self.out.len() - marker.len());
- return;
- }
- self.out.push_str(marker);
- }
-
- fn close_link(&mut self) {
- let Some((start, href)) = self.links.pop() else {
- return;
- };
- if start > self.out.len() {
- return;
- }
- let text = self.out[start..].trim().to_string();
- // A link with no text, or one pointing at a fragment/javascript
- // handler, is navigation chrome — keep the words, drop the wrap.
- if text.is_empty() || !is_useful_href(&href) {
- return;
- }
- self.out.truncate(start);
- self.out.push_str(&format!("[{text}]({href})"));
- }
-
- fn break_line(&mut self) {
- if !self.out.is_empty() && !self.out.ends_with('\n') {
- self.out.push('\n');
- }
- }
-}
-
-fn is_useful_href(href: &str) -> bool {
- let h = href.trim();
- !(h.is_empty()
- || h.starts_with('#')
- || h.len() > MAX_HREF_CHARS
- || h.to_ascii_lowercase().starts_with("javascript:")
- || h.to_ascii_lowercase().starts_with("data:"))
-}
-
-/// Lowercased element name from inside a tag (`/p class=x` → `p`).
-fn element_name(inner: &str) -> String {
- inner
- .trim_start_matches('/')
- .split(|c: char| c.is_whitespace() || c == '/' || c == '>')
- .next()
- .unwrap_or("")
- .to_ascii_lowercase()
-}
-
-/// Pull a quoted attribute value out of a tag's interior.
-fn attribute(inner: &str, name: &str) -> Option<String> {
- let lower = inner.to_ascii_lowercase();
- let mut from = 0usize;
- while let Some(at) = lower[from..].find(name) {
- let at = from + at;
- // Must be preceded by whitespace and followed by `=`, so `href`
- // doesn't match inside `data-href` or a text run.
- let before_ok = at == 0 || inner[..at].ends_with(char::is_whitespace);
- let rest = inner[at + name.len()..].trim_start();
- if before_ok && rest.starts_with('=') {
- let value = rest[1..].trim_start();
- let quoted = value.strip_prefix('"').map(|v| (v, '"')).or_else(|| {
- value
- .strip_prefix('\'')
- .map(|v| (v, '\''))
- });
- return Some(match quoted {
- Some((v, q)) => decode_entities(v.split(q).next().unwrap_or("")),
- None => decode_entities(
- value
- .split(|c: char| c.is_whitespace() || c == '>')
- .next()
- .unwrap_or(""),
- ),
- });
- }
- from = at + name.len();
- }
- None
-}
-
-fn strip_tags(raw: &str) -> String {
- let mut out = String::with_capacity(raw.len());
- let mut depth = 0usize;
- for ch in raw.chars() {
- match ch {
- '<' => depth += 1,
- '>' => depth = depth.saturating_sub(1),
- c if depth == 0 => out.push(c),
- _ => {}
- }
- }
- out
-}
-
-fn find_after(hay: &str, from: usize, needle: &str) -> Option<usize> {
- hay.get(from..)
- .and_then(|rest| rest.find(needle).map(|at| from + at + needle.len()))
-}
-
-/// Byte index of the `<` that opens `</name`, searching case-insensitively.
-fn find_close_tag_start(hay: &str, from: usize, name: &str) -> Option<usize> {
- let rest = hay.get(from..)?;
- let lower = rest.to_ascii_lowercase();
- lower.find(&format!("</{name}")).map(|at| from + at)
-}
-
-/// Byte index just past `</name …>`.
-fn find_close_tag(hay: &str, from: usize, name: &str) -> Option<usize> {
- let start = find_close_tag_start(hay, from, name)?;
- find_after(hay, start, ">")
-}
-
-/// The entities that actually show up in prose. A full named-entity
-/// table is a dependency's worth of data for diminishing returns; the
-/// numeric forms cover the long tail.
-fn decode_entities(input: &str) -> String {
- if !input.contains('&') {
- return input.to_string();
- }
- let mut out = String::with_capacity(input.len());
- let mut rest = input;
- while let Some(amp) = rest.find('&') {
- out.push_str(&rest[..amp]);
- rest = &rest[amp..];
- let window = &rest[..rest.len().min(12)];
- let Some(semi) = window.find(';') else {
- out.push('&');
- rest = &rest[1..];
- continue;
- };
- let decoded = match &rest[1..semi] {
- "amp" => Some('&'),
- "lt" => Some('<'),
- "gt" => Some('>'),
- "quot" => Some('"'),
- "apos" => Some('\''),
- "nbsp" => Some(' '),
- "mdash" => Some('—'),
- "ndash" => Some('–'),
- "hellip" => Some('…'),
- "rsquo" | "lsquo" => Some('\''),
- "ldquo" | "rdquo" => Some('"'),
- other => numeric_entity(other),
- };
- match decoded {
- Some(c) => {
- out.push(c);
- rest = &rest[semi + 1..];
- }
- None => {
- out.push('&');
- rest = &rest[1..];
- }
- }
- }
- out.push_str(rest);
- out
-}
-
-fn numeric_entity(entity: &str) -> Option<char> {
- let digits = entity.strip_prefix('#')?;
- let code = match digits.strip_prefix(['x', 'X']) {
- Some(hex) => u32::from_str_radix(hex, 16).ok()?,
- None => digits.parse::<u32>().ok()?,
- };
- char::from_u32(code)
-}
-
-/// Collapse the whitespace markup leaves behind: runs of spaces within a
-/// line, and runs of blank lines down to one. Fenced blocks are passed
-/// through untouched so code keeps its indentation.
-fn normalize(raw: &str) -> String {
- let mut out = String::with_capacity(raw.len());
- let mut blank_run = 0usize;
- let mut in_fence = false;
-
- for line in raw.lines() {
- if line.trim_start().starts_with("```") {
- in_fence = !in_fence;
- out.push_str(line.trim_end());
- out.push('\n');
- blank_run = 0;
- continue;
- }
- if in_fence {
- out.push_str(line);
- out.push('\n');
- continue;
- }
-
- let mut collapsed = String::with_capacity(line.len());
- let mut pending_space = false;
- for ch in line.chars() {
- if ch.is_whitespace() {
- pending_space = true;
- continue;
- }
- if pending_space && !collapsed.is_empty() {
- collapsed.push(' ');
- }
- pending_space = false;
- collapsed.push(ch);
- }
-
- // A list bullet or heading marker with nothing after it is a
- // leftover from stripped markup, not content.
- let trimmed = collapsed.trim_end();
- if trimmed == "-" || trimmed.chars().all(|c| c == '#') && !trimmed.is_empty() {
- continue;
- }
-
- if trimmed.is_empty() {
- blank_run += 1;
- if blank_run > 1 {
- continue;
- }
- } else {
- blank_run = 0;
- }
- out.push_str(trimmed);
- out.push('\n');
- }
- out.trim().to_string()
-}
-
-#[cfg(test)]
-#[path = "html_markdown_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/tools/impl/network/html_markdown_tests.rs b/crates/openhuman-core/src/tools/impl/network/html_markdown_tests.rs
deleted file mode 100644
index ac5912e54f..0000000000
--- a/crates/openhuman-core/src/tools/impl/network/html_markdown_tests.rs
+++ /dev/null
@@ -1,174 +0,0 @@
-//! Behavior tests for the `web_fetch` HTML→Markdown converter.
-
-use super::*;
-
-#[test]
-fn script_and_style_contents_are_dropped_entirely() {
- let html = r#"<html><head><style>body{color:red}</style></head>
- <body><script>var x = 1 < 2 && 3 > 2;</script><p>Hello</p></body></html>"#;
- let md = html_to_markdown(html);
- assert_eq!(md, "Hello");
- assert!(!md.contains("color"), "style body leaked: {md}");
- assert!(!md.contains("var x"), "script body leaked: {md}");
-}
-
-#[test]
-fn headings_become_atx_markers_at_their_level() {
- let html = "<h1>Title</h1><p>Intro.</p><h3>Detail</h3><p>Body.</p>";
- assert_eq!(html_to_markdown(html), "# Title\nIntro.\n### Detail\nBody.");
-}
-
-#[test]
-fn the_document_title_is_kept_as_a_top_level_heading() {
- // Often the highest-signal string on the page.
- let html = "<html><head><title>Rust Docs</title></head><body><p>x</p></body></html>";
- assert!(html_to_markdown(html).starts_with("# Rust Docs"));
-}
-
-#[test]
-fn links_keep_their_target_so_the_model_can_fetch_the_next_page() {
- let html = r#"<p>See <a href="https://example.com/spec">the spec</a> for more.</p>"#;
- assert_eq!(
- html_to_markdown(html),
- "See [the spec](https://example.com/spec) for more."
- );
-}
-
-#[test]
-fn navigation_chrome_links_keep_their_words_but_drop_the_wrapper() {
- for href in ["#", "#section-2", "javascript:void(0)", "data:text/html,x"] {
- let html = format!(r#"<p>Go <a href="{href}">here</a> now.</p>"#);
- assert_eq!(html_to_markdown(&html), "Go here now.", "href={href}");
- }
-}
-
-#[test]
-fn an_overlong_tracking_url_is_dropped_rather_than_paid_for() {
- let href = format!("https://e.com/?{}", "utm=x&".repeat(100));
- assert!(href.len() > MAX_HREF_CHARS);
- let html = format!(r#"<a href="{href}">click</a>"#);
- assert_eq!(html_to_markdown(&html), "click");
-}
-
-#[test]
-fn list_items_become_bullets_and_nest_by_depth() {
- let html = "<ul><li>one</li><li>two<ul><li>inner</li></ul></li></ul>";
- assert_eq!(html_to_markdown(html), "- one\n- two\n - inner");
-}
-
-#[test]
-fn pre_blocks_are_fenced_and_keep_their_indentation() {
- let html = "<pre><code>fn main() {\n println!(\"hi\");\n}</code></pre>";
- let md = html_to_markdown(html);
- assert_eq!(md, "```\nfn main() {\n println!(\"hi\");\n}\n```");
-}
-
-#[test]
-fn emphasis_survives_as_markdown() {
- let html = "<p><strong>bold</strong> and <em>italic</em> and <code>lit</code></p>";
- assert_eq!(html_to_markdown(html), "**bold** and *italic* and `lit`");
-}
-
-#[test]
-fn images_keep_their_caption_and_never_their_base64_payload() {
- let html = r#"<p><img alt="A chart" src="data:image/png;base64,AAAAAAAAAAAAAAAA"></p>"#;
- let md = html_to_markdown(html);
- assert_eq!(md, "[IMAGE: A chart]");
- assert!(!md.contains("base64"), "data URI leaked: {md}");
-}
-
-#[test]
-fn an_image_with_no_alt_text_contributes_nothing() {
- assert_eq!(html_to_markdown(r#"<p>a<img src="/x.png">b</p>"#), "ab");
-}
-
-#[test]
-fn entities_are_decoded_including_numeric_and_hex_forms() {
- let html = "<p>A &amp; B &lt;tag&gt; &#39;q&#39; &#x2014; &nbsp;done&hellip;</p>";
- assert_eq!(html_to_markdown(html), "A & B <tag> 'q' — done…");
-}
-
-#[test]
-fn an_unknown_entity_is_left_alone_rather_than_swallowed() {
- assert_eq!(html_to_markdown("<p>a &zzz; b</p>"), "a &zzz; b");
-}
-
-#[test]
-fn comments_and_doctype_vanish() {
- assert_eq!(
- html_to_markdown("<!doctype html><!-- hidden --><p>Visible</p>"),
- "Visible"
- );
-}
-
-#[test]
-fn an_unterminated_script_swallows_the_rest_like_a_browser_would() {
- assert_eq!(html_to_markdown("<p>before</p><script>junk(); more"), "before");
-}
-
-#[test]
-fn an_unterminated_angle_bracket_is_kept_as_text_not_dropped() {
- let md = html_to_markdown("<p>done</p>5 < 6 and counting");
- assert!(md.contains("5 < 6 and counting"), "tail lost: {md}");
-}
-
-#[test]
-fn a_data_href_attribute_is_not_mistaken_for_href() {
- let html = r#"<a data-href="/wrong" href="/right">t</a>"#;
- assert_eq!(html_to_markdown(html), "[t](/right)");
-}
-
-#[test]
-fn unquoted_attribute_values_are_still_read() {
- assert_eq!(html_to_markdown("<a href=/plain>t</a>"), "[t](/plain)");
-}
-
-#[test]
-fn extraction_shrinks_a_markup_heavy_page_by_an_order_of_magnitude() {
- // The property that matters for cost: a page whose bytes are mostly
- // machinery must come out near the size of its prose.
- let mut html = String::from("<html><head>");
- for i in 0..200 {
- html.push_str(&format!("<script>function f{i}(){{return {i}*2;}}</script>"));
- }
- html.push_str("</head><body>");
- for _ in 0..10 {
- html.push_str("<div class=\"a b c\"><span>Real sentence of prose.</span></div>");
- }
- html.push_str("</body></html>");
-
- let md = html_to_markdown(&html);
- assert!(
- md.len() * 10 < html.len(),
- "expected >10x shrink, got {} -> {}",
- html.len(),
- md.len()
- );
- assert!(md.contains("Real sentence of prose."));
- assert!(!md.contains("return"));
-}
-
-#[test]
-fn html_is_detected_by_doctype_root_or_tag_density() {
- assert!(looks_like_html("<!DOCTYPE html><html><body>hi</body></html>"));
- assert!(looks_like_html(
- "<div><p>one</p><span>two</span><a href=\"#\">three</a></div>"
- ));
-}
-
-#[test]
-fn json_and_plain_text_are_not_mistaken_for_html() {
- assert!(!looks_like_html(
- r#"{"note": "use <b> for bold", "n": 1, "ok": true}"#
- ));
- assert!(!looks_like_html(
- "# A markdown README\n\nSome prose about x < y comparisons.\n"
- ));
-}
-
-#[test]
-fn a_multibyte_document_does_not_panic_on_the_sniff_boundary() {
- // `looks_like_html` slices the first 4 KB; the cut must land on a
- // char boundary even when the padding is multibyte.
- assert!(!looks_like_html(&"あ".repeat(4096)));
-}
diff --git a/crates/openhuman-core/src/tools/impl/network/mod.rs b/crates/openhuman-core/src/tools/impl/network/mod.rs
index a2a2648e02..7d96ab001d 100644
--- a/crates/openhuman-core/src/tools/impl/network/mod.rs
+++ b/crates/openhuman-core/src/tools/impl/network/mod.rs
@@ -6,7 +6,6 @@
mod curl;
mod gitbooks;
mod gmail_unsubscribe;
-mod html_markdown;
mod http_request;
// Leaf-gated: the only consumers of these two are the `#[cfg(feature = "mcp")]`
// blocks in `tools/ops.rs`, so no stub is needed — nothing names them when the
diff --git a/vendor/tinyjuice b/vendor/tinyjuice
index 2f02042029..bae3527b3c 160000
--- a/vendor/tinyjuice
+++ b/vendor/tinyjuice
@@ -1 +1 @@
-Subproject commit 2f020420298f728949540a5801297b0400b86093
+Subproject commit bae3527b3c961d3c1dfd4f184c8696e37acb19b9