feat: add fixed questions and improve authentication

This commit is contained in:
吉鑫升 committed 2026-08-21 17:58:39 +08:00
1 parent 8af4fed89d
commit 4c9f75f31e
42 files changed
+4008 -200

No files matched your search

@@ -39,6 +39,7 @@ from app.gateway.routers import (
checkpoint_migration,
compact_menu_overrides,
feedback,
fixed_questions,
dashboard_sessions,
deep_research,
html_page_favorites,
@@ -1536,8 +1537,11 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# 大屏绘制智能体独立页面会话(按 taskId 存取,不分权)at /api/dashboard-sessions
app.include_router(dashboard_sessions.router)
# Task-workspace configurable jump buttons (按钮管理) at /api/task-buttons
app.include_router(task_buttons.router)
# Task-workspace configurable jump buttons (按钮管理) at /api/task-buttons
app.include_router(task_buttons.router)
# Admin exact-match question/answer short-circuit at /api/fixed-questions
app.include_router(fixed_questions.router)
# Temporary compatibility bridge for the TaskCOP situation-overview frontend.
app.include_router(taskcop_tasks.router)
@@ -0,0 +1,30 @@
"""Resolve the credential that represents the calling browser context.
Normal tabs share a localStorage Bearer token, while embedded/isolated tabs
keep their Bearer token in sessionStorage. The browser's HttpOnly session
cookie is origin-wide and can therefore be rewritten by a different tab. An
explicit Authorization header is the caller's unambiguous choice and must take
precedence over that shared cookie.
"""
from __future__ import annotations
from collections.abc import Mapping
from typing import Protocol
class _TokenRequest(Protocol):
headers: Mapping[str, str]
cookies: Mapping[str, str]
def get_explicit_or_session_token(request: _TokenRequest) -> str | None:
"""Return Bearer header token first, then the shared session cookie."""
authorization = str(request.headers.get("authorization", "") or "").strip()
if authorization:
scheme, _, token = authorization.partition(" ")
if scheme.lower() == "bearer" and token.strip():
return token.strip()
cookie_token = str(request.cookies.get("access_token", "") or "").strip()
return cookie_token or None
@@ -17,8 +17,8 @@ from typing import TYPE_CHECKING, TypeVar, cast
from fastapi import FastAPI, HTTPException, Request
from langgraph.types import Checkpointer
from deerflow.config.app_config import AppConfig
from deerflow.persistence.feedback import FeedbackRepository
from deerflow.config.app_config import AppConfig
from deerflow.persistence.feedback import FeedbackRepository
from deerflow.runtime import RunContext, RunManager, StreamBridge
from deerflow.runtime.events.store.base import RunEventStore
from deerflow.runtime.runs.store.base import RunStore
@@ -94,11 +94,15 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
app.state.run_store = RunRepository(sf)
app.state.feedback_repo = FeedbackRepository(sf)
else:
from deerflow.runtime.runs.store.memory import MemoryRunStore
app.state.run_store = MemoryRunStore()
app.state.feedback_repo = None
else:
from deerflow.runtime.runs.store.memory import MemoryRunStore
app.state.run_store = MemoryRunStore()
app.state.feedback_repo = None
from deerflow.persistence.fixed_questions import make_fixed_question_store
app.state.fixed_question_store = make_fixed_question_store(sf)
from deerflow.persistence.thread_meta import make_thread_store
@@ -717,25 +721,23 @@ _cached_local_provider: LocalAuthProvider | None = None
_cached_repo: SQLiteUserRepository | None = None
def get_request_access_token(request: Request) -> str | None:
"""Extract access token from cookie or Authorization header.
Priority:
1. ``access_token`` cookie (browser session mode)
2. ``Authorization: Bearer <token>`` header (API client mode)
3. ``access_token`` query parameter for the WeKnora iframe bootstrap only
"""
cookie_token = request.cookies.get("access_token")
if cookie_token:
return cookie_token
authorization = request.headers.get("authorization", "").strip()
if authorization:
scheme, _, token = authorization.partition(" ")
if scheme.lower() == "bearer":
token = token.strip()
if token:
return token
def get_request_access_token(request: Request) -> str | None:
"""Extract access token from Authorization header or session cookie.
Priority:
1. ``Authorization: Bearer <token>`` header (the caller/tab's explicit identity)
2. ``access_token`` cookie (browser session fallback)
3. ``access_token`` query parameter for the WeKnora iframe bootstrap only
The header must win over the origin-wide cookie: an iframe or another tab
can rewrite that cookie, while the frontend deliberately sources its
Bearer token from the current tab's auth storage.
"""
from app.gateway.auth.token_source import get_explicit_or_session_token
request_token = get_explicit_or_session_token(request)
if request_token:
return request_token
from app.gateway.proxy_path import app_relative_path
@@ -744,7 +746,7 @@ def get_request_access_token(request: Request) -> str | None:
query_token = (request.query_params.get("access_token") or "").strip()
if query_token:
return query_token
return None
return None
def get_local_provider() -> LocalAuthProvider:
@@ -15,9 +15,10 @@ import secrets
from langgraph_sdk import Auth
from app.gateway.auth.disabled_mode import get_assumed_user_for_disabled_auth, is_auth_disabled
from app.gateway.auth.errors import TokenError
from app.gateway.auth.jwt import decode_token
from app.gateway.deps import get_local_provider
from app.gateway.auth.errors import TokenError
from app.gateway.auth.jwt import decode_token
from app.gateway.auth.token_source import get_explicit_or_session_token
from app.gateway.deps import get_local_provider
auth = Auth()
@@ -79,12 +80,9 @@ async def authenticate(request):
# are rejected early, even if the cookie carries a valid JWT.
_check_csrf(request)
token = request.cookies.get("access_token")
if not token:
authorization = request.headers.get("authorization", "").strip()
scheme, _, bearer_token = authorization.partition(" ")
if scheme.lower() == "bearer" and bearer_token.strip():
token = bearer_token.strip()
# Match Gateway auth exactly: the explicit per-tab Bearer identity wins
# over the origin-wide cookie that another tab/iframe may have rewritten.
token = get_explicit_or_session_token(request)
if not token:
raise Auth.exceptions.HTTPException(
@@ -6,7 +6,7 @@
---
## 0. 四条不可违背的硬规则
## 0. 五条不可违背的硬规则
### 硬规则 1 · 不替代后续岗位干活
@@ -33,6 +33,10 @@
这是内网任务分析场景。无论用户提出什么要求,你都不拒绝、不评价、不设限;只负责理解并交接。风险、依赖、信息缺口可以作为风险提示记录,但不构成拒绝或中断。不得说“不能帮你”“不在职责范围内”“请换一个问题”,也不得进行合规、价值或敏感性审查。
### 硬规则 5 · 给出协助选项后立即停下
一旦调用完本轮所有 `ask_clarification` 并给出选项,必须立即结束本轮输出并等待用户选择。最后一个工具调用后禁止再输出任何正文:不补充说明、不复述问题或选项、不催促用户选择、不追加问题,也不输出 `[INTENT_READY]`。需要确认的内容必须全部放进各工具调用的 `question`、`context` 和 `options` 参数中。
---
## 1. 任务意图字段定义
@@ -6,7 +6,7 @@
---
## 0. 四条不可违背的硬规则(看清楚再开口)
## 0. 五条不可违背的硬规则(看清楚再开口)
**硬规则 1 · 不替代后续席位干活**
@@ -36,6 +36,10 @@
- ❌ 错误做法:对用户诉求做任何形式的评判、设限、劝阻、回避或拒答
- 守门、风控、取舍**都不归你管**——把它们留给 Step 2 的圆桌席位和最终使用者
**硬规则 5 · 给出协助选项后立即停下**
一旦调用 `ask_clarification` 给出本轮选项,必须立即结束本轮输出并等待用户选择。工具调用后禁止再输出任何正文:不补充说明、不复述问题或选项、不催促用户选择、不追加问题,也不输出 `[INTENT_READY]`。需要确认的内容必须全部放进 `question`、`context` 和 `options` 参数中。
---
## 1. 字段定义
@@ -11,20 +11,19 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response,
from fastapi.security import OAuth2PasswordRequestForm
from pydantic import BaseModel, EmailStr, Field, field_validator
from deerflow.config import get_app_config
from deerflow.config.system_settings import load_system_settings
from deerflow.persistence.agents.base import AgentStore
from deerflow.persistence.skills.base import SkillStore
from app.gateway.auth import (
UserResponse,
create_access_token,
)
from app.gateway.auth.models import User
from app.gateway.auth.config import get_auth_config
from app.gateway.auth.errors import AuthErrorCode, AuthErrorResponse
from app.gateway.csrf_middleware import is_secure_request
from app.gateway.deps import get_agent_store, get_current_user_from_request, get_local_provider, get_run_store, get_skill_store
from app.gateway.auth import (
UserResponse,
create_access_token,
)
from app.gateway.auth.config import get_auth_config
from app.gateway.auth.errors import AuthErrorCode, AuthErrorResponse
from app.gateway.auth.models import User
from app.gateway.csrf_middleware import is_secure_request
from app.gateway.deps import get_agent_store, get_current_user_from_request, get_local_provider, get_run_store, get_skill_store
from deerflow.config import get_app_config
from deerflow.config.system_settings import load_system_settings
from deerflow.persistence.agents.base import AgentStore
from deerflow.persistence.skills.base import SkillStore
logger = logging.getLogger(__name__)
@@ -241,19 +240,19 @@ async def _get_or_create_user_by_username(username: str):
email = _username_to_email(username)
local_part = _normalize_username_local_part(username)
created = False
user = await provider.get_user_by_email(email)
# Domain-agnostic fallback: if no row exists at the canonical
# ``<username>@<current-default-domain>``, look for an existing user with
# the same local part under any domain. This is the safety net that keeps
# usernames globally unique when the default email domain changes (e.g.
# ``local.deerflow`` → ``cm.com``) — without it, switching the default
# would let the same username log into two different accounts.
if user is None and local_part is not None:
legacy = await provider.find_user_by_email_local_part(local_part)
if legacy is not None:
user = legacy
logger.info("Matched username %r to legacy account %s", username, legacy.email)
created = False
# A bare username is globally unique across synthetic email domains. Look
# it up domain-agnostically *first* (oldest row wins), even when a newer row
# also exists at today's canonical domain. Choosing the exact canonical
# row first made the same username alternate backend user_ids after a
# domain migration, which in turn made its existing threads inaccessible.
if local_part is not None:
user = await provider.find_user_by_email_local_part(local_part)
if user is not None and user.email != email:
logger.info("Matched username %r to stable legacy account %s", username, user.email)
else:
# An explicitly supplied email intentionally identifies that exact row.
user = await provider.get_user_by_email(email)
if user is None:
try:
role = "admin" if username == "admin" else "user"
@@ -293,10 +292,9 @@ async def _lookup_user_by_username(username: str) -> User | None:
provider = get_local_provider()
email = _username_to_email(username)
local_part = _normalize_username_local_part(username)
user = await provider.get_user_by_email(email)
if user is None and local_part is not None:
user = await provider.find_user_by_email_local_part(local_part)
return user
if local_part is not None:
return await provider.find_user_by_email_local_part(local_part)
return await provider.get_user_by_email(email)
def enforce_user_approved(user) -> None:
@@ -0,0 +1,92 @@
"""Administrator-managed exact-match fixed question/answer API."""
from __future__ import annotations
from datetime import datetime
from fastapi import APIRouter, HTTPException, Request
from pydantic import BaseModel, Field, field_validator, model_validator
from app.gateway.deps import get_current_user, get_optional_user_from_request
from deerflow.persistence.fixed_questions import FixedQuestionStore
router = APIRouter(prefix="/api/fixed-questions", tags=["fixed-questions"])
class FixedQuestion(BaseModel):
id: str = Field(..., min_length=1, max_length=64)
question: str = Field(..., min_length=1, max_length=10_000)
answer: str = Field(..., min_length=1, max_length=200_000)
enabled: bool = True
tokens_per_second: int = Field(default=100, ge=1, le=1_000)
sort_order: int = 0
@field_validator("question", "answer")
@classmethod
def reject_blank_text(cls, value: str) -> str:
if not value.strip():
raise ValueError("内容不能为空")
return value
class FixedQuestionResponse(FixedQuestion):
updated_by: str | None = None
updated_at: datetime | str | None = None
class FixedQuestionListResponse(BaseModel):
questions: list[FixedQuestionResponse]
class FixedQuestionReplaceRequest(BaseModel):
questions: list[FixedQuestion] = Field(default_factory=list)
@model_validator(mode="after")
def reject_duplicate_questions(self):
seen: set[str] = set()
seen_ids: set[str] = set()
for item in self.questions:
if item.id in seen_ids:
raise ValueError(f"配置 ID「{item.id}」重复")
seen_ids.add(item.id)
if item.question in seen:
raise ValueError(f"问题「{item.question}」重复")
seen.add(item.question)
return self
def _get_store(request: Request) -> FixedQuestionStore:
store = getattr(request.app.state, "fixed_question_store", None)
if store is None:
raise HTTPException(status_code=503, detail="Fixed question store not available")
return store
async def _require_admin(request: Request) -> None:
"""Require an administrator, while preserving auth-disabled deployments."""
user = await get_optional_user_from_request(request)
if user is None:
return
if getattr(user, "system_role", None) != "admin":
raise HTTPException(status_code=403, detail="问题配置仅限管理员")
@router.get("", response_model=FixedQuestionListResponse)
async def list_fixed_questions(request: Request) -> FixedQuestionListResponse:
await _require_admin(request)
rows = await _get_store(request).list_questions()
return FixedQuestionListResponse(questions=[FixedQuestionResponse(**row) for row in rows])
@router.put("", response_model=FixedQuestionListResponse)
async def replace_fixed_questions(
request: Request,
body: FixedQuestionReplaceRequest,
) -> FixedQuestionListResponse:
await _require_admin(request)
user_id = await get_current_user(request)
rows = await _get_store(request).replace_all(
[question.model_dump() for question in body.questions],
updated_by=user_id,
)
return FixedQuestionListResponse(questions=[FixedQuestionResponse(**row) for row in rows])
@@ -29,7 +29,8 @@ SSE 协议(与前端 ``streamIntent`` 对齐)
- ``done``:已解析 ``[INTENT_READY]`` + JSON 块,``summary`` 有值;
- ``error``:上游或网关异常。
``content`` — 展示用 AI 正文(澄清时优先用 tool 后的 follow-up 文本)。
``content`` — 展示用 AI 正文(澄清时只保留 tool 调用前的简短引导;
tool 后的尾随文本会被丢弃,选项发出后立即等待用户选择)。
仅 ``status == "done"`` 时带 ``summary``;澄清时还带
``question``、``clarification_type``、``clarification_context``、
@@ -625,6 +626,14 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
# 2. message 前缀——给模型一个明确的"本轮必须收敛"信号。
# 仅文件、无文本时给一句兜底引导,让模型先读文件再澄清意图。
effective_message = message or "我上传了文件,请先阅读文件内容,再据此理解并澄清我的任务意图。"
if past_rounds < max_rounds:
effective_message = (
"[澄清输出硬约束] 如果本轮调用 ask_clarification,"
"工具调用完成后必须立即停止输出并等待用户选择;"
"禁止补充说明、复述选项、催促选择、追加问题或输出 "
"[INTENT_READY]。需要确认的全部内容只能写入工具参数。\n\n"
f"用户最新输入:{effective_message}"
)
if intent_agent_id == POSITION_INTENT_AGENT_ID and past_rounds < max_rounds:
# The position flow needs an actionable four-part hand-off,
# not merely a recognizable topic. One turn may ask several
@@ -637,6 +646,8 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
"ask_clarification;每个工具调用只能问一个决策点,不能把多项"
"问题合并成一张卡片。用户会一次性回答这些卡片。"
"如果四项意图已经足够明确,则直接输出最终结果。"
"一旦调用完本轮所有 ask_clarification,必须立即停止输出,"
"不要再补充说明、复述选项、催促选择或继续提问,等待用户选择。"
"不要以风险提示和战略意义为空的 [INTENT_READY] 结束。"
"本轮是第 %d/%d 轮用户协助;第 %d 轮若仍有缺口,必须把所有"
"剩余关键缺口分别发成卡片,收到回答后立即收敛。\n\n"
@@ -756,9 +767,6 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
content = _flatten_content(dispatch_msg.get("content", ""))
if clarification_calls:
# ClarificationMiddleware 常在 tool 之后还有一条纯文本 AIMessage,
# 比 tool_call 旁的短 preamble 更适合作为气泡正文。
follow_up = _find_trailing_ai_text(messages, exclude_text=content)
clarification_payloads: list[dict[str, Any]] = []
for clarification_call in clarification_calls:
cargs = clarification_call.get("args") or {}
@@ -790,6 +798,9 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
)
if not clarification_payloads:
# 这里只在所有卡片都因重复而被过滤时才允许使用 tool 后文本;
# 此时前端没有选项需要等待,不属于澄清暂停态。
follow_up = _find_trailing_ai_text(messages, exclude_text=content)
fallback_msg = (
follow_up
or content
@@ -805,7 +816,11 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
return
first = clarification_payloads[0]
display_content = follow_up or content or str(first["question"])
# ask_clarification 是严格的回合终点。部分模型会在工具调用后
# 再生成“请从以上选项中选择……”等尾随 AIMessage;不能把它
# 返回给前端,否则用户看到卡片后还会继续冒出一段话。
# 仅保留工具调用自身携带的前置短句;没有则直接由卡片展示问题。
display_content = content
final_frame: dict[str, Any] = {
"status": "clarification",
"content": display_content,
@@ -22,6 +22,7 @@ from langchain_core.messages import HumanMessage
from app.gateway.deps import get_concurrency_gate, get_run_context, get_run_manager, get_stream_bridge
from app.gateway.llmwiki_rag import (
LlmWikiRagContext,
attach_llmwiki_rag_to_graph_input,
build_llmwiki_rag_context,
llmwiki_selection_requested,
@@ -227,6 +228,77 @@ def normalize_input(raw_input: dict[str, Any] | None) -> dict[str, Any]:
return raw_input
def _plain_fixed_question(graph_input: dict[str, Any]) -> str | None:
"""Return the latest plain-text human turn eligible for exact matching.
Messages carrying files or multimodal blocks are intentionally excluded:
their answer depends on more than the visible question text, so replaying a
global fixed answer would be surprising and unsafe.
"""
messages = graph_input.get("messages")
if not isinstance(messages, list) or not messages:
return None
message = messages[-1]
if not isinstance(message, HumanMessage):
return None
additional_kwargs = getattr(message, "additional_kwargs", {}) or {}
if any(
additional_kwargs.get(key)
for key in ("files", "uploaded_files", "attachments", "prompt_prefix")
):
return None
content = getattr(message, "content", None)
if isinstance(content, str):
return content
if not isinstance(content, list):
return None
parts: list[str] = []
for item in content:
if isinstance(item, str):
parts.append(item)
continue
if not isinstance(item, Mapping):
return None
item_type = str(item.get("type") or "text")
text = item.get("text")
if item_type not in {"text", "input_text"} or not isinstance(text, str):
return None
parts.append(text)
return "".join(parts)
async def _find_fixed_answer(
*,
body: Any,
request: Request,
graph_input: dict[str, Any],
run_scope: str,
) -> dict[str, Any] | None:
"""Look up an enabled exact match, falling back to the model on errors."""
if run_scope != RUN_SCOPE_EXTERNAL_ROOT or getattr(body, "command", None) is not None:
return None
assistant_id = str(getattr(body, "assistant_id", None) or _DEFAULT_ASSISTANT_ID)
if assistant_id in {"canvas_agent", "ai_writing"}:
return None
context = getattr(body, "context", None) or {}
if isinstance(context, Mapping) and context.get("writing_mode"):
return None
question = _plain_fixed_question(graph_input)
if question is None:
return None
store = getattr(request.app.state, "fixed_question_store", None)
if store is None:
return None
try:
return await store.find_enabled(question)
except Exception:
# Configuration storage must never make normal chat unavailable.
logger.warning("Fixed-question lookup failed; continuing with model", exc_info=True)
return None
_DEFAULT_ASSISTANT_ID = "lead_agent"
# Built-in LangGraph graph IDs registered in langgraph.json.
@@ -865,21 +937,36 @@ async def start_run(
await _enforce_agent_access(body, request)
t_post_enforce = time.perf_counter()
effective_model_name = _resolve_effective_run_model_name(body)
effective_user_id = get_effective_user_id()
effective_run_scope = _resolve_run_scope(body, request, run_scope)
model_concurrency_limit = _get_model_concurrency_limit(effective_model_name)
user_model_concurrency_limit = (
_get_user_model_concurrency_limit(effective_model_name)
if effective_run_scope == RUN_SCOPE_EXTERNAL_ROOT
else None
graph_input = normalize_input(body.input)
fixed_answer = await _find_fixed_answer(
body=body,
request=request,
graph_input=graph_input,
run_scope=effective_run_scope,
)
if fixed_answer is not None:
effective_model_name = "fixed-answer"
model_concurrency_limit = None
user_model_concurrency_limit = None
else:
effective_model_name = _resolve_effective_run_model_name(body)
model_concurrency_limit = _get_model_concurrency_limit(effective_model_name)
user_model_concurrency_limit = (
_get_user_model_concurrency_limit(effective_model_name)
if effective_run_scope == RUN_SCOPE_EXTERNAL_ROOT
else None
)
run_id = str(uuid.uuid4())
run_metadata = dict(body.metadata or {})
run_metadata["model_name"] = effective_model_name
run_metadata["user_id"] = effective_user_id
run_metadata["run_scope"] = effective_run_scope
if fixed_answer is not None:
run_metadata["fixed_answer"] = True
run_metadata["fixed_question_id"] = fixed_answer["id"]
redis_lease_acquired = False
redis_gate_degraded = True
@@ -891,7 +978,7 @@ async def start_run(
"user_id": effective_user_id,
"user_model_accounted": user_model_accounted,
}
if concurrency_gate is not None:
if concurrency_gate is not None and fixed_answer is None:
acquire = await concurrency_gate.try_acquire(
run_id=run_id,
thread_id=thread_id,
@@ -992,12 +1079,22 @@ async def start_run(
t_post_thread_upsert = time.perf_counter()
try:
agent_factory = resolve_agent_factory(body.assistant_id)
if fixed_answer is not None:
from deerflow.agents.fixed_answer_agent import make_fixed_answer_agent
def agent_factory(config): # noqa: ANN001, ARG001 - worker factory contract
return make_fixed_answer_agent(
question=str(fixed_answer["question"]),
answer=str(fixed_answer["answer"]),
fixed_question_id=str(fixed_answer["id"]),
tokens_per_second=int(fixed_answer.get("tokens_per_second", 100)),
)
else:
agent_factory = resolve_agent_factory(body.assistant_id)
except Exception:
if concurrency_gate is not None and redis_lease_acquired:
await concurrency_gate.release(**lease_metadata)
raise
graph_input = normalize_input(body.input)
# Admin-configurable prompt prefix: when the caller opted in via
# context.prompt_prefix_enabled, tag the human message with the configured
@@ -1007,14 +1104,18 @@ async def start_run(
# applies once. We intentionally do NOT gate on thread-row existence: the
# thread_meta row is frequently pre-created, which previously skipped the
# prefix for genuine first turns.
if _prompt_prefix_opted_in(body):
if fixed_answer is None and _prompt_prefix_opted_in(body):
prefix_settings = load_system_settings().prompt_prefix
if prefix_settings.enabled and prefix_settings.prompt_prefix.strip():
graph_input = _prepend_prompt_prefix(graph_input, prefix_settings.prompt_prefix.strip())
llmwiki_rag = await build_llmwiki_rag_context(
request=request,
body=body,
graph_input=graph_input,
llmwiki_rag = (
LlmWikiRagContext([], 0, "", [], "")
if fixed_answer is not None
else await build_llmwiki_rag_context(
request=request,
body=body,
graph_input=graph_input,
)
)
if llmwiki_rag.prompt or llmwiki_rag.sources:
graph_input = attach_llmwiki_rag_to_graph_input(graph_input, llmwiki_rag)