sync update from LAN repo (source dhk-1 @ 3e87be55)
This commit is contained in:
1 parent
ab6068a7c1
commit
42f2a26fa8
1482 files changed
+227759
-9584
No files matched your search
@@ -19,6 +19,8 @@ from typing import TYPE_CHECKING, Any
|
||||
|
||||
from langchain_core.messages import BaseMessage
|
||||
|
||||
from deerflow.utils.tool_visibility import is_internal_tool_name
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from deerflow.config.app_config import AppConfig
|
||||
|
||||
@@ -35,7 +37,7 @@ def _get_lock(thread_id: str) -> asyncio.Lock:
|
||||
|
||||
|
||||
def extract_tool_call_count(messages: list[BaseMessage]) -> int:
|
||||
"""Count total tool calls across all AI messages in the conversation."""
|
||||
"""Count user-visible tool calls across all AI messages."""
|
||||
count = 0
|
||||
for msg in messages:
|
||||
tool_calls: Any = None
|
||||
@@ -44,22 +46,47 @@ def extract_tool_call_count(messages: list[BaseMessage]) -> int:
|
||||
elif isinstance(msg, dict):
|
||||
tool_calls = msg.get("tool_calls")
|
||||
if tool_calls:
|
||||
count += len(tool_calls)
|
||||
count += sum(
|
||||
not is_internal_tool_name(
|
||||
tc.get("name") if isinstance(tc, dict) else getattr(tc, "name", None)
|
||||
)
|
||||
for tc in tool_calls
|
||||
)
|
||||
return count
|
||||
|
||||
|
||||
def _format_messages_for_review(messages: list[BaseMessage], max_messages: int) -> str:
|
||||
"""Serialize recent messages to a readable text block for the LLM reviewer."""
|
||||
recent = messages[-max_messages:]
|
||||
internal_tool_call_ids: set[str] = set()
|
||||
for msg in messages:
|
||||
for tc in getattr(msg, "tool_calls", None) or []:
|
||||
name = tc.get("name") if isinstance(tc, dict) else getattr(tc, "name", None)
|
||||
tool_call_id = tc.get("id") if isinstance(tc, dict) else getattr(tc, "id", None)
|
||||
if is_internal_tool_name(name) and tool_call_id:
|
||||
internal_tool_call_ids.add(str(tool_call_id))
|
||||
lines: list[str] = []
|
||||
for msg in recent:
|
||||
if is_internal_tool_name(getattr(msg, "name", None)) or str(
|
||||
getattr(msg, "tool_call_id", "") or ""
|
||||
) in internal_tool_call_ids:
|
||||
continue
|
||||
role = type(msg).__name__.replace("Message", "")
|
||||
content = msg.content if isinstance(msg.content, str) else str(msg.content)
|
||||
tool_calls = getattr(msg, "tool_calls", None)
|
||||
if tool_calls:
|
||||
visible_tool_calls = [
|
||||
tc
|
||||
for tc in tool_calls
|
||||
if not is_internal_tool_name(
|
||||
tc.get("name") if isinstance(tc, dict) else getattr(tc, "name", None)
|
||||
)
|
||||
]
|
||||
if not visible_tool_calls:
|
||||
continue
|
||||
calls_str = ", ".join(
|
||||
f"{tc.name if hasattr(tc, 'name') else tc.get('name', '?')}()"
|
||||
for tc in tool_calls
|
||||
for tc in visible_tool_calls
|
||||
)
|
||||
lines.append(f"[{role}] <tool_calls: {calls_str}>")
|
||||
else:
|
||||
@@ -213,7 +240,7 @@ _REVIEW_SYSTEM_PROMPT = textwrap.dedent("""\
|
||||
""")
|
||||
|
||||
|
||||
async def _run_review(thread_id: str, messages: list[BaseMessage], app_config: "AppConfig", user_id: str = "default") -> None:
|
||||
async def _run_review(thread_id: str, messages: list[BaseMessage], app_config: AppConfig, user_id: str = "default") -> None:
|
||||
"""Core review logic: create_agent with full middleware chain, no 'silent creation' constraint.
|
||||
|
||||
Uses create_agent (the deerflow agent factory) directly so we can:
|
||||
@@ -289,7 +316,7 @@ async def _run_review(thread_id: str, messages: list[BaseMessage], app_config: "
|
||||
)
|
||||
|
||||
def _invoke_with_user_context() -> dict:
|
||||
from deerflow.runtime.user_context import set_current_user, reset_current_user
|
||||
from deerflow.runtime.user_context import reset_current_user, set_current_user
|
||||
|
||||
class _SimpleUser:
|
||||
def __init__(self, uid: str) -> None:
|
||||
@@ -328,7 +355,7 @@ async def _run_review(thread_id: str, messages: list[BaseMessage], app_config: "
|
||||
async def _run_review_with_pending(
|
||||
thread_id: str,
|
||||
messages: list[BaseMessage],
|
||||
app_config: "AppConfig",
|
||||
app_config: AppConfig,
|
||||
user_id: str = "default",
|
||||
) -> None:
|
||||
"""Run one review; if a newer snapshot arrived while we ran, drain it."""
|
||||
@@ -344,7 +371,7 @@ async def _run_review_with_pending(
|
||||
def maybe_start_review(
|
||||
thread_id: str,
|
||||
messages: list[BaseMessage],
|
||||
app_config: "AppConfig",
|
||||
app_config: AppConfig,
|
||||
user_id: str = "default",
|
||||
) -> bool:
|
||||
"""Schedule a background review for ``thread_id``.
|
||||
|
||||
Reference in new issue
Block a user