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

@@ -0,0 +1,186 @@
"""Small local streaming graph used for exact-match fixed answers.
The graph deliberately uses a local ``BaseChatModel`` implementation so
LangGraph emits the same token/message stream events as a normal model-backed
answer, while making no network or model-provider request.
"""
from __future__ import annotations
import asyncio
import codecs
import math
import uuid
from collections.abc import AsyncIterator, Sequence
from typing import Any
import tiktoken
from langchain_core.callbacks import AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import AIMessage, AIMessageChunk, BaseMessage
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
from langchain_core.runnables import RunnableConfig
from langgraph.graph import END, START, StateGraph
from deerflow.agents.thread_state import ThreadState
_TOKEN_ENCODING = tiktoken.get_encoding("cl100k_base")
_TARGET_CHUNKS_PER_SECOND = 10
_MAX_TOKENS_PER_CHUNK = 20
_MAX_TOKENS_PER_SECOND = 1_000
class FixedAnswerChatModel(BaseChatModel):
"""A local streaming model that only replays an administrator answer."""
answer: str
message_id: str
fixed_question_id: str
tokens_per_second: int = 100
@property
def _llm_type(self) -> str:
return "fixed-answer"
@property
def _identifying_params(self) -> dict[str, Any]:
return {
"fixed_question_id": self.fixed_question_id,
"tokens_per_second": self.tokens_per_second,
}
def _message(self) -> AIMessage:
return AIMessage(
content=self.answer,
id=self.message_id,
additional_kwargs={
"fixed_answer": True,
"fixed_question_id": self.fixed_question_id,
"tokens_per_second": self.tokens_per_second,
},
)
def _generate(
self,
messages: list[BaseMessage],
stop: list[str] | None = None,
run_manager: CallbackManagerForLLMRun | None = None,
**kwargs: Any,
) -> ChatResult:
del messages, stop, run_manager, kwargs
return ChatResult(generations=[ChatGeneration(message=self._message())])
async def _astream(
self,
messages: list[BaseMessage],
stop: list[str] | None = None,
run_manager: AsyncCallbackManagerForLLMRun | None = None,
**kwargs: Any,
) -> AsyncIterator[ChatGenerationChunk]:
del messages, stop, run_manager, kwargs
answer = self.answer
if not answer:
yield ChatGenerationChunk(message=AIMessageChunk(content="", id=self.message_id))
return
token_ids = _TOKEN_ENCODING.encode(answer, disallowed_special=())
tokens_per_second = max(
1,
min(_MAX_TOKENS_PER_SECOND, self.tokens_per_second),
)
chunk_size = max(
1,
min(
_MAX_TOKENS_PER_CHUNK,
math.ceil(tokens_per_second / _TARGET_CHUNKS_PER_SECOND),
),
)
decoder = codecs.getincrementaldecoder("utf-8")()
token_groups = [token_ids[:1], *(token_ids[offset : offset + chunk_size] for offset in range(1, len(token_ids), chunk_size))]
for index, chunk_token_ids in enumerate(token_groups):
if index > 0:
await asyncio.sleep(len(chunk_token_ids) / tokens_per_second)
chunk_bytes = b"".join(_TOKEN_ENCODING.decode_single_token_bytes(token_id) for token_id in chunk_token_ids)
is_last = index == len(token_groups) - 1
content = decoder.decode(chunk_bytes, final=is_last)
chunk_kwargs = (
{
"fixed_answer": True,
"fixed_question_id": self.fixed_question_id,
"tokens_per_second": tokens_per_second,
}
if index == 0
else {}
)
yield ChatGenerationChunk(
message=AIMessageChunk(
content=content,
id=self.message_id,
additional_kwargs=chunk_kwargs,
)
)
def _fallback_title(question: str) -> str:
if len(question) > 50:
return question[:50].rstrip() + "..."
return question or "New Conversation"
def make_fixed_answer_agent(
*,
question: str,
answer: str,
fixed_question_id: str,
tokens_per_second: int = 100,
):
"""Build a one-node graph that streams and persists a fixed answer."""
tokens_per_second = max(
1,
min(_MAX_TOKENS_PER_SECOND, int(tokens_per_second)),
)
message_id = f"fixed-answer-{uuid.uuid4()}"
model = FixedAnswerChatModel(
answer=answer,
message_id=message_id,
fixed_question_id=fixed_question_id,
tokens_per_second=tokens_per_second,
).with_config(tags=["fixed-answer"])
async def respond(
state: ThreadState,
config: RunnableConfig,
) -> dict[str, Any]:
messages: Sequence[BaseMessage] = state.get("messages", [])
# Calling the local model through ``astream`` makes the runtime publish
# normal messages-mode chunks. The full AIMessage returned below is the
# durable checkpoint value used on refresh/history reads.
async for _chunk in model.astream(list(messages), config=config):
pass
update: dict[str, Any] = {
"messages": [
AIMessage(
content=answer,
id=message_id,
additional_kwargs={
"fixed_answer": True,
"fixed_question_id": fixed_question_id,
"tokens_per_second": tokens_per_second,
},
)
]
}
if not state.get("title"):
update["title"] = _fallback_title(question)
update["title_provisional"] = False
elif state.get("title_provisional") is True:
# A fixed-answer run must not trigger the worker's background LLM
# title finalizer either; keep the already-visible local title.
update["title_provisional"] = False
return update
graph = StateGraph(ThreadState)
graph.add_node("fixed_answer", respond)
graph.add_edge(START, "fixed_answer")
graph.add_edge("fixed_answer", END)
return graph.compile()
@@ -0,0 +1,22 @@
"""Exact-match fixed-question persistence."""
from __future__ import annotations
from deerflow.persistence.fixed_questions.base import FixedQuestionStore
from deerflow.persistence.fixed_questions.memory import MemoryFixedQuestionStore
from deerflow.persistence.fixed_questions.model import FixedQuestionRow
from deerflow.persistence.fixed_questions.sql import FixedQuestionRepository
__all__ = [
"FixedQuestionRepository",
"FixedQuestionRow",
"FixedQuestionStore",
"MemoryFixedQuestionStore",
"make_fixed_question_store",
]
def make_fixed_question_store(session_factory):
if session_factory is None:
return MemoryFixedQuestionStore()
return FixedQuestionRepository(session_factory)
@@ -0,0 +1,30 @@
"""Abstract persistence API for administrator-managed fixed questions."""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any
class FixedQuestionStore(ABC):
"""Global fixed-question configuration shared by every account."""
@abstractmethod
async def list_questions(self) -> list[dict[str, Any]]:
"""Return every configured question in display order."""
raise NotImplementedError
@abstractmethod
async def find_enabled(self, question: str) -> dict[str, Any] | None:
"""Return the enabled row whose question exactly equals ``question``."""
raise NotImplementedError
@abstractmethod
async def replace_all(
self,
rows: list[dict[str, Any]],
*,
updated_by: str | None = None,
) -> list[dict[str, Any]]:
"""Atomically replace the complete fixed-question configuration."""
raise NotImplementedError
@@ -0,0 +1,56 @@
"""In-memory fixed-question store used by memory deployments and tests."""
from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from deerflow.persistence.fixed_questions.base import FixedQuestionStore
def _normalize(data: dict[str, Any], updated_by: str | None) -> dict[str, Any]:
return {
"id": str(data["id"]),
"question": str(data.get("question") or ""),
"answer": str(data.get("answer") or ""),
"enabled": bool(data.get("enabled", True)),
"tokens_per_second": int(data.get("tokens_per_second", 100)),
"sort_order": int(data.get("sort_order", 0)),
"updated_by": updated_by,
"updated_at": datetime.now(UTC).isoformat(),
}
class MemoryFixedQuestionStore(FixedQuestionStore):
def __init__(self) -> None:
self._rows: dict[str, dict[str, Any]] = {}
async def list_questions(self) -> list[dict[str, Any]]:
rows = sorted(
self._rows.values(),
key=lambda row: (row.get("sort_order", 0), row.get("question", "")),
)
return [dict(row) for row in rows]
async def find_enabled(self, question: str) -> dict[str, Any] | None:
for row in self._rows.values():
if row["enabled"] and row["question"] == question:
return dict(row)
return None
async def replace_all(
self,
rows: list[dict[str, Any]],
*,
updated_by: str | None = None,
) -> list[dict[str, Any]]:
# Last duplicate question wins, mirroring the SQL repository.
by_id: dict[str, dict[str, Any]] = {}
for row in rows:
question = row.get("question")
row_id = row.get("id")
if row_id and question not in (None, ""):
by_id[str(row_id)] = row
by_question = {str(row["question"]): row for row in by_id.values()}
self._rows = {str(row["id"]): _normalize(row, updated_by) for row in by_question.values()}
return await self.list_questions()
@@ -0,0 +1,41 @@
"""ORM model for exact-match, administrator-managed fixed questions."""
from __future__ import annotations
from datetime import UTC, datetime
from sqlalchemy import Boolean, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime, PortableLongText
class FixedQuestionRow(Base):
"""One globally shared question/answer pair.
``question_hash`` provides a portable indexed lookup for questions stored in
``Text``. The repository always compares the original question as well, so a
theoretical SHA-256 collision cannot cause a false match.
"""
__tablename__ = "fixed_questions"
__table_args__ = (UniqueConstraint("question_hash", name="uq_fixed_questions_question_hash"),)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
question: Mapped[str] = mapped_column(Text, nullable=False)
question_hash: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
answer: Mapped[str] = mapped_column(PortableLongText, nullable=False)
enabled: Mapped[bool] = mapped_column(Boolean, default=True, server_default="1")
tokens_per_second: Mapped[int] = mapped_column(
Integer,
default=100,
server_default="100",
)
sort_order: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
updated_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
updated_at: Mapped[datetime] = mapped_column(
BeijingDateTime(),
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)
@@ -0,0 +1,88 @@
"""SQLAlchemy-backed fixed-question store."""
from __future__ import annotations
import hashlib
from datetime import datetime
from typing import Any
from sqlalchemy import delete, select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.fixed_questions.base import FixedQuestionStore
from deerflow.persistence.fixed_questions.model import FixedQuestionRow
def question_hash(question: str) -> str:
return hashlib.sha256(question.encode("utf-8")).hexdigest()
def _to_dict(row: FixedQuestionRow) -> dict[str, Any]:
data = row.to_dict()
data.pop("question_hash", None)
value = data.get("updated_at")
if isinstance(value, datetime):
data["updated_at"] = value.isoformat()
return data
def _row_from(data: dict[str, Any], updated_by: str | None) -> FixedQuestionRow:
question = str(data.get("question") or "")
return FixedQuestionRow(
id=str(data["id"]),
question=question,
question_hash=question_hash(question),
answer=str(data.get("answer") or ""),
enabled=bool(data.get("enabled", True)),
tokens_per_second=int(data.get("tokens_per_second", 100)),
sort_order=int(data.get("sort_order", 0)),
updated_by=updated_by,
)
class FixedQuestionRepository(FixedQuestionStore):
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
async def list_questions(self) -> list[dict[str, Any]]:
stmt = select(FixedQuestionRow).order_by(
FixedQuestionRow.sort_order.asc(),
FixedQuestionRow.question.asc(),
)
async with self._sf() as session:
result = await session.execute(stmt)
return [_to_dict(row) for row in result.scalars()]
async def find_enabled(self, question: str) -> dict[str, Any] | None:
stmt = select(FixedQuestionRow).where(
FixedQuestionRow.question_hash == question_hash(question),
FixedQuestionRow.enabled.is_(True),
)
async with self._sf() as session:
row = (await session.execute(stmt)).scalar_one_or_none()
if row is None or row.question != question:
return None
return _to_dict(row)
async def replace_all(
self,
rows: list[dict[str, Any]],
*,
updated_by: str | None = None,
) -> list[dict[str, Any]]:
# Last duplicate question wins, preventing a unique-hash violation even
# if a non-UI caller submits malformed data.
by_id: dict[str, dict[str, Any]] = {}
for row in rows:
question = row.get("question")
row_id = row.get("id")
if row_id and question not in (None, ""):
by_id[str(row_id)] = row
by_question = {str(row["question"]): row for row in by_id.values()}
async with self._sf() as session:
await session.execute(delete(FixedQuestionRow))
new_rows = [_row_from(row, updated_by) for row in by_question.values()]
session.add_all(new_rows)
await session.commit()
return [_to_dict(row) for row in new_rows]
@@ -0,0 +1,51 @@
"""Create administrator-managed exact-match fixed questions.
Revision ID: 20260821_01
Revises: 20260816_01
Create Date: 2026-08-21
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import mysql
revision = "20260821_01"
down_revision = "20260816_01"
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
if sa.inspect(bind).has_table("fixed_questions"):
return
long_text = sa.Text().with_variant(mysql.LONGTEXT(), "mysql")
op.create_table(
"fixed_questions",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("question", sa.Text(), nullable=False),
sa.Column("question_hash", sa.String(length=64), nullable=False),
sa.Column("answer", long_text, nullable=False),
sa.Column("enabled", sa.Boolean(), nullable=False, server_default=sa.true()),
sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
sa.Column("updated_by", sa.String(length=64), nullable=True),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint(
"question_hash",
name="uq_fixed_questions_question_hash",
),
)
op.create_index(
"ix_fixed_questions_question_hash",
"fixed_questions",
["question_hash"],
unique=False,
)
def downgrade() -> None:
bind = op.get_bind()
if sa.inspect(bind).has_table("fixed_questions"):
op.drop_table("fixed_questions")
@@ -0,0 +1,44 @@
"""Add configurable fixed-answer streaming speed.
Revision ID: 20260821_02
Revises: 20260821_01
Create Date: 2026-08-21
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260821_02"
down_revision = "20260821_01"
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table("fixed_questions"):
return
columns = {column["name"] for column in inspector.get_columns("fixed_questions")}
if "tokens_per_second" not in columns:
op.add_column(
"fixed_questions",
sa.Column(
"tokens_per_second",
sa.Integer(),
nullable=False,
server_default="100",
),
)
def downgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table("fixed_questions"):
return
columns = {column["name"] for column in inspector.get_columns("fixed_questions")}
if "tokens_per_second" in columns:
op.drop_column("fixed_questions", "tokens_per_second")
@@ -47,6 +47,7 @@ from deerflow.persistence.deep_research_sources.model import DeepResearchSourceR
from deerflow.persistence.document_rewrite_versions.model import DocumentRewriteVersionRow
from deerflow.persistence.embed_sessions.model import EmbedSessionRow
from deerflow.persistence.feedback.model import FeedbackRow
from deerflow.persistence.fixed_questions.model import FixedQuestionRow
from deerflow.persistence.intent_turns.model import IntentTurnRow
from deerflow.persistence.light_apps.model import LightAppRow
from deerflow.persistence.llm_metrics.model import LlmCallMetricRow
@@ -97,6 +98,7 @@ from deerflow.persistence.user_prompts.model import UserCustomPromptRow
__all__ = [
"FeedbackRow",
"FixedQuestionRow",
"AgentExtrasRow",
"AgentFavoriteRow",
"AgentRow",