feat: 深度研究支持独立新报告与原文改写,并完善沙箱、模型管理与全文改写
新报告不再覆盖当前文件卡片,写入时打开沙箱;步骤条仅在改写原文时位于卡片上方。同时补齐改写本地回退、模型管理与会商第三步相关改动。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
11f928bc95
commit
a5a9c952e8
63 files changed
+4113
-929
No files matched your search
@@ -24,9 +24,10 @@ import mimetypes
|
||||
import re
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
from typing import Any, Literal
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from fastapi.responses import FileResponse, StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
@@ -50,6 +51,15 @@ _POLL_INTERVAL_S = 0.8
|
||||
_LIVE_WAIT_S = 0.2
|
||||
_REPORT_IMAGE_REF_RE = re.compile(r"deep-research://([A-Za-z0-9._-]+)")
|
||||
_INLINE_REPORT_IMAGE_RE = re.compile(r"!\[([^\]]*)\]\(deep-research://([A-Za-z0-9._-]+)\)")
|
||||
# Reports produced by the research writer can contain either its in-progress
|
||||
# ``[[source:id]]`` marker or the saved ``[来源:id]`` marker. A structural
|
||||
# variant gets new source ids, so both forms must be rebased before the copied
|
||||
# report is ever used as the new writer's input.
|
||||
_REPORT_SOURCE_MARKER_RE = re.compile(
|
||||
r"\[\[\s*source\s*[::]\s*([A-Za-z0-9_-]{1,128})\s*\]\]"
|
||||
r"|\[\s*来源\s*[::]\s*([A-Za-z0-9_-]{1,128})\s*\]",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
_MAX_HTML_EXPORT_IMAGE_BYTES = 20 * 1024 * 1024
|
||||
|
||||
|
||||
@@ -157,12 +167,22 @@ class FullReportRewriteRequest(BaseModel):
|
||||
"""A full report rewrite initiated from the sandbox file header."""
|
||||
|
||||
instruction: str = Field(..., min_length=1, max_length=4_000)
|
||||
operation: Literal["rewrite", "generate"] = "rewrite"
|
||||
report_outline: str | None = Field(default=None, alias="reportOutline", max_length=20_000)
|
||||
style: str | None = Field(default=None, max_length=64)
|
||||
model_name: str | None = Field(default=None, alias="modelName", max_length=256)
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class CreateReportVariantRequest(BaseModel):
|
||||
"""Create an independent report before regenerating it with a new structure."""
|
||||
|
||||
report_outline: str | None = Field(default=None, alias="reportOutline", max_length=20_000)
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class SourceSelectionRequest(BaseModel):
|
||||
"""A user's explicit source-admission decision for follow-up chat.
|
||||
|
||||
@@ -302,6 +322,41 @@ def _report_hash(report: str) -> str:
|
||||
return hashlib.sha256(report.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _instruction_with_report_outline(instruction: str, report_outline: str) -> str:
|
||||
"""Make a selected report template an explicit rewrite constraint."""
|
||||
normalized_instruction = instruction.strip()
|
||||
normalized_outline = report_outline.strip()
|
||||
if not normalized_outline:
|
||||
return normalized_instruction
|
||||
return "\n\n".join(
|
||||
(
|
||||
normalized_instruction,
|
||||
"【报告结构要求】",
|
||||
"请按以下 Markdown 章节结构重组全文;不适用的章节可以合并或省略,且不得为套用结构编造事实。",
|
||||
normalized_outline,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _rebase_report_source_markers(report: str, source_id_map: dict[str, str]) -> str:
|
||||
"""Make inherited citations safe for a structurally regenerated report.
|
||||
|
||||
A report variant owns copied source rows with fresh ids. Its source set
|
||||
contains *only* the parent's selected evidence. Keeping a marker for a
|
||||
parent source that was not selected would make the next writer faithfully
|
||||
repeat an id it cannot use, then fail the grounded-citation validation.
|
||||
Rebase selected markers to the writer's canonical ``[[source:id]]`` form
|
||||
and remove excluded markers before the model receives the source report.
|
||||
"""
|
||||
|
||||
def replace(match: re.Match[str]) -> str:
|
||||
original_id = match.group(1) or match.group(2)
|
||||
cloned_id = source_id_map.get(original_id)
|
||||
return f"[[source:{cloned_id}]]" if cloned_id else ""
|
||||
|
||||
return _REPORT_SOURCE_MARKER_RE.sub(replace, report)
|
||||
|
||||
|
||||
def _report_visible_char_count(report: str) -> int:
|
||||
return len("".join(report.split()))
|
||||
|
||||
@@ -500,8 +555,26 @@ async def list_sessions(
|
||||
) -> dict[str, Any]:
|
||||
user_id = await _require_user(request)
|
||||
store = _get_session_store(request)
|
||||
items = await store.list_by_user(user_id=user_id, status=status, limit=limit, offset=offset)
|
||||
total = await store.count_by_user(user_id=user_id, status=status)
|
||||
# Filter child reports before applying the public page window. Otherwise a
|
||||
# page containing many variants can look empty even though parent research
|
||||
# records still exist after it.
|
||||
unfiltered = await store.list_by_user(
|
||||
user_id=user_id,
|
||||
status=status,
|
||||
limit=100,
|
||||
offset=0,
|
||||
include_config=True,
|
||||
)
|
||||
# A generated report variant is a child file in its parent's conversation,
|
||||
# not a separate research history entry. Older builds leaked these rows into
|
||||
# the rail; filter them without deleting their durable reports.
|
||||
visible = [
|
||||
row
|
||||
for row in unfiltered
|
||||
if not bool((row.get("config_snapshot") or {}).get("report_variant"))
|
||||
]
|
||||
items = visible[offset : offset + limit]
|
||||
total = len(visible)
|
||||
return {
|
||||
"items": [_session_to_response(r) for r in items],
|
||||
"total": total,
|
||||
@@ -520,6 +593,32 @@ async def get_session(session_id: str, request: Request) -> SessionResponse:
|
||||
return _session_to_response(row, full=True)
|
||||
|
||||
|
||||
@router.get("/sessions/{session_id}/report-variants")
|
||||
async def list_report_variants(session_id: str, request: Request) -> dict[str, Any]:
|
||||
"""Return durable child reports for rendering inside the parent history."""
|
||||
user_id = await _require_user(request)
|
||||
store = _get_session_store(request)
|
||||
parent = await store.get(session_id, user_id=user_id)
|
||||
if parent is None:
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
|
||||
rows = await store.list_by_user(user_id=user_id, limit=100, offset=0, include_config=True)
|
||||
children = [
|
||||
row
|
||||
for row in rows
|
||||
if bool((row.get("config_snapshot") or {}).get("report_variant"))
|
||||
and str((row.get("config_snapshot") or {}).get("parent_session_id") or "") == session_id
|
||||
]
|
||||
# Stores list newest first; a conversation reads from oldest to newest.
|
||||
children.reverse()
|
||||
full_children: list[dict[str, Any]] = []
|
||||
for child in children:
|
||||
full = await store.get(str(child["id"]), user_id=user_id)
|
||||
if full is not None:
|
||||
full_children.append(full)
|
||||
return {"items": [_session_to_response(row, full=True) for row in full_children]}
|
||||
|
||||
|
||||
@router.patch("/sessions/{session_id}", response_model=SessionResponse)
|
||||
async def update_session(session_id: str, body: UpdateSessionRequest, request: Request) -> SessionResponse:
|
||||
user_id = await _require_user(request)
|
||||
@@ -583,31 +682,51 @@ async def start_job(session_id: str, body: StartJobRequest, request: Request) ->
|
||||
session = await session_store.get(session_id, user_id=user_id)
|
||||
if session is None:
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
if body.entry not in ("chat", "legacy"):
|
||||
if body.entry not in ("chat", "legacy", "regenerate"):
|
||||
raise HTTPException(status_code=422, detail=f"Unknown job entry '{body.entry}'")
|
||||
|
||||
config_dict = session.get("config_snapshot") or {}
|
||||
session_config = dict(session.get("config_snapshot") or {})
|
||||
is_report_variant = session_config.pop("report_variant", False) is True
|
||||
# Parent linkage is persistence metadata, not a DeepResearchConfig field.
|
||||
# Keep it on the child session, but never pass it to Pydantic or the runner.
|
||||
parent_session_id = session_config.pop("parent_session_id", None)
|
||||
config_dict = session_config
|
||||
|
||||
if body.entry == "chat":
|
||||
# Chat entry requires a real collector-agent thread to harvest from.
|
||||
if not session.get("runtime_thread_id"):
|
||||
raise HTTPException(status_code=409, detail="Session has no collection thread to harvest")
|
||||
if body.config:
|
||||
# The report-config card is confirmed at write time; merge its
|
||||
# choices over the frozen snapshot so the job (and later replays)
|
||||
# see exactly what the user confirmed.
|
||||
merged = {**config_dict, **body.config}
|
||||
try:
|
||||
merged_config = DeepResearchConfig.model_validate(merged).clamp()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
raise HTTPException(status_code=422, detail=f"Invalid report config: {exc}") from None
|
||||
config_dict = merged_config.model_dump()
|
||||
await session_store.update(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
config_snapshot=config_dict,
|
||||
mode=merged_config.mode,
|
||||
)
|
||||
elif body.entry == "regenerate":
|
||||
if not is_report_variant:
|
||||
raise HTTPException(status_code=409, detail="Only an independent report variant can be regenerated")
|
||||
selected_count = await _get_source_store(request).count_by_session(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
selected=True,
|
||||
)
|
||||
if selected_count < 1:
|
||||
raise HTTPException(status_code=409, detail="No selected source material is available for regeneration")
|
||||
|
||||
if body.entry in ("chat", "regenerate") and body.config:
|
||||
# Both first-write and regenerate use the ordinary report engine. The
|
||||
# latter swaps network collection for the already selected source pool.
|
||||
merged = {**config_dict, **body.config}
|
||||
try:
|
||||
merged_config = DeepResearchConfig.model_validate(merged).clamp()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
raise HTTPException(status_code=422, detail=f"Invalid report config: {exc}") from None
|
||||
config_dict = merged_config.model_dump()
|
||||
persisted_config = dict(config_dict)
|
||||
if is_report_variant:
|
||||
persisted_config["report_variant"] = True
|
||||
if parent_session_id:
|
||||
persisted_config["parent_session_id"] = parent_session_id
|
||||
await session_store.update(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
config_snapshot=persisted_config,
|
||||
mode=merged_config.mode,
|
||||
)
|
||||
|
||||
# Per-user active job limit (§19.4).
|
||||
active = await session_store.count_active_by_user(user_id=user_id)
|
||||
@@ -673,6 +792,85 @@ async def start_job(session_id: str, body: StartJobRequest, request: Request) ->
|
||||
}
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/report-variants", response_model=SessionResponse)
|
||||
async def create_report_variant(
|
||||
session_id: str,
|
||||
body: CreateReportVariantRequest,
|
||||
request: Request,
|
||||
) -> SessionResponse:
|
||||
"""Fork a completed report so a structural regeneration never overwrites it.
|
||||
|
||||
The new session starts as a blank report with a cloned selected-evidence
|
||||
pool. The browser then queues the ordinary report engine against it. The
|
||||
parent stays independently visible and no stale parent Markdown can flash
|
||||
or overwrite the new stream.
|
||||
"""
|
||||
user_id = await _require_user(request)
|
||||
session_store = _get_session_store(request)
|
||||
source_store = _get_source_store(request)
|
||||
parent = await session_store.get(session_id, user_id=user_id)
|
||||
if parent is None:
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
report = str(parent.get("report_markdown") or "")
|
||||
if parent.get("status") != "completed" or not report.strip():
|
||||
raise HTTPException(status_code=409, detail="A completed report is required before regeneration")
|
||||
|
||||
outline = body.report_outline.strip() if body.report_outline else ""
|
||||
config_snapshot = dict(parent.get("config_snapshot") or {})
|
||||
# A structural regeneration writes through the same durable executor as a
|
||||
# chat-collected report, which gives it a runtime thread for sandbox I/O.
|
||||
# Keep an explicit marker so the frontend can seed the topic bubble for the
|
||||
# otherwise-empty variant thread while reusing the normal report timeline.
|
||||
config_snapshot["report_variant"] = True
|
||||
config_snapshot["parent_session_id"] = session_id
|
||||
if outline:
|
||||
config_snapshot["custom_outline"] = outline
|
||||
|
||||
parent_title = str(parent.get("title") or parent.get("query") or "研究报告").strip()
|
||||
variant_id = _new_id("drs")
|
||||
variant_row = await session_store.create(
|
||||
id=variant_id,
|
||||
user_id=user_id,
|
||||
title=f"{parent_title} · 新结构报告",
|
||||
query=str(parent.get("query") or ""),
|
||||
mode=str(parent.get("mode") or "basic"),
|
||||
status="draft",
|
||||
config_snapshot=config_snapshot,
|
||||
)
|
||||
try:
|
||||
# Give the variant the same hidden thread-backed sandbox as an initial
|
||||
# report. History can therefore reuse the normal message/timeline UI
|
||||
# instead of falling into the one-off legacy summary layout.
|
||||
await _ensure_chat_runtime_thread(request, variant_row, user_id)
|
||||
source_id_map = await source_store.clone_selected_to_session(
|
||||
session_id,
|
||||
variant_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
updated = await session_store.update(
|
||||
variant_id,
|
||||
user_id=user_id,
|
||||
report_markdown=None,
|
||||
report_html=None,
|
||||
source_count=len(source_id_map),
|
||||
# Never copy ``lastJobId``: doing so would make the variant replay
|
||||
# its parent's historical progress trace as if it were its own.
|
||||
usage_snapshot={},
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
# A half-created report would be confusing in history. Keep the
|
||||
# original untouched and fail this request before any rewrite starts.
|
||||
try:
|
||||
await session_store.delete(variant_id, user_id=user_id)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("could not clean up incomplete report variant %s", variant_id)
|
||||
logger.exception("could not clone deep-research report variant from %s", session_id)
|
||||
raise HTTPException(status_code=500, detail="创建新结构报告失败,请稍后重试") from None
|
||||
if updated is None:
|
||||
raise HTTPException(status_code=500, detail="创建新结构报告失败,请稍后重试")
|
||||
return _session_to_response(updated, full=True)
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/document-rewrite")
|
||||
async def start_full_report_rewrite_job(
|
||||
session_id: str,
|
||||
@@ -696,25 +894,37 @@ async def start_full_report_rewrite_job(
|
||||
if session.get("status") != "completed" or not report.strip():
|
||||
raise HTTPException(status_code=409, detail="A completed report is required before full rewrite")
|
||||
|
||||
instruction = body.instruction.strip()
|
||||
report_outline = body.report_outline.strip() if body.report_outline else ""
|
||||
instruction = _instruction_with_report_outline(body.instruction, report_outline)
|
||||
style = body.style.strip() if body.style else "formal_analysis"
|
||||
sources = await source_store.list_by_session(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
selected=True,
|
||||
limit=32,
|
||||
# Regeneration must see the same admitted evidence set as the source
|
||||
# report. The prompt builder enforces its own total character budget.
|
||||
limit=500,
|
||||
include_content=True,
|
||||
)
|
||||
# Historical SQL-backed rows expose ``created_at`` as a datetime whereas
|
||||
# older SQLite rows happened to expose strings. The durable job snapshot
|
||||
# must be JSON-only on both backends; otherwise a completed historical
|
||||
# report fails before it can queue a rewrite (and the browser misreports
|
||||
# that 500 response as a CORS failure).
|
||||
sources_snapshot = jsonable_encoder(sources)
|
||||
request_payload = {
|
||||
"kind": "full_report_rewrite",
|
||||
"operation": body.operation,
|
||||
"base_hash": _report_hash(report),
|
||||
"instruction": instruction,
|
||||
"report_outline": report_outline,
|
||||
"style": style,
|
||||
"model_name": body.model_name,
|
||||
"source_ids": [str(source.get("id") or "") for source in sources],
|
||||
}
|
||||
snapshot = {
|
||||
"kind": "full_report_rewrite",
|
||||
"operation": body.operation,
|
||||
"session_id": session_id,
|
||||
"user_id": user_id,
|
||||
"query": session.get("query", ""),
|
||||
@@ -727,8 +937,9 @@ async def start_full_report_rewrite_job(
|
||||
},
|
||||
"config": session.get("config_snapshot") or {},
|
||||
"report": report,
|
||||
"sources": sources,
|
||||
"sources": sources_snapshot,
|
||||
"instruction": instruction,
|
||||
"report_outline": report_outline,
|
||||
"style": style,
|
||||
"model_name": body.model_name,
|
||||
"entry": "document_rewrite",
|
||||
@@ -775,7 +986,9 @@ async def get_latest_full_report_rewrite_job(session_id: str, request: Request)
|
||||
"job": _job_to_response(row).model_dump(),
|
||||
"result": row.get("result_snapshot") or {},
|
||||
"request": {
|
||||
"operation": str(snapshot.get("operation") or "rewrite"),
|
||||
"instruction": str(snapshot.get("instruction") or ""),
|
||||
"reportOutline": str(snapshot.get("report_outline") or ""),
|
||||
"style": str(snapshot.get("style") or "formal_analysis"),
|
||||
"modelName": snapshot.get("model_name"),
|
||||
},
|
||||
@@ -1195,7 +1408,8 @@ async def stream_full_report_rewrite(
|
||||
)
|
||||
base_hash = _report_hash(report)
|
||||
style = body.style.strip() if body.style else "formal_analysis"
|
||||
instruction = body.instruction.strip()
|
||||
report_outline = body.report_outline.strip() if body.report_outline else ""
|
||||
instruction = _instruction_with_report_outline(body.instruction, report_outline)
|
||||
rewrite_target = _make_rewrite_target(report, 0, len(report), "全文")
|
||||
question = (
|
||||
"请重写整篇研究报告。"
|
||||
@@ -1335,6 +1549,7 @@ async def stream_full_report_rewrite(
|
||||
)
|
||||
return
|
||||
version_id: str | None = None
|
||||
version_snapshot_failed = False
|
||||
if version_store is not None:
|
||||
try:
|
||||
version_id = f"drv_{uuid.uuid4().hex}"
|
||||
@@ -1351,27 +1566,24 @@ async def stream_full_report_rewrite(
|
||||
model_name=body.model_name,
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("could not create report rewrite version for %s", session_id)
|
||||
await session_store.replace_report_if_unchanged(
|
||||
# A version snapshot is best effort. The rewritten
|
||||
# report is already committed and must remain usable.
|
||||
logger.exception(
|
||||
"could not persist report rewrite version for %s; keeping rewritten report",
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
expected_report=rewritten,
|
||||
report_markdown=report,
|
||||
)
|
||||
yield format_sse(
|
||||
"document_rewrite",
|
||||
{"type": "error", "message": "保存改写版本失败,报告已恢复原文。", "stage": "commit"},
|
||||
)
|
||||
return
|
||||
try:
|
||||
await version_store.prune_source(
|
||||
user_id=user_id,
|
||||
source_type="deep_research_report",
|
||||
source_id=session_id,
|
||||
source_path="report.md",
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("could not prune deep-research rewrite versions for %s", session_id)
|
||||
version_id = None
|
||||
version_snapshot_failed = True
|
||||
else:
|
||||
try:
|
||||
await version_store.prune_source(
|
||||
user_id=user_id,
|
||||
source_type="deep_research_report",
|
||||
source_id=session_id,
|
||||
source_path="report.md",
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("could not prune deep-research rewrite versions for %s", session_id)
|
||||
yield format_sse(
|
||||
"document_rewrite",
|
||||
{
|
||||
@@ -1379,6 +1591,7 @@ async def stream_full_report_rewrite(
|
||||
"content": rewritten,
|
||||
"committedHash": committed_hash,
|
||||
"versionId": version_id,
|
||||
"versionSnapshotFailed": version_snapshot_failed,
|
||||
},
|
||||
)
|
||||
yield format_sse(
|
||||
@@ -1387,6 +1600,7 @@ async def stream_full_report_rewrite(
|
||||
"type": "done",
|
||||
"committedHash": committed_hash,
|
||||
"versionId": version_id,
|
||||
"versionSnapshotFailed": version_snapshot_failed,
|
||||
},
|
||||
)
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user