Merge branch 'ji' into master
按钮管理「登录参数」两套实现冲突解决——两分支各自独立给跳转按钮加了登录态追加能力, 二者正交、保留两套,互不影响: - master: login_params (JSON 列 login_params_json,从 userInfo 解析,类似轻应用登录参数) - ji: append_auth + auth_token_param + auth_name_param (token 登录时追加 token1+用户名) 迁移 revision 碰撞修复(两分支都用了 20260625_01):重编号为线性链 20260620_02 → 20260625_01 (login_params) → 20260625_02 (auth_params) → 20260625_03 (users_approved) 并同步 column_additions_mysql.sql 注释里的 migration 编号。 前后端 task_buttons(model/sql/memory/router/前端 type·api·admin 页)均保留两套字段; 两份 CLAUDE.md 文档合并为同时记录两种机制。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
58357547c9
44 files changed
+1207
-46
No files matched your search
+10
-3
@@ -11,6 +11,7 @@ from langgraph.runtime import Runtime
|
||||
from deerflow.agents.thread_state import ThreadDataState
|
||||
from deerflow.config.paths import Paths, get_paths
|
||||
from deerflow.runtime.thread_paths import resolve_path_user_id
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -81,10 +82,15 @@ class ThreadDataMiddleware(AgentMiddleware[ThreadDataMiddlewareState]):
|
||||
@override
|
||||
def before_agent(self, state: ThreadDataMiddlewareState, runtime: Runtime) -> dict | None:
|
||||
context = runtime.context or {}
|
||||
configurable = {}
|
||||
try:
|
||||
config = get_config()
|
||||
configurable = config.get("configurable", {}) or {}
|
||||
except RuntimeError:
|
||||
pass
|
||||
thread_id = context.get("thread_id")
|
||||
if thread_id is None:
|
||||
config = get_config()
|
||||
thread_id = config.get("configurable", {}).get("thread_id")
|
||||
thread_id = configurable.get("thread_id")
|
||||
|
||||
if thread_id is None:
|
||||
raise ValueError("Thread ID is required in runtime context or config.configurable")
|
||||
@@ -93,7 +99,8 @@ class ThreadDataMiddleware(AgentMiddleware[ThreadDataMiddlewareState]):
|
||||
# thread creator so every account that shares the conversation writes to
|
||||
# the same physical user-data directory. Personal threads resolve to the
|
||||
# current login unchanged.
|
||||
user_id = resolve_path_user_id(thread_id)
|
||||
is_ephemeral = bool(context.get("ephemeral") or configurable.get("ephemeral"))
|
||||
user_id = get_effective_user_id() if is_ephemeral else resolve_path_user_id(thread_id)
|
||||
|
||||
if self._lazy_init:
|
||||
# Lazy initialization: only compute paths, don't create directories
|
||||
|
||||
+31
-23
@@ -6,7 +6,6 @@ from typing import TYPE_CHECKING, Any, NotRequired, override
|
||||
|
||||
from langchain.agents import AgentState
|
||||
from langchain.agents.middleware import AgentMiddleware
|
||||
from langgraph.config import get_config
|
||||
from langgraph.runtime import Runtime
|
||||
|
||||
from deerflow.config.title_config import get_title_config
|
||||
@@ -139,13 +138,29 @@ class TitleMiddleware(AgentMiddleware[TitleMiddlewareState]):
|
||||
return text.strip()
|
||||
|
||||
def _parse_title(self, content: object) -> str:
|
||||
"""Normalize model output into a clean title string."""
|
||||
"""Normalize model output into a clean title string (strips <think> blocks)."""
|
||||
config = self._get_title_config()
|
||||
title_content = self._normalize_content(content)
|
||||
title_content = self._strip_think_tags(title_content)
|
||||
title_content = self._strip_think_tags(self._normalize_content(content))
|
||||
title = title_content.strip().strip('"').strip("'")
|
||||
return title[: config.max_chars] if len(title) > config.max_chars else title
|
||||
|
||||
def _isolated_config(self) -> dict[str, Any]:
|
||||
"""Build a RunnableConfig that does NOT inherit the parent's streaming callbacks.
|
||||
|
||||
The leak that pushed title-generation reasoning into the live UI stream came
|
||||
from inheriting the parent RunnableConfig (and with it the SSE/streaming
|
||||
callbacks) for the title model call. Here we deliberately start from an empty
|
||||
config with ``callbacks: []`` so the title model's tokens — including any
|
||||
``<think>`` reasoning from a reasoning-only deployment — never reach the
|
||||
frontend stream or the run journal as conversation output. The stored title
|
||||
is additionally cleaned by ``_parse_title``/``_strip_think_tags``.
|
||||
"""
|
||||
return {
|
||||
"run_name": "title_agent",
|
||||
"tags": ["middleware:title"],
|
||||
"callbacks": [],
|
||||
}
|
||||
|
||||
def _fallback_title(self, user_msg: str) -> str:
|
||||
config = self._get_title_config()
|
||||
fallback_chars = min(config.max_chars, 50)
|
||||
@@ -153,21 +168,6 @@ class TitleMiddleware(AgentMiddleware[TitleMiddlewareState]):
|
||||
return user_msg[:fallback_chars].rstrip() + "..."
|
||||
return user_msg if user_msg else "New Conversation"
|
||||
|
||||
def _get_runnable_config(self) -> dict[str, Any]:
|
||||
"""Inherit the parent RunnableConfig and add middleware tag.
|
||||
|
||||
This ensures RunJournal identifies LLM calls from this middleware
|
||||
as ``middleware:title`` instead of ``lead_agent``.
|
||||
"""
|
||||
try:
|
||||
parent = get_config()
|
||||
except Exception:
|
||||
parent = {}
|
||||
config = {**parent}
|
||||
config["run_name"] = "title_agent"
|
||||
config["tags"] = [*(config.get("tags") or []), "middleware:title"]
|
||||
return config
|
||||
|
||||
def _provisional_title_result(self, state: TitleMiddlewareState) -> dict | None:
|
||||
"""Set an instant placeholder title from the first user message (no LLM)."""
|
||||
if not self._should_set_provisional_title(state):
|
||||
@@ -221,7 +221,15 @@ class TitleMiddleware(AgentMiddleware[TitleMiddlewareState]):
|
||||
logger.debug("Could not override extra_body to disable thinking on title model", exc_info=True)
|
||||
|
||||
async def _agenerate_title_result(self, state: TitleMiddlewareState) -> dict | None:
|
||||
"""Generate a title asynchronously and fall back locally on failure."""
|
||||
"""Polish the title with an LLM, without leaking its reasoning to the UI.
|
||||
|
||||
The title model is invoked with an *isolated* config (``_isolated_config``)
|
||||
that does not inherit the parent's streaming callbacks, so even a
|
||||
reasoning-only deployment's ``<think>`` tokens never reach the live UI
|
||||
stream or the run journal. Thinking is also force-disabled on the model,
|
||||
and the final stored title is stripped of any residual ``<think>`` blocks.
|
||||
On any failure we fall back to the local question-derived title.
|
||||
"""
|
||||
if not self._should_generate_title(state):
|
||||
return None
|
||||
|
||||
@@ -229,7 +237,7 @@ class TitleMiddleware(AgentMiddleware[TitleMiddlewareState]):
|
||||
prompt, user_msg = self._build_title_prompt(state)
|
||||
|
||||
try:
|
||||
model_kwargs = {"thinking_enabled": False}
|
||||
model_kwargs: dict[str, Any] = {"thinking_enabled": False, "force_disable_thinking": True}
|
||||
if self._app_config is not None:
|
||||
model_kwargs["app_config"] = self._app_config
|
||||
if config.model_name:
|
||||
@@ -237,12 +245,12 @@ class TitleMiddleware(AgentMiddleware[TitleMiddlewareState]):
|
||||
else:
|
||||
model = create_chat_model(**model_kwargs)
|
||||
self._force_disable_thinking(model)
|
||||
response = await model.ainvoke(prompt, config=self._get_runnable_config())
|
||||
response = await model.ainvoke(prompt, config=self._isolated_config())
|
||||
title = self._parse_title(response.content)
|
||||
if title:
|
||||
return {"title": title, "title_provisional": False}
|
||||
except Exception:
|
||||
logger.debug("Failed to generate async title; falling back to local title", exc_info=True)
|
||||
logger.debug("Failed to generate async title via LLM; falling back to local title", exc_info=True)
|
||||
return {"title": self._fallback_title(user_msg), "title_provisional": False}
|
||||
|
||||
@override
|
||||
|
||||
+7
@@ -285,6 +285,13 @@ class UploadsMiddleware(AgentMiddleware[UploadsMiddlewareState]):
|
||||
if not isinstance(last_message, HumanMessage):
|
||||
return None
|
||||
|
||||
# Only turns with fresh upload metadata need the uploads directory.
|
||||
# Avoid resolving the thread owner (which may hit the DB) for ordinary
|
||||
# no-file chat turns, including ephemeral open-chat requests.
|
||||
kwargs_files = (last_message.additional_kwargs or {}).get("files")
|
||||
if not isinstance(kwargs_files, list) or not kwargs_files:
|
||||
return None
|
||||
|
||||
# Resolve uploads directory for existence checks
|
||||
thread_id = (runtime.context or {}).get("thread_id")
|
||||
if thread_id is None:
|
||||
|
||||
@@ -74,6 +74,16 @@ class TokenLoginConfig(BaseModel):
|
||||
"certificate. Takes precedence over verify_ssl when set."
|
||||
),
|
||||
)
|
||||
mock_tokens: dict[str, str] = Field(
|
||||
default_factory=dict,
|
||||
description=(
|
||||
"Offline test mapping of token -> username. When an incoming token "
|
||||
"matches a key here, the gateway skips the token_info_url call and "
|
||||
"signs in as the mapped username — useful where the upstream "
|
||||
"getTokenInfo endpoint is unreachable (e.g. external networks). "
|
||||
"Empty in production (no effect). Example: {\"123ewq\": \"admin\"}."
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class AuthLoginConfig(BaseModel):
|
||||
|
||||
@@ -221,10 +221,23 @@ def set_module_tag_ids(
|
||||
return settings.model_copy(update={"modules": modules})
|
||||
|
||||
|
||||
class LoginWhitelistSettings(BaseModel):
|
||||
"""登录白名单总开关(管理员可在「白名单管理」页切换,即时生效、免重启)。
|
||||
|
||||
``enabled`` 关(默认) → 忽略每个用户的 ``approved`` 放行标记,人人可登录;
|
||||
开 → 仅 ``approved=True`` 或 admin 可登录,其余用户注册后处于「待审批」,
|
||||
被管理员放行前一律 403。空/默认即关,故部署后行为零变化。
|
||||
"""
|
||||
|
||||
enabled: bool = Field(default=False, description="是否启用登录白名单;关=所有人可登录,开=仅已放行用户(或 admin)可登录")
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
|
||||
class SystemSettings(BaseModel):
|
||||
"""Top-level system settings document."""
|
||||
|
||||
prompt_prefix: PromptPrefixSettings = Field(default_factory=PromptPrefixSettings)
|
||||
login_whitelist: LoginWhitelistSettings = Field(default_factory=LoginWhitelistSettings)
|
||||
private_square: PrivateSquareSettings = Field(default_factory=PrivateSquareSettings)
|
||||
publish_squares: PublishSquaresSettings = Field(default_factory=PublishSquaresSettings)
|
||||
appearance: AppearanceSettings = Field(default_factory=AppearanceSettings)
|
||||
|
||||
+12
@@ -41,6 +41,18 @@ class TaskDeeplinkConfig(BaseModel):
|
||||
default=False,
|
||||
description="为真时前端「行动→任务」解析走 mock.action_detail 假数据(不打真实接口)",
|
||||
)
|
||||
cop_task_detail_test: bool = Field(
|
||||
default=False,
|
||||
description="为真时开放接口 POST /api/open/3qfx/ask 按 taskId 取任务详情走 mock.cop_task_detail 假数据(上游 consumer 不可达时用)",
|
||||
)
|
||||
cop_task_detail_url: str = Field(
|
||||
default="",
|
||||
description="任务详情接口完整 URL(开放接口 /api/open/3qfx/ask 后端按 taskId 拉取,追加 ?taskId=);与前端 cop-task-detail 同一上游。留空则该开放接口报 503。",
|
||||
)
|
||||
cop_task_detail_authorization: str = Field(
|
||||
default="Bearer admin",
|
||||
description="拉取任务详情时下发的 Authorization 头(与前端 axios 拦截器一致,默认 Bearer admin)",
|
||||
)
|
||||
purpose_detail_url: str = Field(
|
||||
default="",
|
||||
description="目的树查询完整 URL,调用时追加 ?taskId=",
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
"""Add append_auth / auth_token_param / auth_name_param to task_buttons.
|
||||
|
||||
These back the「追加登录态」option on task-workspace jump buttons (按钮管理):
|
||||
a ``url`` button can append the login token (``token``) + username (``username``)
|
||||
to its external link so the target system can identify the current user.
|
||||
|
||||
Revision ID: 20260625_02
|
||||
Revises: 20260625_01
|
||||
Create Date: 2026-06-25
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
|
||||
|
||||
revision = "20260625_02"
|
||||
down_revision = "20260625_01"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _has_table(table_name: str) -> bool:
|
||||
inspector = inspect(op.get_bind())
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def _has_column(table_name: str, column_name: str) -> bool:
|
||||
if not _has_table(table_name):
|
||||
return False
|
||||
inspector = inspect(op.get_bind())
|
||||
return any(col["name"] == column_name for col in inspector.get_columns(table_name))
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ``task_buttons`` may not exist yet on a brand-new DB — create_all handles
|
||||
# that case with the columns already present, so only patch existing tables.
|
||||
if _has_table("task_buttons"):
|
||||
if not _has_column("task_buttons", "append_auth"):
|
||||
op.add_column(
|
||||
"task_buttons",
|
||||
sa.Column("append_auth", sa.Boolean(), nullable=False, server_default="0"),
|
||||
)
|
||||
if not _has_column("task_buttons", "auth_token_param"):
|
||||
op.add_column(
|
||||
"task_buttons",
|
||||
sa.Column("auth_token_param", sa.String(length=64), nullable=True, server_default="token"),
|
||||
)
|
||||
if not _has_column("task_buttons", "auth_name_param"):
|
||||
op.add_column(
|
||||
"task_buttons",
|
||||
sa.Column("auth_name_param", sa.String(length=64), nullable=True, server_default="username"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
for col in ("auth_name_param", "auth_token_param", "append_auth"):
|
||||
if _has_column("task_buttons", col):
|
||||
op.drop_column("task_buttons", col)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
"""Add users.approved (登录白名单放行标记).
|
||||
|
||||
Backs the login-whitelist feature: when system setting ``login_whitelist.enabled``
|
||||
is on, only ``approved=True`` users (or admins) may log in; self-registered
|
||||
regular users default to ``approved=False`` (待审批) until an admin approves them.
|
||||
|
||||
Existing rows are backfilled to TRUE (``server_default="1"``) so flipping the
|
||||
switch on for the first time never locks out users who already had accounts.
|
||||
|
||||
Revision ID: 20260625_03
|
||||
Revises: 20260625_02
|
||||
Create Date: 2026-06-25
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
|
||||
|
||||
revision = "20260625_03"
|
||||
down_revision = "20260625_02"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _has_table(table_name: str) -> bool:
|
||||
inspector = inspect(op.get_bind())
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def _has_column(table_name: str, column_name: str) -> bool:
|
||||
if not _has_table(table_name):
|
||||
return False
|
||||
inspector = inspect(op.get_bind())
|
||||
return any(col["name"] == column_name for col in inspector.get_columns(table_name))
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# A brand-new DB gets the column from create_all; only patch existing tables.
|
||||
if _has_table("users") and not _has_column("users", "approved"):
|
||||
op.add_column(
|
||||
"users",
|
||||
# server_default="1" backfills existing rows to 已放行 (avoid lockout).
|
||||
sa.Column("approved", sa.Boolean(), nullable=False, server_default="1"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _has_column("users", "approved"):
|
||||
op.drop_column("users", "approved")
|
||||
+3
@@ -20,6 +20,9 @@ def _normalize(data: dict[str, Any], updated_by: str | None) -> dict[str, Any]:
|
||||
"id_kind": str(data.get("id_kind") or "task"),
|
||||
"open_mode": str(data.get("open_mode") or "blank"),
|
||||
"login_params": list(data.get("login_params") or []),
|
||||
"append_auth": bool(data.get("append_auth", False)),
|
||||
"auth_token_param": str(data.get("auth_token_param") or "token"),
|
||||
"auth_name_param": str(data.get("auth_name_param") or "username"),
|
||||
"enabled": bool(data.get("enabled", True)),
|
||||
"sort_order": int(data.get("sort_order", 0)),
|
||||
"updated_by": updated_by,
|
||||
|
||||
+9
@@ -53,6 +53,15 @@ class TaskButtonRow(Base):
|
||||
# (``other`` 时取 ``custom_value`` 字面值)。每项 ``{key, source, custom_value}``。
|
||||
# 旧行 / NULL 读时归一化为 ``[]``。
|
||||
login_params_json: Mapped[list] = mapped_column(PortableJSON(), default=list)
|
||||
# ``url`` 跳转是否追加登录态(token 登录的上游 token + 登录用户名),供外链所在的外部系统
|
||||
# 校验当前登录用户。仅 token 登录(前端存有 token1)时实际拼上;非 token 登录自动跳过。
|
||||
# ``server_default`` 必须显式声明:否则在已存在 ``task_buttons`` 表的旧库上,
|
||||
# 启动期自动补列 ``_ensure_orm_columns_sync`` 会跳过「NOT NULL 且无 server_default」
|
||||
# 的列(见 engine.py),导致该列永远补不上 → GET 查询报 Unknown column → 500。
|
||||
append_auth: Mapped[bool] = mapped_column(Boolean, default=False, server_default="0")
|
||||
# 追加 token / 用户名的查询参数名。可空,读时归一化为 ``token`` / ``username``。
|
||||
auth_token_param: Mapped[str | None] = mapped_column(String(64), default="token")
|
||||
auth_name_param: Mapped[str | None] = mapped_column(String(64), default="username")
|
||||
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
sort_order: Mapped[int] = mapped_column(Integer, default=0)
|
||||
updated_by: Mapped[str | None] = mapped_column(String(64))
|
||||
|
||||
+6
@@ -23,6 +23,9 @@ def _to_dict(row: TaskButtonRow) -> dict[str, Any]:
|
||||
data["open_mode"] = data.get("open_mode") or "blank"
|
||||
# JSON 列改名 login_params_json → login_params;旧行 / NULL 归一化为 []。
|
||||
data["login_params"] = data.pop("login_params_json", None) or []
|
||||
data["append_auth"] = bool(data.get("append_auth", False))
|
||||
data["auth_token_param"] = data.get("auth_token_param") or "token"
|
||||
data["auth_name_param"] = data.get("auth_name_param") or "username"
|
||||
return data
|
||||
|
||||
|
||||
@@ -38,6 +41,9 @@ def _row_from(data: dict[str, Any], updated_by: str | None) -> TaskButtonRow:
|
||||
id_kind=str(data.get("id_kind") or "task"),
|
||||
open_mode=str(data.get("open_mode") or "blank"),
|
||||
login_params_json=list(data.get("login_params") or []),
|
||||
append_auth=bool(data.get("append_auth", False)),
|
||||
auth_token_param=str(data.get("auth_token_param") or "token"),
|
||||
auth_name_param=str(data.get("auth_name_param") or "username"),
|
||||
enabled=bool(data.get("enabled", True)),
|
||||
sort_order=int(data.get("sort_order", 0)),
|
||||
updated_by=updated_by,
|
||||
|
||||
@@ -49,6 +49,13 @@ class UserRow(Base):
|
||||
needs_setup: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
||||
token_version: Mapped[int] = mapped_column(nullable=False, default=0)
|
||||
|
||||
# 登录白名单(放行)标记。仅当系统设置 ``login_whitelist.enabled`` 打开时生效:
|
||||
# 关 → 忽略此列,人人可登录;开 → 仅 approved=True(或 admin)可登录,其余
|
||||
# 注册后处于「待审批」直到管理员放行。新注册的普通用户默认 False(待审批),
|
||||
# admin 创建即 True;存量用户由迁移回填 True(避免首次打开开关时把老用户锁死)。
|
||||
# server_default=1 让迁移 ADD COLUMN 时把已存在行置为已放行。
|
||||
approved: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=text("1"))
|
||||
|
||||
# 岗位 (position) the user is assigned to (1:1). NULL = no explicit
|
||||
# position → the user inherits the ``is_default`` position's grants. Drives
|
||||
# the subscription sync engine (see ``deerflow.services.position_sync``).
|
||||
|
||||
@@ -42,6 +42,12 @@ logger = logging.getLogger(__name__)
|
||||
_owner_cache: dict[str, str] = {}
|
||||
_owner_cache_lock = threading.Lock()
|
||||
_OWNER_CACHE_MAX = 4096
|
||||
_LOCAL_ONLY_THREAD_PREFIXES = ("open-", "__skill-review__")
|
||||
|
||||
|
||||
def _is_local_only_thread(thread_id: str | None) -> bool:
|
||||
"""Return True for synthetic threads that are never persisted in threads_meta."""
|
||||
return bool(thread_id) and str(thread_id).startswith(_LOCAL_ONLY_THREAD_PREFIXES)
|
||||
|
||||
|
||||
def _cache_get(thread_id: str) -> str | None:
|
||||
@@ -112,7 +118,7 @@ def resolve_path_user_id(thread_id: str | None) -> str:
|
||||
share the conversation read/write the same physical directory. For every
|
||||
other case it is the current login (``get_effective_user_id``).
|
||||
"""
|
||||
if not thread_id:
|
||||
if not thread_id or _is_local_only_thread(thread_id):
|
||||
return get_effective_user_id()
|
||||
cached = _cache_get(thread_id)
|
||||
if cached is not None:
|
||||
@@ -132,7 +138,7 @@ def resolve_path_user_id(thread_id: str | None) -> str:
|
||||
|
||||
async def aresolve_path_user_id(thread_id: str | None) -> str:
|
||||
"""Async variant of :func:`resolve_path_user_id` for FastAPI handlers."""
|
||||
if not thread_id:
|
||||
if not thread_id or _is_local_only_thread(thread_id):
|
||||
return get_effective_user_id()
|
||||
cached = _cache_get(thread_id)
|
||||
if cached is not None:
|
||||
|
||||
Reference in new issue
Block a user