Merge branch 'ji' into master

合并 ji 的功能:开放问答接口(/api/open/chat[/stream] 回传生成文件 + 临时不留存)、
技能地址管理、智能体详情「最近对话」等。
冲突 frontend StrategyRoutes.tsx 已解决:同时保留 admin/roundtable-diagnostics(master)
与 admin/skill-addresses(ji) 两个管理员路由。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
吉鑫升andClaude Opus 4.8 committed 2026-06-22 19:39:35 +08:00
commit fbec222eb4
38 files changed
+3365 -94

No files matched your search

@@ -306,17 +306,25 @@ def _build_middlewares(
if not cfg.get("is_scheduled_run", False):
middlewares.append(TitleMiddleware(app_config=resolved_app_config))
# Add MemoryMiddleware (after TitleMiddleware) — skip in bootstrap mode (agent creation flow)
# Ephemeral runs (e.g. the open Q&A API /api/open/chat) must NOT touch any
# server-side per-user state: no memory recall/retain, no knowledge-base
# recall/auto-ingest. Those runs use the shared "default" user bucket, so
# persisting their conversations there would both store records the caller
# explicitly asked NOT to keep and leak content across unrelated callers.
# Skip both write-capable middlewares entirely.
is_ephemeral = cfg.get("ephemeral", False)
# Add MemoryMiddleware (after TitleMiddleware) — skip in bootstrap mode (agent creation flow) and ephemeral runs
memory_injection_enabled = cfg.get("memory_injection_enabled", True)
if not is_bootstrap and not is_writing_setup:
if not is_bootstrap and not is_writing_setup and not is_ephemeral:
middlewares.append(MemoryMiddleware(agent_name=agent_name, memory_config=resolved_app_config.memory, injection_enabled=memory_injection_enabled))
# KnowledgeRagMiddleware — inject knowledge-base recall (phase 2). Gated
# per-conversation by the ``knowledge_rag_enabled`` runtime flag, defaulting
# to the global ``knowledge.rag_enabled`` config. No-op when knowledge is
# disabled. Skipped in bootstrap/writing-setup like MemoryMiddleware.
# disabled. Skipped in bootstrap/writing-setup/ephemeral like MemoryMiddleware.
knowledge_cfg = getattr(resolved_app_config, "knowledge", None)
if not is_bootstrap and not is_writing_setup and knowledge_cfg is not None and knowledge_cfg.enabled:
if not is_bootstrap and not is_writing_setup and not is_ephemeral and knowledge_cfg is not None and knowledge_cfg.enabled:
from deerflow.agents.middlewares.knowledge_rag_middleware import KnowledgeRagMiddleware
middlewares.append(KnowledgeRagMiddleware(limit=knowledge_cfg.rag_limit, default_enabled=knowledge_cfg.rag_enabled))
@@ -412,6 +420,11 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
resolved_app_config = app_config
thinking_enabled = cfg.get("thinking_enabled", True)
# Hard "thinking off" switch for internal vLLM/Qwen models whose config only
# declares `supports_thinking: true` (no disable shape) — passes through to
# create_chat_model(force_disable_thinking=...) so enable_thinking=false is
# actually injected. Default off → no behavior change for normal chats.
thinking_force_disabled = bool(cfg.get("thinking_force_disabled", False))
reasoning_effort = cfg.get("reasoning_effort", None)
requested_model_name: str | None = cfg.get("model_name") or cfg.get("model")
is_plan_mode = cfg.get("is_plan_mode", False)
@@ -501,7 +514,7 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
from deerflow.agents.middlewares.writing_setup_round_cap_middleware import WritingSetupRoundCapMiddleware
return create_agent(
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, app_config=resolved_app_config),
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, force_disable_thinking=thinking_force_disabled, app_config=resolved_app_config),
tools=[setup_writing, ask_clarification_tool],
middleware=[
*_build_middlewares(config, model_name=model_name, app_config=resolved_app_config),
@@ -514,7 +527,7 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
if is_bootstrap:
# Special bootstrap agent with minimal prompt for initial custom agent creation flow
return create_agent(
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, app_config=resolved_app_config),
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, force_disable_thinking=thinking_force_disabled, app_config=resolved_app_config),
tools=get_available_tools(
model_name=model_name,
subagent_enabled=subagent_enabled,
@@ -593,7 +606,7 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
)
return create_agent(
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, reasoning_effort=reasoning_effort, app_config=resolved_app_config),
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, reasoning_effort=reasoning_effort, force_disable_thinking=thinking_force_disabled, app_config=resolved_app_config),
tools=default_tools,
middleware=_build_middlewares(config, model_name=model_name, agent_name=agent_id, app_config=resolved_app_config),
system_prompt=system_prompt,
@@ -5,7 +5,8 @@
随时改 ``config.yaml`` 即可(Gateway 有 mtime 热重载,无需重新构建前端)。
测试开关(线上接口未通时用假数据,互相独立):
- ``test`` → 目的树查询(``purpose_detail_url``)走 ``mock.purpose_detail`` 假数据。
- ``test`` → 目的树查询(``purpose_detail_url``)走 ``mock.purpose_detail`` 假数据;
同时 rwfx 进入「3q 是否完成」预检(``selectSqReport``,外部接口不可用)走 ``mock.sq_report`` 假数据。
- ``delete_test`` → 删除接口(``delete_url``)走 ``mock.delete`` 假数据(该接口尚未开发完时打开)。
- ``action_test`` → 行动详情接口(``action_detail_url``)走 ``mock.action_detail`` 假数据。
xdfx 深链传入的是「行动id」,需先调该接口拿 ``data.taskId`` 解析出真正的「任务id」。
@@ -25,7 +26,7 @@ class TaskDeeplinkConfig(BaseModel):
test: bool = Field(
default=False,
description="为真时前端目的树查询走 mock.purpose_detail 假数据(不打真实接口)",
description="为真时前端目的树查询走 mock.purpose_detail 假数据;同时 rwfx 进入「3q 是否完成」预检(selectSqReport)走 mock.sq_report 假数据(都不打真实接口)",
)
delete_test: bool = Field(
default=False,
@@ -61,5 +62,5 @@ class TaskDeeplinkConfig(BaseModel):
)
mock: dict[str, Any] = Field(
default_factory=dict,
description="test/delete_test/action_test 为真时返回的假数据:{purpose_detail: {...}, delete: {message, success}, action_detail: {state, msg, data: {taskId, ...}}}",
description="test/delete_test/action_test 为真时返回的假数据:{purpose_detail: {...}, delete: {message, success}, action_detail: {state, msg, data: {taskId, ...}}, sq_report: {state, msg, data: [...]}}(sq_report 供 rwfx 3q 预检,test 为真时用)",
)
@@ -47,11 +47,22 @@ def _enable_stream_usage_by_default(model_use_path: str, model_settings_from_con
model_settings_from_config["stream_usage"] = True
def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *, app_config: AppConfig | None = None, **kwargs) -> BaseChatModel:
def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *, app_config: AppConfig | None = None, force_disable_thinking: bool = False, **kwargs) -> BaseChatModel:
"""Create a chat model instance from the config.
Args:
name: The name of the model to create. If None, the first model in the config will be used.
thinking_enabled: Turn the model's extended thinking / reasoning on.
force_disable_thinking: Hard override that disables thinking even when the
per-model config declares no ``when_thinking_*`` shape. Many internal
vLLM / Qwen models only set ``supports_thinking: true`` (no disable
shape), so passing ``thinking_enabled=False`` alone injects nothing and
the model keeps reasoning on. When this flag is set (and thinking is
off) we additionally inject both common OpenAI-compatible disable shapes
— vLLM/Qwen ``chat_template_kwargs.enable_thinking=False`` and Zhipu GLM
``thinking.type="disabled"`` — mirroring
``TitleMiddleware._force_disable_thinking``. No effect when thinking is
enabled, and skipped for model classes that don't accept ``extra_body``.
Returns:
A chat model instance.
@@ -120,6 +131,21 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
kwargs.pop("reasoning_effort", None)
model_settings_from_config.pop("reasoning_effort", None)
# Hard override: force-disable thinking regardless of the per-model config shape.
# Injected only for OpenAI-compatible classes (those declaring `extra_body`);
# other providers would reject the kwarg, so they are skipped (they ignore it).
# Three disable shapes are injected so the common OpenAI-compatible gateways are
# all covered: SiliconFlow / DashScope-style **top-level** ``enable_thinking``
# (DeepSeek-V3.1+/Qwen3/GLM hybrid models on api.siliconflow.cn — the knob the
# nested ``chat_template_kwargs`` shape does NOT reach), vLLM/Qwen native
# ``chat_template_kwargs.enable_thinking``, and Zhipu GLM ``thinking.type``.
# Unknown fields are ignored by these gateways, so sending all three is safe.
if force_disable_thinking and not thinking_enabled and "extra_body" in getattr(model_class, "model_fields", {}):
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
{"enable_thinking": False, "chat_template_kwargs": {"enable_thinking": False}, "thinking": {"type": "disabled"}},
)
_enable_stream_usage_by_default(model_config.use, model_settings_from_config)
# For Codex Responses API models: map thinking mode to reasoning_effort
@@ -16,8 +16,11 @@ Resolution rule (per resource type):
- ``scheduled_task`` → empty (no built-in tasks)
- ``skill`` → empty (the 广场 still shows every visible skill;
a position only *adds* its tagged skills to the member's "我的技能")
- ``recommended_question`` → *unrestricted* (handled at query time, never
materialized — see :meth:`resolve_visible`)
- ``recommended_question`` → *untagged-only* (handled at query time, never
materialized — see :meth:`resolve_visible`). The default set is the
questions carrying **no** 标签; tagged questions surface only for a position
that binds their tag. (The recommended-questions router interprets a
``None`` from :meth:`resolve_visible` as "show only untagged".)
Materialization (``sync_user``) only ever touches rows it created itself
(``origin='position'``), so a user's own favorites / subscriptions are never
@@ -67,12 +70,14 @@ class PositionSyncService:
return await self._positions.get_default_position_id()
async def resolve_visible(self, user_id: str, resource_type: str) -> set[str] | None:
"""Return the set of visible ``resource_type`` ids, or ``None`` = unrestricted.
"""Return the set of visible ``resource_type`` ids, or ``None`` = no tag binding.
Used by the recommended-questions filter (the only remaining query-time
visibility type; skills are now *materialized*, not filtered). ``None``
means "no position restriction → show everything"; a set means "restrict
to exactly these ids".
visibility type; skills are now *materialized*, not filtered). A set
means "restrict to exactly these ids". ``None`` means the effective
position binds no tag for this type — the recommended-questions router
treats that as "默认岗位 → show only untagged questions" (see
``app/gateway/routers/recommended_questions.py``).
"""
pid = await self._effective_position_id(user_id)
if not pid:
@@ -0,0 +1,255 @@
"""Scan skill text for URL / IP addresses and apply safe batch replacements.
Pure-logic layer for the 技能地址(URL/IP)批量替换 feature. This module knows
**only about text** — it never walks the filesystem. The HTTP router
(``app/gateway/routers/skill_addresses.py``) walks ``skills/{public,custom}``,
reads each ``*.md`` / ``*.py`` file, and feeds the text here. Keeping the
extraction / replacement logic here (no ``app.*`` import) makes it unit-testable
in isolation and respects the harness/app boundary.
Two address kinds are supported (the feature scope):
* ``"url"`` — full ``http(s)://…`` URLs.
* ``"ip"`` — dotted-quad IPv4, optionally ``:port``. Each octet is range
checked (0–255) so version-number noise like ``1.2.3.400`` is not matched.
The single most important invariant is **substring-safe replacement**: we never
``str.replace`` raw file text (that would rewrite ``1.2.3.4`` *inside*
``11.2.3.40``). Instead we re-detect the exact token spans and only rewrite a
span whose text is *exactly* one of the requested ``from`` values.
"""
from __future__ import annotations
import re
from dataclasses import dataclass
__all__ = [
"VALID_KINDS",
"AddressSpan",
"Occurrence",
"normalize_kinds",
"find_spans",
"scan_occurrences",
"count_matches",
"apply_replacements",
"is_valid_replacement",
"infer_kind",
]
# Supported address kinds (the configured feature scope: URL + IP).
VALID_KINDS: tuple[str, ...] = ("url", "ip")
# A URL body stops at whitespace, quotes, brackets, angle brackets, backticks
# and backslashes — everything that commonly *delimits* a URL in md / py. The
# trailing punctuation that often clings to the end (a sentence period, a
# closing paren already excluded, a Markdown comma) is stripped afterwards.
_URL_RE = re.compile(r"https?://[^\s'\"`<>)\]}\\]+", re.IGNORECASE)
# Dotted-quad IPv4 (optionally :port). Guarded so it is not glued to a longer
# number run (``(?<![\d.])`` / ``(?![\d.])``); octets are range-checked below.
_IP_RE = re.compile(r"(?<![\d.])(\d{1,3}(?:\.\d{1,3}){3})(?::(\d{1,5}))?(?![\d.])")
# Punctuation (ASCII + CJK) that should not be considered part of a URL token
# when it appears at the very end of the match.
_URL_TRAILING = set(".,;:!?'\")]}>`" + ")。,;:!?】》、…")
@dataclass(frozen=True)
class AddressSpan:
"""An address token located at an exact ``[start, end)`` offset in text."""
kind: str
value: str
start: int
end: int
@dataclass(frozen=True)
class Occurrence:
"""A located address enriched with 1-based line/column + the source line."""
kind: str
value: str
line: int
col: int
snippet: str
def normalize_kinds(kinds: object) -> tuple[str, ...]:
"""Coerce arbitrary input into an ordered, de-duped subset of ``VALID_KINDS``.
``None`` / empty means "all kinds". Unknown kinds are dropped.
"""
if kinds is None:
return VALID_KINDS
if isinstance(kinds, str):
raw = [p.strip() for p in kinds.split(",")]
else:
try:
raw = [str(p).strip() for p in kinds] # type: ignore[assignment]
except TypeError:
return VALID_KINDS
seen: list[str] = []
for k in raw:
kl = k.lower()
if kl in VALID_KINDS and kl not in seen:
seen.append(kl)
return tuple(seen) if seen else VALID_KINDS
def _valid_ipv4(quad: str) -> bool:
parts = quad.split(".")
if len(parts) != 4:
return False
for p in parts:
if not p.isdigit() or len(p) > 3:
return False
if int(p) > 255:
return False
return True
def _valid_port(port: str | None) -> bool:
if port is None:
return True
return port.isdigit() and 0 <= int(port) <= 65535
def _url_spans(text: str) -> list[AddressSpan]:
spans: list[AddressSpan] = []
for m in _URL_RE.finditer(text):
start, end = m.start(), m.end()
value = m.group(0)
# Strip clinging trailing punctuation.
while value and value[-1] in _URL_TRAILING:
value = value[:-1]
end -= 1
if value:
spans.append(AddressSpan(kind="url", value=value, start=start, end=end))
return spans
def _ip_spans(text: str, exclude: list[AddressSpan]) -> list[AddressSpan]:
spans: list[AddressSpan] = []
for m in _IP_RE.finditer(text):
quad = m.group(1)
port = m.group(2)
if not _valid_ipv4(quad) or not _valid_port(port):
continue
start, end = m.start(), m.end()
# Skip an IP that lives *inside* a URL (e.g. http://1.2.3.4:8080/x) —
# replacing just the IP there would corrupt the URL.
if any(s.start <= start < s.end for s in exclude):
continue
spans.append(AddressSpan(kind="ip", value=m.group(0), start=start, end=end))
return spans
def find_spans(text: str, kinds: object = None) -> list[AddressSpan]:
"""Return every address token span in *text*, ordered by position.
URL regions are always computed first so that IPs embedded inside a URL are
not separately surfaced, even when only ``"ip"`` is requested.
"""
wanted = normalize_kinds(kinds)
url_spans = _url_spans(text)
result: list[AddressSpan] = []
if "url" in wanted:
result.extend(url_spans)
if "ip" in wanted:
result.extend(_ip_spans(text, exclude=url_spans))
result.sort(key=lambda s: s.start)
return result
def scan_occurrences(text: str, kinds: object = None, *, snippet_max: int = 240) -> list[Occurrence]:
"""Locate addresses with 1-based line/column and a trimmed source line."""
spans = find_spans(text, kinds)
if not spans:
return []
# Precompute line-start offsets for O(log n) line lookup.
line_starts = [0]
for i, ch in enumerate(text):
if ch == "\n":
line_starts.append(i + 1)
occ: list[Occurrence] = []
import bisect
for s in spans:
line_idx = bisect.bisect_right(line_starts, s.start) - 1
line_no = line_idx + 1
col = s.start - line_starts[line_idx] + 1
line_end = line_starts[line_idx + 1] - 1 if line_idx + 1 < len(line_starts) else len(text)
raw_line = text[line_starts[line_idx]:line_end].rstrip("\r")
snippet = raw_line.strip()
if len(snippet) > snippet_max:
snippet = snippet[: snippet_max - 1] + "…"
occ.append(Occurrence(kind=s.kind, value=s.value, line=line_no, col=col, snippet=snippet))
return occ
def count_matches(text: str, mapping: dict[str, str], kinds: object = None) -> int:
"""How many address spans in *text* would be rewritten by *mapping*."""
if not mapping:
return 0
return sum(1 for s in find_spans(text, kinds) if s.value in mapping)
def apply_replacements(text: str, mapping: dict[str, str], kinds: object = None) -> tuple[str, int]:
"""Substring-safe batch replace.
Only spans whose text is *exactly* a key of *mapping* are rewritten, applied
right-to-left so earlier offsets stay valid. Returns ``(new_text, count)``.
A no-op mapping (or no hits) returns the original text and ``0``.
"""
if not mapping:
return text, 0
spans = [s for s in find_spans(text, kinds) if s.value in mapping]
if not spans:
return text, 0
out = text
for s in sorted(spans, key=lambda s: s.start, reverse=True):
out = out[: s.start] + mapping[s.value] + out[s.end:]
return out, len(spans)
def infer_kind(value: str) -> str | None:
"""Return the address kind of *value* if it is a single, whole address.
Used to classify a user-supplied ``from`` value (so the matching ``to`` can
be validated against the right rules). Returns ``None`` when *value* is not
exactly one URL or IP token.
"""
if not value:
return None
spans = find_spans(value, VALID_KINDS)
if len(spans) == 1 and spans[0].value == value:
return spans[0].kind
return None
def is_valid_replacement(kind: str, value: str) -> bool:
"""Validate that a user-supplied replacement is well-formed for its kind.
Keeps an admin from turning a URL into garbage (e.g. dropping the scheme) or
an IP into a non-IP. Empty replacements are rejected.
"""
if not value or value.strip() != value or any(c.isspace() for c in value):
return False
if kind == "url":
return bool(re.fullmatch(r"https?://[^\s'\"`<>]+", value, re.IGNORECASE))
if kind == "ip":
m = _IP_RE.fullmatch(value)
if not m:
return False
return _valid_ipv4(m.group(1)) and _valid_port(m.group(2))
return False