feat(attachments): support local files and video attachments (#6321)

* feat(attachments): support local files and video attachments

Share gateway inspection and send validation for borrowed local paths while preserving binary uploads, original-file ownership, and existing limits.

Related to NAN-284. Native provider video input remains outside this change.

* test: make cross-platform verification fixtures self-contained

* test(webui): align typed fixtures with runtime contracts

* fix(webui): preserve uploaded video playback during history sync

* test(tui): preserve local attachment drafts across resizes

* perf(attachments): preserve upload caches and release video previews

* docs(webui): keep attachment guidance user-focused

* chore: remove redundant dependency comment

* test(providers): make stream idle renewal timing deterministic

* fix(attachments): keep upload byte limits out of local references

Validate common format/count rules separately from upload byte budgets. Keep local inspection and send-time checks metadata-only, and exclude local references from composer and pending-upload totals. Explain upload size rejections with actual size and maximum in all ten locales. Related to NAN-284.

* fix(attachments): retain image references and draft order

* fix(attachments): close local reference UX and authorization gaps

* fix(attachments): preserve focus during asynchronous path confirmation
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-22 18:32:18 +08:00
1 parent 7dfeff9ddf
commit 254f94b068
65 files changed
+1816 -292

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
"""Read-only local attachment checks shared by WebUI and native TUI ingress."""
from __future__ import annotations
import mimetypes
import os
import stat
from typing import Literal, TypedDict
from nanobot.channels.websocket.attachment_policy import (
AttachmentIngressLimits,
validate_attachment_batch,
)
from nanobot.security.workspace_access import WorkspaceScope
from nanobot.webui.file_preview import WebUIFilePreviewError, resolve_file_path
class LocalAttachment(TypedDict):
path: str
name: str
size: int
mime: str
kind: Literal["image", "video", "file"]
# Match the upload ingress formats even on hosts with different MIME registries.
_MIME_BY_SUFFIX = {
".md": "text/markdown", ".log": "text/plain", ".txt": "text/plain",
".ini": "text/plain", ".cfg": "text/plain", ".csv": "text/csv",
".yaml": "application/yaml", ".yml": "application/yaml", ".toml": "application/toml",
".mp4": "video/mp4", ".webm": "video/webm", ".mov": "video/quicktime",
}
def inspect_local_attachment(
path: str, *, scope: WorkspaceScope, limits: AttachmentIngressLimits,
) -> LocalAttachment:
"""Check accessibility and metadata without reading or copying the file body."""
resolved = resolve_file_path(path, scope=scope, literal=True)
try:
fd = os.open(resolved, os.O_RDONLY | getattr(os, "O_NONBLOCK", 0))
try:
info = os.fstat(fd)
if not stat.S_ISREG(info.st_mode):
raise WebUIFilePreviewError(400, "attachment must be a regular file")
finally:
os.close(fd)
except PermissionError as exc:
raise WebUIFilePreviewError(403, "file is not readable") from exc
except OSError as exc:
raise WebUIFilePreviewError(404, "file is unavailable") from exc
if info.st_size == 0:
raise WebUIFilePreviewError(400, "Empty files cannot be attached")
mime = _MIME_BY_SUFFIX.get(resolved.suffix.lower()) or mimetypes.guess_type(resolved.name)[0] or "application/octet-stream"
try:
validate_attachment_batch([mime], limits=limits)
except ValueError as exc:
raise WebUIFilePreviewError(400, str(exc)) from exc
kind = "video" if mime.startswith("video/") else "image" if mime.startswith("image/") else "file"
return {"path": str(resolved), "name": resolved.name, "size": info.st_size, "mime": mime, "kind": kind}