sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -17,7 +17,7 @@ import logging
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from pathlib import Path, PurePosixPath
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
@@ -27,6 +27,7 @@ from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.authz import require_permission
|
||||
from app.gateway.deps import get_config, get_current_user
|
||||
from app.gateway.document_rewrite_diagnostics import build_document_rewrite_diagnostic
|
||||
from app.gateway.document_rewrite_pipeline import (
|
||||
DOCUMENT_REWRITE_SYSTEM_PROMPT as _DOCUMENT_REWRITE_SYSTEM_PROMPT,
|
||||
)
|
||||
@@ -66,6 +67,7 @@ from app.gateway.word_export import (
|
||||
build_markdown_docx,
|
||||
)
|
||||
from deerflow.config.app_config import AppConfig
|
||||
from deerflow.config.system_settings import load_system_settings
|
||||
from deerflow.models import create_chat_model
|
||||
from deerflow.utils.stream_text import InlineThinkTagFilter
|
||||
|
||||
@@ -150,9 +152,8 @@ class DocumentRewriteRequest(BaseModel):
|
||||
"""Request for a complete Markdown artifact rewrite.
|
||||
|
||||
This endpoint deliberately streams a *temporary* draft. The artifact is
|
||||
written only after generation and Markdown validation succeed and the
|
||||
original hash still matches. It is therefore safe for the sandbox to
|
||||
visually clear and retype the document without risking a half-written file.
|
||||
written to a separate sibling file only after generation and Markdown
|
||||
validation succeed. The source artifact is never overwritten.
|
||||
"""
|
||||
|
||||
thread_id: str = Field(..., alias="threadId")
|
||||
@@ -189,6 +190,29 @@ def _document_rewrite_key(path: Path) -> str:
|
||||
return os.path.normcase(str(path.resolve()))
|
||||
|
||||
|
||||
def _next_document_rewrite_output_path(source_path: Path, virtual_path: str) -> tuple[Path, str]:
|
||||
"""Reserve a readable, unused sibling name for one generated rewrite.
|
||||
|
||||
The actual and virtual paths must share the same filename. We make the
|
||||
candidate deterministic and only advance the numeric suffix when a prior
|
||||
rewrite already exists, so a conversation's new file card remains easy to
|
||||
understand without ever replacing an earlier result.
|
||||
"""
|
||||
virtual = PurePosixPath(virtual_path)
|
||||
suffix = source_path.suffix
|
||||
stem = source_path.stem
|
||||
for index in range(1, 10_000):
|
||||
output_name = (
|
||||
f"{stem}-改写版{suffix}"
|
||||
if index == 1
|
||||
else f"{stem}-改写版-{index}{suffix}"
|
||||
)
|
||||
candidate = source_path.with_name(output_name)
|
||||
if not candidate.exists():
|
||||
return candidate, str(virtual.with_name(output_name))
|
||||
raise HTTPException(status_code=409, detail="改写版文件过多,请整理后再试。")
|
||||
|
||||
|
||||
def _instruction_with_report_outline(instruction: str, report_outline: str | None) -> str:
|
||||
"""Make an optional report template an explicit constraint for the rewrite model."""
|
||||
normalized_instruction = instruction.strip()
|
||||
@@ -235,13 +259,20 @@ def _artifact_job_matches(job: dict, *, thread_id: str, virtual_path: str) -> bo
|
||||
def _artifact_job_response(job: dict) -> dict:
|
||||
"""Limit durable job data to the rewrite fields the browser can hydrate."""
|
||||
snapshot = job.get("input_snapshot") or {}
|
||||
result = job.get("result_snapshot") or {}
|
||||
output_path = snapshot.get("output_path") or result.get("outputPath")
|
||||
diagnostic = result.get("diagnostic") if isinstance(result, dict) else None
|
||||
return {
|
||||
"id": job.get("id"),
|
||||
"status": job.get("status"),
|
||||
"phase": job.get("phase"),
|
||||
"progress": job.get("progress"),
|
||||
"createdAt": job.get("created_at"),
|
||||
"result": job.get("result_snapshot") or {},
|
||||
"result": result,
|
||||
"errorCode": job.get("error_code"),
|
||||
"errorMessage": job.get("error_message"),
|
||||
"diagnostic": diagnostic if isinstance(diagnostic, dict) else None,
|
||||
"outputPath": output_path if isinstance(output_path, str) else None,
|
||||
"request": {
|
||||
"instruction": str(snapshot.get("instruction") or ""),
|
||||
"style": str(snapshot.get("style") or "professional"),
|
||||
@@ -250,6 +281,7 @@ def _artifact_job_response(job: dict) -> dict:
|
||||
"generateImages": bool(snapshot.get("generate_images")),
|
||||
"maxGeneratedImages": int(snapshot.get("max_generated_images") or 2),
|
||||
"originalContent": snapshot.get("original_content") or "",
|
||||
"outputPath": output_path if isinstance(output_path, str) else None,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -297,11 +329,29 @@ async def _stream_durable_artifact_rewrite_job(
|
||||
# Never let the browser retain a spinning timeline
|
||||
# merely because the terminal job row already knows
|
||||
# the failure.
|
||||
result = current.get("result_snapshot") or {}
|
||||
diagnostic = result.get("diagnostic") if isinstance(result, dict) else None
|
||||
if not isinstance(diagnostic, dict):
|
||||
diagnostic = build_document_rewrite_diagnostic(
|
||||
job_id=job_id,
|
||||
stage=str(current.get("phase") or "generation"),
|
||||
error_code=str(current.get("error_code") or "DOCUMENT_REWRITE_FAILED"),
|
||||
message=str(
|
||||
current.get("error_message")
|
||||
or "全文改写任务未完成,原文件未被修改。"
|
||||
),
|
||||
)
|
||||
yield _sse(
|
||||
"error",
|
||||
{
|
||||
"message": str(current.get("error_message") or "全文改写任务未完成,原文件未被修改。"),
|
||||
"stage": str(current.get("phase") or "generation"),
|
||||
"message": str(diagnostic.get("message") or "全文改写任务未完成,原文件未被修改。"),
|
||||
"stage": str(diagnostic.get("stage") or current.get("phase") or "generation"),
|
||||
"errorCode": str(
|
||||
diagnostic.get("errorCode")
|
||||
or current.get("error_code")
|
||||
or "DOCUMENT_REWRITE_FAILED"
|
||||
),
|
||||
"diagnostic": diagnostic,
|
||||
},
|
||||
)
|
||||
return
|
||||
@@ -392,11 +442,13 @@ async def export_markdown_docx(body: MarkdownDocxExportRequest) -> Response:
|
||||
persist the supplied Markdown.
|
||||
"""
|
||||
try:
|
||||
heading_numbering_style = load_system_settings().prompt_prefix.word_heading_numbering_style
|
||||
content = await asyncio.to_thread(
|
||||
build_markdown_docx,
|
||||
body.markdown,
|
||||
body.title,
|
||||
embedded_images=body.embedded_images or None,
|
||||
heading_numbering_style=heading_numbering_style,
|
||||
)
|
||||
except WordExportFontError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
||||
@@ -418,9 +470,7 @@ async def export_markdown_docx(body: MarkdownDocxExportRequest) -> Response:
|
||||
)
|
||||
|
||||
|
||||
@router.post("/document-rewrite/jobs")
|
||||
@require_permission("threads", "write", owner_check=True)
|
||||
async def start_durable_document_rewrite_job(
|
||||
async def _start_durable_document_rewrite_job_impl(
|
||||
body: DocumentRewriteRequest,
|
||||
request: Request,
|
||||
) -> dict:
|
||||
@@ -449,12 +499,26 @@ async def start_durable_document_rewrite_job(
|
||||
)
|
||||
base_hash = _content_hash(original)
|
||||
if body.expected_hash and body.expected_hash != base_hash:
|
||||
raise HTTPException(status_code=409, detail="文件已变化,请刷新后重新发起全文改写。")
|
||||
# The editor can hold a slightly stale artifact query while the
|
||||
# sandbox is finishing a write. Do not reject the durable job based
|
||||
# on that browser-side preflight hash: the server has just captured
|
||||
# the authoritative source snapshot above. The generated result is
|
||||
# written to a separate output file, so the source artifact is never
|
||||
# overwritten.
|
||||
logger.info(
|
||||
"writing/document-rewrite client snapshot stale; using current artifact: "
|
||||
"thread=%s path=%s expected_hash=%s actual_hash=%s",
|
||||
body.thread_id,
|
||||
requested_path,
|
||||
body.expected_hash,
|
||||
base_hash,
|
||||
)
|
||||
|
||||
job_store = getattr(request.app.state, "deep_research_job_store", None)
|
||||
dispatcher = getattr(request.app.state, "deep_research_dispatcher", None)
|
||||
if job_store is None or dispatcher is None:
|
||||
raise HTTPException(status_code=503, detail="改写任务服务暂不可用。")
|
||||
_output_actual_path, output_virtual_path = _next_document_rewrite_output_path(actual_path, requested_path)
|
||||
scope = _artifact_rewrite_scope(body.thread_id, requested_path)
|
||||
report_outline = body.report_outline.strip() if body.report_outline else ""
|
||||
instruction = _instruction_with_report_outline(body.instruction, report_outline)
|
||||
@@ -464,6 +528,7 @@ async def start_durable_document_rewrite_job(
|
||||
"user_id": user_id,
|
||||
"thread_id": body.thread_id,
|
||||
"path": requested_path,
|
||||
"output_path": output_virtual_path,
|
||||
"original_content": original,
|
||||
"base_hash": base_hash,
|
||||
"instruction": instruction,
|
||||
@@ -498,7 +563,28 @@ async def start_durable_document_rewrite_job(
|
||||
phase="initializing",
|
||||
)
|
||||
if not _artifact_job_matches(job, thread_id=body.thread_id, virtual_path=requested_path):
|
||||
raise HTTPException(status_code=409, detail="该文件已有不兼容的改写任务,请先等待或停止它。")
|
||||
logger.warning(
|
||||
"writing/document-rewrite active-job scope collision: thread=%s path=%s "
|
||||
"existing_job=%s existing_session=%s existing_kind=%s",
|
||||
body.thread_id,
|
||||
requested_path,
|
||||
job.get("id"),
|
||||
job.get("session_id"),
|
||||
(job.get("input_snapshot") or {}).get("kind")
|
||||
if isinstance(job.get("input_snapshot"), dict)
|
||||
else None,
|
||||
)
|
||||
message = "该文件已有不兼容的改写任务,请先等待或停止它。"
|
||||
diagnostic = build_document_rewrite_diagnostic(
|
||||
job_id=str(job.get("id") or "") or None,
|
||||
stage="start",
|
||||
error_code="ACTIVE_JOB_SCOPE_COLLISION",
|
||||
message=message,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={"message": message, "diagnostic": diagnostic},
|
||||
)
|
||||
if created:
|
||||
dispatcher.nudge()
|
||||
return {
|
||||
@@ -506,9 +592,55 @@ async def start_durable_document_rewrite_job(
|
||||
"status": job["status"],
|
||||
"reused": not created,
|
||||
"baseHash": base_hash,
|
||||
"outputPath": str((job.get("input_snapshot") or {}).get("output_path") or output_virtual_path),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/document-rewrite/jobs")
|
||||
@require_permission("threads", "write", owner_check=True)
|
||||
async def start_durable_document_rewrite_job(
|
||||
body: DocumentRewriteRequest,
|
||||
request: Request,
|
||||
) -> dict:
|
||||
"""Start a durable rewrite and return safe diagnostics for unexpected failures."""
|
||||
try:
|
||||
return await _start_durable_document_rewrite_job_impl(body, request)
|
||||
except HTTPException as exc:
|
||||
if isinstance(exc.detail, dict) or exc.status_code < 409:
|
||||
raise
|
||||
diagnostic = build_document_rewrite_diagnostic(
|
||||
job_id=None,
|
||||
stage="start",
|
||||
error_code=f"HTTP_{exc.status_code}",
|
||||
message=str(exc.detail or "全文改写任务启动失败。"),
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=exc.status_code,
|
||||
detail={"message": diagnostic["message"], "diagnostic": diagnostic},
|
||||
headers=exc.headers,
|
||||
) from exc
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.exception(
|
||||
"writing/document-rewrite could not start: thread=%s path=%s",
|
||||
body.thread_id,
|
||||
body.path,
|
||||
)
|
||||
diagnostic = build_document_rewrite_diagnostic(
|
||||
job_id=None,
|
||||
stage="start",
|
||||
error_code=type(exc).__name__,
|
||||
message=str(exc) or "全文改写任务启动失败。",
|
||||
exc=exc,
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail={
|
||||
"message": diagnostic["message"],
|
||||
"diagnostic": diagnostic,
|
||||
},
|
||||
) from exc
|
||||
|
||||
|
||||
@router.get("/document-rewrite/jobs/latest")
|
||||
@require_permission("threads", "read")
|
||||
async def get_latest_durable_document_rewrite_job(
|
||||
|
||||
Reference in new issue
Block a user