MCPcopy Create free account
hub / github.com/agentforce314/clawcodex / _convert

Function _convert

src/tool_system/tools/web_fetch.py:97–113  ·  view source on GitHub ↗

Convert a fetched body to the requested ``fmt`` (markdown/text/html). Non-HTML bodies (json, plain text, already-markdown) pass through unchanged; binary image types return a short placeholder instead of decoded garbage. Mirrors opencode's deterministic ``convert`` — no model involved.

(content: str, content_type: str, fmt: str)

Source from the content-addressed store, hash-verified

95
96
97def _convert(content: str, content_type: str, fmt: str) -> str:
98 """Convert a fetched body to the requested ``fmt`` (markdown/text/html).
99
100 Non-HTML bodies (json, plain text, already-markdown) pass through unchanged;
101 binary image types return a short placeholder instead of decoded garbage.
102 Mirrors opencode's deterministic ``convert`` — no model involved.
103 """
104 mime = content_type.split(";", 1)[0].strip().lower()
105 if mime.startswith("image/") and mime != "image/svg+xml":
106 return f"[non-text content: {mime}]"
107 if "html" not in mime:
108 return content # text/markdown/json/xml/etc — already usable
109 if fmt == "markdown":
110 return _html_to_markdown(content)
111 if fmt == "text":
112 return _html_to_text(content)
113 return content # fmt == "html": return the raw HTML the caller asked for
114
115
116# -- URL Validation ------------------------------------------------------------

Calls 2

_html_to_markdownFunction · 0.85
_html_to_textFunction · 0.85