content.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. """ACP prompt content blocks -> Hermes/OpenAI user-content payloads (text, images, resources)."""
  2. from __future__ import annotations
  3. import base64
  4. import logging
  5. from pathlib import Path
  6. from typing import Any
  7. from urllib.parse import unquote, urlparse
  8. from acp.schema import (
  9. AudioContentBlock, BlobResourceContents, EmbeddedResourceContentBlock, ImageContentBlock,
  10. ResourceContentBlock, TextContentBlock, TextResourceContents,
  11. )
  12. logger = logging.getLogger("acp_adapter.server")
  13. PromptBlock = (
  14. TextContentBlock | ImageContentBlock | AudioContentBlock | ResourceContentBlock | EmbeddedResourceContentBlock
  15. )
  16. _MAX_ACP_RESOURCE_BYTES = 512 * 1024
  17. _TEXT_RESOURCE_MIME_TYPES = {
  18. "application/json",
  19. "application/javascript",
  20. "application/typescript",
  21. "application/xml",
  22. "application/x-yaml",
  23. "application/yaml",
  24. "application/toml",
  25. "application/sql",
  26. }
  27. def _resource_display_name(uri: str, name: str | None = None, title: str | None = None) -> str:
  28. """Human-readable attachment name for prompt context."""
  29. raw_name = (name or "").strip()
  30. raw_title = (title or "").strip()
  31. if raw_title and raw_name and raw_title != raw_name:
  32. return f"{raw_title} ({raw_name})"
  33. if raw_title or raw_name:
  34. return raw_title or raw_name
  35. parsed = urlparse(uri)
  36. candidate = parsed.path if parsed.scheme else uri
  37. return Path(unquote(candidate)).name or uri or "resource"
  38. def _mime_main(mime_type: str | None) -> str:
  39. return (mime_type or "").split(";", 1)[0].strip().lower()
  40. def _is_text_resource(mime_type: str | None) -> bool:
  41. mime = _mime_main(mime_type)
  42. return mime.startswith("text/") or mime in _TEXT_RESOURCE_MIME_TYPES
  43. def _is_image_resource(mime_type: str | None) -> bool:
  44. return _mime_main(mime_type).startswith("image/")
  45. _IMAGE_SUFFIX_MIME = {
  46. ".png": "image/png",
  47. ".jpg": "image/jpeg",
  48. ".jpeg": "image/jpeg",
  49. ".gif": "image/gif",
  50. ".webp": "image/webp",
  51. ".bmp": "image/bmp",
  52. ".svg": "image/svg+xml",
  53. }
  54. def _path_from_file_uri(uri: str) -> Path | None:
  55. """Local file URI/path from an ACP client -> readable Path (None for non-file URIs).
  56. Windows drive forms (Zed via wsl.exe) become ``/mnt/<drive>/...``."""
  57. raw = (uri or "").strip()
  58. if not raw:
  59. return None
  60. # urlparse treats a bare Windows drive as a URI scheme.
  61. if len(raw) >= 3 and raw[0].isalpha() and raw[1] == ":" and raw[2] in "/\\":
  62. raw = "file:///" + raw.replace("\\", "/")
  63. parsed = urlparse(raw)
  64. if parsed.scheme and parsed.scheme != "file":
  65. return None
  66. if parsed.scheme == "file" and parsed.netloc and parsed.netloc not in {"", "localhost"}:
  67. return None
  68. path_text = unquote(parsed.path or "") if parsed.scheme == "file" else unquote(raw)
  69. # file:///C:/Users/... or C:\Users\...
  70. if len(path_text) >= 3 and path_text[0] == "/" and path_text[2] == ":" and path_text[1].isalpha():
  71. drive, rest = path_text[1], path_text[3:]
  72. elif len(path_text) >= 2 and path_text[1] == ":" and path_text[0].isalpha():
  73. drive, rest = path_text[0], path_text[2:]
  74. else:
  75. return Path(path_text)
  76. import os
  77. rest = rest.lstrip("/\\").replace("\\", "/")
  78. if os.name == "nt":
  79. return Path(f"{drive}:/{rest}")
  80. return Path("/mnt") / drive.lower() / rest
  81. def _decode_text_bytes(data: bytes, mime_type: str | None) -> str | None:
  82. """Decode resource bytes if they are probably text; return None for binary."""
  83. if b"\x00" in data and not _is_text_resource(mime_type):
  84. return None
  85. for encoding in ("utf-8-sig", "utf-8", "latin-1"):
  86. try:
  87. return data.decode(encoding)
  88. except UnicodeDecodeError:
  89. continue
  90. # Binary (ELF/Mach-O/PE), not a shell script: feeding its decoded bytes back into the guard tokenizes
  91. # machine code into bogus NUL-bearing paths and crashes the scanner (#77703). Mirror
  92. # lifecycle_guard._read_referenced_script and treat it as nothing to scan.
  93. return data.decode("utf-8", errors="replace")
  94. def _format_resource_text(
  95. *, uri: str, body: str, name: str | None = None, title: str | None = None, note: str | None = None
  96. ) -> str:
  97. display = _resource_display_name(uri, name=name, title=title)
  98. header = f"[Attached file: {display}]"
  99. if note:
  100. header += f" ({note})"
  101. return f"{header}\nURI: {uri}\n\n{body}"
  102. def _text_parts(**kwargs: Any) -> list[dict[str, Any]]:
  103. """Single OpenAI text part wrapping ``_format_resource_text(**kwargs)``."""
  104. return [{"type": "text", "text": _format_resource_text(**kwargs)}]
  105. def _image_parts(uri: str, display: str, data: bytes, mime: str) -> list[dict[str, Any]]:
  106. """Text header + image_url data URL so vision models can see the attachment."""
  107. return [
  108. {"type": "text", "text": f"[Attached image: {display}]" + (f"\nURI: {uri}" if uri else "")},
  109. {"type": "image_url", "image_url": {"url": f"data:{mime};base64,{base64.b64encode(data).decode('ascii')}"}},
  110. ]
  111. def _attr(obj: Any, name: str) -> str | None:
  112. """Stripped string attribute, ``None`` when missing/blank."""
  113. return str(getattr(obj, name, "") or "").strip() or None
  114. def _resource_link_to_parts(block: ResourceContentBlock) -> list[dict[str, Any]]:
  115. """ACP resource_link -> OpenAI content parts: images become a text header + image_url,
  116. everything else a single text part with the inlined body (or a binary-omit note)."""
  117. uri = _attr(block, "uri")
  118. if not uri:
  119. return []
  120. name, title, mime_type = _attr(block, "name"), _attr(block, "title"), _attr(block, "mime_type")
  121. path = _path_from_file_uri(uri)
  122. ident = dict(uri=uri, name=name, title=title)
  123. if path is None:
  124. return _text_parts(
  125. **ident, body="[Resource link only; Hermes cannot read non-file ACP resource URIs directly.]"
  126. )
  127. image_mime = mime_type if _is_image_resource(mime_type) else _IMAGE_SUFFIX_MIME.get(path.suffix.lower())
  128. if image_mime and _is_image_resource(image_mime):
  129. try:
  130. size = path.stat().st_size
  131. if size > _MAX_ACP_RESOURCE_BYTES:
  132. return _text_parts(
  133. **ident, body=f"[Image too large to inline: {size} bytes, cap={_MAX_ACP_RESOURCE_BYTES}]"
  134. )
  135. with path.open("rb") as fh:
  136. data = fh.read()
  137. except OSError as exc:
  138. logger.warning("ACP image resource read failed: %s", uri, exc_info=True)
  139. return _text_parts(**ident, body=f"[Could not read attached image: {exc}]")
  140. return _image_parts(uri, _resource_display_name(uri, name=name, title=title), data, image_mime)
  141. try:
  142. size = path.stat().st_size
  143. with path.open("rb") as fh:
  144. data = fh.read(min(size, _MAX_ACP_RESOURCE_BYTES))
  145. text = _decode_text_bytes(data, mime_type)
  146. if text is None:
  147. return _text_parts(**ident, body=f"[Binary file omitted: {size} bytes, mime={mime_type or 'unknown'}]")
  148. note = f"truncated to {_MAX_ACP_RESOURCE_BYTES} of {size} bytes" if size > _MAX_ACP_RESOURCE_BYTES else None
  149. return _text_parts(**ident, body=text, note=note)
  150. except OSError as exc:
  151. logger.warning("ACP resource read failed: %s", uri, exc_info=True)
  152. return _text_parts(**ident, body=f"[Could not read attached file: {exc}]")
  153. def _embedded_resource_to_parts(block: EmbeddedResourceContentBlock) -> list[dict[str, Any]]:
  154. resource = getattr(block, "resource", None)
  155. if resource is None:
  156. return []
  157. uri = _attr(resource, "uri") or ""
  158. mime_type = _attr(resource, "mime_type")
  159. if isinstance(resource, TextResourceContents):
  160. return _text_parts(uri=uri, body=resource.text)
  161. if isinstance(resource, BlobResourceContents):
  162. blob = resource.blob or ""
  163. try:
  164. data = base64.b64decode(blob, validate=True)
  165. except Exception:
  166. data = blob.encode("utf-8", errors="replace")
  167. if _is_image_resource(mime_type):
  168. if len(data) > _MAX_ACP_RESOURCE_BYTES:
  169. return _text_parts(
  170. uri=uri,
  171. body=f"[Embedded image too large to inline: {len(data)} bytes, cap={_MAX_ACP_RESOURCE_BYTES}]",
  172. )
  173. return _image_parts(uri, _resource_display_name(uri), data, mime_type or "image/png")
  174. body = _decode_text_bytes(data[:_MAX_ACP_RESOURCE_BYTES], mime_type)
  175. if body is None:
  176. body = f"[Binary embedded file omitted: {len(data)} bytes, mime={mime_type or 'unknown'}]"
  177. elif len(data) > _MAX_ACP_RESOURCE_BYTES:
  178. body += f"\n\n[Truncated to {_MAX_ACP_RESOURCE_BYTES} of {len(data)} bytes]"
  179. return _text_parts(uri=uri, body=body)
  180. text = getattr(resource, "text", None)
  181. if text:
  182. return _text_parts(uri=uri, body=str(text))
  183. return []
  184. def _extract_text(prompt: list[PromptBlock]) -> str:
  185. """Extract plain text from ACP content blocks for display/commands."""
  186. return "\n".join(str(block.text) for block in prompt if hasattr(block, "text"))
  187. def _image_block_to_openai_part(block: ImageContentBlock) -> dict[str, Any] | None:
  188. """Convert an ACP image content block to OpenAI-style multimodal content."""
  189. data, uri = _attr(block, "data"), _attr(block, "uri")
  190. mime_type = _attr(block, "mime_type") or "image/png"
  191. if data:
  192. url = data if data.startswith("data:") else f"data:{mime_type};base64,{data}"
  193. elif uri:
  194. url = uri
  195. else:
  196. return None
  197. return {"type": "image_url", "image_url": {"url": url}}
  198. def _append_parts(parts: list, text_parts: list[str], new_parts: list[dict[str, Any]]) -> None:
  199. for part in new_parts:
  200. parts.append(part)
  201. if part.get("type") == "text":
  202. text_parts.append(part["text"])
  203. def _content_blocks_to_openai_user_content(prompt: list[PromptBlock]) -> str | list[dict[str, Any]]:
  204. """Convert ACP prompt blocks into a Hermes/OpenAI-compatible user content payload."""
  205. parts: list[dict[str, Any]] = []
  206. text_parts: list[str] = []
  207. for block in prompt:
  208. if isinstance(block, TextContentBlock):
  209. if block.text:
  210. parts.append({"type": "text", "text": block.text})
  211. text_parts.append(block.text)
  212. elif isinstance(block, ImageContentBlock):
  213. image_part = _image_block_to_openai_part(block)
  214. if image_part is not None:
  215. parts.append(image_part)
  216. elif isinstance(block, ResourceContentBlock):
  217. _append_parts(parts, text_parts, _resource_link_to_parts(block))
  218. elif isinstance(block, EmbeddedResourceContentBlock):
  219. _append_parts(parts, text_parts, _embedded_resource_to_parts(block))
  220. if not parts:
  221. return _extract_text(prompt)
  222. # Pure text stays a string (slash commands, text-only providers); structured only for media.
  223. if all(part.get("type") == "text" for part in parts):
  224. return "\n".join(text_parts)
  225. return parts