mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-27 19:25:15 +02:00
perf(subagent): add subagent compile timing log
This commit is contained in:
parent
9bfba34e8e
commit
9e81f2a35b
1 changed files with 43 additions and 8 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import time
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from deepagents.backends.protocol import BackendFactory, BackendProtocol
|
from deepagents.backends.protocol import BackendFactory, BackendProtocol
|
||||||
|
|
@ -15,8 +16,12 @@ from langchain.agents import create_agent
|
||||||
from langchain.chat_models import init_chat_model
|
from langchain.chat_models import init_chat_model
|
||||||
from langgraph.types import Checkpointer
|
from langgraph.types import Checkpointer
|
||||||
|
|
||||||
|
from app.utils.perf import get_perf_logger
|
||||||
|
|
||||||
from .task_tool import build_task_tool_with_parent_config
|
from .task_tool import build_task_tool_with_parent_config
|
||||||
|
|
||||||
|
_perf_log = get_perf_logger()
|
||||||
|
|
||||||
|
|
||||||
class SurfSenseCheckpointedSubAgentMiddleware(SubAgentMiddleware):
|
class SurfSenseCheckpointedSubAgentMiddleware(SubAgentMiddleware):
|
||||||
"""``SubAgentMiddleware`` variant that compiles each subagent against the parent checkpointer."""
|
"""``SubAgentMiddleware`` variant that compiles each subagent against the parent checkpointer."""
|
||||||
|
|
@ -54,8 +59,11 @@ class SurfSenseCheckpointedSubAgentMiddleware(SubAgentMiddleware):
|
||||||
def _surf_compile_subagent_graphs(self) -> list[dict[str, Any]]:
|
def _surf_compile_subagent_graphs(self) -> list[dict[str, Any]]:
|
||||||
"""Mirror of ``SubAgentMiddleware._get_subagents`` that threads the parent checkpointer."""
|
"""Mirror of ``SubAgentMiddleware._get_subagents`` that threads the parent checkpointer."""
|
||||||
specs: list[dict[str, Any]] = []
|
specs: list[dict[str, Any]] = []
|
||||||
|
loop_start = time.perf_counter()
|
||||||
|
timings: list[tuple[str, float, str]] = [] # (name, elapsed, source)
|
||||||
|
|
||||||
for spec in self._subagents:
|
for spec in self._subagents:
|
||||||
|
spec_start = time.perf_counter()
|
||||||
if "runnable" in spec:
|
if "runnable" in spec:
|
||||||
compiled = cast(CompiledSubAgent, spec)
|
compiled = cast(CompiledSubAgent, spec)
|
||||||
specs.append(
|
specs.append(
|
||||||
|
|
@ -65,6 +73,9 @@ class SurfSenseCheckpointedSubAgentMiddleware(SubAgentMiddleware):
|
||||||
"runnable": compiled["runnable"],
|
"runnable": compiled["runnable"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
timings.append(
|
||||||
|
(compiled["name"], time.perf_counter() - spec_start, "precompiled")
|
||||||
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if "model" not in spec:
|
if "model" not in spec:
|
||||||
|
|
@ -79,20 +90,44 @@ class SurfSenseCheckpointedSubAgentMiddleware(SubAgentMiddleware):
|
||||||
model = init_chat_model(model)
|
model = init_chat_model(model)
|
||||||
|
|
||||||
middleware: list[Any] = list(spec.get("middleware", []))
|
middleware: list[Any] = list(spec.get("middleware", []))
|
||||||
|
tools_count = len(spec.get("tools") or [])
|
||||||
|
mw_count = len(middleware)
|
||||||
|
|
||||||
specs.append(
|
compile_start = time.perf_counter()
|
||||||
{
|
runnable = create_agent(
|
||||||
"name": spec["name"],
|
|
||||||
"description": spec["description"],
|
|
||||||
"runnable": create_agent(
|
|
||||||
model,
|
model,
|
||||||
system_prompt=spec["system_prompt"],
|
system_prompt=spec["system_prompt"],
|
||||||
tools=spec["tools"],
|
tools=spec["tools"],
|
||||||
middleware=middleware,
|
middleware=middleware,
|
||||||
name=spec["name"],
|
name=spec["name"],
|
||||||
checkpointer=self._surf_checkpointer,
|
checkpointer=self._surf_checkpointer,
|
||||||
),
|
)
|
||||||
|
compile_elapsed = time.perf_counter() - compile_start
|
||||||
|
specs.append(
|
||||||
|
{
|
||||||
|
"name": spec["name"],
|
||||||
|
"description": spec["description"],
|
||||||
|
"runnable": runnable,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
timings.append(
|
||||||
|
(
|
||||||
|
spec["name"],
|
||||||
|
compile_elapsed,
|
||||||
|
f"compiled tools={tools_count} mw={mw_count}",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
total_elapsed = time.perf_counter() - loop_start
|
||||||
|
per_subagent = ", ".join(
|
||||||
|
f"{name}={elapsed * 1000:.0f}ms[{source}]"
|
||||||
|
for name, elapsed, source in timings
|
||||||
|
)
|
||||||
|
_perf_log.info(
|
||||||
|
"[subagent_compile] total=%.3fs count=%d details=[%s]",
|
||||||
|
total_elapsed,
|
||||||
|
len(timings),
|
||||||
|
per_subagent,
|
||||||
|
)
|
||||||
|
|
||||||
return specs
|
return specs
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue