feat(i18n): 全局将「专家」文案改为「智能体」
统一智能体功能的 UI 文案:侧边栏、智能体页、新建/保存流程、 审批卡片、登录提示及后端审批消息中的「专家」均改为「智能体」。 保留 prompt 人设、QA 子应用等非该功能语义的「专家」表述。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
a59d80d7f3
48 files changed
+2191
-110
No files matched your search
+78
@@ -0,0 +1,78 @@
|
||||
"""Middleware that injects the admin-configured prompt prefix at model-call time.
|
||||
|
||||
The prompt-prefix feature lets an admin prepend a fixed instruction to the
|
||||
user's question before it reaches the LLM. To keep the prefix invisible in the
|
||||
chat UI, the Gateway does **not** concatenate it into the persisted
|
||||
``HumanMessage.content``; instead it tags the first human message with
|
||||
``additional_kwargs["prompt_prefix"]`` (see ``app.gateway.services``).
|
||||
|
||||
This middleware reads that tag and prepends the prefix to the message content
|
||||
*only in the request handed to the model*. The persisted state — and therefore
|
||||
what the frontend renders — keeps the clean, prefix-free content.
|
||||
"""
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import override
|
||||
|
||||
from langchain.agents import AgentState
|
||||
from langchain.agents.middleware import AgentMiddleware
|
||||
from langchain.agents.middleware.types import ModelCallResult, ModelRequest, ModelResponse
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
PROMPT_PREFIX_KEY = "prompt_prefix"
|
||||
|
||||
|
||||
class PromptPrefixMiddleware(AgentMiddleware[AgentState]):
|
||||
"""Prepends ``additional_kwargs['prompt_prefix']`` to human messages for the LLM only.
|
||||
|
||||
The transformation is applied to the model request copy, never written back
|
||||
to state, so it is naturally idempotent across the multiple model calls of a
|
||||
single turn.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _apply(messages: list) -> list | None:
|
||||
"""Return a patched copy with prefixes prepended, or None if nothing to do."""
|
||||
patched: list | None = None
|
||||
for idx, msg in enumerate(messages):
|
||||
if not isinstance(msg, HumanMessage):
|
||||
continue
|
||||
raw_prefix = (getattr(msg, "additional_kwargs", None) or {}).get(PROMPT_PREFIX_KEY)
|
||||
prefix = str(raw_prefix).strip() if raw_prefix else ""
|
||||
if not prefix:
|
||||
continue
|
||||
content = msg.content
|
||||
if isinstance(content, str):
|
||||
new_content: object = f"{prefix}\n\n{content}".strip("\n")
|
||||
elif isinstance(content, list):
|
||||
# Structured content (text blocks + media): prepend a text block.
|
||||
new_content = [{"type": "text", "text": prefix}, *content]
|
||||
else:
|
||||
new_content = f"{prefix}\n\n{content!s}"
|
||||
if patched is None:
|
||||
patched = list(messages)
|
||||
# model_copy preserves the message id so streaming dedup is unaffected.
|
||||
patched[idx] = msg.model_copy(update={"content": new_content})
|
||||
return patched
|
||||
|
||||
@override
|
||||
def wrap_model_call(
|
||||
self,
|
||||
request: ModelRequest,
|
||||
handler: Callable[[ModelRequest], ModelResponse],
|
||||
) -> ModelCallResult:
|
||||
patched = self._apply(request.messages)
|
||||
if patched is not None:
|
||||
request = request.override(messages=patched)
|
||||
return handler(request)
|
||||
|
||||
@override
|
||||
async def awrap_model_call(
|
||||
self,
|
||||
request: ModelRequest,
|
||||
handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
|
||||
) -> ModelCallResult:
|
||||
patched = self._apply(request.messages)
|
||||
if patched is not None:
|
||||
request = request.override(messages=patched)
|
||||
return await handler(request)
|
||||
Reference in new issue
Block a user