Files
agent-zero/plugins/_document_query/helpers/document.py
T
Jan Tomášek 188cff2073 Move optional runtimes into persistent gVisor plugin sandboxes
Move tunnel, browser, document, speech and integration runtimes into plugin-owned
sandboxes with lazy dependencies and instance-scoped lifecycle management.
Reuse Tuxee display assets and the sandbox image's Python/Node runtimes, enable
automatic Desktop login, and show elapsed time for each browser launch.

Slim the outer image, use offline ONNX embeddings, and calculate sandbox storage
only on demand. Include regression coverage and updated runtime documentation.
Exclude local handoff notes and temporary verification artifacts from publication.
2026-11-08 21:43:22 +01:00

52 lines
1.4 KiB
Python

from __future__ import annotations
import mimetypes
import os
import tempfile
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from urllib.parse import urlparse
@dataclass(frozen=True)
class FetchedDocument:
"""Fetched document bytes plus metadata needed by parsers."""
uri: str
scheme: str
mimetype: str
content: bytes
encoding: str | None = None
charset: str | None = None
local_path: str | None = None
source_uri: str | None = None
def text(self) -> str:
charset = self.charset or "utf-8"
return self.content.decode(charset, errors="replace")
def suffix(self) -> str:
path = self.local_path or urlparse(self.uri).path or self.uri
suffix = Path(path).suffix
if suffix:
return suffix
guessed = mimetypes.guess_extension(self.mimetype)
return guessed or ".bin"
@contextmanager
def local_file(self):
"""Yield a filesystem path for parsers that cannot consume bytes."""
if self.local_path and os.path.exists(self.local_path):
yield self.local_path
return
tmp = ""
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=self.suffix()) as f:
f.write(self.content)
tmp = f.name
yield tmp
finally:
if tmp and os.path.exists(tmp):
os.unlink(tmp)