Merge remote-tracking branch 'origin/master' into ji
# Conflicts: # .gitignore # frontend-web/src/components/workspace/artifacts/context.tsx # frontend-web/src/components/workspace/messages/message-group.tsx # offline-backend-20260512/backend/CLAUDE.md # offline-backend-20260512/backend/README.md # offline-backend-20260512/backend/app/gateway/app.py # offline-backend-20260512/backend/app/gateway/deps.py # offline-backend-20260512/backend/app/gateway/routers/llmwiki.py
This commit is contained in:
commit
e818c24769
262 files changed
+29527
-5236
No files matched your search
+2
-2
@@ -9,9 +9,9 @@
|
||||
收到研究课题后,按以下顺序工作,并把过程自然地讲给用户听:
|
||||
|
||||
1. **理解课题**:先用一小段话说明你对课题的理解、打算从哪些方面收集信息(背景、现状、数据、案例、趋势、争议点等)。这段话直接输出,不要调用工具。
|
||||
2. **拆分检索词**:把课题拆成若干具体的检索词,覆盖不同方面、避免重复。
|
||||
2. **拆分检索词**:把课题拆成若干具体的检索词,覆盖不同方面、避免重复。**例外**:若用户消息含有 `<retrieval-directions>` 块,则**按这些方向来拆分检索词**——每个方向拆成若干具体、可检索的词(专名、时间、地点、同义/中英对照等),**不要把方向名称本身当检索词**(方向标签往往搜不到结果);不要扩展到未列出的方向。某个检索词没有结果时换更具体或更短的词再试。
|
||||
3. **按顺序检索**:**一次只检索一个检索词**,检索完看结果,再决定下一个。这样用户能看清每个检索词换回了什么材料。
|
||||
4. **需要时用技能**:如果某个检索词更适合知识库或某个技能,先读取技能说明(read_file 技能的 SKILL.md),再按说明调用。
|
||||
4. **需要时用技能**:如果某个检索词更适合知识库或某个技能,先读取技能说明(read_file 技能的 SKILL.md),再按说明调用。**例外**:若用户消息含有 `<retrieval-skills>` 块,则必须按其中列出的技能检索:先 read_file 每个技能的 SKILL.md,再按其说明调用;不要改用未列出的技能,也不要自行改成纯 web_search(除非技能说明要求联网)。
|
||||
5. **交叉验证与补漏**:发现关键信息缺来源、或多个来源说法矛盾时,补一轮针对性检索。
|
||||
6. **输出收集小结**:检索完成后,输出一段「收集小结」,包含:
|
||||
- 覆盖了哪些方面、共收集到多少条材料;
|
||||
|
||||
@@ -64,15 +64,15 @@ class AgentResponse(BaseModel):
|
||||
description="入库模板名 (external knowledge-ingest templateName). When set, markdown artifacts produced in this agent's chats can be published to the external knowledge-ingest API.",
|
||||
)
|
||||
|
||||
seat_skill_directive: bool | None = Field(
|
||||
seat_skill_directive: bool | None = Field(
|
||||
default=None,
|
||||
description="会商席位「技能识别强化」开关 (config.yaml). True = 本智能体作为会商席位时强化技能识别 (与业务链条同名开关取 OR). None/False = 关 (默认).",
|
||||
)
|
||||
|
||||
llmwiki_knowledge_base_ids: list[str] | None = Field(
|
||||
default=None,
|
||||
description="DeerFlow LLMWiki mapping ids this agent may offer for conversation retrieval.",
|
||||
)
|
||||
)
|
||||
|
||||
llmwiki_knowledge_base_ids: list[str] | None = Field(
|
||||
default=None,
|
||||
description="DeerFlow LLMWiki mapping ids this agent may offer for conversation retrieval.",
|
||||
)
|
||||
|
||||
tags: list[dict] = Field(default_factory=list, description="Tags assigned to this agent")
|
||||
|
||||
@@ -125,12 +125,12 @@ class AgentCreateRequest(BaseModel):
|
||||
tool_groups: list[str] | None = Field(default=None, description="Optional tool group whitelist persisted to config.yaml")
|
||||
common_questions: list[CommonQuestion] | None = Field(default=None, description="Recommended questions (常用问题) persisted to config.yaml. Each item is {title, prompt}; legacy strings are coerced.")
|
||||
template_name: str | None = Field(default=None, description="入库模板名 persisted to config.yaml. Enables publishing markdown artifacts to the external knowledge-ingest API.")
|
||||
seat_skill_directive: bool | None = Field(default=None, description="会商席位「技能识别强化」开关 persisted to config.yaml. None/False = off (默认).")
|
||||
llmwiki_knowledge_base_ids: list[str] | None = Field(
|
||||
default=None,
|
||||
max_length=100,
|
||||
description="Allowed DeerFlow LLMWiki mapping ids persisted to config.yaml.",
|
||||
)
|
||||
seat_skill_directive: bool | None = Field(default=None, description="会商席位「技能识别强化」开关 persisted to config.yaml. None/False = off (默认).")
|
||||
llmwiki_knowledge_base_ids: list[str] | None = Field(
|
||||
default=None,
|
||||
max_length=100,
|
||||
description="Allowed DeerFlow LLMWiki mapping ids persisted to config.yaml.",
|
||||
)
|
||||
|
||||
|
||||
class AgentUpdateRequest(BaseModel):
|
||||
@@ -147,12 +147,12 @@ class AgentUpdateRequest(BaseModel):
|
||||
tool_groups: list[str] | None = Field(default=None, description="Updated tool group whitelist (config.yaml)")
|
||||
common_questions: list[CommonQuestion] | None = Field(default=None, description="Updated recommended questions (常用问题, config.yaml). Each item is {title, prompt}; legacy strings are coerced.")
|
||||
template_name: str | None = Field(default=None, description="Updated 入库模板名 (config.yaml). Empty string clears it.")
|
||||
seat_skill_directive: bool | None = Field(default=None, description="Updated 会商席位「技能识别强化」开关 (config.yaml).")
|
||||
llmwiki_knowledge_base_ids: list[str] | None = Field(
|
||||
default=None,
|
||||
max_length=100,
|
||||
description="Updated allowed DeerFlow LLMWiki mapping ids (config.yaml).",
|
||||
)
|
||||
seat_skill_directive: bool | None = Field(default=None, description="Updated 会商席位「技能识别强化」开关 (config.yaml).")
|
||||
llmwiki_knowledge_base_ids: list[str] | None = Field(
|
||||
default=None,
|
||||
max_length=100,
|
||||
description="Updated allowed DeerFlow LLMWiki mapping ids (config.yaml).",
|
||||
)
|
||||
|
||||
|
||||
class AgentSelectorResponse(BaseModel):
|
||||
@@ -193,38 +193,38 @@ def _current_user_id(request: Request) -> str:
|
||||
return get_effective_user_id()
|
||||
|
||||
|
||||
def _is_admin_user(request: Request) -> bool:
|
||||
def _is_admin_user(request: Request) -> bool:
|
||||
"""Return True when the request is authenticated as a system admin."""
|
||||
user = getattr(request.state, "user", None)
|
||||
return getattr(user, "system_role", None) == "admin"
|
||||
|
||||
|
||||
async def _validate_llmwiki_bindings(
|
||||
request: Request,
|
||||
mapping_ids: list[str] | None,
|
||||
) -> list[str] | None:
|
||||
"""Normalize bindings and require read access for the editing actor."""
|
||||
|
||||
if mapping_ids is None:
|
||||
return None
|
||||
normalized = list(dict.fromkeys(item.strip() for item in mapping_ids if item.strip()))
|
||||
if not normalized:
|
||||
return []
|
||||
store = getattr(request.app.state, "llmwiki_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="LLMWiki metadata store is unavailable")
|
||||
user_id = _current_user_id(request)
|
||||
is_admin = _is_admin_user(request)
|
||||
for mapping_id in normalized:
|
||||
row = await store.get_authorized(
|
||||
mapping_id,
|
||||
user_id,
|
||||
write=False,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=422, detail=f"LLMWiki knowledge base '{mapping_id}' is unavailable")
|
||||
return normalized
|
||||
return getattr(user, "system_role", None) == "admin"
|
||||
|
||||
|
||||
async def _validate_llmwiki_bindings(
|
||||
request: Request,
|
||||
mapping_ids: list[str] | None,
|
||||
) -> list[str] | None:
|
||||
"""Normalize bindings and require read access for the editing actor."""
|
||||
|
||||
if mapping_ids is None:
|
||||
return None
|
||||
normalized = list(dict.fromkeys(item.strip() for item in mapping_ids if item.strip()))
|
||||
if not normalized:
|
||||
return []
|
||||
store = getattr(request.app.state, "llmwiki_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="LLMWiki metadata store is unavailable")
|
||||
user_id = _current_user_id(request)
|
||||
is_admin = _is_admin_user(request)
|
||||
for mapping_id in normalized:
|
||||
row = await store.get_authorized(
|
||||
mapping_id,
|
||||
user_id,
|
||||
write=False,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=422, detail=f"LLMWiki knowledge base '{mapping_id}' is unavailable")
|
||||
return normalized
|
||||
|
||||
|
||||
def _email_local_part(email: str | None) -> str | None:
|
||||
@@ -276,7 +276,7 @@ def _validate_storage_id(agent_id: str) -> str:
|
||||
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
||||
|
||||
|
||||
def _write_agent_config(
|
||||
def _write_agent_config(
|
||||
agent_id: str,
|
||||
*,
|
||||
name: str,
|
||||
@@ -286,9 +286,9 @@ def _write_agent_config(
|
||||
tool_groups: list[str] | None = None,
|
||||
common_questions: list[CommonQuestion] | None = None,
|
||||
template_name: str | None = None,
|
||||
seat_skill_directive: bool | None = None,
|
||||
llmwiki_knowledge_base_ids: list[str] | None = None,
|
||||
) -> None:
|
||||
seat_skill_directive: bool | None = None,
|
||||
llmwiki_knowledge_base_ids: list[str] | None = None,
|
||||
) -> None:
|
||||
agent_dir = get_paths().agent_dir(agent_id)
|
||||
agent_dir.mkdir(parents=True, exist_ok=True)
|
||||
config_file = agent_dir / "config.yaml"
|
||||
@@ -317,19 +317,27 @@ def _write_agent_config(
|
||||
if template_name is not None and template_name.strip():
|
||||
config_data["template_name"] = template_name.strip()
|
||||
# 会商席位技能识别强化开关:仅当显式设置时写入(None = 不写, 由 AgentConfig 默认为关)。
|
||||
if seat_skill_directive is not None:
|
||||
config_data["seat_skill_directive"] = bool(seat_skill_directive)
|
||||
if llmwiki_knowledge_base_ids is not None:
|
||||
config_data["llmwiki_knowledge_base_ids"] = list(
|
||||
dict.fromkeys(item.strip() for item in llmwiki_knowledge_base_ids if item.strip())
|
||||
)
|
||||
if seat_skill_directive is not None:
|
||||
config_data["seat_skill_directive"] = bool(seat_skill_directive)
|
||||
if llmwiki_knowledge_base_ids is not None:
|
||||
config_data["llmwiki_knowledge_base_ids"] = list(
|
||||
dict.fromkeys(item.strip() for item in llmwiki_knowledge_base_ids if item.strip())
|
||||
)
|
||||
with open(config_file, "w", encoding="utf-8") as f:
|
||||
yaml.dump(config_data, f, default_flow_style=False, allow_unicode=True)
|
||||
|
||||
|
||||
def _load_config_extras(agent_id: str) -> dict[str, Any]:
|
||||
"""Read skills/model/tool_groups/common_questions from the agent's config.yaml, tolerant of missing files."""
|
||||
empty = {"skills": None, "model": None, "tool_groups": None, "common_questions": None, "template_name": None, "seat_skill_directive": None, "llmwiki_knowledge_base_ids": None}
|
||||
empty = {
|
||||
"skills": None,
|
||||
"model": None,
|
||||
"tool_groups": None,
|
||||
"common_questions": None,
|
||||
"template_name": None,
|
||||
"seat_skill_directive": None,
|
||||
"llmwiki_knowledge_base_ids": None,
|
||||
}
|
||||
try:
|
||||
cfg = load_agent_config(agent_id)
|
||||
except (FileNotFoundError, ValueError):
|
||||
@@ -342,8 +350,8 @@ def _load_config_extras(agent_id: str) -> dict[str, Any]:
|
||||
"tool_groups": cfg.tool_groups,
|
||||
"common_questions": cfg.common_questions,
|
||||
"template_name": cfg.template_name,
|
||||
"seat_skill_directive": cfg.seat_skill_directive,
|
||||
"llmwiki_knowledge_base_ids": cfg.llmwiki_knowledge_base_ids,
|
||||
"seat_skill_directive": cfg.seat_skill_directive,
|
||||
"llmwiki_knowledge_base_ids": cfg.llmwiki_knowledge_base_ids,
|
||||
}
|
||||
|
||||
|
||||
@@ -504,7 +512,15 @@ def _record_to_response(
|
||||
can happen lazily when the agent is actually opened.
|
||||
"""
|
||||
soul: str | None = None
|
||||
extras: dict[str, Any] = {"skills": None, "model": None, "tool_groups": None, "common_questions": None, "template_name": None, "seat_skill_directive": None, "llmwiki_knowledge_base_ids": None}
|
||||
extras: dict[str, Any] = {
|
||||
"skills": None,
|
||||
"model": None,
|
||||
"tool_groups": None,
|
||||
"common_questions": None,
|
||||
"template_name": None,
|
||||
"seat_skill_directive": None,
|
||||
"llmwiki_knowledge_base_ids": None,
|
||||
}
|
||||
if include_extras:
|
||||
_ensure_agent_dir_from_record(record)
|
||||
extras = _load_config_extras(record["id"])
|
||||
@@ -528,8 +544,8 @@ def _record_to_response(
|
||||
skills=extras["skills"],
|
||||
common_questions=extras.get("common_questions"),
|
||||
template_name=extras.get("template_name"),
|
||||
seat_skill_directive=extras.get("seat_skill_directive"),
|
||||
llmwiki_knowledge_base_ids=extras.get("llmwiki_knowledge_base_ids"),
|
||||
seat_skill_directive=extras.get("seat_skill_directive"),
|
||||
llmwiki_knowledge_base_ids=extras.get("llmwiki_knowledge_base_ids"),
|
||||
featured_order=record.get("featured_order"),
|
||||
)
|
||||
|
||||
@@ -830,7 +846,7 @@ async def get_agent(agent_id: str, request: Request) -> AgentResponse:
|
||||
summary="Create Agent",
|
||||
description="Create a new user-owned agent. The display name can be Chinese and may duplicate existing names.",
|
||||
)
|
||||
async def create_agent_endpoint(request: AgentCreateRequest, http_request: Request) -> AgentResponse:
|
||||
async def create_agent_endpoint(request: AgentCreateRequest, http_request: Request) -> AgentResponse:
|
||||
_require_agents_api_enabled()
|
||||
name = _validate_display_name(request.name)
|
||||
store = get_agent_store(http_request)
|
||||
@@ -839,13 +855,13 @@ async def create_agent_endpoint(request: AgentCreateRequest, http_request: Reque
|
||||
agent_id = _validate_storage_id(request.id)
|
||||
if await store.get_any(agent_id) is not None:
|
||||
raise HTTPException(status_code=409, detail=f"Agent id '{agent_id}' already exists")
|
||||
else:
|
||||
agent_id = uuid4().hex
|
||||
agent_dir = get_paths().agent_dir(agent_id)
|
||||
llmwiki_ids = await _validate_llmwiki_bindings(
|
||||
http_request,
|
||||
request.llmwiki_knowledge_base_ids,
|
||||
)
|
||||
else:
|
||||
agent_id = uuid4().hex
|
||||
agent_dir = get_paths().agent_dir(agent_id)
|
||||
llmwiki_ids = await _validate_llmwiki_bindings(
|
||||
http_request,
|
||||
request.llmwiki_knowledge_base_ids,
|
||||
)
|
||||
|
||||
try:
|
||||
if agent_dir.exists():
|
||||
@@ -860,8 +876,8 @@ async def create_agent_endpoint(request: AgentCreateRequest, http_request: Reque
|
||||
tool_groups=request.tool_groups,
|
||||
common_questions=request.common_questions,
|
||||
template_name=request.template_name,
|
||||
seat_skill_directive=request.seat_skill_directive,
|
||||
llmwiki_knowledge_base_ids=llmwiki_ids,
|
||||
seat_skill_directive=request.seat_skill_directive,
|
||||
llmwiki_knowledge_base_ids=llmwiki_ids,
|
||||
)
|
||||
_write_agent_soul(agent_id, request.soul)
|
||||
|
||||
@@ -942,14 +958,14 @@ async def update_agent(agent_id: str, request: AgentUpdateRequest, http_request:
|
||||
next_tool_groups = request.tool_groups if "tool_groups" in fields_set else existing_extras["tool_groups"]
|
||||
next_common_questions = request.common_questions if "common_questions" in fields_set else existing_extras["common_questions"]
|
||||
next_template_name = request.template_name if "template_name" in fields_set else existing_extras["template_name"]
|
||||
next_seat_skill_directive = request.seat_skill_directive if "seat_skill_directive" in fields_set else existing_extras["seat_skill_directive"]
|
||||
next_llmwiki_ids = (
|
||||
await _validate_llmwiki_bindings(http_request, request.llmwiki_knowledge_base_ids)
|
||||
if "llmwiki_knowledge_base_ids" in fields_set
|
||||
else existing_extras["llmwiki_knowledge_base_ids"]
|
||||
)
|
||||
|
||||
config_dirty = bool(update_data) or any(key in fields_set for key in ("skills", "model", "tool_groups", "common_questions", "template_name", "seat_skill_directive", "llmwiki_knowledge_base_ids"))
|
||||
next_seat_skill_directive = request.seat_skill_directive if "seat_skill_directive" in fields_set else existing_extras["seat_skill_directive"]
|
||||
next_llmwiki_ids = (
|
||||
await _validate_llmwiki_bindings(http_request, request.llmwiki_knowledge_base_ids)
|
||||
if "llmwiki_knowledge_base_ids" in fields_set
|
||||
else existing_extras["llmwiki_knowledge_base_ids"]
|
||||
)
|
||||
|
||||
config_dirty = bool(update_data) or any(key in fields_set for key in ("skills", "model", "tool_groups", "common_questions", "template_name", "seat_skill_directive", "llmwiki_knowledge_base_ids"))
|
||||
if config_dirty:
|
||||
_write_agent_config(
|
||||
agent_id,
|
||||
@@ -960,8 +976,8 @@ async def update_agent(agent_id: str, request: AgentUpdateRequest, http_request:
|
||||
tool_groups=next_tool_groups,
|
||||
common_questions=next_common_questions,
|
||||
template_name=next_template_name,
|
||||
seat_skill_directive=next_seat_skill_directive,
|
||||
llmwiki_knowledge_base_ids=next_llmwiki_ids,
|
||||
seat_skill_directive=next_seat_skill_directive,
|
||||
llmwiki_knowledge_base_ids=next_llmwiki_ids,
|
||||
)
|
||||
# Keep the listing cache in lock-step with config.yaml, but only
|
||||
# when the cached fields were actually touched — a partial update
|
||||
|
||||
@@ -36,6 +36,27 @@ EDITABLE_TEXT_SUFFIXES = {
|
||||
".log",
|
||||
}
|
||||
|
||||
_VIRTUAL_USER_DATA_PREFIX = "mnt/user-data"
|
||||
|
||||
|
||||
def coerce_artifact_virtual_path(path: str) -> str:
|
||||
"""Normalize an artifacts URL path to a sandbox virtual path.
|
||||
|
||||
Streaming write previews may request a bare filename such as
|
||||
``未命名文件.md`` before the model has emitted a real
|
||||
``/mnt/user-data/outputs/...`` path. Mapping a single path segment into
|
||||
the thread outputs directory turns that into a normal 404 (file not written
|
||||
yet) instead of HTTP 400 ``Path must start with /mnt/user-data``.
|
||||
"""
|
||||
stripped = path.strip().lstrip("/").replace("\\", "/")
|
||||
if not stripped:
|
||||
raise HTTPException(status_code=400, detail="Path must start with /mnt/user-data")
|
||||
if stripped == _VIRTUAL_USER_DATA_PREFIX or stripped.startswith(_VIRTUAL_USER_DATA_PREFIX + "/"):
|
||||
return stripped
|
||||
if "/" in stripped or stripped in {".", ".."}:
|
||||
raise HTTPException(status_code=400, detail="Path must start with /mnt/user-data")
|
||||
return f"{_VIRTUAL_USER_DATA_PREFIX}/outputs/{stripped}"
|
||||
|
||||
|
||||
class ArtifactUpdateRequest(BaseModel):
|
||||
content: str
|
||||
@@ -198,6 +219,7 @@ async def get_artifact(thread_id: str, path: str, request: Request, download: bo
|
||||
- Download file: `/api/threads/abc123/artifacts/mnt/user-data/outputs/data.csv?download=true`
|
||||
- Active web content such as `.html`, `.xhtml`, and `.svg` artifacts is always downloaded
|
||||
"""
|
||||
path = coerce_artifact_virtual_path(path)
|
||||
# Check if this is a request for a file inside a .skill archive (e.g., xxx.skill/SKILL.md)
|
||||
if ".skill/" in path:
|
||||
# Split the path at ".skill/" to get the ZIP file path and internal path
|
||||
@@ -285,6 +307,7 @@ async def update_artifact(
|
||||
if ".skill/" in path:
|
||||
raise HTTPException(status_code=400, detail="Skill archive contents are read-only.")
|
||||
|
||||
path = coerce_artifact_virtual_path(path)
|
||||
actual_path = await aresolve_thread_virtual_path(thread_id, path)
|
||||
logger.info(f"Updating artifact path: thread_id={thread_id}, requested_path={path}, actual_path={actual_path}")
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ from datetime import datetime
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.deps import get_agent_store
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -125,6 +126,37 @@ async def _validate_public_chain_mapping(
|
||||
status_code=400,
|
||||
detail="Global business mappings require a public business chain",
|
||||
)
|
||||
|
||||
# 公共业务映射会被任意账号启动,链条中的席位也必须对任意账号可见。
|
||||
# 仅把链条本身发布、却把其中某个自定义智能体保留为未发布/私有,会导致
|
||||
# 前端只能解析到不足两个席位,最终被误判为“没有可用业务链”。
|
||||
seats = chain.get("seats")
|
||||
if isinstance(seats, list):
|
||||
agent_store = get_agent_store(request)
|
||||
unavailable_names: list[str] = []
|
||||
for raw_seat in seats:
|
||||
if not isinstance(raw_seat, dict):
|
||||
continue
|
||||
agent_id = str(raw_seat.get("agent_id") or "").strip()
|
||||
if not agent_id:
|
||||
continue
|
||||
agent = await agent_store.get_any(agent_id)
|
||||
is_builtin = agent is not None and agent.get("user_id") is None
|
||||
is_public_agent = bool(agent and agent.get("published")) and not bool(
|
||||
agent and agent.get("is_private")
|
||||
)
|
||||
if is_builtin or is_public_agent:
|
||||
continue
|
||||
name = str(raw_seat.get("name") or agent_id).strip()
|
||||
unavailable_names.append(name)
|
||||
if unavailable_names:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=(
|
||||
"Global business mappings require every seat to be published and public; "
|
||||
f"unavailable agents: {', '.join(unavailable_names)}"
|
||||
),
|
||||
)
|
||||
fields["chain_id"] = chain_id
|
||||
fields["chain_title"] = str(chain.get("title") or "") or None
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
"""Compact/sidebar-summary menu override API.
|
||||
|
||||
Routes (prefix ``/api/compact-menu-overrides``):
|
||||
GET "" list all compact-menu overrides
|
||||
PUT "" replace the entire override set (admin)
|
||||
GET "" list compact-menu overrides.
|
||||
``menu_type`` omitted / empty → type A (original legacy data).
|
||||
PUT "" replace that type's override set (admin).
|
||||
``menu_type`` omitted / empty → type A, does not wipe type B.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -10,11 +12,15 @@ from __future__ import annotations
|
||||
from datetime import datetime
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.deps import get_current_user, get_optional_user_from_request
|
||||
from deerflow.persistence.compact_menu_overrides import CompactMenuOverrideStore
|
||||
from deerflow.persistence.compact_menu_overrides import (
|
||||
CompactMenuOverrideStore,
|
||||
DEFAULT_COMPACT_MENU_TYPE,
|
||||
normalize_compact_menu_type,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/compact-menu-overrides", tags=["compact-menu-overrides"])
|
||||
|
||||
@@ -27,15 +33,22 @@ class CompactMenuOverride(BaseModel):
|
||||
|
||||
|
||||
class CompactMenuOverrideResponse(CompactMenuOverride):
|
||||
menu_type: str = DEFAULT_COMPACT_MENU_TYPE
|
||||
updated_by: str | None = None
|
||||
updated_at: datetime | str | None = None
|
||||
|
||||
|
||||
class CompactMenuOverrideListResponse(BaseModel):
|
||||
menu_type: str = DEFAULT_COMPACT_MENU_TYPE
|
||||
overrides: list[CompactMenuOverrideResponse]
|
||||
|
||||
|
||||
class CompactMenuOverrideReplaceRequest(BaseModel):
|
||||
menu_type: str | None = Field(
|
||||
default=None,
|
||||
max_length=32,
|
||||
description="A or B. Omitted/empty = A (original compact-menu set).",
|
||||
)
|
||||
overrides: list[CompactMenuOverride] = Field(default_factory=list)
|
||||
|
||||
|
||||
@@ -55,10 +68,21 @@ async def _require_admin(request: Request) -> None:
|
||||
|
||||
|
||||
@router.get("", response_model=CompactMenuOverrideListResponse)
|
||||
async def list_compact_menu_overrides(request: Request) -> CompactMenuOverrideListResponse:
|
||||
async def list_compact_menu_overrides(
|
||||
request: Request,
|
||||
menu_type: str | None = Query(
|
||||
default=None,
|
||||
max_length=32,
|
||||
description="A or B. Omitted/empty = A, returning the original compact-menu rows.",
|
||||
),
|
||||
) -> CompactMenuOverrideListResponse:
|
||||
t = normalize_compact_menu_type(menu_type)
|
||||
store = _get_store(request)
|
||||
rows = await store.list_overrides()
|
||||
return CompactMenuOverrideListResponse(overrides=[CompactMenuOverrideResponse(**r) for r in rows])
|
||||
rows = await store.list_overrides(t)
|
||||
return CompactMenuOverrideListResponse(
|
||||
menu_type=t,
|
||||
overrides=[CompactMenuOverrideResponse(**r) for r in rows],
|
||||
)
|
||||
|
||||
|
||||
@router.put("", response_model=CompactMenuOverrideListResponse)
|
||||
@@ -67,10 +91,15 @@ async def replace_compact_menu_overrides(
|
||||
body: CompactMenuOverrideReplaceRequest,
|
||||
) -> CompactMenuOverrideListResponse:
|
||||
await _require_admin(request)
|
||||
t = normalize_compact_menu_type(body.menu_type)
|
||||
store = _get_store(request)
|
||||
user_id = await get_current_user(request)
|
||||
rows = await store.replace_all(
|
||||
[o.model_dump() for o in body.overrides],
|
||||
menu_type=t,
|
||||
updated_by=user_id,
|
||||
)
|
||||
return CompactMenuOverrideListResponse(overrides=[CompactMenuOverrideResponse(**r) for r in rows])
|
||||
return CompactMenuOverrideListResponse(
|
||||
menu_type=t,
|
||||
overrides=[CompactMenuOverrideResponse(**r) for r in rows],
|
||||
)
|
||||
@@ -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,24 @@ 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)
|
||||
structure_mode: Literal["fixed", "adaptive"] | None = Field(default=None, alias="structureMode")
|
||||
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)
|
||||
structure_mode: Literal["fixed", "adaptive"] | None = Field(default=None, alias="structureMode")
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class SourceSelectionRequest(BaseModel):
|
||||
"""A user's explicit source-admission decision for follow-up chat.
|
||||
|
||||
@@ -302,6 +324,58 @@ def _report_hash(report: str) -> str:
|
||||
return hashlib.sha256(report.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _instruction_with_report_outline(
|
||||
instruction: str,
|
||||
report_outline: str,
|
||||
structure_mode: str | None = None,
|
||||
) -> 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
|
||||
if structure_mode == "fixed":
|
||||
constraint = (
|
||||
"请按以下结构输出全文:文首题名与「日期:」「类型:」「密级:」等版头必须保留并填入真实值"
|
||||
"(占位符 XXX/xxx/xxxxx 不得照抄或删行;日期用当天中文日期;类型按题名判断;密级无另行规定时写「内部」)。"
|
||||
"章节标题、编号与层级必须与大纲一致,不得改写、合并、省略或重排任何小节;"
|
||||
"材料不足的小节保留原标题并写明资料不足,不得为凑结构编造事实。"
|
||||
)
|
||||
else:
|
||||
constraint = (
|
||||
"请按以下结构输出全文:文首题名与「日期:」「类型:」「密级:」等版头必须保留并填入真实值"
|
||||
"(占位符 XXX/xxx/xxxxx 不得照抄或删行;日期用当天中文日期;类型按题名判断;密级无另行规定时写「内部」)。"
|
||||
"章节按大纲展开;仅当某小节完全没有材料时才可合并或省略该小节,不得为套用结构编造事实。"
|
||||
)
|
||||
return "\n\n".join(
|
||||
(
|
||||
normalized_instruction,
|
||||
"【报告结构要求】",
|
||||
constraint,
|
||||
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 +574,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 +612,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 +701,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)
|
||||
@@ -639,17 +777,30 @@ async def start_job(session_id: str, body: StartJobRequest, request: Request) ->
|
||||
input_snapshot=input_snapshot,
|
||||
)
|
||||
|
||||
# Update session status.
|
||||
await session_store.update(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
status="running",
|
||||
active_job_id=job["id"],
|
||||
)
|
||||
# The durable job is the source of truth once it has been committed. A
|
||||
# transient failure updating the display projection must not turn that
|
||||
# successful write into a misleading 500 response or leave it undispatched.
|
||||
# The executor re-projects the terminal status when it completes.
|
||||
try:
|
||||
await session_store.update(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
status="running",
|
||||
active_job_id=job["id"],
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception(
|
||||
"deep-research job %s was queued but its session running projection could not be updated",
|
||||
job["id"],
|
||||
)
|
||||
|
||||
# Nudge the dispatcher to claim immediately.
|
||||
# Nudge the dispatcher even for an idempotent retry. If an earlier request
|
||||
# committed the job but failed before this point (for example a transient
|
||||
# MySQL read-after-write failure), the retry receives the existing queued
|
||||
# job. Restricting the nudge to ``created`` would leave that durable job
|
||||
# queued until the next periodic scan and make the UI appear stuck.
|
||||
dispatcher = _get_dispatcher(request)
|
||||
if dispatcher is not None and created:
|
||||
if dispatcher is not None:
|
||||
dispatcher.nudge()
|
||||
|
||||
return {
|
||||
@@ -660,6 +811,87 @@ 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
|
||||
if body.structure_mode:
|
||||
config_snapshot["structure_mode"] = body.structure_mode
|
||||
|
||||
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,
|
||||
@@ -683,25 +915,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, body.structure_mode)
|
||||
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", ""),
|
||||
@@ -714,8 +958,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",
|
||||
@@ -762,7 +1007,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"),
|
||||
},
|
||||
@@ -810,36 +1057,68 @@ async def stream_job(
|
||||
if loop.time() >= next_durable_poll:
|
||||
# Durable replay repairs a missed live delta after
|
||||
# reconnect or when another worker owned the job.
|
||||
events = await event_store.list_after(job_id, after=cursor, limit=200)
|
||||
for ev in events:
|
||||
payload = {
|
||||
"seq": ev["seq"],
|
||||
"sessionId": session_id,
|
||||
"jobId": job_id,
|
||||
"type": ev["event_type"],
|
||||
"phase": ev.get("phase", "initializing"),
|
||||
"timestamp": _iso(ev.get("created_at")),
|
||||
"payload": ev.get("payload") or {},
|
||||
}
|
||||
yield format_sse("progress", payload, event_id=str(ev["seq"]))
|
||||
cursor = max(cursor, ev["seq"])
|
||||
current = await job_store.get_unscoped(job_id)
|
||||
if current and current.get("status") in TERMINAL_STATUSES:
|
||||
try:
|
||||
events = await event_store.list_after(job_id, after=cursor, limit=200)
|
||||
for ev in events:
|
||||
payload = {
|
||||
"seq": ev["seq"],
|
||||
"sessionId": session_id,
|
||||
"jobId": job_id,
|
||||
"type": ev["event_type"],
|
||||
"phase": ev.get("phase", "initializing"),
|
||||
"timestamp": _iso(ev.get("created_at")),
|
||||
"payload": ev.get("payload") or {},
|
||||
}
|
||||
yield format_sse("progress", payload, event_id=str(ev["seq"]))
|
||||
cursor = max(cursor, ev["seq"])
|
||||
current = await job_store.get_unscoped(job_id)
|
||||
if current and current.get("status") in TERMINAL_STATUSES:
|
||||
yield format_sse(
|
||||
"progress",
|
||||
{
|
||||
"seq": cursor + 1,
|
||||
"sessionId": session_id,
|
||||
"jobId": job_id,
|
||||
"type": "job_status",
|
||||
"phase": current.get("phase", "done"),
|
||||
"timestamp": _iso(datetime.now(UTC)),
|
||||
"payload": {"status": current["status"], "progress": current.get("progress", 0)},
|
||||
},
|
||||
event_id=str(cursor + 1),
|
||||
)
|
||||
return
|
||||
next_durable_poll = loop.time() + _POLL_INTERVAL_S
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception: # noqa: BLE001
|
||||
# Do not let one read-replica / transient DB failure
|
||||
# abort the ASGI generator (which surfaces as an
|
||||
# ExceptionGroup and leaves the page stuck). Live
|
||||
# frames can still reach an attached browser, and the
|
||||
# next durable poll repairs the cursor on recovery.
|
||||
logger.warning(
|
||||
"deep-research SSE durable replay temporarily failed for job %s; keeping stream open",
|
||||
job_id,
|
||||
exc_info=True,
|
||||
)
|
||||
yield format_sse(
|
||||
"progress",
|
||||
{
|
||||
"seq": cursor + 1,
|
||||
"seq": 0,
|
||||
"liveId": f"durable-replay-retry:{job_id}:{int(loop.time() * 1000)}",
|
||||
"sessionId": session_id,
|
||||
"jobId": job_id,
|
||||
"type": "job_status",
|
||||
"phase": current.get("phase", "done"),
|
||||
"type": "warning",
|
||||
"phase": "initializing",
|
||||
"timestamp": _iso(datetime.now(UTC)),
|
||||
"payload": {"status": current["status"], "progress": current.get("progress", 0)},
|
||||
"payload": {
|
||||
"code": "durable_replay_temporarily_unavailable",
|
||||
"message": "进度同步暂时不可用,任务仍在继续;正在自动重试。",
|
||||
"recoverable": True,
|
||||
},
|
||||
},
|
||||
event_id=str(cursor + 1),
|
||||
)
|
||||
return
|
||||
next_durable_poll = loop.time() + _POLL_INTERVAL_S
|
||||
next_durable_poll = loop.time() + max(_POLL_INTERVAL_S, 3.0)
|
||||
if live_queue is None:
|
||||
await asyncio.sleep(max(0.01, next_durable_poll - loop.time()))
|
||||
continue
|
||||
@@ -1150,7 +1429,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, body.structure_mode)
|
||||
rewrite_target = _make_rewrite_target(report, 0, len(report), "全文")
|
||||
question = (
|
||||
"请重写整篇研究报告。"
|
||||
@@ -1290,6 +1570,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}"
|
||||
@@ -1306,27 +1587,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",
|
||||
{
|
||||
@@ -1334,6 +1612,7 @@ async def stream_full_report_rewrite(
|
||||
"content": rewritten,
|
||||
"committedHash": committed_hash,
|
||||
"versionId": version_id,
|
||||
"versionSnapshotFailed": version_snapshot_failed,
|
||||
},
|
||||
)
|
||||
yield format_sse(
|
||||
@@ -1342,6 +1621,7 @@ async def stream_full_report_rewrite(
|
||||
"type": "done",
|
||||
"committedHash": committed_hash,
|
||||
"versionId": version_id,
|
||||
"versionSnapshotFailed": version_snapshot_failed,
|
||||
},
|
||||
)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,710 @@
|
||||
"""Independent enterprise-research workbench API.
|
||||
|
||||
This router intentionally does not alter the established ``/api/deep-research``
|
||||
workflow. It stores a small owner-scoped research task and calls WeKnora through
|
||||
the same DeerFlow-owned authorization mapping used by the knowledge-base page.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, Literal
|
||||
from urllib.parse import urlencode
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request, status
|
||||
from pydantic import BaseModel, Field
|
||||
from starlette.responses import StreamingResponse
|
||||
|
||||
from app.gateway.deps import get_optional_user_from_request
|
||||
from deerflow.agents.deep_research.adapters.llm import DeerFlowCompletionBackend
|
||||
from deerflow.agents.deep_research.config import DeepResearchConfig
|
||||
from deerflow.integrations.weknora.client import WeKnoraError
|
||||
from deerflow.integrations.weknora.runtime import build_weknora_client, get_resolved_llmwiki_runtime
|
||||
from deerflow.persistence.enterprise_research import EnterpriseResearchReportJobStore, EnterpriseResearchTaskStore
|
||||
from deerflow.persistence.llmwiki import LlmWikiStore
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/enterprise-research", tags=["enterprise-research"])
|
||||
|
||||
TaskStatus = Literal["draft", "collecting", "ready", "partial", "failed"]
|
||||
|
||||
|
||||
class EnterpriseResearchEvidence(BaseModel):
|
||||
chunk_id: str = Field(default="", max_length=256)
|
||||
content: str = Field(default="", max_length=4_000)
|
||||
knowledge_id: str = Field(default="", max_length=256)
|
||||
knowledge_base_id: str = Field(default="", max_length=256)
|
||||
knowledge_base_name: str = Field(default="", max_length=512)
|
||||
title: str = Field(default="", max_length=1_024)
|
||||
filename: str = Field(default="", max_length=1_024)
|
||||
score: float = 0.0
|
||||
chunk_index: int = 0
|
||||
url: str = Field(default="", max_length=4_096)
|
||||
|
||||
|
||||
class EnterpriseResearchTaskResponse(BaseModel):
|
||||
id: str
|
||||
subject: str
|
||||
focus: str
|
||||
template_id: str
|
||||
knowledge_base_ids: list[str]
|
||||
queries: list[str]
|
||||
selected_source_ids: list[str] = Field(default_factory=list)
|
||||
sources: list[EnterpriseResearchEvidence]
|
||||
plan_markdown: str | None = None
|
||||
report_markdown: str | None = None
|
||||
report_summary: str | None = None
|
||||
report_model_name: str | None = None
|
||||
active_report_job_id: str | None = None
|
||||
status: TaskStatus
|
||||
warning: str | None = None
|
||||
created_at: str | None = None
|
||||
updated_at: str | None = None
|
||||
|
||||
|
||||
class EnterpriseResearchTaskListResponse(BaseModel):
|
||||
tasks: list[EnterpriseResearchTaskResponse]
|
||||
|
||||
|
||||
class EnterpriseResearchTaskCreate(BaseModel):
|
||||
subject: str = Field(..., min_length=1, max_length=180)
|
||||
focus: str = Field(default="", max_length=800)
|
||||
template_id: str = Field(default="topic", min_length=1, max_length=64)
|
||||
knowledge_base_ids: list[str] = Field(default_factory=list, max_length=100)
|
||||
queries: list[str] = Field(..., min_length=1, max_length=8)
|
||||
|
||||
|
||||
class EnterpriseResearchPlanUpdate(BaseModel):
|
||||
plan_markdown: str = Field(..., min_length=1, max_length=16_000)
|
||||
selected_source_ids: list[str] = Field(default_factory=list, max_length=48)
|
||||
|
||||
|
||||
class EnterpriseResearchAiPlanCreate(BaseModel):
|
||||
model_name: str | None = Field(default=None, max_length=256)
|
||||
instruction: str = Field(default="", max_length=2_000)
|
||||
|
||||
|
||||
class EnterpriseResearchReportJobCreate(BaseModel):
|
||||
model_name: str | None = Field(default=None, max_length=256)
|
||||
action: Literal["initial", "rewrite", "followup"] = "initial"
|
||||
instruction: str = Field(default="", max_length=4_000)
|
||||
style: Literal["formal", "analytical", "concise"] = "formal"
|
||||
target_length: int | None = Field(default=None, ge=800, le=30_000)
|
||||
|
||||
|
||||
class EnterpriseResearchReportJobResponse(BaseModel):
|
||||
id: str
|
||||
task_id: str
|
||||
status: Literal["queued", "running", "completed", "failed", "cancelled"]
|
||||
phase: str
|
||||
model_name: str | None = None
|
||||
action: Literal["initial", "rewrite", "followup"] = "initial"
|
||||
instruction: str | None = None
|
||||
style: str | None = None
|
||||
target_length: int | None = None
|
||||
original_report: str | None = None
|
||||
report_markdown: str = ""
|
||||
report_summary: str | None = None
|
||||
error_message: str | None = None
|
||||
created_at: str | None = None
|
||||
updated_at: str | None = None
|
||||
|
||||
|
||||
class EnterpriseResearchReportJobListResponse(BaseModel):
|
||||
jobs: list[EnterpriseResearchReportJobResponse]
|
||||
|
||||
|
||||
def _task_store(request: Request) -> EnterpriseResearchTaskStore:
|
||||
store = getattr(request.app.state, "enterprise_research_task_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="企业深度研究任务存储暂不可用")
|
||||
return store
|
||||
|
||||
|
||||
def _report_job_store(request: Request) -> EnterpriseResearchReportJobStore:
|
||||
store = getattr(request.app.state, "enterprise_research_report_job_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="企业研究报告任务存储暂不可用")
|
||||
return store
|
||||
|
||||
|
||||
def _report_executor(request: Request):
|
||||
executor = getattr(request.app.state, "enterprise_research_report_executor", None)
|
||||
if executor is None:
|
||||
raise HTTPException(status_code=503, detail="企业研究报告执行器暂不可用")
|
||||
return executor
|
||||
|
||||
|
||||
def _report_start_lock(request: Request) -> asyncio.Lock:
|
||||
lock = getattr(request.app.state, "enterprise_research_report_start_lock", None)
|
||||
if lock is None:
|
||||
lock = asyncio.Lock()
|
||||
request.app.state.enterprise_research_report_start_lock = lock
|
||||
return lock
|
||||
|
||||
|
||||
def _llmwiki_store(request: Request) -> LlmWikiStore:
|
||||
store = getattr(request.app.state, "llmwiki_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="知识库权限存储暂不可用")
|
||||
return store
|
||||
|
||||
|
||||
async def _actor(request: Request) -> tuple[str, bool]:
|
||||
user = await get_optional_user_from_request(request)
|
||||
if user is None:
|
||||
return "default", True
|
||||
return str(user.id), getattr(user, "system_role", None) == "admin"
|
||||
|
||||
|
||||
def _response(row: dict[str, Any]) -> EnterpriseResearchTaskResponse:
|
||||
return EnterpriseResearchTaskResponse(
|
||||
id=str(row["id"]),
|
||||
subject=str(row.get("subject") or ""),
|
||||
focus=str(row.get("focus") or ""),
|
||||
template_id=str(row.get("template_id") or "topic"),
|
||||
knowledge_base_ids=[str(item) for item in row.get("knowledge_base_ids") or []],
|
||||
queries=[str(item) for item in row.get("queries") or []],
|
||||
selected_source_ids=[str(item) for item in row.get("selected_source_ids") or []],
|
||||
sources=[EnterpriseResearchEvidence.model_validate(item) for item in row.get("sources") or [] if isinstance(item, dict)],
|
||||
plan_markdown=str(row["plan_markdown"]) if row.get("plan_markdown") else None,
|
||||
report_markdown=str(row["report_markdown"]) if row.get("report_markdown") else None,
|
||||
report_summary=str(row["report_summary"]) if row.get("report_summary") else None,
|
||||
report_model_name=str(row["report_model_name"]) if row.get("report_model_name") else None,
|
||||
active_report_job_id=str(row["active_report_job_id"]) if row.get("active_report_job_id") else None,
|
||||
status=row.get("status") if row.get("status") in {"draft", "collecting", "ready", "partial", "failed"} else "failed",
|
||||
warning=str(row["warning"]) if row.get("warning") else None,
|
||||
created_at=row.get("created_at"),
|
||||
updated_at=row.get("updated_at"),
|
||||
)
|
||||
|
||||
|
||||
def _report_job_response(row: dict[str, Any]) -> EnterpriseResearchReportJobResponse:
|
||||
job_status = str(row.get("status") or "failed")
|
||||
if job_status not in {"queued", "running", "completed", "failed", "cancelled"}:
|
||||
job_status = "failed"
|
||||
action = str(row.get("action") or "initial")
|
||||
if action not in {"initial", "rewrite", "followup"}:
|
||||
action = "initial"
|
||||
return EnterpriseResearchReportJobResponse(
|
||||
id=str(row["id"]), task_id=str(row["task_id"]), status=job_status, phase=str(row.get("phase") or job_status),
|
||||
model_name=str(row["model_name"]) if row.get("model_name") else None,
|
||||
action=action,
|
||||
instruction=str(row["instruction"]) if row.get("instruction") else None,
|
||||
style=str(row["style"]) if row.get("style") else None,
|
||||
target_length=int(row["target_length"]) if row.get("target_length") else None,
|
||||
original_report=str(row["original_report"]) if row.get("original_report") else None,
|
||||
report_markdown=str(row.get("report_markdown") or ""),
|
||||
report_summary=str(row["report_summary"]) if row.get("report_summary") else None,
|
||||
error_message=str(row["error_message"]) if row.get("error_message") else None,
|
||||
created_at=row.get("created_at"), updated_at=row.get("updated_at"),
|
||||
)
|
||||
|
||||
|
||||
def _normalise_ids(values: list[str]) -> list[str]:
|
||||
seen: set[str] = set()
|
||||
result: list[str] = []
|
||||
for value in values:
|
||||
item = str(value).strip()
|
||||
if item and item not in seen:
|
||||
seen.add(item)
|
||||
result.append(item)
|
||||
return result[:100]
|
||||
|
||||
|
||||
def _normalise_queries(values: list[str]) -> list[str]:
|
||||
seen: set[str] = set()
|
||||
result: list[str] = []
|
||||
for value in values:
|
||||
item = str(value).strip()[:512]
|
||||
if item and item not in seen:
|
||||
seen.add(item)
|
||||
result.append(item)
|
||||
return result[:8]
|
||||
|
||||
|
||||
def _default_plan(task: dict[str, Any], selected_source_ids: list[str]) -> str:
|
||||
"""Build a transparent editable plan; report prose is a later, separate job."""
|
||||
|
||||
subject = str(task.get("subject") or "研究主题")
|
||||
focus = str(task.get("focus") or "")
|
||||
template_id = str(task.get("template_id") or "topic")
|
||||
if template_id == "event":
|
||||
chapters = [
|
||||
"一、事件边界与时间线",
|
||||
"二、相关主体与立场",
|
||||
"三、影响传导与风险研判",
|
||||
"四、后续观察与应对建议",
|
||||
]
|
||||
elif template_id == "policy":
|
||||
chapters = [
|
||||
"一、政策与行业背景",
|
||||
"二、现状、数据与变化趋势",
|
||||
"三、传导机制与关键影响",
|
||||
"四、风险、机会与行动建议",
|
||||
]
|
||||
else:
|
||||
chapters = [
|
||||
"一、研究对象与事实背景",
|
||||
"二、关键问题、观点与分歧",
|
||||
"三、影响、风险与机会",
|
||||
"四、结论、待验证问题与建议",
|
||||
]
|
||||
sources_by_id = {str(item.get("chunk_id") or ""): item for item in task.get("sources") or [] if isinstance(item, dict)}
|
||||
selected_titles = [
|
||||
str(sources_by_id[item].get("title") or sources_by_id[item].get("filename") or "未命名资料")
|
||||
for item in selected_source_ids
|
||||
if item in sources_by_id
|
||||
]
|
||||
source_lines = "\n".join(f"- {title}" for title in selected_titles[:12]) or "- 暂无已选资料"
|
||||
scope = focus or "围绕主题的事实、影响与可验证判断"
|
||||
return (
|
||||
f"# {subject}研究计划\n\n"
|
||||
"## 研究目标\n"
|
||||
f"围绕“{subject}”形成可追溯、可核验的研究结论;重点范围:{scope}。\n\n"
|
||||
"## 核心问题\n"
|
||||
"1. 当前已知事实、背景和时间范围是什么?\n"
|
||||
f"2. 与“{subject}”相关的主要主体、观点或变化是什么?\n"
|
||||
"3. 证据能够支持哪些影响、风险、机会或待验证判断?\n\n"
|
||||
"## 报告结构\n"
|
||||
+ "\n".join(f"{chapter}\n- 说明本章需回答的核心问题,并在正文中标记资料依据。" for chapter in chapters)
|
||||
+ "\n\n## 已选证据\n"
|
||||
+ source_lines
|
||||
+ "\n\n## 写作约束\n"
|
||||
"- 仅使用已选资料作为事实依据;不补充未经验证的外部事实。\n"
|
||||
"- 对资料不足或观点相互矛盾之处明确标记,避免将推测写成结论。\n"
|
||||
"- 生成报告前可继续编辑本计划与已选证据。\n"
|
||||
)
|
||||
|
||||
|
||||
def _validate_selected_source_ids(task: dict[str, Any], values: list[str]) -> list[str]:
|
||||
available_ids = {str(item.get("chunk_id") or "") for item in task.get("sources") or [] if isinstance(item, dict)}
|
||||
selected_ids = _normalise_ids(values)[:48]
|
||||
if any(item not in available_ids for item in selected_ids):
|
||||
raise HTTPException(status_code=422, detail="选中的资料已变化,请刷新后重新选择")
|
||||
return selected_ids
|
||||
|
||||
|
||||
async def _visible_knowledge_bases(request: Request, task: dict[str, Any], user_id: str, is_admin: bool) -> list[dict[str, Any]]:
|
||||
store = _llmwiki_store(request)
|
||||
selected_ids = _normalise_ids(task.get("knowledge_base_ids") or [])
|
||||
if not selected_ids:
|
||||
return await store.list_visible(user_id, scope="all", is_admin=False)
|
||||
rows: list[dict[str, Any]] = []
|
||||
for mapping_id in selected_ids:
|
||||
row = await store.get_authorized(mapping_id, user_id, write=False, is_admin=is_admin)
|
||||
if row is None:
|
||||
# Never reveal whether this belongs to a different account.
|
||||
raise HTTPException(status_code=404, detail="知识库不存在或无访问权限")
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
|
||||
def _client(request: Request):
|
||||
runtime = get_resolved_llmwiki_runtime(getattr(request.app.state, "config", None))
|
||||
if not runtime.weknora_enabled:
|
||||
raise HTTPException(status_code=409, detail="WeKnora 知识服务尚未启用")
|
||||
try:
|
||||
return build_weknora_client(runtime)
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc)) from None
|
||||
|
||||
|
||||
def _evidence_view(result: dict[str, Any], mappings: dict[str, dict[str, Any]]) -> dict[str, Any] | None:
|
||||
remote_base_id = str(result.get("knowledge_base_id") or "")
|
||||
mapping = mappings.get(remote_base_id)
|
||||
if mapping is None:
|
||||
return None
|
||||
params = urlencode({"knowledge_base_id": mapping["id"], "chunk_id": result.get("chunk_id") or ""})
|
||||
return {
|
||||
"chunk_id": str(result.get("chunk_id") or ""),
|
||||
"content": str(result.get("content") or "")[:4_000],
|
||||
"knowledge_id": str(result.get("knowledge_id") or ""),
|
||||
"knowledge_base_id": str(mapping["id"]),
|
||||
"knowledge_base_name": str(mapping.get("name") or ""),
|
||||
"title": str(result.get("title") or ""),
|
||||
"filename": str(result.get("filename") or ""),
|
||||
"score": float(result.get("score") or 0),
|
||||
"chunk_index": int(result.get("chunk_index") or 0),
|
||||
"url": f"/weknora-source-preview?{params}",
|
||||
}
|
||||
|
||||
|
||||
def _dedupe_evidence(results: list[dict[str, Any]], mappings: dict[str, dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
seen: set[str] = set()
|
||||
evidence: list[dict[str, Any]] = []
|
||||
for result in results:
|
||||
item = _evidence_view(result, mappings)
|
||||
if item is None:
|
||||
continue
|
||||
key = item["chunk_id"] or f"{item['knowledge_id']}:{item['chunk_index']}:{item['title']}"
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
evidence.append(item)
|
||||
if len(evidence) >= 48:
|
||||
break
|
||||
return evidence
|
||||
|
||||
|
||||
@router.get("/tasks", response_model=EnterpriseResearchTaskListResponse)
|
||||
async def list_tasks(request: Request, limit: int = Query(default=20, ge=1, le=50)) -> EnterpriseResearchTaskListResponse:
|
||||
user_id, _ = await _actor(request)
|
||||
rows = await _task_store(request).list_tasks(user_id, limit=limit)
|
||||
return EnterpriseResearchTaskListResponse(tasks=[_response(row) for row in rows])
|
||||
|
||||
|
||||
@router.post("/tasks", response_model=EnterpriseResearchTaskResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_task(request: Request, body: EnterpriseResearchTaskCreate) -> EnterpriseResearchTaskResponse:
|
||||
user_id, _ = await _actor(request)
|
||||
queries = _normalise_queries(body.queries)
|
||||
if not queries:
|
||||
raise HTTPException(status_code=422, detail="至少需要一个有效检索词")
|
||||
row = await _task_store(request).create_task(
|
||||
{
|
||||
"id": f"ert_{uuid4().hex}",
|
||||
"user_id": user_id,
|
||||
"subject": body.subject.strip(),
|
||||
"focus": body.focus.strip(),
|
||||
"template_id": body.template_id.strip(),
|
||||
"knowledge_base_ids": _normalise_ids(body.knowledge_base_ids),
|
||||
"queries": queries,
|
||||
"status": "draft",
|
||||
}
|
||||
)
|
||||
return _response(row)
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/collect", response_model=EnterpriseResearchTaskResponse)
|
||||
async def collect_task(request: Request, task_id: str) -> EnterpriseResearchTaskResponse:
|
||||
"""Collect a bounded, user-authorized WeKnora evidence snapshot for a task."""
|
||||
|
||||
user_id, is_admin = await _actor(request)
|
||||
task_store = _task_store(request)
|
||||
task = await task_store.get_task(task_id, user_id)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="研究任务不存在")
|
||||
task = await task_store.update_task(task_id, user_id, {"status": "collecting", "warning": None})
|
||||
assert task is not None
|
||||
|
||||
try:
|
||||
bases = await _visible_knowledge_bases(request, task, user_id, is_admin)
|
||||
if not bases:
|
||||
row = await task_store.update_task(task_id, user_id, {"status": "failed", "warning": "当前账号没有可用知识库。"})
|
||||
return _response(row or task)
|
||||
client = _client(request)
|
||||
remote_ids = [str(row["weknora_id"]) for row in bases]
|
||||
outcomes = await asyncio.gather(
|
||||
*(client.search(query, remote_ids) for query in task["queries"]),
|
||||
return_exceptions=True,
|
||||
)
|
||||
raw_results: list[dict[str, Any]] = []
|
||||
failed_queries = 0
|
||||
for outcome in outcomes:
|
||||
if isinstance(outcome, Exception):
|
||||
failed_queries += 1
|
||||
logger.warning("Enterprise research query failed: %s", outcome)
|
||||
else:
|
||||
raw_results.extend(outcome)
|
||||
sources = _dedupe_evidence(raw_results, {str(row["weknora_id"]): row for row in bases})
|
||||
if not sources:
|
||||
task_status: TaskStatus = "failed"
|
||||
warning = "未检索到可用证据,请调整研究主题或检查知识库解析状态。"
|
||||
elif failed_queries:
|
||||
task_status = "partial"
|
||||
warning = f"{failed_queries} 个检索方向暂时不可用,已保留其余结果。"
|
||||
else:
|
||||
task_status = "ready"
|
||||
warning = None
|
||||
row = await task_store.update_task(task_id, user_id, {"status": task_status, "sources": sources, "warning": warning})
|
||||
return _response(row or task)
|
||||
except HTTPException:
|
||||
raise
|
||||
except WeKnoraError as exc:
|
||||
await task_store.update_task(task_id, user_id, {"status": "failed", "warning": str(exc)})
|
||||
raise HTTPException(status_code=exc.status_code, detail=str(exc)) from None
|
||||
except Exception:
|
||||
logger.exception("Enterprise research collection failed for %s", task_id)
|
||||
await task_store.update_task(task_id, user_id, {"status": "failed", "warning": "证据收集出现异常,请稍后重试。"})
|
||||
raise HTTPException(status_code=502, detail="证据收集失败,请稍后重试") from None
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/plan/draft", response_model=EnterpriseResearchTaskResponse)
|
||||
async def create_plan_draft(request: Request, task_id: str) -> EnterpriseResearchTaskResponse:
|
||||
"""Create a deterministic, editable research-plan draft from persisted evidence."""
|
||||
|
||||
user_id, _ = await _actor(request)
|
||||
task_store = _task_store(request)
|
||||
task = await task_store.get_task(task_id, user_id)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="研究任务不存在")
|
||||
if not task.get("sources"):
|
||||
raise HTTPException(status_code=409, detail="请先完成证据收集,再生成研究计划")
|
||||
selected_source_ids = _validate_selected_source_ids(task, [
|
||||
str(item.get("chunk_id") or "") for item in task["sources"][:24] if isinstance(item, dict)
|
||||
])
|
||||
row = await task_store.update_task(
|
||||
task_id,
|
||||
user_id,
|
||||
{
|
||||
"selected_source_ids": selected_source_ids,
|
||||
"plan_markdown": _default_plan(task, selected_source_ids),
|
||||
},
|
||||
)
|
||||
return _response(row or task)
|
||||
|
||||
|
||||
@router.put("/tasks/{task_id}/plan", response_model=EnterpriseResearchTaskResponse)
|
||||
async def update_plan(request: Request, task_id: str, body: EnterpriseResearchPlanUpdate) -> EnterpriseResearchTaskResponse:
|
||||
"""Persist the user's selected evidence and editable plan before report generation."""
|
||||
|
||||
user_id, _ = await _actor(request)
|
||||
task_store = _task_store(request)
|
||||
task = await task_store.get_task(task_id, user_id)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="研究任务不存在")
|
||||
if not task.get("sources"):
|
||||
raise HTTPException(status_code=409, detail="当前任务没有可用于编排的证据")
|
||||
selected_source_ids = _validate_selected_source_ids(task, body.selected_source_ids)
|
||||
plan_markdown = body.plan_markdown.strip()
|
||||
if not plan_markdown:
|
||||
raise HTTPException(status_code=422, detail="研究计划不能为空")
|
||||
row = await task_store.update_task(
|
||||
task_id,
|
||||
user_id,
|
||||
{"selected_source_ids": selected_source_ids, "plan_markdown": plan_markdown},
|
||||
)
|
||||
return _response(row or task)
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/plan/ai", response_model=EnterpriseResearchTaskResponse)
|
||||
async def create_ai_plan(
|
||||
request: Request, task_id: str, body: EnterpriseResearchAiPlanCreate
|
||||
) -> EnterpriseResearchTaskResponse:
|
||||
"""Use the configured model to improve a plan, without collecting new evidence."""
|
||||
|
||||
user_id, _ = await _actor(request)
|
||||
task_store = _task_store(request)
|
||||
task = await task_store.get_task(task_id, user_id)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="研究任务不存在")
|
||||
if not task.get("sources"):
|
||||
raise HTTPException(status_code=409, detail="请先完成证据收集")
|
||||
selected_ids = list(task.get("selected_source_ids") or [])
|
||||
if not selected_ids:
|
||||
selected_ids = [
|
||||
str(item.get("chunk_id") or "")
|
||||
for item in task["sources"][:24]
|
||||
if isinstance(item, dict) and item.get("chunk_id")
|
||||
]
|
||||
selected_ids = _validate_selected_source_ids(task, selected_ids)
|
||||
selected = {
|
||||
str(item.get("chunk_id") or ""): item
|
||||
for item in task.get("sources") or []
|
||||
if isinstance(item, dict)
|
||||
}
|
||||
evidence_titles = "\n".join(
|
||||
f"- {selected[item].get('title') or selected[item].get('filename') or item}"
|
||||
for item in selected_ids
|
||||
if item in selected
|
||||
)
|
||||
current = str(task.get("plan_markdown") or _default_plan(task, selected_ids))
|
||||
backend = DeerFlowCompletionBackend(
|
||||
DeepResearchConfig(mode="basic", language="zh-CN", smart_model=(body.model_name or "").strip() or None).clamp()
|
||||
)
|
||||
try:
|
||||
result = await backend.stream_complete(
|
||||
model_role="smart",
|
||||
operation="enterprise_research_plan",
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": (
|
||||
"你是企业研究规划师。仅优化研究目标、核心问题、章节结构和证据使用计划;"
|
||||
"不要撰写报告正文,不要联网,不要虚构资料。仅输出 Markdown 研究计划。"
|
||||
),
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": (
|
||||
f"研究主题:{task['subject']}\n重点范围:{task.get('focus') or '未限定'}\n"
|
||||
f"用户补充要求:{body.instruction or '提高结构完整性和章节关联性'}\n\n"
|
||||
f"当前计划:\n{current}\n\n已选资料标题:\n{evidence_titles}"
|
||||
),
|
||||
},
|
||||
],
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.exception("enterprise research AI plan generation failed task=%s", task_id)
|
||||
raise HTTPException(status_code=502, detail="研究计划生成失败,请检查模型配置后重试") from exc
|
||||
plan = result.text.strip()
|
||||
if plan.startswith("```"):
|
||||
lines = plan.splitlines()[1:]
|
||||
if lines and lines[-1].strip().startswith("```"):
|
||||
lines.pop()
|
||||
plan = "\n".join(lines).strip()
|
||||
if not plan:
|
||||
raise HTTPException(status_code=502, detail="模型没有返回可用研究计划")
|
||||
row = await task_store.update_task(
|
||||
task_id, user_id, {"plan_markdown": plan[:16_000], "selected_source_ids": selected_ids}
|
||||
)
|
||||
return _response(row or task)
|
||||
|
||||
|
||||
@router.get("/tasks/{task_id}/report-jobs", response_model=EnterpriseResearchReportJobListResponse)
|
||||
async def list_report_jobs(
|
||||
request: Request, task_id: str, limit: int = Query(default=10, ge=1, le=20)
|
||||
) -> EnterpriseResearchReportJobListResponse:
|
||||
user_id, _ = await _actor(request)
|
||||
if await _task_store(request).get_task(task_id, user_id) is None:
|
||||
raise HTTPException(status_code=404, detail="研究任务不存在")
|
||||
rows = await _report_job_store(request).list_jobs(task_id, user_id, limit=limit)
|
||||
return EnterpriseResearchReportJobListResponse(jobs=[_report_job_response(row) for row in rows])
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/report-jobs", response_model=EnterpriseResearchReportJobResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_report_job(
|
||||
request: Request, task_id: str, body: EnterpriseResearchReportJobCreate
|
||||
) -> EnterpriseResearchReportJobResponse:
|
||||
"""Freeze the approved plan/evidence, then launch an independent report job."""
|
||||
|
||||
user_id, _ = await _actor(request)
|
||||
task_store = _task_store(request)
|
||||
job_store = _report_job_store(request)
|
||||
executor = _report_executor(request)
|
||||
async with _report_start_lock(request):
|
||||
task = await task_store.get_task(task_id, user_id)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="研究任务不存在")
|
||||
active = await job_store.get_active_for_task(task_id, user_id)
|
||||
if active is not None:
|
||||
await executor.ensure_started(active["id"], user_id)
|
||||
return _report_job_response(active)
|
||||
plan = str(task.get("plan_markdown") or "").strip()
|
||||
selected_ids = _validate_selected_source_ids(task, list(task.get("selected_source_ids") or []))
|
||||
if not plan:
|
||||
raise HTTPException(status_code=409, detail="请先生成并保存研究计划")
|
||||
if not selected_ids:
|
||||
raise HTTPException(status_code=409, detail="请至少选择一条资料后再撰写报告")
|
||||
sources_by_id = {
|
||||
str(item.get("chunk_id") or ""): item
|
||||
for item in task.get("sources") or []
|
||||
if isinstance(item, dict)
|
||||
}
|
||||
sources = [sources_by_id[source_id] for source_id in selected_ids if source_id in sources_by_id]
|
||||
if not sources:
|
||||
raise HTTPException(status_code=409, detail="已选资料不可用,请重新保存研究计划")
|
||||
if body.action in {"rewrite", "followup"} and not task.get("report_markdown"):
|
||||
raise HTTPException(status_code=409, detail="当前任务还没有可供改写或追问的报告")
|
||||
if body.action == "followup" and not body.instruction.strip():
|
||||
raise HTTPException(status_code=422, detail="请填写报告追问")
|
||||
row = await job_store.create_job(
|
||||
{
|
||||
"id": f"erj_{uuid4().hex}", "task_id": task_id, "user_id": user_id,
|
||||
"model_name": (body.model_name or "").strip() or None,
|
||||
"action": body.action, "instruction": body.instruction.strip(), "style": body.style,
|
||||
"target_length": body.target_length, "plan_snapshot": plan, "sources": sources,
|
||||
"original_report": task.get("report_markdown") if body.action in {"rewrite", "followup"} else None,
|
||||
}
|
||||
)
|
||||
await task_store.update_task(task_id, user_id, {"active_report_job_id": row["id"]})
|
||||
await executor.ensure_started(row["id"], user_id)
|
||||
return _report_job_response(row)
|
||||
|
||||
|
||||
@router.get("/tasks/{task_id}/report-jobs/{job_id}/stream")
|
||||
async def stream_report_job(request: Request, task_id: str, job_id: str) -> StreamingResponse:
|
||||
"""SSE stream: durable draft snapshot first, then live Markdown/reasoning deltas."""
|
||||
|
||||
user_id, _ = await _actor(request)
|
||||
job = await _report_job_store(request).get_job(job_id, user_id)
|
||||
if job is None or job.get("task_id") != task_id:
|
||||
raise HTTPException(status_code=404, detail="报告任务不存在")
|
||||
if job["status"] == "queued":
|
||||
await _report_executor(request).ensure_started(job_id, user_id)
|
||||
|
||||
async def body():
|
||||
async for event in _report_executor(request).stream_events(job_id, user_id):
|
||||
yield "event: enterprise_research\n"
|
||||
yield "data: " + json.dumps(event, ensure_ascii=False, separators=(",", ":")) + "\n\n"
|
||||
|
||||
return StreamingResponse(
|
||||
body(), media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/tasks/{task_id}/report-jobs/{job_id}/cancel",
|
||||
response_model=EnterpriseResearchReportJobResponse,
|
||||
)
|
||||
async def cancel_report_job(request: Request, task_id: str, job_id: str) -> EnterpriseResearchReportJobResponse:
|
||||
user_id, _ = await _actor(request)
|
||||
job = await _report_job_store(request).get_job(job_id, user_id)
|
||||
if job is None or job.get("task_id") != task_id:
|
||||
raise HTTPException(status_code=404, detail="报告任务不存在")
|
||||
cancelled = await _report_executor(request).cancel(job_id, user_id)
|
||||
return _report_job_response(cancelled or job)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/tasks/{task_id}/report-jobs/{job_id}/restore",
|
||||
response_model=EnterpriseResearchTaskResponse,
|
||||
)
|
||||
async def restore_report_version(request: Request, task_id: str, job_id: str) -> EnterpriseResearchTaskResponse:
|
||||
"""Restore the before-image frozen by a completed full-report rewrite."""
|
||||
|
||||
user_id, _ = await _actor(request)
|
||||
job = await _report_job_store(request).get_job(job_id, user_id)
|
||||
if job is None or job.get("task_id") != task_id:
|
||||
raise HTTPException(status_code=404, detail="报告任务不存在")
|
||||
original = str(job.get("original_report") or "")
|
||||
if job.get("status") != "completed" or job.get("action") != "rewrite" or not original.strip():
|
||||
raise HTTPException(status_code=409, detail="该任务没有可恢复的改写前版本")
|
||||
if await _report_job_store(request).get_active_for_task(task_id, user_id) is not None:
|
||||
raise HTTPException(status_code=409, detail="报告任务正在执行,请完成或停止后再恢复版本")
|
||||
task_store = _task_store(request)
|
||||
current = await task_store.get_task(task_id, user_id)
|
||||
if current is None:
|
||||
raise HTTPException(status_code=404, detail="研究任务不存在")
|
||||
current_report = str(current.get("report_markdown") or "")
|
||||
if not current_report.strip():
|
||||
raise HTTPException(status_code=409, detail="当前没有可替换的报告")
|
||||
# The restore itself is a versioned operation: its before-image allows the
|
||||
# user to reverse a mistaken restore through the same version UI.
|
||||
await _report_job_store(request).create_job(
|
||||
{
|
||||
"id": f"erj_{uuid4().hex}", "task_id": task_id, "user_id": user_id,
|
||||
"status": "completed", "phase": "completed", "action": "rewrite",
|
||||
"instruction": f"恢复报告任务 {job_id} 的改写前版本", "style": "formal",
|
||||
"model_name": current.get("report_model_name"),
|
||||
"plan_snapshot": str(current.get("plan_markdown") or ""),
|
||||
"sources": [
|
||||
item for item in current.get("sources") or []
|
||||
if isinstance(item, dict) and item.get("chunk_id") in set(current.get("selected_source_ids") or [])
|
||||
],
|
||||
"original_report": current_report, "report_markdown": original,
|
||||
"report_summary": f"已恢复到报告任务 {job_id} 的改写前版本。",
|
||||
}
|
||||
)
|
||||
task = await task_store.update_task(
|
||||
task_id,
|
||||
user_id,
|
||||
{
|
||||
"report_markdown": original,
|
||||
"report_summary": f"已恢复到报告任务 {job_id} 的改写前版本。",
|
||||
"active_report_job_id": None,
|
||||
},
|
||||
)
|
||||
assert task is not None
|
||||
return _response(task)
|
||||
|
||||
|
||||
@router.delete("/tasks/{task_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_task(request: Request, task_id: str) -> None:
|
||||
user_id, _ = await _actor(request)
|
||||
if not await _task_store(request).delete_task(task_id, user_id):
|
||||
raise HTTPException(status_code=404, detail="研究任务不存在")
|
||||
@@ -159,20 +159,19 @@ def _clarification_blocks_ready(
|
||||
*,
|
||||
is_fresh: bool,
|
||||
) -> bool:
|
||||
"""Position intent must wait for a fresh clarification answer.
|
||||
"""A fresh clarification always pauses intent resolution.
|
||||
|
||||
The generic Step-1 route intentionally preserves READY-first behavior for
|
||||
stale tool calls in historical snapshots. Position roundtable instead has
|
||||
a four-part hand-off whose missing context is requested interactively, so
|
||||
a newly dispatched clarification is a strict terminal pause. A historical
|
||||
tool call from the preceding user-assistance turn must not block the next
|
||||
user answer from reaching READY.
|
||||
A tool call and a premature ``[INTENT_READY]`` block may coexist in the
|
||||
same upstream run. The tool call is an explicit request for user input,
|
||||
so it wins for every Step-1 workspace (including 8BF): do not consume the
|
||||
assistance card before the user can answer it. Stale calls from earlier
|
||||
turns remain harmless because only a fresh call blocks READY.
|
||||
|
||||
``intent_agent_id`` stays in the signature for route-call compatibility
|
||||
and to keep the policy extensible per agent if a future flow needs one.
|
||||
"""
|
||||
return (
|
||||
intent_agent_id == POSITION_INTENT_AGENT_ID
|
||||
and bool(clarification_calls)
|
||||
and is_fresh
|
||||
)
|
||||
del intent_agent_id
|
||||
return bool(clarification_calls) and is_fresh
|
||||
|
||||
|
||||
def _intent_thread_metadata(
|
||||
@@ -196,17 +195,22 @@ def _intent_thread_metadata(
|
||||
if not task_id:
|
||||
return metadata
|
||||
|
||||
metadata.update(
|
||||
{
|
||||
"taskId": task_id,
|
||||
"position_roundtable_task_id": task_id,
|
||||
"position_task_id": f"{task_id}:intelligence",
|
||||
"position_id": "intelligence",
|
||||
"position_roundtable_phase": "intent",
|
||||
}
|
||||
)
|
||||
if intent_agent_id == POSITION_INTENT_AGENT_ID:
|
||||
metadata["position_roundtable_intent_agent_id"] = intent_agent_id
|
||||
metadata["taskId"] = task_id
|
||||
# 8BF 使用通用意图接口但有明确的上下文 kind;它不能被岗位会商的
|
||||
# 会话索引误认为岗位任务,否则两个页面的历史记录会串在一起。
|
||||
# 其它历史调用保持原有 metadata 形状,避免影响既有会商恢复逻辑。
|
||||
is_eightbf_context = task_context.get("kind") == "eightbf_roundtable_task"
|
||||
if intent_agent_id == POSITION_INTENT_AGENT_ID or not is_eightbf_context:
|
||||
metadata.update(
|
||||
{
|
||||
"position_roundtable_task_id": task_id,
|
||||
"position_task_id": f"{task_id}:intelligence",
|
||||
"position_id": "intelligence",
|
||||
"position_roundtable_phase": "intent",
|
||||
}
|
||||
)
|
||||
if intent_agent_id == POSITION_INTENT_AGENT_ID:
|
||||
metadata["position_roundtable_intent_agent_id"] = intent_agent_id
|
||||
return metadata
|
||||
|
||||
|
||||
@@ -702,11 +706,10 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
)
|
||||
clarification_is_fresh = current_rounds > past_rounds
|
||||
|
||||
# A position-roundtable clarification is an explicit pause.
|
||||
# Some models emit a premature [INTENT_READY] text after the
|
||||
# tool call in the same upstream run. For this dedicated flow
|
||||
# the fresh clarification must win so the user can answer it;
|
||||
# generic roundtable keeps its historical READY-first order.
|
||||
# A fresh clarification is an explicit pause. Some models emit
|
||||
# a premature [INTENT_READY] block after its tool call in the
|
||||
# same upstream run; the user-assistance card must win so the
|
||||
# user can answer it before any summary is accepted.
|
||||
dispatch_msg = _find_dispatch_message(messages)
|
||||
tool_calls = dispatch_msg.get("tool_calls") or []
|
||||
clarification_calls = [
|
||||
@@ -716,10 +719,10 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
and (tc.get("name") or "").lower() == "ask_clarification"
|
||||
]
|
||||
|
||||
# --- 终态分支 1:结构化意图已就绪(最高优先级)---
|
||||
# --- 终态分支 1:结构化意图已就绪(无新追问时)---
|
||||
# 模型可能在较早的 AIMessage 里已输出 [INTENT_READY],而最后一轮
|
||||
# 仍残留 ask_clarification 的 tool_calls;必须先扫 READY,否则前端
|
||||
# 会错误展示澄清卡片且 intentReady 无法解锁 Step 2。
|
||||
# 仍残留 ask_clarification 的 tool_calls;若它是本轮新产生的追问,
|
||||
# 必须先等待用户回答,不能让 READY 覆盖可交互的协助卡。
|
||||
summary: dict[str, Any] | None = None
|
||||
ready_text: str = ""
|
||||
for msg in reversed(messages):
|
||||
|
||||
@@ -12,7 +12,7 @@ from dataclasses import replace
|
||||
from datetime import UTC, datetime
|
||||
from io import BytesIO
|
||||
from typing import Any, Literal
|
||||
from urllib.parse import quote, urlencode, urlsplit
|
||||
from urllib.parse import quote, unquote, urlencode, urlsplit
|
||||
|
||||
import httpx
|
||||
from fastapi import APIRouter, BackgroundTasks, File, HTTPException, Query, Request, UploadFile
|
||||
@@ -317,21 +317,9 @@ def _admin_session_cache_key(runtime: Any) -> str:
|
||||
def _extract_weknora_token(payload: dict[str, Any]) -> tuple[str, str, dict[str, Any] | None, dict[str, Any] | None]:
|
||||
source = payload.get("data") if isinstance(payload.get("data"), dict) else payload
|
||||
token = str(source.get("token") or source.get("access_token") or payload.get("token") or payload.get("access_token") or "")
|
||||
refresh_token = str(
|
||||
source.get("refresh_token")
|
||||
or source.get("refreshToken")
|
||||
or payload.get("refresh_token")
|
||||
or payload.get("refreshToken")
|
||||
or ""
|
||||
)
|
||||
refresh_token = str(source.get("refresh_token") or source.get("refreshToken") or payload.get("refresh_token") or payload.get("refreshToken") or "")
|
||||
user = source.get("user") if isinstance(source.get("user"), dict) else payload.get("user")
|
||||
tenant = (
|
||||
source.get("tenant")
|
||||
if isinstance(source.get("tenant"), dict)
|
||||
else source.get("active_tenant")
|
||||
if isinstance(source.get("active_tenant"), dict)
|
||||
else payload.get("tenant")
|
||||
)
|
||||
tenant = source.get("tenant") if isinstance(source.get("tenant"), dict) else source.get("active_tenant") if isinstance(source.get("active_tenant"), dict) else payload.get("tenant")
|
||||
return token, refresh_token, user if isinstance(user, dict) else None, tenant if isinstance(tenant, dict) else None
|
||||
|
||||
|
||||
@@ -402,6 +390,79 @@ async def _read_embed_context(request: Request) -> WeKnoraEmbedContext:
|
||||
ctx,
|
||||
can_write=writable is not None,
|
||||
is_conversation_deposit=_is_conversation_deposit_mapping(readable),
|
||||
allowed_weknora_ids=_embed_allowed_weknora_ids(ctx),
|
||||
)
|
||||
|
||||
|
||||
def _embed_allowed_weknora_ids(ctx: WeKnoraEmbedContext) -> tuple[str, ...]:
|
||||
"""Return the signed knowledge-base allowlist, always retaining the active base."""
|
||||
|
||||
return tuple(dict.fromkeys(value for value in (ctx.weknora_id, *ctx.allowed_weknora_ids) if value))
|
||||
|
||||
|
||||
def _embed_context_payload(ctx: WeKnoraEmbedContext) -> dict[str, Any]:
|
||||
return {
|
||||
"mapping_id": ctx.mapping_id,
|
||||
"weknora_id": ctx.weknora_id,
|
||||
"user_id": ctx.user_id,
|
||||
"is_admin": ctx.is_admin,
|
||||
"can_write": ctx.can_write,
|
||||
"is_conversation_deposit": ctx.is_conversation_deposit,
|
||||
"allowed_weknora_ids": list(_embed_allowed_weknora_ids(ctx)),
|
||||
"exp": ctx.exp,
|
||||
}
|
||||
|
||||
|
||||
def _weknora_id_from_platform_path(path: str) -> str | None:
|
||||
prefix = "/platform/knowledge-bases/"
|
||||
if not path.startswith(prefix):
|
||||
return None
|
||||
raw_id = path.removeprefix(prefix)
|
||||
if not raw_id or "/" in raw_id:
|
||||
return None
|
||||
return unquote(raw_id)
|
||||
|
||||
|
||||
async def _switch_embed_context(
|
||||
request: Request,
|
||||
ctx: WeKnoraEmbedContext,
|
||||
weknora_id: str,
|
||||
) -> WeKnoraEmbedContext:
|
||||
"""Resolve a permitted native-UI switch to the exact cmzs mapping.
|
||||
|
||||
The embedded WeKnora UI uses a service-admin token, so every target remains
|
||||
constrained to an ID signed into this short-lived session and is checked
|
||||
again against the cmzs visibility rules before its page can be proxied.
|
||||
"""
|
||||
|
||||
if weknora_id not in _embed_allowed_weknora_ids(ctx):
|
||||
raise HTTPException(status_code=403, detail="Knowledge base is outside the embedded session scope")
|
||||
store = _store(request)
|
||||
mapped = await store.get_by_weknora_id(weknora_id)
|
||||
if mapped is None:
|
||||
raise HTTPException(status_code=403, detail="Knowledge base is not managed by cmzs")
|
||||
readable = await store.get_authorized(
|
||||
str(mapped["id"]),
|
||||
ctx.user_id,
|
||||
write=False,
|
||||
is_admin=ctx.is_admin,
|
||||
)
|
||||
if readable is None or str(readable.get("weknora_id") or "") != weknora_id:
|
||||
raise HTTPException(status_code=403, detail="Knowledge base is no longer authorized")
|
||||
writable = None
|
||||
if not _is_conversation_deposit_mapping(readable):
|
||||
writable = await store.get_authorized(
|
||||
str(readable["id"]),
|
||||
ctx.user_id,
|
||||
write=True,
|
||||
is_admin=ctx.is_admin,
|
||||
)
|
||||
return replace(
|
||||
ctx,
|
||||
mapping_id=str(readable["id"]),
|
||||
weknora_id=weknora_id,
|
||||
can_write=writable is not None,
|
||||
is_conversation_deposit=_is_conversation_deposit_mapping(readable),
|
||||
)
|
||||
|
||||
|
||||
@@ -456,12 +517,7 @@ def _body_kb_id(body: Any) -> str:
|
||||
|
||||
|
||||
def _request_kb_id(request: Request, body: Any) -> str:
|
||||
return _body_kb_id(body) or str(
|
||||
request.query_params.get("kb_id")
|
||||
or request.query_params.get("knowledge_base_id")
|
||||
or request.query_params.get("source_kb_id")
|
||||
or ""
|
||||
)
|
||||
return _body_kb_id(body) or str(request.query_params.get("kb_id") or request.query_params.get("knowledge_base_id") or request.query_params.get("source_kb_id") or "")
|
||||
|
||||
|
||||
async def _admin_json(runtime: Any, path: str, *, token: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
@@ -558,12 +614,7 @@ async def _authorize_weknora_api_proxy(
|
||||
raise HTTPException(status_code=403, detail="Global WeKnora knowledge collection is not allowed")
|
||||
action = tail[1]
|
||||
if read_method and action == "batch":
|
||||
ids = [
|
||||
item.strip()
|
||||
for raw in request.query_params.getlist("ids")
|
||||
for item in raw.split(",")
|
||||
if item.strip()
|
||||
]
|
||||
ids = [item.strip() for raw in request.query_params.getlist("ids") for item in raw.split(",") if item.strip()]
|
||||
if not ids:
|
||||
raise HTTPException(status_code=403, detail="Batch operation must include document ids")
|
||||
for document_id in ids:
|
||||
@@ -599,14 +650,19 @@ async def _authorize_weknora_api_proxy(
|
||||
# A few read-only metadata endpoints are required by the original detail
|
||||
# page to render upload controls and current-tenant state. They do not
|
||||
# expose other knowledge-base contents.
|
||||
if read_method and tail and tail[0] in {
|
||||
if (
|
||||
read_method
|
||||
and tail
|
||||
and tail[0]
|
||||
in {
|
||||
"models",
|
||||
"system",
|
||||
"storage-backends",
|
||||
"vector-stores",
|
||||
"web-search-providers",
|
||||
"mcp-services",
|
||||
}:
|
||||
}
|
||||
):
|
||||
return
|
||||
|
||||
raise HTTPException(status_code=403, detail="This WeKnora endpoint is blocked in embedded detail mode")
|
||||
@@ -623,27 +679,29 @@ def _filter_knowledge_base_list_response(response: httpx.Response, ctx: WeKnoraE
|
||||
if not isinstance(payload, dict):
|
||||
return None
|
||||
|
||||
def keep_current(value: Any) -> Any:
|
||||
allowed_weknora_ids = set(_embed_allowed_weknora_ids(ctx))
|
||||
|
||||
def keep_allowed(value: Any) -> Any:
|
||||
if isinstance(value, list):
|
||||
return [item for item in value if isinstance(item, dict) and str(item.get("id") or "") == ctx.weknora_id]
|
||||
return [item for item in value if isinstance(item, dict) and str(item.get("id") or "") in allowed_weknora_ids]
|
||||
if isinstance(value, dict):
|
||||
result = dict(value)
|
||||
for key in ("items", "list", "knowledge_bases", "knowledge", "records", "data"):
|
||||
if isinstance(result.get(key), list):
|
||||
result[key] = keep_current(result[key])
|
||||
result[key] = keep_allowed(result[key])
|
||||
result["total"] = len(result[key])
|
||||
return result
|
||||
return value
|
||||
|
||||
if isinstance(payload.get("data"), list):
|
||||
payload["data"] = keep_current(payload["data"])
|
||||
payload["data"] = keep_allowed(payload["data"])
|
||||
payload["total"] = len(payload["data"])
|
||||
elif isinstance(payload.get("data"), dict):
|
||||
payload["data"] = keep_current(payload["data"])
|
||||
payload["data"] = keep_allowed(payload["data"])
|
||||
else:
|
||||
for key in ("items", "list", "knowledge_bases", "records"):
|
||||
if isinstance(payload.get(key), list):
|
||||
payload[key] = keep_current(payload[key])
|
||||
payload[key] = keep_allowed(payload[key])
|
||||
payload["total"] = len(payload[key])
|
||||
return json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
||||
|
||||
@@ -851,6 +909,7 @@ def _weknora_embed_injection(
|
||||
"user": user,
|
||||
"tenant": tenant,
|
||||
"weknoraId": ctx.weknora_id,
|
||||
"allowedWeKnoraIds": list(_embed_allowed_weknora_ids(ctx)),
|
||||
"publicPrefix": public_prefix,
|
||||
"apiPrefix": _with_public_prefix(public_prefix, _WEKNORA_EMBED_API_PREFIX),
|
||||
}
|
||||
@@ -921,8 +980,7 @@ html.deerflow-weknora-embed .document-header {{
|
||||
padding-right: 32px !important;
|
||||
background: var(--td-bg-color-page) !important;
|
||||
}}
|
||||
html.deerflow-weknora-embed .breadcrumb-link:first-child,
|
||||
html.deerflow-weknora-embed .breadcrumb-link.dropdown {{
|
||||
html.deerflow-weknora-embed .breadcrumb-link:first-child {{
|
||||
pointer-events: none !important;
|
||||
cursor: default !important;
|
||||
}}
|
||||
@@ -1248,11 +1306,24 @@ html.deerflow-source-detail-open [data-deerflow-source-detail-panel="true"] [cla
|
||||
}}
|
||||
|
||||
const allowedPath = cfg.allowedPath;
|
||||
const allowedWeKnoraIds = new Set(
|
||||
[cfg.weknoraId, ...(Array.isArray(cfg.allowedWeKnoraIds) ? cfg.allowedWeKnoraIds : [])]
|
||||
.filter(Boolean)
|
||||
);
|
||||
const knowledgeBasePathPrefix = (cfg.publicPrefix || "") + "/platform/knowledge-bases/";
|
||||
function isAllowedKnowledgeBasePath(pathname) {{
|
||||
if (!pathname.startsWith(knowledgeBasePathPrefix)) return false;
|
||||
const encodedId = pathname.slice(knowledgeBasePathPrefix.length);
|
||||
if (!encodedId || encodedId.includes("/")) return false;
|
||||
try {{ return allowedWeKnoraIds.has(decodeURIComponent(encodedId)); }} catch (_) {{ return false; }}
|
||||
}}
|
||||
function allowedUrl(url) {{
|
||||
try {{
|
||||
const next = new URL(url, location.origin);
|
||||
if (next.origin !== location.origin) return null;
|
||||
if (next.pathname === allowedPath) return next.pathname + next.search + next.hash;
|
||||
if (next.pathname === allowedPath || isAllowedKnowledgeBasePath(next.pathname)) {{
|
||||
return next.pathname + next.search + next.hash;
|
||||
}}
|
||||
const query = new URLSearchParams(location.search);
|
||||
if (cfg.tab && !query.has("tab")) query.set("tab", cfg.tab);
|
||||
return allowedPath + (query.toString() ? "?" + query.toString() : "") + location.hash;
|
||||
@@ -1261,10 +1332,21 @@ html.deerflow-source-detail-open [data-deerflow-source-detail-panel="true"] [cla
|
||||
}}
|
||||
}}
|
||||
function enforceLocation() {{
|
||||
if (location.pathname !== allowedPath) {{
|
||||
if (location.pathname !== allowedPath && !isAllowedKnowledgeBasePath(location.pathname)) {{
|
||||
history.replaceState(history.state, "", allowedPath + location.search + location.hash);
|
||||
}}
|
||||
}}
|
||||
function reloadForKnowledgeBaseSwitch(target, replace) {{
|
||||
try {{
|
||||
const next = new URL(target, location.origin);
|
||||
if (next.pathname === location.pathname || !isAllowedKnowledgeBasePath(next.pathname)) return false;
|
||||
if (replace) location.replace(next.href);
|
||||
else location.assign(next.href);
|
||||
return true;
|
||||
}} catch (_) {{
|
||||
return false;
|
||||
}}
|
||||
}}
|
||||
function purgeGuides() {{
|
||||
document
|
||||
.querySelectorAll(
|
||||
@@ -1364,8 +1446,16 @@ html.deerflow-source-detail-open [data-deerflow-source-detail-panel="true"] [cla
|
||||
}}
|
||||
const rawPushState = history.pushState.bind(history);
|
||||
const rawReplaceState = history.replaceState.bind(history);
|
||||
history.pushState = (state, title, url) => rawPushState(state, title, url ? allowedUrl(url) : location.href);
|
||||
history.replaceState = (state, title, url) => rawReplaceState(state, title, url ? allowedUrl(url) : location.href);
|
||||
history.pushState = (state, title, url) => {{
|
||||
const target = url ? allowedUrl(url) : location.href;
|
||||
if (url && target && reloadForKnowledgeBaseSwitch(target, false)) return;
|
||||
return rawPushState(state, title, target);
|
||||
}};
|
||||
history.replaceState = (state, title, url) => {{
|
||||
const target = url ? allowedUrl(url) : location.href;
|
||||
if (url && target && reloadForKnowledgeBaseSwitch(target, true)) return;
|
||||
return rawReplaceState(state, title, target);
|
||||
}};
|
||||
window.addEventListener("popstate", enforceLocation);
|
||||
document.addEventListener("click", (event) => {{
|
||||
const anchor = event.target && event.target.closest ? event.target.closest("a[href]") : null;
|
||||
@@ -1374,6 +1464,7 @@ html.deerflow-source-detail-open [data-deerflow-source-detail-panel="true"] [cla
|
||||
if (target && new URL(anchor.href, location.origin).pathname !== allowedPath) {{
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (reloadForKnowledgeBaseSwitch(target, false)) return;
|
||||
history.replaceState(history.state, "", target);
|
||||
}}
|
||||
}}, true);
|
||||
@@ -1390,7 +1481,7 @@ html.deerflow-source-detail-open [data-deerflow-source-detail-panel="true"] [cla
|
||||
hideWeKnoraShell();
|
||||
tickSourceDetail();
|
||||
if (ready) return;
|
||||
if (!force && (location.pathname !== allowedPath || !hasDetailShell())) return;
|
||||
if (!force && (!isAllowedKnowledgeBasePath(location.pathname) && location.pathname !== allowedPath || !hasDetailShell())) return;
|
||||
ready = true;
|
||||
root.classList.add("deerflow-weknora-ready");
|
||||
}}
|
||||
@@ -1578,10 +1669,7 @@ async def _proxy_weknora_request(
|
||||
|
||||
|
||||
def _is_conversation_deposit_mapping(row: dict[str, Any]) -> bool:
|
||||
return (
|
||||
str(row.get("owner_user_id") or "") == CONVERSATION_DEPOSIT_OWNER_USER_ID
|
||||
and str(row.get("name") or "").strip() == CONVERSATION_DEPOSIT_KB_NAME
|
||||
)
|
||||
return str(row.get("owner_user_id") or "") == CONVERSATION_DEPOSIT_OWNER_USER_ID and str(row.get("name") or "").strip() == CONVERSATION_DEPOSIT_KB_NAME
|
||||
|
||||
|
||||
_LEGACY_BRAND_PATTERN = re.compile(r"deer[\s_-]*flow", re.IGNORECASE)
|
||||
@@ -1637,13 +1725,7 @@ def _mapping_view(row: dict[str, Any], remote: dict[str, Any] | None, *, actor_u
|
||||
source = remote or {}
|
||||
is_conversation_deposit = _is_conversation_deposit_mapping(row)
|
||||
is_owner = row.get("owner_user_id") == actor_user_id
|
||||
description = (
|
||||
CONVERSATION_DEPOSIT_KB_DESCRIPTION
|
||||
if is_conversation_deposit
|
||||
else source.get("description")
|
||||
if source.get("description") is not None
|
||||
else row.get("description") or ""
|
||||
)
|
||||
description = CONVERSATION_DEPOSIT_KB_DESCRIPTION if is_conversation_deposit else source.get("description") if source.get("description") is not None else row.get("description") or ""
|
||||
return {
|
||||
"id": row["id"],
|
||||
"remote_status": "available" if remote is not None else "missing",
|
||||
@@ -2028,19 +2110,28 @@ async def _prepare_weknora_embed_session(
|
||||
) -> tuple[str, dict[str, Any]]:
|
||||
_runtime_for_iframe_proxy(request)
|
||||
row, user_id, is_admin = await _authorized_mapping(request, mapping_id, write=False)
|
||||
visible_rows = await _store(request).list_visible(
|
||||
user_id,
|
||||
scope="all",
|
||||
# The iframe must mirror the knowledge-base picker, not grant an
|
||||
# administrator an unlisted user's private knowledge bases.
|
||||
is_admin=False,
|
||||
)
|
||||
writable = None
|
||||
if not _is_conversation_deposit_mapping(row):
|
||||
writable = await _store(request).get_authorized(mapping_id, user_id, write=True, is_admin=is_admin)
|
||||
weknora_id = str(row["weknora_id"])
|
||||
return weknora_id, {
|
||||
"mapping_id": mapping_id,
|
||||
"weknora_id": weknora_id,
|
||||
"user_id": user_id,
|
||||
"is_admin": is_admin,
|
||||
"can_write": writable is not None,
|
||||
"is_conversation_deposit": _is_conversation_deposit_mapping(row),
|
||||
"exp": int(time.time()) + _WEKNORA_EMBED_TTL_SECONDS,
|
||||
}
|
||||
ctx = WeKnoraEmbedContext(
|
||||
mapping_id=mapping_id,
|
||||
weknora_id=weknora_id,
|
||||
user_id=user_id,
|
||||
is_admin=is_admin,
|
||||
can_write=writable is not None,
|
||||
is_conversation_deposit=_is_conversation_deposit_mapping(row),
|
||||
allowed_weknora_ids=tuple(dict.fromkeys(value for value in (weknora_id, *(str(item.get("weknora_id") or "") for item in visible_rows)) if value)),
|
||||
exp=int(time.time()) + _WEKNORA_EMBED_TTL_SECONDS,
|
||||
)
|
||||
return weknora_id, _embed_context_payload(ctx)
|
||||
|
||||
|
||||
@router.post("/api/llmwiki/knowledge-bases/{mapping_id}/weknora/session")
|
||||
@@ -2089,8 +2180,21 @@ async def open_weknora_detail_frame(
|
||||
async def proxy_weknora_platform_page(request: Request, proxied_path: str = "") -> Response:
|
||||
ctx = await _read_embed_context(request)
|
||||
requested = f"/platform/{proxied_path}".rstrip("/")
|
||||
allowed_path = f"/platform/knowledge-bases/{ctx.weknora_id}"
|
||||
allowed_path = f"/platform/knowledge-bases/{quote(ctx.weknora_id, safe='')}"
|
||||
if requested != allowed_path:
|
||||
requested_weknora_id = _weknora_id_from_platform_path(requested)
|
||||
if requested_weknora_id is not None:
|
||||
selected_ctx = await _switch_embed_context(request, ctx, requested_weknora_id)
|
||||
response = await _proxy_weknora_request(
|
||||
request,
|
||||
target_base_url=_runtime_for_iframe_proxy(request).web_base_url,
|
||||
upstream_path=f"/platform/knowledge-bases/{quote(requested_weknora_id, safe='')}",
|
||||
ctx=selected_ctx,
|
||||
is_api=False,
|
||||
tab=request.query_params.get("tab") or "wiki",
|
||||
)
|
||||
_set_weknora_embed_cookie(request, response, _embed_context_payload(selected_ctx))
|
||||
return response
|
||||
static_upstream_path = _weknora_static_fallback_upstream_path(request)
|
||||
if static_upstream_path is not None:
|
||||
runtime = _runtime_for_iframe_proxy(request)
|
||||
@@ -2308,11 +2412,7 @@ async def list_knowledge_bases(
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
deposit_remote = next(
|
||||
(
|
||||
item
|
||||
for item in remote_items
|
||||
if str(item.get("name") or "").strip() == CONVERSATION_DEPOSIT_KB_NAME
|
||||
),
|
||||
(item for item in remote_items if str(item.get("name") or "").strip() == CONVERSATION_DEPOSIT_KB_NAME),
|
||||
None,
|
||||
)
|
||||
if deposit_remote is not None:
|
||||
@@ -2322,11 +2422,7 @@ async def list_knowledge_bases(
|
||||
client,
|
||||
remote_knowledge_bases=remote_items,
|
||||
)
|
||||
description_was_synchronized = (
|
||||
previous_mapping is None
|
||||
or not _is_conversation_deposit_mapping(previous_mapping)
|
||||
or str(previous_mapping.get("description") or "") != CONVERSATION_DEPOSIT_KB_DESCRIPTION
|
||||
)
|
||||
description_was_synchronized = previous_mapping is None or not _is_conversation_deposit_mapping(previous_mapping) or str(previous_mapping.get("description") or "") != CONVERSATION_DEPOSIT_KB_DESCRIPTION
|
||||
if deposit_mapping is not None and description_was_synchronized:
|
||||
remote_id = str(deposit_mapping["weknora_id"])
|
||||
remote[remote_id] = {
|
||||
@@ -2566,13 +2662,7 @@ async def list_wiki_pages(
|
||||
query=query.strip(),
|
||||
)
|
||||
raw_pages = result.get("pages") if isinstance(result, dict) else []
|
||||
pages = [
|
||||
view
|
||||
for page_item in (raw_pages if isinstance(raw_pages, list) else [])
|
||||
if isinstance(page_item, dict)
|
||||
for view in [_wiki_page_view(page_item)]
|
||||
if view is not None
|
||||
]
|
||||
pages = [view for page_item in (raw_pages if isinstance(raw_pages, list) else []) if isinstance(page_item, dict) for view in [_wiki_page_view(page_item)] if view is not None]
|
||||
return {**result, "pages": pages}
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
@@ -2839,28 +2929,13 @@ async def get_source_context(
|
||||
if sanitize_deposit and wiki_page is not None:
|
||||
wiki_page = _sanitize_conversation_deposit_value(wiki_page)
|
||||
focus_chunk = next((chunk for chunk in chunks if str(chunk.get("id") or "") == chunk_id), {})
|
||||
document_id = str(
|
||||
focus_chunk.get("knowledge_id")
|
||||
or focus_chunk.get("document_id")
|
||||
or focus_chunk.get("knowledgeId")
|
||||
or ""
|
||||
)
|
||||
document_title = str(
|
||||
focus_chunk.get("knowledge_title")
|
||||
or focus_chunk.get("title")
|
||||
or focus_chunk.get("file_name")
|
||||
or focus_chunk.get("filename")
|
||||
or ""
|
||||
)
|
||||
document_id = str(focus_chunk.get("knowledge_id") or focus_chunk.get("document_id") or focus_chunk.get("knowledgeId") or "")
|
||||
document_title = str(focus_chunk.get("knowledge_title") or focus_chunk.get("title") or focus_chunk.get("file_name") or focus_chunk.get("filename") or "")
|
||||
normalized_chunks = [
|
||||
{
|
||||
"id": str(chunk.get("id") or ""),
|
||||
"chunk_index": int(chunk.get("chunk_index") or 0),
|
||||
"content": str(
|
||||
_sanitize_conversation_deposit_value(chunk.get("content") or "")
|
||||
if sanitize_deposit
|
||||
else chunk.get("content") or ""
|
||||
),
|
||||
"content": str(_sanitize_conversation_deposit_value(chunk.get("content") or "") if sanitize_deposit else chunk.get("content") or ""),
|
||||
"is_focus": str(chunk.get("id") or "") == chunk_id,
|
||||
}
|
||||
for chunk in chunks
|
||||
|
||||
@@ -148,6 +148,12 @@ class ModelTestRequest(BaseModel):
|
||||
model: ModelUpsertRequest
|
||||
|
||||
|
||||
class ModelOrderRequest(BaseModel):
|
||||
"""The complete ordered set of model stable names."""
|
||||
|
||||
names: list[str] = Field(min_length=1, max_length=500)
|
||||
|
||||
|
||||
class ModelTestResponse(BaseModel):
|
||||
success: bool
|
||||
latency_ms: int
|
||||
@@ -378,6 +384,36 @@ async def create_model_configuration(body: ModelUpsertRequest, request: Request)
|
||||
return _to_admin_item(created)
|
||||
|
||||
|
||||
@router.put("/order", response_model=list[ModelAdminItem])
|
||||
async def reorder_model_configurations(body: ModelOrderRequest, request: Request) -> list[ModelAdminItem]:
|
||||
"""Persist the configured model order.
|
||||
|
||||
The first item is intentionally the deployment default: public model
|
||||
selectors preserve the ``config.yaml`` order and use their first option
|
||||
when no model was explicitly chosen. Requiring a complete permutation
|
||||
prevents a stale browser from accidentally dropping a newly added model.
|
||||
"""
|
||||
|
||||
await _require_admin(request)
|
||||
if len(body.names) != len(set(body.names)):
|
||||
raise HTTPException(status_code=422, detail="Each model may appear only once in the order")
|
||||
|
||||
async with _MUTATION_LOCK:
|
||||
path = _resolve_config_path()
|
||||
raw_before, _, models = await asyncio.to_thread(_read_config_data, path)
|
||||
configured_names = [str(item.get("name") or "") for item in models]
|
||||
if len(body.names) != len(configured_names) or set(body.names) != set(configured_names):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="The model list changed. Refresh the page and try again.",
|
||||
)
|
||||
models_by_name = {str(item.get("name")): item for item in models}
|
||||
ordered_models = [models_by_name[name] for name in body.names]
|
||||
await _save_models(request, raw_before, ordered_models)
|
||||
|
||||
return [_to_admin_item(item) for item in ordered_models]
|
||||
|
||||
|
||||
@router.put("/{model_name:path}", response_model=ModelAdminItem)
|
||||
async def update_model_configuration(model_name: str, body: ModelUpsertRequest, request: Request) -> ModelAdminItem:
|
||||
await _require_admin(request)
|
||||
|
||||
@@ -659,7 +659,8 @@ def _build_node_context_text(
|
||||
lines.extend(
|
||||
[
|
||||
"- 本席位最终只需交付一份 Markdown 报告文档。",
|
||||
"- 请在本轮或最终回复中创建 `/mnt/user-data/outputs/` 下的 `.md` 文件,并调用 `present_files` 呈现该文件。",
|
||||
"- 请在本轮用 `write_file` **只写一次** `/mnt/user-data/outputs/` 下的单个 `.md` 文件,然后调用 `present_files` **只呈现一次**该文件并简短收尾。",
|
||||
"- **禁止**在同一轮再调用 `write_file` 生成第二份(或内容相同的)产物,也**禁止**对同一文件再次 `present_files`。",
|
||||
"- 只有检测到该席位产出的 `.md` 文件后,系统才会把当前席位标记为已完成;仅有文字回答不会完成节点。",
|
||||
"- 报告内容应直接围绕当前岗位职责,吸收任务信息、任务意图与上游席位产物。",
|
||||
]
|
||||
@@ -1108,7 +1109,8 @@ async def _build_builtin_prompt(
|
||||
if kind == "summary":
|
||||
instructions = [
|
||||
"请基于以下岗位会商材料,生成或按用户要求修订《岗位会商方案总结报告》。",
|
||||
"必须使用 write_file 一次性写入 `/mnt/user-data/outputs/岗位会商方案总结报告.md`,随后在对话中输出完整报告正文。",
|
||||
"必须使用 write_file **只写一次** `/mnt/user-data/outputs/岗位会商方案总结报告.md`,随后在对话中输出完整报告正文。",
|
||||
"**禁止**再次 write_file 生成第二份(或相同)报告;写完后 present_files **只调用一次**并简短收尾即可。",
|
||||
"报告要覆盖任务目标、岗位关键结论、综合分析、方案建议、风险与执行路径;不要简单拼接原文。",
|
||||
]
|
||||
else:
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
"""CRUD API for deep-research report structures (报告结构 / 工作流配置).
|
||||
|
||||
The deep-research workbench's 高级配置 → 工作流配置 dialog renders a card per
|
||||
row; picking a card applies ``content`` to the research config's
|
||||
``custom_outline``. Global / shared — every account sees the same cards.
|
||||
|
||||
Routes (prefix ``/api/report-structures``):
|
||||
GET "" list (optional ``enabled`` / ``title`` filters)
|
||||
POST "" create (title / name / content / structure_mode /
|
||||
retrieval_directions / retrieval_skills;
|
||||
enabled forced True)
|
||||
GET "/{id}" get one
|
||||
PUT "/{id}" update title / name / content / structure_mode /
|
||||
retrieval_directions / retrieval_skills
|
||||
PUT "/{id}/enabled" toggle 启用/停用
|
||||
DELETE "/{id}" delete
|
||||
|
||||
The first list call seeds the two built-in outlines (通用分析结构 /
|
||||
递进式论证结构) when the table has never held a row — see
|
||||
``deerflow.persistence.report_structures.defaults``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.deps import get_optional_user_from_request
|
||||
from deerflow.persistence.report_structures import ReportStructureStore
|
||||
from deerflow.persistence.report_structures.directions import normalize_retrieval_directions
|
||||
from deerflow.persistence.report_structures.skills import normalize_retrieval_skills
|
||||
from deerflow.persistence.report_structures.modes import (
|
||||
DEFAULT_STRUCTURE_MODE,
|
||||
StructureMode,
|
||||
normalize_structure_mode,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/report-structures", tags=["report-structures"])
|
||||
|
||||
|
||||
# ── schemas ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class ReportStructureResponse(BaseModel):
|
||||
id: str
|
||||
title: str
|
||||
name: str
|
||||
content: str
|
||||
structure_mode: StructureMode = DEFAULT_STRUCTURE_MODE
|
||||
retrieval_directions: list[str] = []
|
||||
retrieval_skills: list[str] = []
|
||||
enabled: bool = True
|
||||
created_by: str | None = None
|
||||
created_at: str | None = None
|
||||
updated_at: str | None = None
|
||||
|
||||
|
||||
class ReportStructureListResponse(BaseModel):
|
||||
structures: list[ReportStructureResponse]
|
||||
|
||||
|
||||
class ReportStructureCreateRequest(BaseModel):
|
||||
title: str = Field(..., min_length=1, max_length=256)
|
||||
name: str = Field(default="", max_length=256)
|
||||
content: str = Field(..., min_length=1, max_length=20_000)
|
||||
structure_mode: StructureMode = DEFAULT_STRUCTURE_MODE
|
||||
retrieval_directions: list[str] = Field(default_factory=list)
|
||||
retrieval_skills: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class ReportStructureUpdateRequest(BaseModel):
|
||||
title: str | None = Field(default=None, min_length=1, max_length=256)
|
||||
name: str | None = Field(default=None, max_length=256)
|
||||
content: str | None = Field(default=None, min_length=1, max_length=20_000)
|
||||
structure_mode: StructureMode | None = None
|
||||
retrieval_directions: list[str] | None = None
|
||||
retrieval_skills: list[str] | None = None
|
||||
|
||||
|
||||
class ReportStructureEnabledRequest(BaseModel):
|
||||
enabled: bool
|
||||
|
||||
|
||||
# ── helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _to_response(row: dict) -> ReportStructureResponse:
|
||||
data = dict(row)
|
||||
data["structure_mode"] = normalize_structure_mode(data.get("structure_mode"))
|
||||
data["retrieval_directions"] = normalize_retrieval_directions(data.get("retrieval_directions"))
|
||||
data["retrieval_skills"] = normalize_retrieval_skills(data.get("retrieval_skills"))
|
||||
return ReportStructureResponse(**data)
|
||||
|
||||
|
||||
def _get_store(request: Request) -> ReportStructureStore:
|
||||
store = getattr(request.app.state, "report_structure_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="Report structure store not available")
|
||||
return store
|
||||
|
||||
|
||||
async def _current_username(request: Request) -> str | None:
|
||||
"""创建用户名:邮箱(本部署的账号即邮箱),无认证时为 None。"""
|
||||
user = await get_optional_user_from_request(request)
|
||||
if user is None:
|
||||
return None
|
||||
return str(user.email) or str(user.id)
|
||||
|
||||
|
||||
# ── routes ───────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@router.get("", response_model=ReportStructureListResponse)
|
||||
async def list_report_structures(
|
||||
request: Request,
|
||||
enabled: bool | None = Query(default=None, description="启用/停用筛选;缺省=全部"),
|
||||
title: str | None = Query(default=None, max_length=256, description="标题模糊筛选(大小写不敏感)"),
|
||||
) -> ReportStructureListResponse:
|
||||
store = _get_store(request)
|
||||
await store.ensure_defaults()
|
||||
rows = await store.list_structures()
|
||||
if enabled is not None:
|
||||
rows = [r for r in rows if bool(r.get("enabled", True)) is enabled]
|
||||
if title:
|
||||
needle = title.strip().lower()
|
||||
if needle:
|
||||
rows = [r for r in rows if needle in str(r.get("title", "")).lower()]
|
||||
return ReportStructureListResponse(structures=[_to_response(r) for r in rows])
|
||||
|
||||
|
||||
@router.post("", response_model=ReportStructureResponse, status_code=201)
|
||||
async def create_report_structure(request: Request, body: ReportStructureCreateRequest) -> ReportStructureResponse:
|
||||
store = _get_store(request)
|
||||
row = await store.create_structure(
|
||||
{
|
||||
"id": str(uuid4()),
|
||||
"title": body.title.strip(),
|
||||
"name": body.name.strip(),
|
||||
"content": body.content.strip(),
|
||||
"structure_mode": normalize_structure_mode(body.structure_mode),
|
||||
"retrieval_directions": normalize_retrieval_directions(body.retrieval_directions),
|
||||
"retrieval_skills": normalize_retrieval_skills(body.retrieval_skills),
|
||||
"created_by": await _current_username(request),
|
||||
}
|
||||
)
|
||||
return _to_response(row)
|
||||
|
||||
|
||||
@router.get("/{structure_id}", response_model=ReportStructureResponse)
|
||||
async def get_report_structure(request: Request, structure_id: str) -> ReportStructureResponse:
|
||||
store = _get_store(request)
|
||||
row = await store.get_structure(structure_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="报告结构不存在")
|
||||
return _to_response(row)
|
||||
|
||||
|
||||
@router.put("/{structure_id}", response_model=ReportStructureResponse)
|
||||
async def update_report_structure(
|
||||
request: Request, structure_id: str, body: ReportStructureUpdateRequest
|
||||
) -> ReportStructureResponse:
|
||||
store = _get_store(request)
|
||||
data: dict = {}
|
||||
if body.title is not None:
|
||||
data["title"] = body.title.strip()
|
||||
if body.name is not None:
|
||||
data["name"] = body.name.strip()
|
||||
if body.content is not None:
|
||||
data["content"] = body.content.strip()
|
||||
if body.structure_mode is not None:
|
||||
data["structure_mode"] = normalize_structure_mode(body.structure_mode)
|
||||
if body.retrieval_directions is not None:
|
||||
data["retrieval_directions"] = normalize_retrieval_directions(body.retrieval_directions)
|
||||
if body.retrieval_skills is not None:
|
||||
data["retrieval_skills"] = normalize_retrieval_skills(body.retrieval_skills)
|
||||
if not data:
|
||||
raise HTTPException(status_code=400, detail="没有需要更新的字段")
|
||||
row = await store.update_structure(structure_id, data)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="报告结构不存在")
|
||||
return _to_response(row)
|
||||
|
||||
|
||||
@router.put("/{structure_id}/enabled", response_model=ReportStructureResponse)
|
||||
async def set_report_structure_enabled(
|
||||
request: Request, structure_id: str, body: ReportStructureEnabledRequest
|
||||
) -> ReportStructureResponse:
|
||||
store = _get_store(request)
|
||||
row = await store.set_enabled(structure_id, body.enabled)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="报告结构不存在")
|
||||
return _to_response(row)
|
||||
|
||||
|
||||
@router.delete("/{structure_id}", status_code=204)
|
||||
async def delete_report_structure(request: Request, structure_id: str) -> None:
|
||||
store = _get_store(request)
|
||||
deleted = await store.delete_structure(structure_id)
|
||||
if not deleted:
|
||||
raise HTTPException(status_code=404, detail="报告结构不存在")
|
||||
+22
@@ -52,6 +52,7 @@ class ArtifactSubmissionRequest(BaseModel):
|
||||
|
||||
task_id: StrictStr | None = Field(default=None, max_length=128, validation_alias=AliasChoices("taskId", "task_id"))
|
||||
submitted: bool
|
||||
register_only: bool = Field(default=False, validation_alias=AliasChoices("registerOnly", "register_only"))
|
||||
submission_id: StrictStr | None = Field(default=None, max_length=64, validation_alias=AliasChoices("submissionId", "submission_id"))
|
||||
task_delivery_id: StrictStr | None = Field(default=None, max_length=255, validation_alias=AliasChoices("taskDeliveryId", "task_delivery_id"))
|
||||
expected_version: int | None = Field(default=None, ge=0, validation_alias=AliasChoices("expectedVersion", "expected_version"))
|
||||
@@ -200,6 +201,27 @@ async def set_artifact_submission(
|
||||
"submission": _response(conflict.row).model_dump(by_alias=True, exclude_none=False),
|
||||
},
|
||||
) from conflict
|
||||
elif body.register_only:
|
||||
if not artifact["thread_id"]:
|
||||
raise HTTPException(status_code=422, detail="artifact.threadId is required when registering an artifact")
|
||||
try:
|
||||
row = await store.register(
|
||||
path_task_id,
|
||||
artifact,
|
||||
task_delivery_id=body.task_delivery_id,
|
||||
actor_id=actor_id,
|
||||
expected_version=body.expected_version,
|
||||
)
|
||||
except SubmissionVersionConflict as conflict:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"code": "version_conflict",
|
||||
"message": "登记产物版本冲突:另一写入已更新,请刷新后重试。",
|
||||
"current_version": conflict.row.get("version", 0),
|
||||
"submission": _response(conflict.row).model_dump(by_alias=True, exclude_none=False),
|
||||
},
|
||||
) from conflict
|
||||
else:
|
||||
try:
|
||||
row = await store.cancel(
|
||||
|
||||
@@ -13,8 +13,9 @@ Routes (prefix ``/api/roundtable-chains``):
|
||||
DELETE "/{chain_id}" delete
|
||||
|
||||
管理员权限:``system_role == "admin"`` 的用户(``_is_admin``)可 list ``scope=all``
|
||||
(所有账号的全部链条),并可 get / 编辑 / 发布 / 删除**任意** owner 的链条;非管理员
|
||||
(所有账号的全部链条),并可 get / 编辑 / 发布 / 启停 / 删除**任意** owner 的链条;非管理员
|
||||
仍仅限本人链条(``scope=all`` 退化为 ``mine``)。编辑/删除不转移链条归属(``user_id`` 不变)。
|
||||
``enabled`` 默认 True:停用后管理列表仍可见,但选链弹窗 / 公共入口不再选用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -70,6 +71,9 @@ class ChainSeatSchema(BaseModel):
|
||||
# 岗位协同专用的席位岗位归属。字段保存在既有 seats JSON 中,普通圆桌编排
|
||||
# 忽略它;显式声明可确保 Pydantic 在 create/update 的 model_dump() 时不剥掉。
|
||||
position_id: str | None = Field(default=None, max_length=64)
|
||||
# 该席位在业务链中的岗位职责。会商智能体配置面板只读展示此字段;
|
||||
# 保存在既有 seats JSON 中,无需迁移。空/None = 未填写。
|
||||
job_responsibilities: str | None = Field(default=None, max_length=4000)
|
||||
|
||||
|
||||
class ChainResponse(BaseModel):
|
||||
@@ -90,6 +94,8 @@ class ChainResponse(BaseModel):
|
||||
gather_first: bool = False
|
||||
# 发布状态:True = 公共(所有账号可在「公共」筛选查看 / 选用)。
|
||||
is_public: bool = False
|
||||
# 启停状态:True = 启用(可选入研讨);False = 停用。缺省 True。
|
||||
enabled: bool = True
|
||||
# 是否当前用户所有(仅本人可编辑 / 删除 / 发布;公共筛选里别人的链条 is_mine=False)。
|
||||
is_mine: bool = True
|
||||
# 配置者(owner)的 user_id 及解析后的可读用户名(email)。owner_name 由路由层
|
||||
@@ -116,6 +122,7 @@ class ChainCreateRequest(BaseModel):
|
||||
coordinator_prompt: str | None = Field(default=None, max_length=4000)
|
||||
seat_mode: str | None = Field(default=None, max_length=16)
|
||||
gather_first: bool = False
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
class ChainUpdateRequest(BaseModel):
|
||||
@@ -131,6 +138,8 @@ class ChainUpdateRequest(BaseModel):
|
||||
gather_first: bool | None = None
|
||||
# 发布开关(True 发布为公共 / False 取消发布);仅 owner 可改(update 本就 owner 限定)。
|
||||
is_public: bool | None = None
|
||||
# 启停开关(True 启用 / False 停用);仅 owner / 管理员可改。omit 不改。
|
||||
enabled: bool | None = None
|
||||
|
||||
|
||||
# ── helpers ──────────────────────────────────────────────────────────────
|
||||
@@ -329,6 +338,7 @@ async def create_chain(request: Request, body: ChainCreateRequest) -> ChainRespo
|
||||
"coordinator_prompt": body.coordinator_prompt,
|
||||
"seat_mode": body.seat_mode,
|
||||
"gather_first": body.gather_first,
|
||||
"enabled": body.enabled,
|
||||
},
|
||||
)
|
||||
await _enrich_owner_names([row])
|
||||
|
||||
@@ -591,6 +591,18 @@ def _normalize_compact_shortcut_buttons(
|
||||
|
||||
route_params: list[AppearanceRouteParam] = []
|
||||
for param in item.route_params or []:
|
||||
if param.paramType == "path":
|
||||
source = (param.source or "access_token").strip() or "access_token"
|
||||
route_params.append(
|
||||
AppearanceRouteParam(
|
||||
key="",
|
||||
paramType="path",
|
||||
source=source,
|
||||
customValue=(param.customValue or "").strip() if source == "other" else None,
|
||||
)
|
||||
)
|
||||
continue
|
||||
|
||||
key = (param.key or "").strip()
|
||||
if not key:
|
||||
continue
|
||||
|
||||
@@ -51,6 +51,7 @@ class TaskButton(BaseModel):
|
||||
id: str = Field(..., min_length=1, max_length=128)
|
||||
business: str = Field(..., min_length=1, max_length=32)
|
||||
label: str = Field(default="", max_length=255)
|
||||
# url=外链 / business=切换业务 / purpose=目的树弹框 / host=宿主路由(iframe postMessage)。
|
||||
link_type: str = Field(default="url", max_length=16)
|
||||
target: str = Field(default="", max_length=1024)
|
||||
append_task_id: bool = True
|
||||
|
||||
@@ -20,7 +20,7 @@ import uuid
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from langgraph.checkpoint.base import empty_checkpoint, uuid6
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from sqlalchemy import select
|
||||
@@ -193,6 +193,18 @@ class ThreadStateResponse(BaseModel):
|
||||
tasks: list[dict[str, Any]] = Field(default_factory=list, description="Interrupted task details")
|
||||
|
||||
|
||||
class ThreadMessagesResponse(BaseModel):
|
||||
"""A page of the latest thread message state.
|
||||
|
||||
``messages`` deliberately has no narrower response model: LangChain message
|
||||
fields such as ``additional_kwargs`` and provider-specific tool-call payloads
|
||||
must reach integration clients unchanged.
|
||||
"""
|
||||
|
||||
total: int = Field(description="Total number of messages in the thread")
|
||||
messages: list[Any] = Field(default_factory=list, description="Messages in chronological order")
|
||||
|
||||
|
||||
class ThreadPatchRequest(BaseModel):
|
||||
"""Request body for patching thread metadata."""
|
||||
|
||||
@@ -321,6 +333,20 @@ def _message_id(message: Any) -> str | None:
|
||||
return str(value) if value else None
|
||||
|
||||
|
||||
def _serialize_thread_messages(messages: Any) -> list[Any]:
|
||||
"""Return checkpoint messages in the same wire form as stream/state APIs.
|
||||
|
||||
The latest checkpoint is the canonical conversation transcript. Reusing the
|
||||
runtime serializer keeps every standard and provider-specific message field
|
||||
(including ``additional_kwargs``, ``tool_calls`` and hidden context flags)
|
||||
while converting LangChain message objects into JSON-safe values.
|
||||
"""
|
||||
if not isinstance(messages, list):
|
||||
return []
|
||||
serialized = serialize_channel_values({"messages": messages}).get("messages", [])
|
||||
return serialized if isinstance(serialized, list) else []
|
||||
|
||||
|
||||
def _message_content(message: Any) -> Any:
|
||||
if isinstance(message, dict):
|
||||
return message.get("content")
|
||||
@@ -587,6 +613,18 @@ async def delete_thread_data(thread_id: str, request: Request) -> ThreadDeleteRe
|
||||
except Exception:
|
||||
logger.debug("Could not delete thread_meta for %s (not critical)", sanitize_log_param(thread_id))
|
||||
|
||||
# Conversation deposits are an auxiliary artifact index. Remove their
|
||||
# records/documents as well so a whole-thread delete cannot leave a stale
|
||||
# searchable copy behind. The helper is intentionally best-effort (the
|
||||
# primary thread state is already deleted above).
|
||||
asyncio.create_task(
|
||||
delete_conversation_deposit_documents(
|
||||
request.app,
|
||||
thread_id=thread_id,
|
||||
message_ids=None,
|
||||
)
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@@ -665,11 +703,12 @@ async def search_threads(body: ThreadSearchRequest, request: Request) -> list[Th
|
||||
from app.gateway.deps import get_thread_store
|
||||
|
||||
repo = get_thread_store(request)
|
||||
# System threads (scheduler runs, roundtable workers, …) carry
|
||||
# ``metadata.system=true`` and are machine-created — hide them from the
|
||||
# default conversation list so they don't crowd out real chats. A caller
|
||||
# that explicitly filters on ``thread_type``/``system`` (e.g. the studio
|
||||
# notebook list) opts out of the exclusion.
|
||||
# System threads (scheduler runs, roundtable workers, report-structure /
|
||||
# writing setup wizards, …) carry ``metadata.system=true`` and/or a known
|
||||
# ``thread_type`` — hide them from the default conversation list so they
|
||||
# don't crowd out real chats. A caller that explicitly filters on
|
||||
# ``thread_type``/``system`` (e.g. the studio notebook list) opts out of
|
||||
# the exclusion.
|
||||
explicit_system_filter = bool(body.metadata) and ("thread_type" in body.metadata or "system" in body.metadata)
|
||||
rows = await repo.search(
|
||||
metadata=body.metadata or None,
|
||||
@@ -1204,6 +1243,38 @@ async def get_thread_last_ai_message(thread_id: str, request: Request, max_chars
|
||||
return {"delivered": full_len > 0, "content": text, "length": full_len}
|
||||
|
||||
|
||||
@router.get("/{thread_id}/messages", response_model=ThreadMessagesResponse)
|
||||
@require_permission("threads", "read", owner_check=True)
|
||||
async def get_thread_messages(
|
||||
thread_id: str,
|
||||
request: Request,
|
||||
limit: int = Query(default=200, ge=1, le=1000, description="Maximum messages to return"),
|
||||
offset: int = Query(default=0, ge=0, description="Number of leading messages to skip"),
|
||||
) -> ThreadMessagesResponse:
|
||||
"""Return a chronological, paginated view of a thread's message transcript.
|
||||
|
||||
This reads the latest checkpoint rather than a derived display transcript, so
|
||||
hidden context messages and all original message fields are retained. The
|
||||
response serialization is shared with ``GET /state`` and streaming events.
|
||||
"""
|
||||
checkpointer = get_checkpointer(request)
|
||||
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
|
||||
try:
|
||||
checkpoint_tuple = await checkpointer.aget_tuple(config)
|
||||
except Exception:
|
||||
logger.exception("Failed to get messages for thread %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to get thread messages")
|
||||
|
||||
if checkpoint_tuple is None:
|
||||
raise HTTPException(status_code=404, detail=f"Thread {thread_id} not found")
|
||||
|
||||
checkpoint = getattr(checkpoint_tuple, "checkpoint", {}) or {}
|
||||
channel_values = checkpoint.get("channel_values", {}) or {}
|
||||
messages = _serialize_thread_messages(channel_values.get("messages"))
|
||||
total = len(messages)
|
||||
return ThreadMessagesResponse(total=total, messages=messages[offset : offset + limit])
|
||||
|
||||
|
||||
@router.delete("/{thread_id}/messages")
|
||||
@require_permission("threads", "write", owner_check=True, require_existing=True)
|
||||
async def clear_thread_messages(thread_id: str, request: Request) -> dict[str, Any]:
|
||||
@@ -1332,6 +1403,12 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
|
||||
|
||||
channel_values["messages"] = filtered
|
||||
checkpoint["channel_values"] = channel_values
|
||||
checkpoint["id"] = str(uuid6(clock_seq=-2))
|
||||
checkpoint["ts"] = now_iso()
|
||||
versions = dict(checkpoint.get("channel_versions", {}))
|
||||
new_ver = checkpointer.get_next_version(versions.get("messages"), None)
|
||||
versions["messages"] = new_ver
|
||||
checkpoint["channel_versions"] = versions
|
||||
metadata["updated_at"] = now_iso()
|
||||
|
||||
write_config: dict[str, Any] = {
|
||||
@@ -1341,7 +1418,7 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
|
||||
}
|
||||
}
|
||||
try:
|
||||
await checkpointer.aput(write_config, checkpoint, metadata, {})
|
||||
await checkpointer.aput(write_config, checkpoint, metadata, {"messages": new_ver})
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to delete message %s from thread %s",
|
||||
|
||||
@@ -59,6 +59,7 @@ from app.gateway.document_rewrite_summary import build_document_rewrite_summary
|
||||
from app.gateway.path_utils import aresolve_thread_virtual_path
|
||||
from deerflow.config.app_config import AppConfig
|
||||
from deerflow.models import create_chat_model
|
||||
from deerflow.utils.stream_text import InlineThinkTagFilter
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -143,6 +144,9 @@ class DocumentRewriteRequest(BaseModel):
|
||||
instruction: str = Field(..., min_length=1, max_length=4_000)
|
||||
style: str | None = Field(default=None)
|
||||
model_name: str | None = Field(default=None, alias="modelName")
|
||||
report_outline: str | None = Field(default=None, alias="reportOutline", max_length=20_000)
|
||||
generate_images: bool = Field(default=False, alias="generateImages")
|
||||
max_generated_images: int = Field(default=2, alias="maxGeneratedImages", ge=1, le=4)
|
||||
expected_hash: str | None = Field(default=None, alias="expectedHash")
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
@@ -169,6 +173,22 @@ def _document_rewrite_key(path: Path) -> str:
|
||||
return os.path.normcase(str(path.resolve()))
|
||||
|
||||
|
||||
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()
|
||||
normalized_outline = (report_outline or "").strip()
|
||||
if not normalized_outline:
|
||||
return normalized_instruction
|
||||
return "\n\n".join(
|
||||
(
|
||||
normalized_instruction,
|
||||
"【报告结构要求】",
|
||||
"请按以下结构输出全文:文首题名与「日期:」「类型:」「密级:」等版头必须保留并填入真实值(占位符 XXX/xxx/xxxxx 不得照抄或删行;日期用当天中文日期;类型按题名判断;密级无另行规定时写「内部」)。章节按大纲展开;仅当某小节完全没有材料时才可合并或省略该小节,不得为套用结构编造事实。",
|
||||
normalized_outline,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def _reserve_document_rewrite(path: Path) -> bool:
|
||||
"""Reserve a physical document until its SSE generator reaches a terminal state."""
|
||||
key = _document_rewrite_key(path)
|
||||
@@ -193,12 +213,7 @@ def _artifact_rewrite_scope(thread_id: str, virtual_path: str) -> str:
|
||||
|
||||
def _artifact_job_matches(job: dict, *, thread_id: str, virtual_path: str) -> bool:
|
||||
snapshot = job.get("input_snapshot")
|
||||
return bool(
|
||||
isinstance(snapshot, dict)
|
||||
and snapshot.get("kind") == "artifact_document_rewrite"
|
||||
and snapshot.get("thread_id") == thread_id
|
||||
and snapshot.get("path") == virtual_path
|
||||
)
|
||||
return bool(isinstance(snapshot, dict) and snapshot.get("kind") == "artifact_document_rewrite" and snapshot.get("thread_id") == thread_id and snapshot.get("path") == virtual_path)
|
||||
|
||||
|
||||
def _artifact_job_response(job: dict) -> dict:
|
||||
@@ -215,6 +230,9 @@ def _artifact_job_response(job: dict) -> dict:
|
||||
"instruction": str(snapshot.get("instruction") or ""),
|
||||
"style": str(snapshot.get("style") or "professional"),
|
||||
"modelName": snapshot.get("model_name"),
|
||||
"reportOutline": str(snapshot.get("report_outline") or ""),
|
||||
"generateImages": bool(snapshot.get("generate_images")),
|
||||
"maxGeneratedImages": int(snapshot.get("max_generated_images") or 2),
|
||||
"originalContent": snapshot.get("original_content") or "",
|
||||
},
|
||||
}
|
||||
@@ -266,10 +284,7 @@ async def _stream_durable_artifact_rewrite_job(
|
||||
yield _sse(
|
||||
"error",
|
||||
{
|
||||
"message": str(
|
||||
current.get("error_message")
|
||||
or "全文改写任务未完成,原文件未被修改。"
|
||||
),
|
||||
"message": str(current.get("error_message") or "全文改写任务未完成,原文件未被修改。"),
|
||||
"stage": str(current.get("phase") or "generation"),
|
||||
},
|
||||
)
|
||||
@@ -386,6 +401,8 @@ async def start_durable_document_rewrite_job(
|
||||
if job_store is None or dispatcher is None:
|
||||
raise HTTPException(status_code=503, detail="改写任务服务暂不可用。")
|
||||
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)
|
||||
snapshot = {
|
||||
"kind": "artifact_document_rewrite",
|
||||
"session_id": scope,
|
||||
@@ -394,17 +411,23 @@ async def start_durable_document_rewrite_job(
|
||||
"path": requested_path,
|
||||
"original_content": original,
|
||||
"base_hash": base_hash,
|
||||
"instruction": body.instruction.strip(),
|
||||
"instruction": instruction,
|
||||
"report_outline": report_outline,
|
||||
"style": body.style or "professional",
|
||||
"model_name": body.model_name,
|
||||
"generate_images": body.generate_images,
|
||||
"max_generated_images": body.max_generated_images,
|
||||
}
|
||||
request_hash = hashlib.sha256(
|
||||
json.dumps(
|
||||
{
|
||||
"base_hash": base_hash,
|
||||
"instruction": body.instruction.strip(),
|
||||
"instruction": instruction,
|
||||
"report_outline": report_outline,
|
||||
"style": body.style or "professional",
|
||||
"model_name": body.model_name,
|
||||
"generate_images": body.generate_images,
|
||||
"max_generated_images": body.max_generated_images,
|
||||
},
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
@@ -432,13 +455,20 @@ async def start_durable_document_rewrite_job(
|
||||
|
||||
|
||||
@router.get("/document-rewrite/jobs/latest")
|
||||
@require_permission("threads", "read", owner_check=True)
|
||||
@require_permission("threads", "read")
|
||||
async def get_latest_durable_document_rewrite_job(
|
||||
request: Request,
|
||||
thread_id: str = Query(..., min_length=1),
|
||||
path: str = Query(..., min_length=1),
|
||||
) -> dict:
|
||||
"""Hydrate the latest artifact rewrite after a page refresh."""
|
||||
"""Hydrate the current user's latest artifact rewrite after a page refresh.
|
||||
|
||||
The durable-job store is already filtered by ``user_id`` below. Do not
|
||||
additionally perform a thread-owner lookup here: a historical thread can
|
||||
be displayed under a guest/SSO alias even though the current user has no
|
||||
matching ``threads_meta`` owner row, and the redundant check would turn an
|
||||
otherwise safe empty history into a 404.
|
||||
"""
|
||||
user_id = await get_current_user(request)
|
||||
job_store = getattr(request.app.state, "deep_research_job_store", None)
|
||||
if job_store is None:
|
||||
@@ -453,14 +483,20 @@ async def get_latest_durable_document_rewrite_job(
|
||||
|
||||
|
||||
@router.get("/document-rewrite/jobs")
|
||||
@require_permission("threads", "read", owner_check=True)
|
||||
@require_permission("threads", "read")
|
||||
async def list_durable_document_rewrite_jobs(
|
||||
request: Request,
|
||||
thread_id: str = Query(..., min_length=1),
|
||||
path: str = Query(..., min_length=1),
|
||||
limit: int = Query(default=20, ge=1, le=20),
|
||||
) -> dict:
|
||||
"""Return recent persisted rewrite jobs for a single sandbox Markdown file."""
|
||||
"""Return the current user's persisted rewrite jobs for one Markdown file.
|
||||
|
||||
``list_by_session(..., user_id=user_id)`` below is the access boundary for
|
||||
this metadata-only recovery endpoint. It must return an empty list rather
|
||||
than a thread-ownership 404 when a legacy thread's display login differs
|
||||
from its persisted creator identity.
|
||||
"""
|
||||
user_id = await get_current_user(request)
|
||||
job_store = getattr(request.app.state, "deep_research_job_store", None)
|
||||
if job_store is None:
|
||||
@@ -468,13 +504,7 @@ async def list_durable_document_rewrite_jobs(
|
||||
virtual_path = path.strip()
|
||||
scope = _artifact_rewrite_scope(thread_id, virtual_path)
|
||||
rows = await job_store.list_by_session(scope, user_id=user_id, limit=limit)
|
||||
return {
|
||||
"items": [
|
||||
_artifact_job_response(row)
|
||||
for row in rows
|
||||
if _artifact_job_matches(row, thread_id=thread_id, virtual_path=virtual_path)
|
||||
]
|
||||
}
|
||||
return {"items": [_artifact_job_response(row) for row in rows if _artifact_job_matches(row, thread_id=thread_id, virtual_path=virtual_path)]}
|
||||
|
||||
|
||||
@router.get("/document-rewrite/jobs/{job_id}/stream", response_class=StreamingResponse)
|
||||
@@ -544,6 +574,8 @@ async def rewrite_document(
|
||||
requested_path = body.path.strip()
|
||||
user_id = await get_current_user(request)
|
||||
version_store = getattr(request.app.state, "document_rewrite_version_store", None)
|
||||
report_outline = body.report_outline.strip() if body.report_outline else ""
|
||||
instruction = _instruction_with_report_outline(body.instruction, report_outline)
|
||||
suffix = Path(requested_path).suffix.lower()
|
||||
if suffix not in {".md", ".markdown"}:
|
||||
raise HTTPException(status_code=400, detail="AI 全文改写仅支持 Markdown 文件。")
|
||||
@@ -613,7 +645,7 @@ async def rewrite_document(
|
||||
async for delta in _astream_rewrite_requirements(
|
||||
model,
|
||||
file_name=actual_path.name,
|
||||
instruction=body.instruction.strip(),
|
||||
instruction=instruction,
|
||||
style_instruction=style_instruction,
|
||||
original=original,
|
||||
):
|
||||
@@ -633,7 +665,7 @@ async def rewrite_document(
|
||||
if not requirements_plan:
|
||||
requirements_fallback = True
|
||||
requirements_plan = _build_fallback_rewrite_requirements_plan(
|
||||
instruction=body.instruction.strip(),
|
||||
instruction=instruction,
|
||||
style_instruction=style_instruction,
|
||||
)
|
||||
yield _sse(
|
||||
@@ -645,7 +677,7 @@ async def rewrite_document(
|
||||
HumanMessage(
|
||||
content=(
|
||||
f"文件名:{actual_path.name}\n"
|
||||
f"改写要求:{body.instruction.strip()}\n"
|
||||
f"改写要求:{instruction}\n"
|
||||
f"风格要求:{style_instruction}\n\n"
|
||||
"【已确认的改写执行计划开始】\n"
|
||||
f"{requirements_plan}\n"
|
||||
@@ -659,16 +691,20 @@ async def rewrite_document(
|
||||
yield _sse(
|
||||
"requirements_resolved",
|
||||
{
|
||||
"instruction": body.instruction.strip(),
|
||||
"instruction": instruction,
|
||||
"style": body.style or "professional",
|
||||
"modelName": resolved_name or "system_default",
|
||||
"plan": requirements_plan,
|
||||
},
|
||||
)
|
||||
inline_think_filter = InlineThinkTagFilter()
|
||||
async for chunk in model.astream(messages, config={"run_name": "document_rewrite"}):
|
||||
thinking_delta, text_delta = _extract_chunk_parts(chunk)
|
||||
if thinking_delta:
|
||||
yield _sse("thinking", {"chunk": thinking_delta})
|
||||
text_delta, inline_thinking = inline_think_filter.push_parts(text_delta)
|
||||
if inline_thinking:
|
||||
yield _sse("thinking", {"chunk": inline_thinking})
|
||||
if text_delta:
|
||||
draft_parts.append(text_delta)
|
||||
yield _sse(
|
||||
@@ -679,6 +715,19 @@ async def rewrite_document(
|
||||
},
|
||||
)
|
||||
|
||||
tail, inline_thinking_tail = inline_think_filter.finish_parts()
|
||||
if inline_thinking_tail:
|
||||
yield _sse("thinking", {"chunk": inline_thinking_tail})
|
||||
if tail:
|
||||
draft_parts.append(tail)
|
||||
yield _sse(
|
||||
"rewrite_delta",
|
||||
{
|
||||
"delta": tail,
|
||||
"charCount": _count_document_chars("".join(draft_parts)),
|
||||
},
|
||||
)
|
||||
|
||||
rewritten = "".join(draft_parts).strip()
|
||||
yield _sse("validation_started", {})
|
||||
validation_error = _markdown_validation_error(rewritten)
|
||||
@@ -688,11 +737,7 @@ async def rewrite_document(
|
||||
|
||||
heading_count_before = sum(1 for line in original.splitlines() if line.lstrip().startswith("#"))
|
||||
heading_count_after = sum(1 for line in rewritten.splitlines() if line.lstrip().startswith("#"))
|
||||
warnings = (
|
||||
["标题数量发生变化,请在前后对比中确认结构。"]
|
||||
if heading_count_before != heading_count_after
|
||||
else []
|
||||
)
|
||||
warnings = ["标题数量发生变化,请在前后对比中确认结构。"] if heading_count_before != heading_count_after else []
|
||||
yield _sse(
|
||||
"validation_completed",
|
||||
{
|
||||
@@ -704,7 +749,7 @@ async def rewrite_document(
|
||||
"comparison_ready",
|
||||
build_document_rewrite_summary(
|
||||
source_display_name=actual_path.name,
|
||||
instruction=body.instruction.strip(),
|
||||
instruction=instruction,
|
||||
model_name=resolved_name or "system_default",
|
||||
original=original,
|
||||
rewritten=rewritten,
|
||||
@@ -726,6 +771,7 @@ async def rewrite_document(
|
||||
committed_hash = _content_hash(rewritten)
|
||||
await asyncio.to_thread(_atomic_write_text, actual_path, rewritten)
|
||||
version_id: str | None = None
|
||||
version_snapshot_failed = False
|
||||
if version_store is not None:
|
||||
try:
|
||||
version_id = f"drv_{uuid.uuid4().hex}"
|
||||
@@ -738,40 +784,44 @@ async def rewrite_document(
|
||||
original_content=original,
|
||||
original_hash=base_hash,
|
||||
committed_hash=committed_hash,
|
||||
instruction=body.instruction.strip(),
|
||||
instruction=instruction,
|
||||
model_name=resolved_name,
|
||||
)
|
||||
except Exception:
|
||||
# The file is never left changed without its promised undo
|
||||
# snapshot. Roll back only when nobody touched it in the
|
||||
# short persistence window.
|
||||
current_after_write = await asyncio.to_thread(
|
||||
actual_path.read_text,
|
||||
encoding="utf-8",
|
||||
)
|
||||
if _content_hash(current_after_write) == committed_hash:
|
||||
await asyncio.to_thread(_atomic_write_text, actual_path, original)
|
||||
raise
|
||||
try:
|
||||
await version_store.prune_source(
|
||||
user_id=user_id,
|
||||
source_type="artifact",
|
||||
source_id=body.thread_id,
|
||||
source_path=requested_path,
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("could not prune artifact rewrite versions for %s", actual_path)
|
||||
# A version snapshot enables undo but is not allowed to
|
||||
# discard a successfully committed document on failure.
|
||||
logger.exception(
|
||||
"could not persist artifact rewrite version for %s; keeping rewritten file",
|
||||
actual_path,
|
||||
)
|
||||
version_id = None
|
||||
version_snapshot_failed = True
|
||||
else:
|
||||
try:
|
||||
await version_store.prune_source(
|
||||
user_id=user_id,
|
||||
source_type="artifact",
|
||||
source_id=body.thread_id,
|
||||
source_path=requested_path,
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("could not prune artifact rewrite versions for %s", actual_path)
|
||||
yield _sse(
|
||||
"committed",
|
||||
{
|
||||
"content": rewritten,
|
||||
"committedHash": committed_hash,
|
||||
"versionId": version_id,
|
||||
"versionSnapshotFailed": version_snapshot_failed,
|
||||
},
|
||||
)
|
||||
yield _sse(
|
||||
"done",
|
||||
{"committedHash": committed_hash, "versionId": version_id},
|
||||
{
|
||||
"committedHash": committed_hash,
|
||||
"versionId": version_id,
|
||||
"versionSnapshotFailed": version_snapshot_failed,
|
||||
},
|
||||
)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
@@ -891,12 +941,21 @@ async def rewrite_selection(
|
||||
async def generate():
|
||||
try:
|
||||
model = create_chat_model(name=body.model_name, thinking_enabled=thinking_enabled, app_config=config)
|
||||
inline_think_filter = InlineThinkTagFilter()
|
||||
async for chunk in model.astream(messages, config={"run_name": "writing_rewrite"}):
|
||||
thinking_delta, text_delta = _extract_chunk_parts(chunk)
|
||||
if thinking_delta:
|
||||
yield _sse("thinking", {"chunk": thinking_delta})
|
||||
text_delta, inline_thinking = inline_think_filter.push_parts(text_delta)
|
||||
if inline_thinking:
|
||||
yield _sse("thinking", {"chunk": inline_thinking})
|
||||
if text_delta:
|
||||
yield _sse("text", {"chunk": text_delta})
|
||||
tail, inline_thinking_tail = inline_think_filter.finish_parts()
|
||||
if inline_thinking_tail:
|
||||
yield _sse("thinking", {"chunk": inline_thinking_tail})
|
||||
if tail:
|
||||
yield _sse("text", {"chunk": tail})
|
||||
yield _sse("done", {})
|
||||
except Exception as e:
|
||||
logger.exception("writing/rewrite failed: thread=%s action=%s", body.thread_id, action)
|
||||
|
||||
Reference in new issue
Block a user