feat(webui): unify file preview actions
This commit is contained in:
1 parent
af5acabb33
commit
05bee21b10
24 files changed
+542
-10
No files matched your search
@@ -38,6 +38,16 @@ class WebUIFilePreviewError(ValueError):
|
||||
self.message = message
|
||||
|
||||
|
||||
def file_reference_payload(raw_path: str | None, *, scope: WorkspaceScope) -> dict[str, str | None]:
|
||||
"""Resolve copyable paths through the preview policy without reading file contents."""
|
||||
resolved = _resolve_preview_path(raw_path, scope=scope)
|
||||
try:
|
||||
relative = resolved.relative_to(scope.project_path).as_posix()
|
||||
except ValueError:
|
||||
relative = None
|
||||
return {"path": str(resolved), "relative_path": relative}
|
||||
|
||||
|
||||
def file_preview_payload(
|
||||
raw_path: str | None,
|
||||
*,
|
||||
|
||||
@@ -41,6 +41,7 @@ from nanobot.webui.file_preview import (
|
||||
WebUIFilePreviewError,
|
||||
file_preview_availability_payload,
|
||||
file_preview_payload,
|
||||
file_reference_payload,
|
||||
)
|
||||
from nanobot.webui.forking import handle_webui_fork_chat
|
||||
from nanobot.webui.gateway_services import GatewayServices
|
||||
@@ -793,6 +794,7 @@ class WebUICommandRouter:
|
||||
chat_id = preview_payload.get("chat_id")
|
||||
path = preview_payload.get("path")
|
||||
probe = preview_payload.get("probe") is True
|
||||
metadata_only = preview_payload.get("metadata") is True
|
||||
if not isinstance(chat_id, str) or not isinstance(path, str):
|
||||
await self.send_webui_response(
|
||||
connection, request_id, status=400, message="invalid preview request",
|
||||
@@ -802,14 +804,18 @@ class WebUICommandRouter:
|
||||
policy = self._temporary_chats.message_policy(connection, chat_id, "")
|
||||
if policy is None:
|
||||
raise TemporaryChatError("temporary_chat_unavailable")
|
||||
preview = file_preview_availability_payload if probe else file_preview_payload
|
||||
result = preview(path, scope=policy.workspace_scope)
|
||||
if metadata_only:
|
||||
result = file_reference_payload(path, scope=policy.workspace_scope)
|
||||
elif probe:
|
||||
result = file_preview_availability_payload(path, scope=policy.workspace_scope)
|
||||
else:
|
||||
result = file_preview_payload(path, scope=policy.workspace_scope)
|
||||
except TemporaryChatError as exc:
|
||||
await self.send_webui_response(
|
||||
connection, request_id, status=404, message=exc.detail,
|
||||
)
|
||||
except WebUIFilePreviewError as exc:
|
||||
if probe and exc.status in {400, 403, 404, 413, 415}:
|
||||
if probe and not metadata_only and exc.status in {400, 403, 404, 413, 415}:
|
||||
await self.send_webui_response(
|
||||
connection, request_id, result={"available": False},
|
||||
)
|
||||
|
||||
@@ -41,6 +41,7 @@ from nanobot.webui.file_preview import (
|
||||
WebUIFilePreviewError,
|
||||
file_preview_availability_payload,
|
||||
file_preview_payload,
|
||||
file_reference_payload,
|
||||
)
|
||||
from nanobot.webui.gateway_tokens import GatewayTokenStore, token_response_payload
|
||||
from nanobot.webui.http_utils import JSONResponseMetrics
|
||||
@@ -1117,14 +1118,17 @@ class GatewayHTTPHandler:
|
||||
query = _parse_query(request.path)
|
||||
path = _query_first(query, "path")
|
||||
is_probe = _query_first(query, "probe") == "1"
|
||||
metadata_only = _query_first(query, "metadata") == "1"
|
||||
try:
|
||||
scope = self.workspaces.scope_for_session_key(decoded_key)
|
||||
if is_probe:
|
||||
if metadata_only:
|
||||
payload = file_reference_payload(path, scope=scope)
|
||||
elif is_probe:
|
||||
payload = file_preview_availability_payload(path, scope=scope)
|
||||
else:
|
||||
payload = file_preview_payload(path, scope=scope)
|
||||
except WebUIFilePreviewError as e:
|
||||
if is_probe and e.status in {400, 403, 404, 413, 415}:
|
||||
if is_probe and not metadata_only and e.status in {400, 403, 404, 413, 415}:
|
||||
return _http_json_response({"available": False}, extra_headers=_NO_STORE_HEADERS)
|
||||
return _http_error(e.status, e.message)
|
||||
return _http_json_response(payload, extra_headers=_NO_STORE_HEADERS)
|
||||
|
||||
Reference in new issue
Block a user