merge: integrate dhk-dev into dhk-1
This commit is contained in:
commit
08ce3dda73
106 files changed
+11921
-910
No files matched your search
@@ -79,6 +79,8 @@ from app.gateway.routers import (
|
||||
system_settings,
|
||||
tags,
|
||||
task_buttons,
|
||||
taskcop_tasks,
|
||||
task_reports,
|
||||
thread_runs,
|
||||
tool_metrics,
|
||||
thread_shares,
|
||||
@@ -86,6 +88,7 @@ from app.gateway.routers import (
|
||||
uploads,
|
||||
user_preferences,
|
||||
user_prompts,
|
||||
weknora_sources,
|
||||
writing,
|
||||
)
|
||||
from deerflow.config import app_config as deerflow_app_config
|
||||
@@ -1352,6 +1355,18 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
"X-Thread-Id",
|
||||
]
|
||||
|
||||
# Public pages (e.g. http://47.x.x.x:7010) calling this Gateway on localhost
|
||||
# trigger Chrome/Edge Private Network Access preflights. Without
|
||||
# allow_private_network=True the browser reports a generic CORS failure even
|
||||
# when Access-Control-Allow-Origin is correct.
|
||||
cors_kwargs = {
|
||||
"allow_credentials": True,
|
||||
"allow_methods": ["*"],
|
||||
"allow_headers": ["*"],
|
||||
"expose_headers": cors_expose_headers,
|
||||
"allow_private_network": True,
|
||||
}
|
||||
|
||||
if _cors_allow_any_origin():
|
||||
logger.warning(
|
||||
"Permissive CORS enabled ? any Origin is echoed back (GATEWAY_CORS_ALLOW_ALL or "
|
||||
@@ -1362,10 +1377,7 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
allow_origins=[],
|
||||
# fullmatch() on the Origin header; browsers send scheme://host[:port]
|
||||
allow_origin_regex=r".+",
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
expose_headers=cors_expose_headers,
|
||||
**cors_kwargs,
|
||||
)
|
||||
elif cors_origins:
|
||||
if "*" in cors_origins:
|
||||
@@ -1378,10 +1390,7 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=cors_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
expose_headers=cors_expose_headers,
|
||||
**cors_kwargs,
|
||||
)
|
||||
else:
|
||||
# Safe-by-default fallback for local frontend development. Without any CORS
|
||||
@@ -1404,10 +1413,7 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=dev_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
expose_headers=cors_expose_headers,
|
||||
**cors_kwargs,
|
||||
)
|
||||
|
||||
# Include routers
|
||||
@@ -1417,6 +1423,9 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
# MCP API is mounted at /api/mcp
|
||||
app.include_router(mcp.router)
|
||||
|
||||
# Authenticated source-context proxy for the WeKnora retrieval skill.
|
||||
app.include_router(weknora_sources.router)
|
||||
|
||||
# Memory API is mounted at /api/memory
|
||||
app.include_router(memory.router)
|
||||
|
||||
@@ -1482,6 +1491,12 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
# Task-workspace configurable jump buttons (按钮管理) at /api/task-buttons
|
||||
app.include_router(task_buttons.router)
|
||||
|
||||
# Temporary compatibility bridge for the TaskCOP situation-overview frontend.
|
||||
app.include_router(taskcop_tasks.router)
|
||||
|
||||
# Task-scoped situation-report details used by dashboard drawing / task gates.
|
||||
app.include_router(task_reports.router)
|
||||
|
||||
# Standalone parallel multi-agent panel: fan-out streaming + saved tasks
|
||||
app.include_router(parallel_agents.router)
|
||||
app.include_router(parallel_agent_tasks.router)
|
||||
|
||||
@@ -53,7 +53,7 @@ _PUBLIC_EXACT_PATHS: frozenset[str] = frozenset(
|
||||
"/api/v1/auth/setup-status",
|
||||
"/api/v1/auth/initialize",
|
||||
# 并行多智能体面板专用的「绕过常规登录」取 token 端点(内网部署限制了标准登录时用)。
|
||||
"/api/parallel-agents/auth/token",
|
||||
"/api/parallel-agents/auth/token",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ _AUTH_EXEMPT_PATHS: frozenset[str] = frozenset(
|
||||
"/api/v1/auth/register",
|
||||
"/api/v1/auth/initialize",
|
||||
# 并行多智能体面板专用取 token 端点(公共 + CSRF 豁免)。
|
||||
"/api/parallel-agents/auth/token",
|
||||
"/api/parallel-agents/auth/token",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -287,9 +287,17 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
from deerflow.persistence.task_buttons import make_task_button_store
|
||||
|
||||
app.state.task_button_store = make_task_button_store(sf)
|
||||
|
||||
from deerflow.persistence.parallel_agent_tasks import make_parallel_agent_task_store
|
||||
app.state.task_button_store = make_task_button_store(sf)
|
||||
|
||||
from deerflow.persistence.taskcop_tasks import make_taskcop_task_store
|
||||
|
||||
app.state.taskcop_task_store = make_taskcop_task_store(sf)
|
||||
|
||||
from deerflow.persistence.task_reports import make_task_report_store
|
||||
|
||||
app.state.task_report_store = make_task_report_store(sf)
|
||||
|
||||
from deerflow.persistence.parallel_agent_tasks import make_parallel_agent_task_store
|
||||
|
||||
app.state.parallel_agent_task_store = make_parallel_agent_task_store(sf)
|
||||
|
||||
|
||||
@@ -13,20 +13,32 @@ GET /api/ai-writing/article-types 列出所有文章类型
|
||||
POST /api/ai-writing/article-types 新增文章类型
|
||||
PATCH /api/ai-writing/article-types/{id} 更新文章类型
|
||||
DELETE /api/ai-writing/article-types/{id} 删除文章类型
|
||||
POST /api/ai-writing/sample/extract 提取样文仿写上传文件文本
|
||||
POST /api/ai-writing/admin/cleanup (admin)手动清理会话
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.authz import require_auth
|
||||
from app.gateway.deps import get_config
|
||||
from deerflow.utils.file_conversion import CONVERTIBLE_EXTENSIONS, convert_file_to_markdown
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/ai-writing", tags=["ai-writing"])
|
||||
|
||||
_SAMPLE_TEXT_EXTENSIONS = {".txt", ".md", ".markdown"}
|
||||
_MAX_SAMPLE_BYTES = 20 * 1024 * 1024
|
||||
_MAX_SAMPLE_TEXT_CHARS = 120_000
|
||||
|
||||
# ── 请求/响应模型 ─────────────────────────────────────────────────────────────
|
||||
|
||||
class RenameSessionRequest(BaseModel):
|
||||
@@ -67,6 +79,13 @@ class SaveTranscriptRequest(BaseModel):
|
||||
transcript: dict
|
||||
|
||||
|
||||
class SampleExtractResponse(BaseModel):
|
||||
filename: str
|
||||
text: str
|
||||
char_count: int
|
||||
truncated: bool = False
|
||||
|
||||
|
||||
class ArticleTypeRequest(BaseModel):
|
||||
key: str
|
||||
label: str
|
||||
@@ -154,6 +173,52 @@ class SessionResponse(BaseModel):
|
||||
updated_at: str
|
||||
|
||||
|
||||
async def _extract_sample_text(file: UploadFile) -> tuple[str, str]:
|
||||
name = file.filename or "sample"
|
||||
ext = Path(name).suffix.lower()
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
raise HTTPException(status_code=400, detail="文件为空")
|
||||
if len(raw) > _MAX_SAMPLE_BYTES:
|
||||
raise HTTPException(status_code=413, detail=f"文件过大,最大支持 {_MAX_SAMPLE_BYTES // (1024 * 1024)}MB")
|
||||
|
||||
if ext in _SAMPLE_TEXT_EXTENSIONS:
|
||||
text = raw.decode("utf-8", errors="ignore")
|
||||
elif ext in CONVERTIBLE_EXTENSIONS:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmp_path = Path(tmp) / f"sample{ext}"
|
||||
tmp_path.write_bytes(raw)
|
||||
md_path = await convert_file_to_markdown(tmp_path)
|
||||
if md_path is None:
|
||||
raise HTTPException(status_code=422, detail=f"无法解析文件:{name}")
|
||||
text = md_path.read_text(encoding="utf-8", errors="ignore")
|
||||
else:
|
||||
raise HTTPException(status_code=415, detail=f"不支持的文件类型:{ext or '未知'}(支持 doc/docx/pdf/md/txt)")
|
||||
|
||||
text = text.strip()
|
||||
if not text:
|
||||
raise HTTPException(status_code=422, detail="文件内容为空或无法提取文本")
|
||||
return name, text
|
||||
|
||||
|
||||
@router.post("/sample/extract", response_model=SampleExtractResponse)
|
||||
@require_auth
|
||||
async def extract_sample_article(request: Request, file: UploadFile = File(...)) -> SampleExtractResponse:
|
||||
"""Extract text from a sample article for AI-writing imitation mode."""
|
||||
_ = request
|
||||
name, text = await _extract_sample_text(file)
|
||||
char_count = len(text)
|
||||
truncated = char_count > _MAX_SAMPLE_TEXT_CHARS
|
||||
if truncated:
|
||||
text = text[:_MAX_SAMPLE_TEXT_CHARS]
|
||||
return SampleExtractResponse(
|
||||
filename=name,
|
||||
text=text,
|
||||
char_count=char_count,
|
||||
truncated=truncated,
|
||||
)
|
||||
|
||||
|
||||
# ── 路由 ──────────────────────────────────────────────────────────────────────
|
||||
@router.get("/sessions", response_model=list[SessionResponse])
|
||||
async def list_sessions(request: Request):
|
||||
@@ -264,6 +329,421 @@ async def save_transcript(session_id: str, body: SaveTranscriptRequest, request:
|
||||
return {"success": True}
|
||||
|
||||
|
||||
# ── 意图解析(对话驱动写作台) ────────────────────────────────────────────────
|
||||
|
||||
|
||||
class IntentParseRequest(BaseModel):
|
||||
# 用户在底部智能输入框发的自由文本
|
||||
text: str
|
||||
# 前端当前 WritingStatus(仅参考;暂停点以 checkpoint 里真实 interrupt 为准)
|
||||
status: Optional[str] = None
|
||||
# 前端认为的当前暂停点(checkpoint 读取失败时的兜底)
|
||||
pause_point: Optional[str] = None
|
||||
|
||||
|
||||
class IntentParseResponse(BaseModel):
|
||||
intent: str
|
||||
pause_point: Optional[str] = None
|
||||
action: Optional[str] = None
|
||||
# 驼峰键(approvedMaterialIds / userQuery / outlineFeedback / userRevisionNotes /
|
||||
# overrideVerdict / selectedIssueIndices / sectionDecisions),前端可直接喂 submitIntervention
|
||||
payload: dict = Field(default_factory=dict)
|
||||
need_research: bool = False
|
||||
research_query: str = ""
|
||||
answer: str = ""
|
||||
confidence: float = 0.0
|
||||
clarify: str = ""
|
||||
|
||||
|
||||
class WritingSuggestionMessage(BaseModel):
|
||||
role: str
|
||||
content: str
|
||||
|
||||
|
||||
class WritingSuggestionsRequest(BaseModel):
|
||||
messages: list[WritingSuggestionMessage] = Field(default_factory=list)
|
||||
n: int = Field(default=3, ge=1, le=5)
|
||||
model_name: Optional[str] = None
|
||||
|
||||
|
||||
class WritingSuggestionsResponse(BaseModel):
|
||||
suggestions: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AnswerStreamRequest(BaseModel):
|
||||
text: str
|
||||
status: Optional[str] = None
|
||||
pause_point: Optional[str] = None
|
||||
model_name: Optional[str] = None
|
||||
|
||||
|
||||
async def _prepare_intent_parse_context(
|
||||
session_id: str,
|
||||
body: IntentParseRequest,
|
||||
request: Request,
|
||||
) -> tuple[str, dict, Optional[dict], Optional[str]]:
|
||||
"""鉴权并读取会话 checkpoint,为一次性/流式意图解析提供同一份上下文。"""
|
||||
text = (body.text or "").strip()
|
||||
if not text:
|
||||
raise HTTPException(status_code=400, detail="text 不能为空")
|
||||
|
||||
await _ensure_session_owner(session_id, request)
|
||||
values, interrupt_value, pause_point = await _read_ai_writing_checkpoint_context(
|
||||
session_id,
|
||||
body.pause_point,
|
||||
request,
|
||||
)
|
||||
return text, values, interrupt_value, pause_point
|
||||
|
||||
|
||||
async def _read_ai_writing_checkpoint_context(
|
||||
session_id: str,
|
||||
fallback_pause_point: Optional[str],
|
||||
request: Request,
|
||||
) -> tuple[dict, Optional[dict], Optional[str]]:
|
||||
from app.gateway.ai_writing_job_executor import _pending_interrupt
|
||||
|
||||
# 读 checkpoint:暂停点以 pending interrupt 为权威(frontend 声明只作读取失败兜底)。
|
||||
values: dict = {}
|
||||
interrupt_value: Optional[dict] = None
|
||||
pause_point: Optional[str] = None
|
||||
checkpoint_ok = False
|
||||
_app_state = getattr(getattr(request, "app", None), "state", None)
|
||||
checkpointer = getattr(_app_state, "checkpointer", None)
|
||||
if checkpointer is not None:
|
||||
from deerflow.agents.ai_writing.graph import make_ai_writing_graph
|
||||
|
||||
graph = make_ai_writing_graph()
|
||||
graph.checkpointer = checkpointer
|
||||
try:
|
||||
snapshot = await graph.aget_state({"configurable": {"thread_id": session_id}})
|
||||
values = getattr(snapshot, "values", None) or {}
|
||||
interrupt_value = _pending_interrupt(snapshot)
|
||||
checkpoint_ok = bool(values)
|
||||
if interrupt_value:
|
||||
pause_point = interrupt_value.get("pause_point")
|
||||
except Exception:
|
||||
logger.warning("ai_writing intent: read checkpoint failed for %s", session_id, exc_info=True)
|
||||
if pause_point is None and not checkpoint_ok:
|
||||
pause_point = (fallback_pause_point or "").strip() or None
|
||||
|
||||
return values, interrupt_value, pause_point
|
||||
|
||||
|
||||
async def _get_session_owner_record(session_id: str, request: Request) -> dict:
|
||||
from app.gateway.deps import get_ai_writing_session_store, get_current_user
|
||||
|
||||
repo = get_ai_writing_session_store(request)
|
||||
if repo is None:
|
||||
raise HTTPException(status_code=503, detail="Session store not available")
|
||||
user_id = await get_current_user(request)
|
||||
record = await repo.get(session_id, user_id=user_id)
|
||||
if record is None:
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
return record
|
||||
|
||||
|
||||
async def _ensure_session_owner(session_id: str, request: Request) -> None:
|
||||
await _get_session_owner_record(session_id, request)
|
||||
|
||||
|
||||
def _clip_answer_context(text: str, limit: int) -> str:
|
||||
text = (text or "").strip()
|
||||
if len(text) <= limit:
|
||||
return text
|
||||
return text[:limit] + "\n…(内容过长已截断)"
|
||||
|
||||
|
||||
def _build_session_answer_context(record: dict) -> str:
|
||||
"""Build a fast context from the persisted session row for plain Q&A."""
|
||||
parts: list[str] = []
|
||||
title = str(record.get("title") or record.get("draft_title") or "").strip()
|
||||
user_intent = str(record.get("user_intent") or "").strip()
|
||||
status = str(record.get("status") or "").strip()
|
||||
if title:
|
||||
parts.append(f"会话标题:{title}")
|
||||
if user_intent:
|
||||
parts.append(f"写作主题:{_clip_answer_context(user_intent, 300)}")
|
||||
if status:
|
||||
parts.append(f"流程状态:{status}")
|
||||
|
||||
draft_markdown = str(record.get("draft_markdown") or "").strip()
|
||||
has_substantive_context = bool(draft_markdown)
|
||||
if draft_markdown:
|
||||
draft_title = str(record.get("draft_title") or title or "").strip()
|
||||
header = f"草稿标题:《{draft_title}》" if draft_title else "当前草稿"
|
||||
parts.append(f"{header}\n--- 草稿正文 ---\n{_clip_answer_context(draft_markdown, 6000)}")
|
||||
|
||||
review = record.get("review_result") if isinstance(record.get("review_result"), dict) else {}
|
||||
if review:
|
||||
has_substantive_context = True
|
||||
lines = [
|
||||
f"编辑结论:{review.get('verdict', '')}(总分 {review.get('overall_score', review.get('overallScore', ''))})"
|
||||
]
|
||||
notes = review.get("revision_notes") or review.get("revisionNotes") or []
|
||||
if isinstance(notes, list):
|
||||
for i, note in enumerate(notes[:8]):
|
||||
if not isinstance(note, dict):
|
||||
continue
|
||||
issue = _clip_answer_context(str(note.get("issue") or ""), 120)
|
||||
suggestion = _clip_answer_context(str(note.get("suggestion") or ""), 120)
|
||||
if issue or suggestion:
|
||||
lines.append(f"意见 {i + 1}:{issue} → {suggestion}")
|
||||
parts.append("\n".join(lines))
|
||||
|
||||
if not has_substantive_context:
|
||||
return ""
|
||||
return "\n\n".join(part for part in parts if part.strip())
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/intent", response_model=IntentParseResponse)
|
||||
async def parse_session_intent(session_id: str, body: IntentParseRequest, request: Request):
|
||||
"""对话驱动写作台:把用户自由文本翻译成结构化写作指令(或问答/澄清)。
|
||||
|
||||
读该会话 graph checkpoint 的素材/大纲/草稿/审核摘要 + 当前 pending interrupt
|
||||
(权威暂停点),交给 ``deerflow.agents.ai_writing.intent_router`` 的 LLM 意图
|
||||
路由。动作白名单 / 高代价置信度门槛 / payload 清洗都在 intent_router 内完成,
|
||||
这里只做鉴权与 state 读取。模型失败不抛 5xx —— intent_router 会降级成 clarify。
|
||||
"""
|
||||
from deerflow.agents.ai_writing.intent_router import parse_user_intent
|
||||
|
||||
text, values, interrupt_value, pause_point = await _prepare_intent_parse_context(session_id, body, request)
|
||||
|
||||
result = await parse_user_intent(
|
||||
text=text,
|
||||
pause_point=pause_point,
|
||||
values=values,
|
||||
interrupt_value=interrupt_value,
|
||||
model_name=str(values.get("model_name") or "") or None,
|
||||
)
|
||||
return IntentParseResponse(**result)
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/intent/stream")
|
||||
async def stream_session_intent(session_id: str, body: IntentParseRequest, request: Request):
|
||||
"""对话驱动写作台:流式意图解析。
|
||||
|
||||
SSE 事件:
|
||||
- ``thinking``: ``{"chunk": "..."}``,模型推理 / ``<think>`` 内容
|
||||
- ``text``: ``{"chunk": "..."}``,模型正文输出(通常是结构化 JSON)
|
||||
- ``result``: 最终规范化后的 ``IntentParseResponse`` 字段
|
||||
- ``done`` / ``error``
|
||||
"""
|
||||
from app.gateway.services import format_sse
|
||||
from deerflow.agents.ai_writing.intent_router import stream_user_intent
|
||||
|
||||
text, values, interrupt_value, pause_point = await _prepare_intent_parse_context(session_id, body, request)
|
||||
|
||||
async def event_stream():
|
||||
try:
|
||||
async for event in stream_user_intent(
|
||||
text=text,
|
||||
pause_point=pause_point,
|
||||
values=values,
|
||||
interrupt_value=interrupt_value,
|
||||
model_name=str(values.get("model_name") or "") or None,
|
||||
):
|
||||
if event.get("type") == "chunk":
|
||||
chunk = str(event.get("chunk") or "")
|
||||
if not chunk:
|
||||
continue
|
||||
yield format_sse(
|
||||
"thinking" if event.get("is_thinking") else "text",
|
||||
{"chunk": chunk},
|
||||
)
|
||||
elif event.get("type") == "result":
|
||||
yield format_sse("result", event.get("result") or {})
|
||||
yield format_sse("done", {})
|
||||
except Exception:
|
||||
logger.exception("ai_writing intent stream failed: session=%s", session_id)
|
||||
yield format_sse("error", {"message": "意图解析流式接口异常,请稍后重试"})
|
||||
|
||||
return StreamingResponse(
|
||||
event_stream(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/suggestions", response_model=WritingSuggestionsResponse)
|
||||
async def generate_session_suggestions(
|
||||
session_id: str,
|
||||
body: WritingSuggestionsRequest,
|
||||
request: Request,
|
||||
config=Depends(get_config),
|
||||
):
|
||||
"""为 AI 写作对话版生成窄栏追问建议。
|
||||
|
||||
与主对话页的 ``/api/threads/{id}/suggestions`` 语义一致,但鉴权对象换成
|
||||
AI 写作 session,避免把写作 sessionId 冒充 workspace threadId。
|
||||
"""
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
|
||||
from app.gateway.routers.suggestions import (
|
||||
_extract_response_text,
|
||||
_format_conversation,
|
||||
_parse_json_string_list,
|
||||
SuggestionMessage,
|
||||
)
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
await _ensure_session_owner(session_id, request)
|
||||
|
||||
if not body.messages:
|
||||
return WritingSuggestionsResponse()
|
||||
|
||||
messages = [
|
||||
SuggestionMessage(role=m.role, content=m.content)
|
||||
for m in body.messages
|
||||
if (m.content or "").strip()
|
||||
]
|
||||
conversation = _format_conversation(messages)
|
||||
if not conversation:
|
||||
return WritingSuggestionsResponse()
|
||||
|
||||
n = body.n
|
||||
system_instruction = (
|
||||
"You are generating compact follow-up questions for a narrow AI writing sidebar.\n"
|
||||
f"Based on the conversation below, produce EXACTLY {n} short questions the user might ask next.\n"
|
||||
"Requirements:\n"
|
||||
"- Questions must be relevant to the article or writing workflow.\n"
|
||||
"- Prefer questions about summary, structure, evidence, style, revisions, or next actions.\n"
|
||||
"- Questions must be written in the same language as the user.\n"
|
||||
"- Keep each question very concise (<= 24 Chinese characters when Chinese).\n"
|
||||
"- Do NOT include numbering, markdown, or any extra text.\n"
|
||||
"- Output MUST be a JSON array of strings only.\n"
|
||||
)
|
||||
user_content = f"Conversation Context:\n{conversation}\n\nGenerate {n} follow-up questions"
|
||||
|
||||
try:
|
||||
model = create_chat_model(name=body.model_name, thinking_enabled=False, app_config=config)
|
||||
response = await model.ainvoke(
|
||||
[SystemMessage(content=system_instruction), HumanMessage(content=user_content)],
|
||||
config={"run_name": "ai_writing_suggestions"},
|
||||
)
|
||||
raw = _extract_response_text(response.content)
|
||||
suggestions = _parse_json_string_list(raw) or []
|
||||
cleaned = [s.replace("\n", " ").strip() for s in suggestions if s.strip()]
|
||||
return WritingSuggestionsResponse(suggestions=cleaned[:n])
|
||||
except Exception:
|
||||
logger.exception("ai_writing: failed to generate suggestions: session=%s", session_id)
|
||||
return WritingSuggestionsResponse()
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/answer/stream")
|
||||
async def stream_session_answer(session_id: str, body: AnswerStreamRequest, request: Request):
|
||||
"""AI 写作对话版问答旁路:纯文本 SSE 回答。
|
||||
|
||||
意图路由仍负责操作类消息;明显的提问走这里,避免把答案塞进 JSON 的
|
||||
``answer`` 字段导致“模型完整生成后才一次性显示”。
|
||||
"""
|
||||
from app.gateway.services import format_sse
|
||||
from deerflow.agents.ai_writing.intent_router import build_context_snapshot
|
||||
from deerflow.agents.ai_writing.nodes._utils import ThinkTagParser, _content_to_text, reasoning_delta
|
||||
from deerflow.models import create_chat_model
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
|
||||
text = (body.text or "").strip()
|
||||
if not text:
|
||||
raise HTTPException(status_code=400, detail="text 不能为空")
|
||||
record = await _get_session_owner_record(session_id, request)
|
||||
session_context = _build_session_answer_context(record)
|
||||
|
||||
async def event_stream():
|
||||
import time
|
||||
|
||||
started = time.monotonic()
|
||||
parser = ThinkTagParser()
|
||||
chunk_count = 0
|
||||
first_chunk_logged = False
|
||||
yield format_sse("start", {})
|
||||
try:
|
||||
values: dict = {}
|
||||
if session_context:
|
||||
context = session_context
|
||||
model_name = (body.model_name or "").strip() or None
|
||||
logger.info(
|
||||
"ai_writing answer context ready: session=%s elapsed=%.3fs source=session_row pause_point=%s",
|
||||
session_id,
|
||||
time.monotonic() - started,
|
||||
body.pause_point,
|
||||
)
|
||||
else:
|
||||
values, interrupt_value, pause_point = await _read_ai_writing_checkpoint_context(
|
||||
session_id,
|
||||
body.pause_point,
|
||||
request,
|
||||
)
|
||||
context = build_context_snapshot(values, pause_point, interrupt_value)
|
||||
model_name = (body.model_name or "").strip() or str(values.get("model_name") or "") or None
|
||||
logger.info(
|
||||
"ai_writing answer context ready: session=%s elapsed=%.3fs source=checkpoint has_values=%s pause_point=%s",
|
||||
session_id,
|
||||
time.monotonic() - started,
|
||||
bool(values),
|
||||
pause_point,
|
||||
)
|
||||
|
||||
messages = [
|
||||
SystemMessage(content=(
|
||||
"你是 AI 智能写作工作台里的问答助手。用户正在写一篇文章,"
|
||||
"你只能根据给定的写作上下文回答问题,不执行确认、检索、修改、定稿等流程动作。"
|
||||
"如果上下文没有足够信息,请明确说明,不要编造。回答用中文,简洁但有信息量。"
|
||||
)),
|
||||
HumanMessage(content=f"【写作上下文】\n{context}\n\n【用户问题】\n{text}\n\n请直接回答:"),
|
||||
]
|
||||
|
||||
model = create_chat_model(
|
||||
name=model_name,
|
||||
thinking_enabled=False,
|
||||
force_disable_thinking=True,
|
||||
)
|
||||
async for chunk in model.astream(messages, config={"run_name": "ai_writing_answer"}):
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
if not first_chunk_logged:
|
||||
first_chunk_logged = True
|
||||
logger.info(
|
||||
"ai_writing answer first chunk: session=%s elapsed=%.3fs kind=thinking",
|
||||
session_id,
|
||||
time.monotonic() - started,
|
||||
)
|
||||
yield format_sse("thinking", {"chunk": reasoning})
|
||||
content = _content_to_text(getattr(chunk, "content", "") or "")
|
||||
if not content:
|
||||
continue
|
||||
for is_thinking, part in parser.feed(content):
|
||||
if not part:
|
||||
continue
|
||||
if not first_chunk_logged:
|
||||
first_chunk_logged = True
|
||||
logger.info(
|
||||
"ai_writing answer first chunk: session=%s elapsed=%.3fs kind=%s",
|
||||
session_id,
|
||||
time.monotonic() - started,
|
||||
"thinking" if is_thinking else "text",
|
||||
)
|
||||
chunk_count += 1
|
||||
yield format_sse("thinking" if is_thinking else "text", {"chunk": part})
|
||||
logger.info(
|
||||
"ai_writing answer stream done: session=%s chunks=%d elapsed=%.3fs first_chunk=%s",
|
||||
session_id,
|
||||
chunk_count,
|
||||
time.monotonic() - started,
|
||||
first_chunk_logged,
|
||||
)
|
||||
yield format_sse("done", {})
|
||||
except Exception:
|
||||
logger.exception("ai_writing answer stream failed: session=%s", session_id)
|
||||
yield format_sse("error", {"message": "回答生成失败,请稍后重试"})
|
||||
|
||||
return StreamingResponse(
|
||||
event_stream(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
class BackgroundResponse(BaseModel):
|
||||
# 是否新拉起了后台任务(False = 已在后台跑,幂等返回)
|
||||
started: bool
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
"""Task-scoped situation-report detail API.
|
||||
|
||||
The report payload intentionally mirrors the existing ``selectSqReport``
|
||||
Consumer contract:
|
||||
``{state, msg, data: [{id, taskId, createTime, updateTime, sessionId,
|
||||
contentJson, categoryType}]}``. ``contentJson`` stays a JSON string so the
|
||||
dashboard parser can continue to deserialize it category by category.
|
||||
|
||||
Routes (all authenticated by the Gateway middleware):
|
||||
GET /api/task-reports/by-task/{task_id}
|
||||
POST /api/task-reports/by-task/{task_id}
|
||||
PUT /api/task-reports/by-task/{task_id}
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, Body, HTTPException, Request
|
||||
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, StrictStr
|
||||
|
||||
from app.gateway.deps import get_current_user
|
||||
from deerflow.persistence.task_reports import TaskReportStore
|
||||
|
||||
router = APIRouter(prefix="/api/task-reports", tags=["task-reports"])
|
||||
|
||||
|
||||
class TaskReportRecordInput(BaseModel):
|
||||
"""One ``data`` entry in the legacy-compatible report envelope."""
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
id: StrictStr | None = Field(default=None, max_length=128)
|
||||
task_id: str | int | None = Field(
|
||||
default=None,
|
||||
validation_alias=AliasChoices("taskId", "task_id"),
|
||||
)
|
||||
create_time: StrictStr | None = Field(
|
||||
default=None,
|
||||
validation_alias=AliasChoices("createTime", "create_time"),
|
||||
)
|
||||
update_time: StrictStr | None = Field(
|
||||
default=None,
|
||||
validation_alias=AliasChoices("updateTime", "update_time"),
|
||||
)
|
||||
session_id: StrictStr = Field(
|
||||
default="",
|
||||
max_length=255,
|
||||
validation_alias=AliasChoices("sessionId", "session_id"),
|
||||
)
|
||||
content_json: StrictStr = Field(
|
||||
...,
|
||||
validation_alias=AliasChoices("contentJson", "content_json"),
|
||||
)
|
||||
category_type: StrictStr = Field(
|
||||
...,
|
||||
max_length=255,
|
||||
validation_alias=AliasChoices("categoryType", "category_type"),
|
||||
)
|
||||
|
||||
|
||||
class TaskReportWriteRequest(BaseModel):
|
||||
data: list[TaskReportRecordInput] = Field(min_length=1)
|
||||
|
||||
|
||||
class TaskReportRecordResponse(BaseModel):
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
id: str
|
||||
task_id: int | str = Field(serialization_alias="taskId")
|
||||
create_time: str = Field(serialization_alias="createTime")
|
||||
update_time: str = Field(serialization_alias="updateTime")
|
||||
session_id: str = Field(serialization_alias="sessionId")
|
||||
content_json: str = Field(serialization_alias="contentJson")
|
||||
category_type: str = Field(serialization_alias="categoryType")
|
||||
|
||||
|
||||
class TaskReportResponse(BaseModel):
|
||||
state: str = "200"
|
||||
msg: str = "操作成功!"
|
||||
data: list[TaskReportRecordResponse]
|
||||
|
||||
|
||||
def _get_store(request: Request) -> TaskReportStore:
|
||||
store = getattr(request.app.state, "task_report_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="Task report store not available")
|
||||
return store
|
||||
|
||||
|
||||
async def _mark_task_analysis_completed(request: Request, task_id: str) -> None:
|
||||
"""Reflect a non-empty saved report in the TaskCOP task-list status."""
|
||||
task_store = getattr(request.app.state, "taskcop_task_store", None)
|
||||
if task_store is not None:
|
||||
await task_store.mark_analysis_completed(task_id)
|
||||
|
||||
|
||||
def _public_task_id(task_id: str) -> int | str:
|
||||
"""Keep numeric task ids numeric, matching the supplied mock payload."""
|
||||
return int(task_id) if task_id.isdecimal() and task_id == str(int(task_id)) else task_id
|
||||
|
||||
|
||||
def _to_response(records: list[dict[str, Any]]) -> TaskReportResponse:
|
||||
return TaskReportResponse(
|
||||
data=[
|
||||
TaskReportRecordResponse(
|
||||
id=str(record["id"]),
|
||||
task_id=record["taskId"],
|
||||
create_time=str(record["createTime"]),
|
||||
update_time=str(record["updateTime"]),
|
||||
session_id=str(record["sessionId"]),
|
||||
content_json=str(record["contentJson"]),
|
||||
category_type=str(record["categoryType"]),
|
||||
)
|
||||
for record in records
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def _normalize_records(
|
||||
task_id: str,
|
||||
data: list[TaskReportRecordInput],
|
||||
previous: list[dict[str, Any]] | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Return the exact public record shape and preserve original create times."""
|
||||
existing_by_id = {str(item.get("id")): item for item in previous or [] if item.get("id")}
|
||||
now = datetime.now(UTC).isoformat()
|
||||
normalized: list[dict[str, Any]] = []
|
||||
seen_ids: set[str] = set()
|
||||
|
||||
for item in data:
|
||||
if item.task_id is not None and str(item.task_id) != task_id:
|
||||
raise HTTPException(status_code=422, detail="data.taskId must match the path task id")
|
||||
record_id = item.id or uuid4().hex
|
||||
if record_id in seen_ids:
|
||||
raise HTTPException(status_code=422, detail="data contains duplicate id values")
|
||||
seen_ids.add(record_id)
|
||||
before = existing_by_id.get(record_id, {})
|
||||
normalized.append(
|
||||
{
|
||||
"id": record_id,
|
||||
"taskId": _public_task_id(task_id),
|
||||
# Importing the supplied mock retains its createTime. On later
|
||||
# edits, matching ids retain their original createTime.
|
||||
"createTime": before.get("createTime") or item.create_time or now,
|
||||
"updateTime": now,
|
||||
"sessionId": item.session_id,
|
||||
"contentJson": item.content_json,
|
||||
"categoryType": item.category_type,
|
||||
}
|
||||
)
|
||||
return normalized
|
||||
|
||||
|
||||
def _import_records(payload: Any, task_id: str) -> list[TaskReportRecordInput]:
|
||||
"""Read the supplied mock envelope and bind every record to ``task_id``.
|
||||
|
||||
Detail files are frequently exported from another task. The import page
|
||||
deliberately makes the selected task authoritative, so stale taskId values
|
||||
inside the file cannot write or reject the intended target.
|
||||
"""
|
||||
if isinstance(payload, list):
|
||||
raw_records = payload
|
||||
elif isinstance(payload, dict):
|
||||
raw_records = payload.get("data", payload.get("records"))
|
||||
else:
|
||||
raw_records = None
|
||||
|
||||
if not isinstance(raw_records, list) or not raw_records:
|
||||
raise HTTPException(status_code=422, detail="详情导入文件必须包含非空的 data 或 records 数组")
|
||||
|
||||
records: list[TaskReportRecordInput] = []
|
||||
for index, raw in enumerate(raw_records, start=1):
|
||||
if not isinstance(raw, dict):
|
||||
raise HTTPException(status_code=422, detail=f"第 {index} 条详情数据不是对象")
|
||||
value = dict(raw)
|
||||
content_key = "contentJson" if "contentJson" in value else "content_json"
|
||||
if content_key in value and not isinstance(value[content_key], str):
|
||||
value[content_key] = json.dumps(value[content_key], ensure_ascii=False, separators=(",", ":"))
|
||||
# Prefer the public key so AliasChoices resolves this value consistently.
|
||||
value.pop("task_id", None)
|
||||
value["taskId"] = task_id
|
||||
try:
|
||||
records.append(TaskReportRecordInput.model_validate(value))
|
||||
except Exception as error:
|
||||
raise HTTPException(status_code=422, detail=f"第 {index} 条详情数据格式不正确:{error}") from error
|
||||
return records
|
||||
|
||||
|
||||
@router.get("/by-task/{task_id}", response_model=TaskReportResponse)
|
||||
async def get_task_report(request: Request, task_id: str) -> TaskReportResponse:
|
||||
"""Read a task report. Missing reports use the same envelope with ``data: []``."""
|
||||
row = await _get_store(request).get_by_task(task_id)
|
||||
return _to_response(row.get("records", []) if row is not None else [])
|
||||
|
||||
|
||||
@router.post("/by-task/{task_id}", response_model=TaskReportResponse)
|
||||
async def create_task_report(
|
||||
request: Request,
|
||||
task_id: str,
|
||||
body: TaskReportWriteRequest,
|
||||
) -> TaskReportResponse:
|
||||
"""Save a task's first report document; reject duplicate initial saves."""
|
||||
records = _normalize_records(task_id, body.data)
|
||||
row = await _get_store(request).create_by_task(
|
||||
task_id,
|
||||
records,
|
||||
updated_by=await get_current_user(request),
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=409, detail="Task report already exists; use PUT to modify it")
|
||||
await _mark_task_analysis_completed(request, task_id)
|
||||
return _to_response(row["records"])
|
||||
|
||||
|
||||
@router.put("/by-task/{task_id}", response_model=TaskReportResponse)
|
||||
async def update_task_report(
|
||||
request: Request,
|
||||
task_id: str,
|
||||
body: TaskReportWriteRequest,
|
||||
) -> TaskReportResponse:
|
||||
"""Modify a report by atomically replacing its complete ``data`` array."""
|
||||
store = _get_store(request)
|
||||
current = await store.get_by_task(task_id)
|
||||
if current is None:
|
||||
raise HTTPException(status_code=404, detail="Task report not found; use POST to save it first")
|
||||
records = _normalize_records(task_id, body.data, current.get("records", []))
|
||||
row = await store.update_by_task(
|
||||
task_id,
|
||||
records,
|
||||
updated_by=await get_current_user(request),
|
||||
)
|
||||
if row is None: # defensive: another process could remove it between read and write
|
||||
raise HTTPException(status_code=404, detail="Task report not found")
|
||||
await _mark_task_analysis_completed(request, task_id)
|
||||
return _to_response(row["records"])
|
||||
|
||||
|
||||
@router.post("/import/by-task/{task_id}", response_model=TaskReportResponse)
|
||||
async def import_task_report(
|
||||
request: Request, task_id: str, payload: Any = Body(...)
|
||||
) -> TaskReportResponse:
|
||||
"""Import and replace a selected task's complete report document."""
|
||||
task_id = task_id.strip()
|
||||
if not task_id:
|
||||
raise HTTPException(status_code=422, detail="task id is required")
|
||||
|
||||
task_store = getattr(request.app.state, "taskcop_task_store", None)
|
||||
if task_store is not None and await task_store.get_task(task_id) is None:
|
||||
raise HTTPException(status_code=404, detail="任务不存在,请先导入或创建任务")
|
||||
|
||||
store = _get_store(request)
|
||||
current = await store.get_by_task(task_id)
|
||||
records = _normalize_records(task_id, _import_records(payload, task_id), current.get("records", []) if current else None)
|
||||
if current is None:
|
||||
row = await store.create_by_task(task_id, records, updated_by=await get_current_user(request))
|
||||
else:
|
||||
row = await store.update_by_task(task_id, records, updated_by=await get_current_user(request))
|
||||
|
||||
if row is None: # defensive: a concurrent delete/import may have won the race
|
||||
raise HTTPException(status_code=409, detail="详情导入时发生并发冲突,请重试")
|
||||
await _mark_task_analysis_completed(request, task_id)
|
||||
return _to_response(row["records"])
|
||||
@@ -0,0 +1,336 @@
|
||||
"""Temporary compatibility API for the TaskCOP situation-overview frontend.
|
||||
|
||||
The Vue page at ``/situation-overview/action/sentiment`` currently talks to a
|
||||
legacy Consumer service. These routes retain that service's paths and response
|
||||
envelopes while persisting data in DeerFlow's own database:
|
||||
|
||||
* ``POST /cop/saveSuperiorTask``
|
||||
* ``GET /taskAnalyseSearch/cop-task-three-list``
|
||||
* ``GET /taskAnalyseSearch/cop-my-tasks``
|
||||
* ``GET /taskAnalyseSearch/cop-task-detail``
|
||||
|
||||
The separately deployed legacy UI signs in through DeerFlow before calling
|
||||
these routes. The Gateway authentication middleware therefore supplies the
|
||||
creator identity, which keeps the "my tasks" endpoint correctly scoped.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Annotated, Any
|
||||
|
||||
from fastapi import APIRouter, Body, HTTPException, Query, Request
|
||||
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, ValidationError, field_validator
|
||||
|
||||
from deerflow.runtime.user_context import DEFAULT_USER_ID, get_effective_user_id
|
||||
|
||||
router = APIRouter(tags=["taskcop-compat"])
|
||||
|
||||
|
||||
class SaveSuperiorTaskRequest(BaseModel):
|
||||
"""Payload posted by the legacy ``AddTaskDialog`` component."""
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
issuing_dept: str = Field(default="", validation_alias=AliasChoices("issuingDept", "issuing_dept"))
|
||||
overview: str = Field(min_length=1, max_length=50_000)
|
||||
task_direction: str = Field(
|
||||
default="W方向", validation_alias=AliasChoices("taskDirection", "task_direction"), max_length=64
|
||||
)
|
||||
task_end_date: datetime | None = Field(
|
||||
default=None, validation_alias=AliasChoices("taskEndDate", "task_end_date")
|
||||
)
|
||||
task_level: str = Field(
|
||||
default="1", validation_alias=AliasChoices("taskLevel", "task_level"), max_length=32
|
||||
)
|
||||
task_name: str = Field(
|
||||
min_length=1, max_length=512, validation_alias=AliasChoices("taskName", "task_name")
|
||||
)
|
||||
task_no: str = Field(default="", validation_alias=AliasChoices("taskNo", "task_no"), max_length=255)
|
||||
task_start_date: datetime | None = Field(
|
||||
default=None, validation_alias=AliasChoices("taskStartDate", "task_start_date")
|
||||
)
|
||||
task_status: int = Field(default=0, validation_alias=AliasChoices("taskStatus", "task_status"))
|
||||
task_superior_flag: int = Field(
|
||||
default=0, validation_alias=AliasChoices("taskSuperiorFlag", "task_superior_flag")
|
||||
)
|
||||
task_type: int = Field(default=1, validation_alias=AliasChoices("taskType", "task_type"))
|
||||
parent_task_id: str | None = Field(
|
||||
default=None, validation_alias=AliasChoices("pTaskId", "parent_task_id"), max_length=64
|
||||
)
|
||||
|
||||
|
||||
class TaskImportRecord(BaseModel):
|
||||
"""One task accepted by the JSON import page.
|
||||
|
||||
Both the legacy ``taskName/taskContent`` keys and the internal snake_case
|
||||
keys are accepted, so exported task-list files can be uploaded unchanged.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
id: str = Field(..., validation_alias=AliasChoices("id", "taskId"), max_length=64)
|
||||
task_name: str = Field(
|
||||
..., validation_alias=AliasChoices("taskName", "task_name"), min_length=1, max_length=512
|
||||
)
|
||||
overview: str = Field(
|
||||
default="", validation_alias=AliasChoices("taskContent", "overview"), max_length=50_000
|
||||
)
|
||||
issuing_dept: str = Field(default="", validation_alias=AliasChoices("issuingDept", "issuing_dept"))
|
||||
task_direction: str = Field(
|
||||
default="W方向", validation_alias=AliasChoices("taskDirection", "task_direction"), max_length=64
|
||||
)
|
||||
task_end_date: datetime | None = Field(
|
||||
default=None, validation_alias=AliasChoices("taskEndDate", "task_end_date")
|
||||
)
|
||||
task_level: str = Field(
|
||||
default="1", validation_alias=AliasChoices("taskLevel", "task_level"), max_length=32
|
||||
)
|
||||
task_no: str = Field(default="", validation_alias=AliasChoices("taskNo", "task_no"), max_length=255)
|
||||
task_start_date: datetime | None = Field(
|
||||
default=None, validation_alias=AliasChoices("taskStartDate", "task_start_date")
|
||||
)
|
||||
task_status: int = Field(default=0, validation_alias=AliasChoices("taskStatus", "task_status"))
|
||||
task_superior_flag: int = Field(
|
||||
default=0, validation_alias=AliasChoices("taskSuperiorFlag", "task_superior_flag")
|
||||
)
|
||||
task_type: int = Field(default=1, validation_alias=AliasChoices("taskType", "task_type"))
|
||||
parent_task_id: str | None = Field(
|
||||
default=None, validation_alias=AliasChoices("pTaskId", "parent_task_id"), max_length=64
|
||||
)
|
||||
|
||||
@field_validator("id", "task_name")
|
||||
@classmethod
|
||||
def strip_required_text(cls, value: str) -> str:
|
||||
value = value.strip()
|
||||
if not value:
|
||||
raise ValueError("must not be blank")
|
||||
return value
|
||||
|
||||
|
||||
def _store(request: Request):
|
||||
store = getattr(request.app.state, "taskcop_task_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="TaskCOP task store not available")
|
||||
return store
|
||||
|
||||
|
||||
def _request_user_id(request: Request) -> str:
|
||||
user = getattr(request.state, "user", None)
|
||||
if user is not None:
|
||||
return str(user.id)
|
||||
try:
|
||||
return get_effective_user_id()
|
||||
except RuntimeError:
|
||||
return DEFAULT_USER_ID
|
||||
|
||||
|
||||
def _parse_statuses(task_status: str | None) -> set[int] | None:
|
||||
if not task_status or not task_status.strip():
|
||||
return None
|
||||
try:
|
||||
return {int(value.strip()) for value in task_status.split(",") if value.strip()}
|
||||
except ValueError:
|
||||
# The legacy service treats malformed filters as no filter.
|
||||
return None
|
||||
|
||||
|
||||
def _bounded_page(page_num: int, page_size: int) -> tuple[int, int]:
|
||||
return max(page_num, 1), min(max(page_size, 1), 100)
|
||||
|
||||
|
||||
async def _list_tasks(
|
||||
request: Request,
|
||||
*,
|
||||
mine: bool,
|
||||
content: str,
|
||||
page_num: int,
|
||||
page_size: int,
|
||||
task_status: str | None,
|
||||
task_direction: str | None,
|
||||
start_time: datetime | None,
|
||||
end_time: datetime | None,
|
||||
) -> dict:
|
||||
page_num, page_size = _bounded_page(page_num, page_size)
|
||||
total, records = await _store(request).list_tasks(
|
||||
created_by=_request_user_id(request) if mine else None,
|
||||
keyword=content,
|
||||
task_direction=task_direction,
|
||||
task_statuses=_parse_statuses(task_status),
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
page_num=page_num,
|
||||
page_size=page_size,
|
||||
)
|
||||
return {"state": 200, "msg": "查询成功", "data": {"total": total, "records": records}}
|
||||
|
||||
|
||||
TaskListQuery = Annotated[str, Query(max_length=512)]
|
||||
|
||||
|
||||
@router.post("/cop/saveSuperiorTask", status_code=201)
|
||||
async def save_superior_task(request: Request, body: SaveSuperiorTaskRequest) -> dict:
|
||||
"""Create a task using the legacy ``{state, msg, data}`` response shape."""
|
||||
row = await _store(request).create_task(
|
||||
{
|
||||
**body.model_dump(),
|
||||
"task_name": body.task_name.strip(),
|
||||
"overview": body.overview.strip(),
|
||||
"created_by": _request_user_id(request),
|
||||
}
|
||||
)
|
||||
return {"state": 201, "msg": "创建任务成功", "data": row}
|
||||
|
||||
|
||||
def _import_task_rows(payload: Any) -> list[Any]:
|
||||
"""Accept array, ``{tasks: [...]}``, or legacy list-response envelopes."""
|
||||
if isinstance(payload, list):
|
||||
rows = payload
|
||||
elif isinstance(payload, dict):
|
||||
data = payload.get("tasks", payload.get("records", payload.get("data", payload)))
|
||||
if isinstance(data, dict):
|
||||
data = data.get("records", data.get("tasks", data))
|
||||
rows = data if isinstance(data, list) else [data]
|
||||
else:
|
||||
raise HTTPException(status_code=422, detail="任务导入文件必须是 JSON 数组或包含 tasks/records 的对象")
|
||||
|
||||
if not rows or not all(isinstance(row, dict) for row in rows):
|
||||
raise HTTPException(status_code=422, detail="任务导入文件中至少需要一条任务对象")
|
||||
return rows
|
||||
|
||||
|
||||
@router.post("/api/taskcop/import/tasks")
|
||||
async def import_tasks(request: Request, payload: Any = Body(...)) -> dict:
|
||||
"""Import tasks by id; a duplicate id atomically replaces that task."""
|
||||
rows = _import_task_rows(payload)
|
||||
created = 0
|
||||
updated = 0
|
||||
records: list[dict[str, Any]] = []
|
||||
for index, raw in enumerate(rows, start=1):
|
||||
try:
|
||||
item = TaskImportRecord.model_validate(raw)
|
||||
except ValidationError as error:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail={"message": f"第 {index} 条任务数据格式不正确", "errors": error.errors()},
|
||||
) from error
|
||||
record, inserted = await _store(request).upsert_task(
|
||||
{
|
||||
**item.model_dump(),
|
||||
"task_name": item.task_name,
|
||||
"overview": item.overview,
|
||||
"created_by": _request_user_id(request),
|
||||
}
|
||||
)
|
||||
records.append(record)
|
||||
created += int(inserted)
|
||||
updated += int(not inserted)
|
||||
|
||||
return {
|
||||
"state": 200,
|
||||
"msg": "任务导入成功",
|
||||
"data": {"created": created, "updated": updated, "records": records},
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/cop/tasks/{task_id}")
|
||||
async def delete_task(request: Request, task_id: str) -> dict:
|
||||
"""Delete a TaskCOP task and its shared report document."""
|
||||
task_id = task_id.strip()
|
||||
if not task_id:
|
||||
raise HTTPException(status_code=422, detail="task id is required")
|
||||
|
||||
task_store = _store(request)
|
||||
if await task_store.get_task(task_id) is None:
|
||||
raise HTTPException(status_code=404, detail="task not found")
|
||||
if not await task_store.delete_task(task_id):
|
||||
raise HTTPException(status_code=404, detail="task not found")
|
||||
|
||||
report_store = getattr(request.app.state, "task_report_store", None)
|
||||
if report_store is not None:
|
||||
await report_store.delete_by_task(task_id)
|
||||
return {"state": 200, "msg": "task deleted", "data": {"id": task_id}}
|
||||
|
||||
|
||||
@router.get("/taskAnalyseSearch/cop-task-three-list")
|
||||
async def list_task_three(
|
||||
request: Request,
|
||||
content: TaskListQuery = "",
|
||||
pageNum: int = Query(default=1),
|
||||
pageSize: int = Query(default=10),
|
||||
taskStatus: str | None = Query(default=None),
|
||||
taskDirection: str | None = Query(default=None),
|
||||
startTime: datetime | None = Query(default=None),
|
||||
endTime: datetime | None = Query(default=None),
|
||||
) -> dict:
|
||||
"""List all tasks for the sentiment screen's default "全部" tab."""
|
||||
return await _list_tasks(
|
||||
request,
|
||||
mine=False,
|
||||
content=content,
|
||||
page_num=pageNum,
|
||||
page_size=pageSize,
|
||||
task_status=taskStatus,
|
||||
task_direction=taskDirection,
|
||||
start_time=startTime,
|
||||
end_time=endTime,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/taskAnalyseSearch/cop-my-tasks")
|
||||
async def list_my_tasks(
|
||||
request: Request,
|
||||
content: TaskListQuery = "",
|
||||
pageNum: int = Query(default=1),
|
||||
pageSize: int = Query(default=10),
|
||||
taskStatus: str | None = Query(default=None),
|
||||
taskDirection: str | None = Query(default=None),
|
||||
startTime: datetime | None = Query(default=None),
|
||||
endTime: datetime | None = Query(default=None),
|
||||
) -> dict:
|
||||
"""List the caller's tasks for the sentiment screen's "我的" tab."""
|
||||
return await _list_tasks(
|
||||
request,
|
||||
mine=True,
|
||||
content=content,
|
||||
page_num=pageNum,
|
||||
page_size=pageSize,
|
||||
task_status=taskStatus,
|
||||
task_direction=taskDirection,
|
||||
start_time=startTime,
|
||||
end_time=endTime,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/taskAnalyseSearch/cop-task-list")
|
||||
async def list_task_analysis(
|
||||
request: Request,
|
||||
content: TaskListQuery = "",
|
||||
pageNum: int = Query(default=1),
|
||||
pageSize: int = Query(default=10),
|
||||
taskStatus: str | None = Query(default=None),
|
||||
taskDirection: str | None = Query(default=None),
|
||||
startTime: datetime | None = Query(default=None),
|
||||
endTime: datetime | None = Query(default=None),
|
||||
) -> dict:
|
||||
"""Compatibility alias used by the neighbouring task-analysis screen."""
|
||||
return await _list_tasks(
|
||||
request,
|
||||
mine=False,
|
||||
content=content,
|
||||
page_num=pageNum,
|
||||
page_size=pageSize,
|
||||
task_status=taskStatus,
|
||||
task_direction=taskDirection,
|
||||
start_time=startTime,
|
||||
end_time=endTime,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/taskAnalyseSearch/cop-task-detail")
|
||||
async def get_task_detail(request: Request, taskId: str = Query(min_length=1, max_length=64)) -> dict:
|
||||
"""Return a task selected in the list or opened via a task link."""
|
||||
row = await _store(request).get_task(taskId)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="任务不存在")
|
||||
return {"state": 200, "msg": "查询成功", "data": row}
|
||||
@@ -0,0 +1,147 @@
|
||||
"""Authenticated proxy for WeKnora source-context previews.
|
||||
|
||||
The browser may ask for the source behind a DeerFlow skill citation, but it
|
||||
must never receive the narrow WeKnora publication token. This router reads
|
||||
the same server-side MCP configuration as the retrieval skill and forwards
|
||||
only an authenticated, read-only context request.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
import httpx
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
|
||||
from deerflow.config.extensions_config import get_extensions_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/weknora", tags=["weknora"])
|
||||
|
||||
_MCP_SERVER_NAME = "weknora-retrieval"
|
||||
_DEFAULT_TIMEOUT_SECONDS = 15.0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeKnoraConnection:
|
||||
base_url: str
|
||||
share_token: str
|
||||
timeout_seconds: float
|
||||
|
||||
|
||||
def _read_secret_file(path_value: str) -> str:
|
||||
try:
|
||||
value = Path(path_value).read_text(encoding="utf-8").strip()
|
||||
except OSError as exc:
|
||||
raise RuntimeError("cannot read the WeKnora share-token file") from exc
|
||||
if not value:
|
||||
raise RuntimeError("the WeKnora share-token file is empty")
|
||||
return value
|
||||
|
||||
|
||||
def _connection_from_env(env: dict[str, str]) -> WeKnoraConnection:
|
||||
base_url = env.get("WEKNORA_BASE_URL", "").strip().rstrip("/")
|
||||
token_file = env.get("WEKNORA_DEERFLOW_SHARE_TOKEN_FILE", "").strip()
|
||||
if not base_url:
|
||||
raise RuntimeError("WEKNORA_BASE_URL is not configured")
|
||||
if not token_file:
|
||||
raise RuntimeError("WEKNORA_DEERFLOW_SHARE_TOKEN_FILE is not configured")
|
||||
try:
|
||||
timeout_seconds = float(
|
||||
env.get("WEKNORA_REQUEST_TIMEOUT_SECONDS", _DEFAULT_TIMEOUT_SECONDS),
|
||||
)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise RuntimeError("invalid WeKnora request timeout") from exc
|
||||
if not 0 < timeout_seconds <= 60:
|
||||
raise RuntimeError("invalid WeKnora request timeout")
|
||||
return WeKnoraConnection(
|
||||
base_url=base_url,
|
||||
share_token=_read_secret_file(token_file),
|
||||
timeout_seconds=timeout_seconds,
|
||||
)
|
||||
|
||||
|
||||
def load_weknora_connection() -> WeKnoraConnection:
|
||||
config = get_extensions_config()
|
||||
server = config.mcp_servers.get(_MCP_SERVER_NAME)
|
||||
if server is None or not server.enabled:
|
||||
raise RuntimeError("the WeKnora retrieval integration is not enabled")
|
||||
return _connection_from_env(server.env)
|
||||
|
||||
|
||||
async def fetch_source_context(
|
||||
client: httpx.AsyncClient,
|
||||
connection: WeKnoraConnection,
|
||||
knowledge_base_id: str,
|
||||
chunk_id: str,
|
||||
) -> dict:
|
||||
"""Fetch one context window and return only the WeKnora response data."""
|
||||
path = (
|
||||
"/api/v1/deerflow/knowledge-bases/"
|
||||
f"{quote(knowledge_base_id, safe='')}/chunks/"
|
||||
f"{quote(chunk_id, safe='')}/context"
|
||||
)
|
||||
response = await client.get(
|
||||
f"{connection.base_url}{path}",
|
||||
headers={"X-DeerFlow-Share-Token": connection.share_token},
|
||||
)
|
||||
if response.status_code == 404:
|
||||
raise HTTPException(status_code=404, detail="The original source is no longer available")
|
||||
if response.status_code >= 400:
|
||||
raise RuntimeError(f"WeKnora source context returned HTTP {response.status_code}")
|
||||
try:
|
||||
payload = response.json()
|
||||
except ValueError as exc:
|
||||
raise RuntimeError("WeKnora source context returned invalid JSON") from exc
|
||||
if not isinstance(payload, dict) or payload.get("success") is not True:
|
||||
raise RuntimeError("WeKnora source context was rejected")
|
||||
data = payload.get("data")
|
||||
if not isinstance(data, dict) or not isinstance(data.get("chunks"), list):
|
||||
raise RuntimeError("WeKnora source context returned an invalid payload")
|
||||
return data
|
||||
|
||||
|
||||
@router.get("/source-context", summary="Preview the original context of a WeKnora retrieval hit")
|
||||
async def get_source_context(
|
||||
knowledge_base_id: str = Query(..., min_length=1, max_length=128),
|
||||
chunk_id: str = Query(..., min_length=1, max_length=128),
|
||||
) -> dict:
|
||||
"""Return nearby original chunks for a result cited by the WeKnora skill."""
|
||||
try:
|
||||
connection = load_weknora_connection()
|
||||
except RuntimeError:
|
||||
logger.exception("WeKnora source-context proxy is not configured")
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="Original source preview is temporarily unavailable",
|
||||
) from None
|
||||
|
||||
try:
|
||||
timeout = httpx.Timeout(connection.timeout_seconds)
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
data = await fetch_source_context(
|
||||
client,
|
||||
connection,
|
||||
knowledge_base_id,
|
||||
chunk_id,
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except httpx.HTTPError:
|
||||
logger.warning("WeKnora source-context request failed", exc_info=True)
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail="Original source preview is temporarily unavailable",
|
||||
) from None
|
||||
except RuntimeError:
|
||||
logger.warning("WeKnora source-context response was invalid", exc_info=True)
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail="Original source preview is temporarily unavailable",
|
||||
) from None
|
||||
|
||||
return {"success": True, "data": data}
|
||||
@@ -239,6 +239,7 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
|
||||
"is_writing_setup",
|
||||
"valid_article_types",
|
||||
"writing_setup_quick",
|
||||
"writing_sample_context",
|
||||
"excluded_tools",
|
||||
"skill_stop_names",
|
||||
"skill_stop_message",
|
||||
|
||||
Reference in new issue
Block a user