From 31fba5a9e92042917cda87dbbc5abf1750248b87 Mon Sep 17 00:00:00 2001 From: minorQAQ <202200120184@mail.sdu.edu.cn> Date: Mon, 1 Jun 2026 22:31:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E6=96=B0=E5=93=A6=EF=BC=8C=E5=88=86?= =?UTF-8?q?=E8=AF=8D=E7=AD=96=E7=95=A5=E6=94=BE=E5=88=B0cli=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/web/server.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/web/server.py b/src/web/server.py index 7221e2e..5268b20 100644 --- a/src/web/server.py +++ b/src/web/server.py @@ -27,13 +27,21 @@ if str(_SRC) not in sys.path: from agent import chatbot_agent as ca # noqa: E402 from web import ui_session as ui # noqa: E402 from agent.utils.image_utils import IMAGE_FILE_EXTENSIONS # noqa: E402 -from agent.utils.agent_utils import DOC_EXT_MAP # noqa: E402 _WEB_DIR = Path(__file__).resolve().parent _IMAGE_DIR = _WEB_DIR / "image" _AUDIO_EXT = frozenset({".wav", ".mp3", ".m4a", ".flac", ".ogg", ".webm"}) -_FILE_EXT = frozenset(DOC_EXT_MAP.keys()) +_IMAGE_EXT = frozenset(IMAGE_FILE_EXTENSIONS) + + +def _infer_upload_kind(fp: str) -> str | None: + ext = os.path.splitext(fp)[1].lower() + if ext in _IMAGE_EXT: + return "image" + if ext in _AUDIO_EXT: + return "audio" + return "file" def _data_url_for_file(fp: str) -> str | None: @@ -84,6 +92,7 @@ def _serialize_content(content: Any, *, meta: dict[str, Any] | None = None, role parts.append({"type": "text", "text": piece.get("text") or ""}) continue fp = piece.get("path") or (piece.get("file") or {}).get("path") + display_name = piece.get("name") if piece.get("type") == "image_url": fp = fp or (piece.get("image_url") or {}).get("url") if piece.get("type") == "audio": @@ -95,19 +104,19 @@ def _serialize_content(content: Any, *, meta: dict[str, Any] | None = None, role continue ext = os.path.splitext(fp)[1].lower() if ext in IMAGE_FILE_EXTENSIONS: - parts.append({"type": "image", "url": data_url, "path": fp, "name": os.path.basename(fp)}) + parts.append({"type": "image", "url": data_url, "path": fp, "name": display_name or os.path.basename(fp)}) elif ext in _AUDIO_EXT: audio_meta = meta.get("audio") if isinstance(meta.get("audio"), dict) else {} parts.append({ "type": "audio", "url": data_url, "path": fp, - "name": os.path.basename(fp), + "name": display_name or os.path.basename(fp), "autoplay": bool(meta.get("autoplay") or audio_meta.get("autoplay")) if role == "assistant" else False, "duration_seconds": audio_meta.get("duration_seconds"), }) else: - parts.append({"type": "file", "url": data_url, "path": fp, "name": os.path.basename(fp)}) + parts.append({"type": "file", "url": data_url, "path": fp, "name": display_name or os.path.basename(fp)}) return parts @@ -244,6 +253,7 @@ def _materialize_uploads_to_temp( """将上传文件落盘到临时目录,返回 ``(query_dict, tmp_root)``;调用方须在 finally 中删除 ``tmp_root``。""" tmp_root = tempfile.mkdtemp(prefix="web_mm_") file_paths: list[str] = [] + file_names: list[str] = [] for uf in images or []: if uf is None or not uf.filename: continue @@ -252,6 +262,7 @@ def _materialize_uploads_to_temp( with open(dest, "wb") as f: shutil.copyfileobj(uf.file, f) file_paths.append(dest) + file_names.append(uf.filename) for uf in files or []: if uf is None or not uf.filename: @@ -261,6 +272,7 @@ def _materialize_uploads_to_temp( with open(dest, "wb") as f: shutil.copyfileobj(uf.file, f) file_paths.append(dest) + file_names.append(uf.filename) if audio is not None and audio.filename: ext = Path(audio.filename).suffix or ".bin" @@ -268,8 +280,9 @@ def _materialize_uploads_to_temp( with open(dest, "wb") as f: shutil.copyfileobj(audio.file, f) file_paths.append(dest) + file_names.append(audio.filename) - query = {"text": (text or "").strip(), "files": file_paths} + query = {"text": (text or "").strip(), "files": file_paths, "file_names": file_names} return query, tmp_root @@ -417,4 +430,4 @@ def serve_css() -> FileResponse: @app.get("/app.js") def serve_js() -> FileResponse: - return FileResponse(_WEB_DIR / "app.js") + return FileResponse(_WEB_DIR / "app.js") \ No newline at end of file