mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-06-10 09:25:57 +00:00
fix(skills): harden slash skill activation across chat channels (#3466)
* support slash skill activation * format slash skill activation * Preserve slash skill activation with uploads * Address slash skill review feedback * Address slash skill follow-up review * Fix lazy slash skill storage resolution * Keep slash skill activation out of system prompt * Address slash skill review issues * fix: harden slash skill command handling * feat(frontend): add slash skill autocomplete * fix: address slash skill review feedback * fix: preserve slash skill text for IM uploads
This commit is contained in:
@@ -49,6 +49,8 @@ from deerflow.tracing import build_tracing_callbacks
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_BOOTSTRAP_SKILL_NAMES = {"bootstrap"}
|
||||
|
||||
|
||||
def _get_runtime_config(config: RunnableConfig) -> dict:
|
||||
"""Merge legacy configurable options with LangGraph runtime context."""
|
||||
@@ -271,6 +273,7 @@ def build_middlewares(
|
||||
agent_name: str | None = None,
|
||||
custom_middlewares: list[AgentMiddleware] | None = None,
|
||||
*,
|
||||
available_skills: set[str] | None = None,
|
||||
app_config: AppConfig | None = None,
|
||||
deferred_setup=None,
|
||||
):
|
||||
@@ -302,6 +305,13 @@ def build_middlewares(
|
||||
|
||||
middlewares.append(DynamicContextMiddleware(agent_name=agent_name, app_config=resolved_app_config))
|
||||
|
||||
# Deterministically load a full SKILL.md when the user starts the turn with
|
||||
# /skill-name. This keeps the base system prompt metadata-only while giving
|
||||
# explicit user activation priority over model-side relevance guessing.
|
||||
from deerflow.agents.middlewares.skill_activation_middleware import SkillActivationMiddleware
|
||||
|
||||
middlewares.append(SkillActivationMiddleware(available_skills=available_skills, app_config=resolved_app_config))
|
||||
|
||||
# Add summarization middleware if enabled
|
||||
summarization_middleware = _create_summarization_middleware(app_config=resolved_app_config)
|
||||
if summarization_middleware is not None:
|
||||
@@ -369,7 +379,7 @@ def build_middlewares(
|
||||
|
||||
def _available_skill_names(agent_config, is_bootstrap: bool) -> set[str] | None:
|
||||
if is_bootstrap:
|
||||
return {"bootstrap"}
|
||||
return set(_BOOTSTRAP_SKILL_NAMES)
|
||||
if agent_config and agent_config.skills is not None:
|
||||
return set(agent_config.skills)
|
||||
return None
|
||||
@@ -475,17 +485,25 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
||||
|
||||
if is_bootstrap:
|
||||
# Special bootstrap agent with minimal prompt for initial custom agent creation flow
|
||||
# Keep the bootstrap skill set intentionally narrow so agent creation
|
||||
# remains deterministic before the custom agent's own config exists.
|
||||
raw_tools = get_available_tools(model_name=model_name, subagent_enabled=subagent_enabled, app_config=resolved_app_config) + [setup_agent]
|
||||
filtered = filter_tools_by_skill_allowed_tools(raw_tools, skills_for_tool_policy)
|
||||
final_tools, setup = assemble_deferred_tools(filtered, enabled=resolved_app_config.tool_search.enabled)
|
||||
return create_agent(
|
||||
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, app_config=resolved_app_config, attach_tracing=False),
|
||||
tools=final_tools,
|
||||
middleware=build_middlewares(config, model_name=model_name, app_config=resolved_app_config, deferred_setup=setup),
|
||||
middleware=build_middlewares(
|
||||
config,
|
||||
model_name=model_name,
|
||||
available_skills=set(_BOOTSTRAP_SKILL_NAMES),
|
||||
app_config=resolved_app_config,
|
||||
deferred_setup=setup,
|
||||
),
|
||||
system_prompt=apply_prompt_template(
|
||||
subagent_enabled=subagent_enabled,
|
||||
max_concurrent_subagents=max_concurrent_subagents,
|
||||
available_skills=set(["bootstrap"]),
|
||||
available_skills=set(_BOOTSTRAP_SKILL_NAMES),
|
||||
app_config=resolved_app_config,
|
||||
deferred_names=setup.deferred_names,
|
||||
),
|
||||
@@ -502,12 +520,19 @@ 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, attach_tracing=False),
|
||||
tools=final_tools,
|
||||
middleware=build_middlewares(config, model_name=model_name, agent_name=agent_name, app_config=resolved_app_config, deferred_setup=setup),
|
||||
middleware=build_middlewares(
|
||||
config,
|
||||
model_name=model_name,
|
||||
agent_name=agent_name,
|
||||
available_skills=available_skills,
|
||||
app_config=resolved_app_config,
|
||||
deferred_setup=setup,
|
||||
),
|
||||
system_prompt=apply_prompt_template(
|
||||
subagent_enabled=subagent_enabled,
|
||||
max_concurrent_subagents=max_concurrent_subagents,
|
||||
agent_name=agent_name,
|
||||
available_skills=set(agent_config.skills) if agent_config and agent_config.skills is not None else None,
|
||||
available_skills=available_skills,
|
||||
app_config=resolved_app_config,
|
||||
deferred_names=setup.deferred_names,
|
||||
),
|
||||
|
||||
@@ -625,6 +625,11 @@ You have access to skills that provide optimized workflows for specific tasks. E
|
||||
4. Load referenced resources only when needed during execution
|
||||
5. Follow the skill's instructions precisely
|
||||
|
||||
**Explicit Slash Skill Activation:**
|
||||
- If the user starts a request with `/<skill-name>`, that skill was explicitly requested for the current turn.
|
||||
- Follow the activated skill before choosing a general workflow.
|
||||
- The runtime injects the activated skill content for explicit slash activations; do not call `read_file` for that SKILL.md again unless the injected skill references supporting resources you need.
|
||||
|
||||
**Skills are located at:** {container_base_path}
|
||||
{skill_evolution_section}
|
||||
{skills_list}
|
||||
|
||||
@@ -0,0 +1,289 @@
|
||||
"""Middleware for explicit slash skill activation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
import html
|
||||
import logging
|
||||
import uuid
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, override
|
||||
|
||||
from langchain.agents.middleware import AgentMiddleware
|
||||
from langchain.agents.middleware.types import ModelRequest, ModelResponse
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
|
||||
from deerflow.skills.slash import parse_slash_skill_reference, resolve_slash_skill
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
from deerflow.skills.storage.skill_storage import SkillStorage
|
||||
from deerflow.skills.types import SKILL_MD_FILE
|
||||
from deerflow.utils.messages import get_original_user_content_text
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from deerflow.config.app_config import AppConfig
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_SLASH_SKILL_ACTIVATION_KEY = "slash_skill_activation"
|
||||
_SLASH_SKILL_ACTIVATION_TARGET_ID_KEY = "slash_skill_activation_target_id"
|
||||
_SUMMARY_MESSAGE_NAME = "summary"
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _Activation:
|
||||
skill_name: str
|
||||
category: str
|
||||
container_file_path: str
|
||||
skill_content: str
|
||||
content_hash: str
|
||||
remaining_text: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _ActivationResolution:
|
||||
activation: _Activation | None = None
|
||||
failure_message: str | None = None
|
||||
|
||||
|
||||
def is_slash_skill_activation_reminder(message: object) -> bool:
|
||||
"""Return whether a message is hidden slash-skill activation context."""
|
||||
return isinstance(message, HumanMessage) and bool(message.additional_kwargs.get(_SLASH_SKILL_ACTIVATION_KEY))
|
||||
|
||||
|
||||
def _is_user_activation_target(message: object) -> bool:
|
||||
if not isinstance(message, HumanMessage):
|
||||
return False
|
||||
if message.name == _SUMMARY_MESSAGE_NAME:
|
||||
return False
|
||||
if message.additional_kwargs.get("hide_from_ui"):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class SkillActivationMiddleware(AgentMiddleware):
|
||||
"""Inject full SKILL.md content when the user explicitly types /skill-name."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
available_skills: set[str] | None = None,
|
||||
app_config: AppConfig | None = None,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self._available_skills = set(available_skills) if available_skills is not None else None
|
||||
self._app_config = app_config
|
||||
|
||||
def _storage(self) -> SkillStorage:
|
||||
if self._app_config is not None:
|
||||
return get_or_new_skill_storage(app_config=self._app_config)
|
||||
return get_or_new_skill_storage()
|
||||
|
||||
@staticmethod
|
||||
def _read_skill_content(skill_file: Path, skills_root: Path) -> str:
|
||||
if skill_file.name != SKILL_MD_FILE:
|
||||
raise ValueError(f"Expected {SKILL_MD_FILE}, got {skill_file.name}")
|
||||
resolved_root = skills_root.resolve()
|
||||
resolved_file = skill_file.resolve()
|
||||
try:
|
||||
resolved_file.relative_to(resolved_root)
|
||||
except ValueError as exc:
|
||||
raise ValueError("Resolved skill file must stay within the configured skills root.") from exc
|
||||
if not resolved_file.is_file():
|
||||
raise FileNotFoundError(resolved_file)
|
||||
return resolved_file.read_text(encoding="utf-8")
|
||||
|
||||
def _resolve_activation(self, text: str) -> _ActivationResolution | None:
|
||||
reference = parse_slash_skill_reference(text)
|
||||
if reference is None:
|
||||
return None
|
||||
|
||||
storage = self._storage()
|
||||
skills = storage.load_skills(enabled_only=False)
|
||||
skill = next((candidate for candidate in skills if candidate.name == reference.name), None)
|
||||
if skill is None:
|
||||
return _ActivationResolution(failure_message=f"Skill `/{reference.name}` is not installed.")
|
||||
if not skill.enabled:
|
||||
return _ActivationResolution(failure_message=f"Skill `/{reference.name}` is installed but disabled. Enable it before using slash activation.")
|
||||
if self._available_skills is not None and reference.name not in self._available_skills:
|
||||
return _ActivationResolution(failure_message=f"Skill `/{reference.name}` is not available for this agent.")
|
||||
|
||||
resolved = resolve_slash_skill(
|
||||
text,
|
||||
skills,
|
||||
available_skills=self._available_skills,
|
||||
container_base_path=storage.get_container_root(),
|
||||
)
|
||||
if resolved is None:
|
||||
return _ActivationResolution(failure_message=f"Skill `/{reference.name}` could not be resolved.")
|
||||
|
||||
try:
|
||||
skill_content = self._read_skill_content(resolved.skill.skill_file, storage.get_skills_root_path())
|
||||
except (OSError, ValueError):
|
||||
logger.exception("Failed to read slash-activated skill %s", resolved.skill.name)
|
||||
return _ActivationResolution(failure_message=f"Skill `/{reference.name}` could not be loaded safely. Please check the skill installation.")
|
||||
|
||||
content_hash = hashlib.sha256(skill_content.encode("utf-8")).hexdigest()
|
||||
return _ActivationResolution(
|
||||
activation=_Activation(
|
||||
skill_name=resolved.skill.name,
|
||||
category=str(resolved.skill.category),
|
||||
container_file_path=resolved.container_file_path,
|
||||
skill_content=skill_content,
|
||||
content_hash=content_hash,
|
||||
remaining_text=resolved.remaining_text,
|
||||
)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _build_activation_reminder(activation: _Activation) -> str:
|
||||
user_request = activation.remaining_text or ("No additional task text was provided after the slash skill command. Ask the user what they want to do with this skill if the next step is unclear.")
|
||||
escaped_user_request = html.escape(user_request, quote=False)
|
||||
escaped_skill_content = html.escape(activation.skill_content, quote=False)
|
||||
escaped_skill_name = html.escape(activation.skill_name, quote=True)
|
||||
escaped_category = html.escape(activation.category, quote=True)
|
||||
escaped_path = html.escape(activation.container_file_path, quote=True)
|
||||
escaped_content_hash = html.escape(activation.content_hash, quote=True)
|
||||
return f"""<slash_skill_activation>
|
||||
The user explicitly activated the `{activation.skill_name}` skill for this turn.
|
||||
Treat the task text as:
|
||||
<user_request>
|
||||
{escaped_user_request}
|
||||
</user_request>
|
||||
|
||||
Follow this skill before choosing a general workflow. Load supporting resources from the same skill directory only when needed.
|
||||
|
||||
<skill name="{escaped_skill_name}" category="{escaped_category}" path="{escaped_path}" sha256="{escaped_content_hash}">
|
||||
<skill_content encoding="xml-escaped">
|
||||
{escaped_skill_content}
|
||||
</skill_content>
|
||||
</skill>
|
||||
</slash_skill_activation>"""
|
||||
|
||||
@staticmethod
|
||||
def _has_existing_activation_for_target(messages: list, target_index: int, target: HumanMessage) -> bool:
|
||||
if target_index <= 0:
|
||||
return False
|
||||
|
||||
if target.id:
|
||||
for previous in messages[:target_index]:
|
||||
if not is_slash_skill_activation_reminder(previous):
|
||||
continue
|
||||
target_id = previous.additional_kwargs.get(_SLASH_SKILL_ACTIVATION_TARGET_ID_KEY)
|
||||
if target_id == target.id or previous.id == f"{target.id}__slash_activation":
|
||||
return True
|
||||
|
||||
previous = messages[target_index - 1]
|
||||
return is_slash_skill_activation_reminder(previous)
|
||||
|
||||
def _find_activation_target(self, messages: list) -> tuple[int, HumanMessage, _ActivationResolution] | None:
|
||||
if not messages:
|
||||
return None
|
||||
|
||||
target_index = next((idx for idx in range(len(messages) - 1, -1, -1) if _is_user_activation_target(messages[idx])), None)
|
||||
if target_index is None:
|
||||
return None
|
||||
|
||||
target = messages[target_index]
|
||||
if target is None:
|
||||
return None
|
||||
if self._has_existing_activation_for_target(messages, target_index, target):
|
||||
return None
|
||||
|
||||
content = get_original_user_content_text(target.content, target.additional_kwargs)
|
||||
resolution = self._resolve_activation(content)
|
||||
if resolution is None:
|
||||
return None
|
||||
return target_index, target, resolution
|
||||
|
||||
@staticmethod
|
||||
def _record_activation(request: ModelRequest, activation: _Activation, *, hook: str) -> None:
|
||||
runtime = getattr(request, "runtime", None)
|
||||
context = getattr(runtime, "context", None)
|
||||
journal = context.get("__run_journal") if isinstance(context, dict) else None
|
||||
if journal is None:
|
||||
return
|
||||
try:
|
||||
journal.record_middleware(
|
||||
"skill_activation",
|
||||
name="SkillActivationMiddleware",
|
||||
hook=hook,
|
||||
action="activate",
|
||||
changes={
|
||||
"skill_name": activation.skill_name,
|
||||
"category": activation.category,
|
||||
"path": activation.container_file_path,
|
||||
"content_hash": activation.content_hash,
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
logger.debug("Failed to record slash skill activation audit event", exc_info=True)
|
||||
|
||||
def _prepare_model_request(self, request: ModelRequest, *, hook: str) -> ModelRequest | AIMessage | None:
|
||||
target_and_resolution = self._find_activation_target(list(request.messages))
|
||||
if target_and_resolution is None:
|
||||
return None
|
||||
|
||||
target_index, target, resolution = target_and_resolution
|
||||
if resolution.failure_message:
|
||||
return AIMessage(content=resolution.failure_message)
|
||||
|
||||
activation = resolution.activation
|
||||
if activation is None:
|
||||
return None
|
||||
|
||||
logger.info(
|
||||
"SkillActivationMiddleware: activating slash skill %s category=%s path=%s hash=%s",
|
||||
activation.skill_name,
|
||||
activation.category,
|
||||
activation.container_file_path,
|
||||
activation.content_hash,
|
||||
)
|
||||
self._record_activation(request, activation, hook=hook)
|
||||
activation_msg = self._make_activation_message(target, self._build_activation_reminder(activation))
|
||||
messages = list(request.messages)
|
||||
messages.insert(target_index, activation_msg)
|
||||
return request.override(messages=messages)
|
||||
|
||||
@staticmethod
|
||||
def _make_activation_message(target: HumanMessage, activation_content: str) -> HumanMessage:
|
||||
stable_id = target.id or str(uuid.uuid4())
|
||||
additional_kwargs = {
|
||||
"hide_from_ui": True,
|
||||
_SLASH_SKILL_ACTIVATION_KEY: True,
|
||||
}
|
||||
if target.id:
|
||||
additional_kwargs[_SLASH_SKILL_ACTIVATION_TARGET_ID_KEY] = target.id
|
||||
return HumanMessage(
|
||||
content=activation_content,
|
||||
id=f"{stable_id}__slash_activation",
|
||||
additional_kwargs=additional_kwargs,
|
||||
)
|
||||
|
||||
@override
|
||||
def wrap_model_call(
|
||||
self,
|
||||
request: ModelRequest,
|
||||
handler: Callable[[ModelRequest], ModelResponse],
|
||||
) -> ModelResponse | AIMessage:
|
||||
prepared = self._prepare_model_request(request, hook="wrap_model_call")
|
||||
if prepared is None:
|
||||
return handler(request)
|
||||
if isinstance(prepared, AIMessage):
|
||||
return prepared
|
||||
return handler(prepared)
|
||||
|
||||
@override
|
||||
async def awrap_model_call(
|
||||
self,
|
||||
request: ModelRequest,
|
||||
handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
|
||||
) -> ModelResponse | AIMessage:
|
||||
prepared = await asyncio.to_thread(self._prepare_model_request, request, hook="awrap_model_call")
|
||||
if prepared is None:
|
||||
return await handler(request)
|
||||
if isinstance(prepared, AIMessage):
|
||||
return prepared
|
||||
return await handler(prepared)
|
||||
@@ -13,6 +13,7 @@ from langgraph.runtime import Runtime
|
||||
from deerflow.config.paths import Paths, get_paths
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
from deerflow.utils.file_conversion import extract_outline
|
||||
from deerflow.utils.messages import ORIGINAL_USER_CONTENT_KEY, message_content_to_text
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -265,6 +266,8 @@ class UploadsMiddleware(AgentMiddleware[UploadsMiddlewareState]):
|
||||
|
||||
# Extract original content - handle both string and list formats
|
||||
original_content = last_message.content
|
||||
additional_kwargs = dict(last_message.additional_kwargs or {})
|
||||
additional_kwargs.setdefault(ORIGINAL_USER_CONTENT_KEY, message_content_to_text(original_content))
|
||||
if isinstance(original_content, str):
|
||||
# Simple case: string content, just prepend files message
|
||||
updated_content = f"{files_message}\n\n{original_content}"
|
||||
@@ -285,7 +288,7 @@ class UploadsMiddleware(AgentMiddleware[UploadsMiddlewareState]):
|
||||
content=updated_content,
|
||||
id=last_message.id,
|
||||
name=last_message.name,
|
||||
additional_kwargs=last_message.additional_kwargs,
|
||||
additional_kwargs=additional_kwargs,
|
||||
)
|
||||
|
||||
messages[last_message_index] = updated_message
|
||||
|
||||
@@ -247,7 +247,15 @@ class DeerFlowClient:
|
||||
# Attaching them again on the model would emit duplicate spans.
|
||||
"model": create_chat_model(name=model_name, thinking_enabled=thinking_enabled, attach_tracing=False),
|
||||
"tools": final_tools,
|
||||
"middleware": build_middlewares(config, model_name=model_name, agent_name=self._agent_name, custom_middlewares=self._middlewares, deferred_setup=deferred_setup),
|
||||
"middleware": build_middlewares(
|
||||
config,
|
||||
model_name=model_name,
|
||||
agent_name=self._agent_name,
|
||||
available_skills=self._available_skills,
|
||||
custom_middlewares=self._middlewares,
|
||||
app_config=self._app_config,
|
||||
deferred_setup=deferred_setup,
|
||||
),
|
||||
"system_prompt": apply_prompt_template(
|
||||
subagent_enabled=subagent_enabled,
|
||||
max_concurrent_subagents=max_concurrent_subagents,
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
|
||||
from deerflow.skills.types import Skill
|
||||
|
||||
RESERVED_SLASH_SKILL_NAMES = frozenset({"bootstrap", "help", "memory", "models", "new", "status"})
|
||||
_SLASH_SKILL_RE = re.compile(r"^/([a-z0-9]+(?:-[a-z0-9]+)*)(?:\s+|$)")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SlashSkillReference:
|
||||
"""Parsed slash-skill command with the skill name and remaining task text."""
|
||||
|
||||
name: str
|
||||
remaining_text: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ResolvedSlashSkill:
|
||||
"""Slash-skill activation resolved against enabled runtime-visible skills."""
|
||||
|
||||
skill: Skill
|
||||
remaining_text: str
|
||||
container_file_path: str
|
||||
|
||||
|
||||
def parse_slash_skill_reference(text: str) -> SlashSkillReference | None:
|
||||
"""Parse strict `/skill-name task` syntax, ignoring reserved control commands."""
|
||||
match = _SLASH_SKILL_RE.match(text)
|
||||
if not match:
|
||||
return None
|
||||
name = match.group(1)
|
||||
if name in RESERVED_SLASH_SKILL_NAMES:
|
||||
return None
|
||||
return SlashSkillReference(
|
||||
name=name,
|
||||
remaining_text=text[match.end() :].lstrip(),
|
||||
)
|
||||
|
||||
|
||||
def resolve_slash_skill(
|
||||
text: str,
|
||||
skills: list[Skill],
|
||||
*,
|
||||
available_skills: set[str] | None = None,
|
||||
container_base_path: str = "/mnt/skills",
|
||||
) -> ResolvedSlashSkill | None:
|
||||
"""Resolve text into an enabled, whitelisted skill activation if possible."""
|
||||
reference = parse_slash_skill_reference(text)
|
||||
if reference is None:
|
||||
return None
|
||||
if available_skills is not None and reference.name not in available_skills:
|
||||
return None
|
||||
|
||||
skill = next((candidate for candidate in skills if candidate.name == reference.name and candidate.enabled), None)
|
||||
if skill is None:
|
||||
return None
|
||||
|
||||
return ResolvedSlashSkill(
|
||||
skill=skill,
|
||||
remaining_text=reference.remaining_text,
|
||||
container_file_path=skill.get_container_file_path(container_base_path),
|
||||
)
|
||||
@@ -0,0 +1,31 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
ORIGINAL_USER_CONTENT_KEY = "original_user_content"
|
||||
|
||||
|
||||
def message_content_to_text(content: Any) -> str:
|
||||
"""Extract text from LangChain message content shapes."""
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
if isinstance(content, list):
|
||||
parts: list[str] = []
|
||||
for item in content:
|
||||
if isinstance(item, str):
|
||||
parts.append(item)
|
||||
elif isinstance(item, dict):
|
||||
text = item.get("text")
|
||||
if isinstance(text, str):
|
||||
parts.append(text)
|
||||
return "\n".join(part for part in parts if part)
|
||||
return str(content)
|
||||
|
||||
|
||||
def get_original_user_content_text(content: Any, additional_kwargs: Mapping[str, Any] | None) -> str:
|
||||
"""Return pre-middleware user text when available, otherwise content text."""
|
||||
original_content = (additional_kwargs or {}).get(ORIGINAL_USER_CONTENT_KEY)
|
||||
if isinstance(original_content, str):
|
||||
return original_content
|
||||
return message_content_to_text(content)
|
||||
Reference in New Issue
Block a user