sync update from LAN repo (source dhk-1 @ c21a2a4a)
This commit is contained in:
1 parent
264e2200d6
commit
a1fa13a238
122 files changed
+8881
-623
No files matched your search
@@ -1,7 +1,9 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import mimetypes
|
||||
import os
|
||||
import re
|
||||
import tempfile
|
||||
import zipfile
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
@@ -11,12 +13,16 @@ from urllib.parse import quote
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi.responses import FileResponse, PlainTextResponse, Response
|
||||
from pydantic import BaseModel, Field
|
||||
from starlette.background import BackgroundTask
|
||||
|
||||
from app.gateway.authz import require_permission
|
||||
from app.gateway.operation_timing import OperationTiming
|
||||
from app.gateway.path_utils import aresolve_thread_virtual_path
|
||||
from app.gateway.utils import sanitize_log_param
|
||||
from deerflow.config.paths import get_paths
|
||||
from deerflow.config.system_settings import generated_page_logo_path, load_system_settings
|
||||
from deerflow.html_runtime.addresses import html_map_tile_url, html_runtime_base_url
|
||||
from deerflow.html_runtime.offline_bundle import build_html_offline_bundle
|
||||
from deerflow.runtime.thread_paths import aresolve_path_user_id
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
from deerflow.utils.file_io import file_io
|
||||
@@ -213,6 +219,44 @@ def _build_attachment_headers(filename: str, extra_headers: dict[str, str] | Non
|
||||
return headers
|
||||
|
||||
|
||||
def _remove_temporary_bundle(path: str) -> None:
|
||||
try:
|
||||
Path(path).unlink(missing_ok=True)
|
||||
except OSError:
|
||||
logger.warning("Failed to remove temporary HTML offline bundle: %s", path, exc_info=True)
|
||||
|
||||
|
||||
async def build_html_offline_bundle_response(actual_path: Path) -> FileResponse:
|
||||
"""Build and stream a portable ZIP for one generated HTML artifact."""
|
||||
|
||||
descriptor, temporary_name = tempfile.mkstemp(prefix="deerflow-html-offline-", suffix=".zip")
|
||||
os.close(descriptor)
|
||||
temporary_path = Path(temporary_name)
|
||||
try:
|
||||
branding = load_system_settings().generated_page_branding
|
||||
logo_path = generated_page_logo_path(branding)
|
||||
await file_io.run_until_complete(
|
||||
build_html_offline_bundle,
|
||||
actual_path,
|
||||
temporary_path,
|
||||
runtime_base_url=html_runtime_base_url(),
|
||||
map_tile_url=html_map_tile_url(),
|
||||
generated_logo_path=logo_path,
|
||||
)
|
||||
except BaseException:
|
||||
temporary_path.unlink(missing_ok=True)
|
||||
raise
|
||||
|
||||
download_name = f"{actual_path.stem}-离线包.zip"
|
||||
return FileResponse(
|
||||
temporary_path,
|
||||
filename=download_name,
|
||||
media_type="application/zip",
|
||||
headers={"Cache-Control": "private, no-store"},
|
||||
background=BackgroundTask(_remove_temporary_bundle, str(temporary_path)),
|
||||
)
|
||||
|
||||
|
||||
def is_text_file_by_content(path: Path, sample_size: int = 8192) -> bool:
|
||||
"""Check if file is text by examining content for null bytes."""
|
||||
try:
|
||||
@@ -988,10 +1032,20 @@ async def preview_artifact(
|
||||
@router.get(
|
||||
"/threads/{thread_id}/artifacts/{path:path}",
|
||||
summary="Get Artifact File",
|
||||
description="Retrieve an artifact file generated by the AI agent. Text and binary files can be viewed inline, while active web content is always downloaded.",
|
||||
description=(
|
||||
"Retrieve an artifact file generated by the AI agent. Text and binary files can be viewed inline, "
|
||||
"while active web content is always downloaded. HTML files can be exported with their DeerFlow "
|
||||
"runtime dependencies as a portable ZIP."
|
||||
),
|
||||
)
|
||||
@require_permission("threads", "read", owner_check=True)
|
||||
async def get_artifact(thread_id: str, path: str, request: Request, download: bool = False) -> Response:
|
||||
async def get_artifact(
|
||||
thread_id: str,
|
||||
path: str,
|
||||
request: Request,
|
||||
download: bool = False,
|
||||
offline_bundle: bool = False,
|
||||
) -> Response:
|
||||
"""Get an artifact file by its path.
|
||||
|
||||
The endpoint automatically detects file types and returns appropriate content types.
|
||||
@@ -1018,6 +1072,8 @@ async def get_artifact(thread_id: str, path: str, request: Request, download: bo
|
||||
download (bool): If true, forces attachment download for file types that are
|
||||
otherwise returned inline or as plain text. Active HTML/XHTML/SVG content
|
||||
is always downloaded regardless of this flag.
|
||||
offline_bundle (bool): If true for an HTML/XHTML file, returns a ZIP containing
|
||||
``index.html`` plus the packaged DeerFlow JS/CSS/image runtime dependencies.
|
||||
|
||||
Example:
|
||||
- Get text file inline: `/api/threads/abc123/artifacts/mnt/user-data/outputs/notes.txt`
|
||||
@@ -1075,6 +1131,11 @@ async def get_artifact(thread_id: str, path: str, request: Request, download: bo
|
||||
|
||||
mime_type, _ = mimetypes.guess_type(actual_path)
|
||||
|
||||
if offline_bundle:
|
||||
if actual_path.suffix.lower() not in {".htm", ".html", ".xhtml"}:
|
||||
raise HTTPException(status_code=400, detail="Offline bundles are available for HTML files only")
|
||||
return await build_html_offline_bundle_response(actual_path)
|
||||
|
||||
if download:
|
||||
return FileResponse(path=actual_path, filename=actual_path.name, media_type=mime_type, headers=_build_attachment_headers(actual_path.name))
|
||||
|
||||
|
||||
Reference in new issue
Block a user