mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-09-05 10:41:58 +03:00
refactor(agent): reduce loop runner parameter plumbing
This commit is contained in:
+26
-41
@@ -76,7 +76,6 @@ from nanobot.session.automation_turns import automation_history_overrides
|
||||
from nanobot.session.goal_state import (
|
||||
goal_state_runtime_lines,
|
||||
runner_wall_llm_timeout_s,
|
||||
sustained_goal_active,
|
||||
)
|
||||
from nanobot.session.history_visibility import HIDDEN_HISTORY_META
|
||||
from nanobot.session.keys import UNIFIED_SESSION_KEY, remember_last_channel
|
||||
@@ -952,12 +951,6 @@ class AgentLoop:
|
||||
*,
|
||||
runtime: LLMRuntime,
|
||||
session: Session | None = None,
|
||||
channel: str = "cli",
|
||||
chat_id: str = "direct",
|
||||
message_id: str | None = None,
|
||||
metadata: dict[str, Any] | None = None,
|
||||
session_key: str | None = None,
|
||||
original_user_text: str | None = None,
|
||||
pending_queue: asyncio.Queue[InboundMessage] | None = None,
|
||||
ephemeral: bool = False,
|
||||
run_extra_hooks_for_ephemeral: bool = False,
|
||||
@@ -1118,23 +1111,25 @@ class AgentLoop:
|
||||
|
||||
return await _drain_pending(limit=limit, first_msg=msg)
|
||||
|
||||
active_session_key = session.key if session else session_key
|
||||
request_ctx = request_context or RequestContext(
|
||||
channel="cli",
|
||||
chat_id="direct",
|
||||
session_key=session.key if session is not None else None,
|
||||
runtime=runtime,
|
||||
)
|
||||
active_session_key = session.key if session else request_ctx.session_key
|
||||
request_metadata = request_ctx.metadata
|
||||
effective_scope = self.workspace_scopes.for_turn(
|
||||
channel=channel,
|
||||
message_metadata=metadata,
|
||||
channel=request_ctx.channel,
|
||||
message_metadata=request_metadata,
|
||||
session_metadata=session.metadata if session is not None else None,
|
||||
)
|
||||
if request_context is None:
|
||||
request_ctx = dataclasses.replace(
|
||||
request_ctx,
|
||||
workspace=effective_scope.project_path,
|
||||
)
|
||||
effective_tools = tools or self.tools
|
||||
request_ctx = request_context or RequestContext(
|
||||
channel=channel,
|
||||
chat_id=chat_id,
|
||||
message_id=message_id,
|
||||
session_key=active_session_key,
|
||||
original_user_text=original_user_text,
|
||||
runtime=runtime,
|
||||
metadata=dict(metadata or {}),
|
||||
workspace=effective_scope.project_path,
|
||||
)
|
||||
file_state_token = bind_file_states(self._file_state_store.for_session(active_session_key))
|
||||
request_token = bind_request_context(request_ctx)
|
||||
workspace_token = bind_workspace_scope(effective_scope)
|
||||
@@ -1159,10 +1154,10 @@ class AgentLoop:
|
||||
on_progress=on_progress,
|
||||
on_stream=on_stream,
|
||||
on_stream_end=on_stream_end,
|
||||
channel=channel,
|
||||
chat_id=chat_id,
|
||||
message_id=message_id,
|
||||
metadata=metadata,
|
||||
channel=request_ctx.channel,
|
||||
chat_id=request_ctx.chat_id,
|
||||
message_id=request_ctx.message_id,
|
||||
metadata=request_metadata,
|
||||
attributes=dict(request_ctx.attributes),
|
||||
session_key=active_session_key,
|
||||
workspace=effective_scope.project_path,
|
||||
@@ -1181,14 +1176,11 @@ class AgentLoop:
|
||||
max_iterations=self.max_iterations,
|
||||
max_tool_result_chars=self.max_tool_result_chars,
|
||||
hook=hook,
|
||||
error_message="Sorry, I encountered an error calling the AI model.",
|
||||
concurrent_tools=True,
|
||||
workspace=effective_scope.project_path,
|
||||
session_key=session.key if session else None,
|
||||
context_block_limit=self.context_block_limit,
|
||||
provider_retry_mode=self.provider_retry_mode,
|
||||
progress_callback=on_progress,
|
||||
stream_progress_deltas=on_stream is not None,
|
||||
retry_wait_callback=on_retry_wait,
|
||||
checkpoint_callback=_checkpoint,
|
||||
injection_callback=_drain_pending,
|
||||
@@ -1197,22 +1189,21 @@ class AgentLoop:
|
||||
# is still capped by NANOBOT_STREAM_IDLE_TIMEOUT_S in streaming providers.
|
||||
llm_timeout_s=runner_wall_llm_timeout_s(
|
||||
self.sessions,
|
||||
session.key if session is not None else session_key,
|
||||
session.key if session is not None else request_ctx.session_key,
|
||||
metadata=session_metadata,
|
||||
message_metadata=metadata,
|
||||
message_metadata=request_metadata,
|
||||
),
|
||||
goal_active_predicate=lambda: sustained_goal_active(session.metadata) if session is not None else False,
|
||||
goal_continue_message=_goal_continue,
|
||||
continuation_callback=_goal_continue,
|
||||
finalize_on_max_iterations=turn_continuation.should_finalize_on_max_iterations(
|
||||
pending_queue_available=pending_queue is not None and session is not None,
|
||||
session_metadata=session_metadata,
|
||||
message_metadata=metadata,
|
||||
message_metadata=request_metadata,
|
||||
),
|
||||
provider_state=provider_state,
|
||||
llm_usage_source=source_from_request(
|
||||
active_session_key,
|
||||
channel=channel,
|
||||
metadata=metadata,
|
||||
channel=request_ctx.channel,
|
||||
metadata=request_metadata,
|
||||
),
|
||||
))
|
||||
finally:
|
||||
@@ -1228,7 +1219,7 @@ class AgentLoop:
|
||||
stop_reason=result.stop_reason,
|
||||
pending_queue_available=pending_queue is not None and session is not None,
|
||||
session_metadata=session_metadata,
|
||||
message_metadata=metadata,
|
||||
message_metadata=request_metadata,
|
||||
)
|
||||
# Push final content through stream so streaming channels (e.g. Feishu)
|
||||
# update the card instead of leaving it empty.
|
||||
@@ -2021,12 +2012,6 @@ class AgentLoop:
|
||||
on_stream_end=ctx.on_stream_end,
|
||||
on_retry_wait=ctx.on_retry_wait,
|
||||
session=ctx.session,
|
||||
channel=ctx.delivery.route.channel,
|
||||
chat_id=ctx.delivery.route.chat_id,
|
||||
message_id=ctx.msg.metadata.get("message_id"),
|
||||
metadata=ctx.msg.metadata,
|
||||
session_key=ctx.session_key,
|
||||
original_user_text=ctx.original_user_text,
|
||||
pending_queue=ctx.pending_queue,
|
||||
ephemeral=ctx.ephemeral,
|
||||
run_extra_hooks_for_ephemeral=ctx.run_extra_hooks_for_ephemeral,
|
||||
|
||||
+22
-22
@@ -60,14 +60,13 @@ from nanobot.utils.runtime import (
|
||||
EMPTY_FINAL_RESPONSE_MESSAGE,
|
||||
build_budget_exhausted_finalization_message,
|
||||
build_finalization_retry_message,
|
||||
build_goal_continue_message,
|
||||
build_length_recovery_message,
|
||||
is_blank_text,
|
||||
repeated_external_lookup_error,
|
||||
repeated_workspace_violation_error,
|
||||
)
|
||||
|
||||
GoalContinueMessage = str | Callable[[], str | None]
|
||||
ContinuationCallback = Callable[[], str | None]
|
||||
ProgressCallback = Callable[[str], Awaitable[None]]
|
||||
RetryWaitCallback = Callable[[str], Awaitable[None]]
|
||||
CheckpointCallback = Callable[[dict[str, Any]], Awaitable[None]]
|
||||
@@ -114,14 +113,12 @@ class AgentRunSpec:
|
||||
context_block_limit: int | None = None
|
||||
provider_retry_mode: str = "standard"
|
||||
progress_callback: ProgressCallback | None = None
|
||||
stream_progress_deltas: bool = True
|
||||
retry_wait_callback: RetryWaitCallback | None = None
|
||||
checkpoint_callback: CheckpointCallback | None = None
|
||||
injection_callback: InjectionCallback | None = None
|
||||
terminal_injection_callback: InjectionCallback | None = None
|
||||
llm_timeout_s: float | None = None
|
||||
goal_active_predicate: Callable[[], bool] | None = None
|
||||
goal_continue_message: GoalContinueMessage | None = None
|
||||
continuation_callback: ContinuationCallback | None = None
|
||||
finalize_on_max_iterations: bool = True
|
||||
provider_state: ProviderConversationState | None = None
|
||||
llm_usage_source: LLMUsageSource | None = None
|
||||
@@ -274,7 +271,7 @@ class AgentRunner:
|
||||
conversation_state: ProviderConversationStateController | None = None,
|
||||
phase: str = "after error",
|
||||
iteration: int | None = None,
|
||||
allow_goal_continue: bool = False,
|
||||
allow_continuation: bool = False,
|
||||
wait_at_terminal: bool = False,
|
||||
) -> tuple[bool, int]:
|
||||
"""Drain pending injections. Returns (should_continue, updated_cycles).
|
||||
@@ -289,10 +286,10 @@ class AgentRunner:
|
||||
if injection_cycles < _MAX_INJECTION_CYCLES:
|
||||
injections = await self._drain_injections(spec)
|
||||
real_injection = bool(injections)
|
||||
if not injections and allow_goal_continue and assistant_message is not None:
|
||||
predicate = spec.goal_active_predicate
|
||||
if predicate is not None and predicate():
|
||||
injections = [self._build_goal_continue_message(spec)]
|
||||
if not injections and allow_continuation and assistant_message is not None:
|
||||
continuation = self._build_continuation_message(spec)
|
||||
if continuation is not None:
|
||||
injections = [continuation]
|
||||
if (
|
||||
not injections
|
||||
and wait_at_terminal
|
||||
@@ -330,18 +327,22 @@ class AgentRunner:
|
||||
len(injections), phase, injection_cycles, _MAX_INJECTION_CYCLES,
|
||||
)
|
||||
else:
|
||||
logger.info("Injected sustained-goal continuation {}", phase)
|
||||
logger.info("Injected caller-requested continuation {}", phase)
|
||||
return True, injection_cycles
|
||||
|
||||
def _build_goal_continue_message(self, spec: AgentRunSpec) -> dict[str, str]:
|
||||
custom = spec.goal_continue_message
|
||||
if callable(custom):
|
||||
try:
|
||||
custom = custom()
|
||||
except Exception:
|
||||
logger.exception("goal_continue_message callback failed")
|
||||
custom = None
|
||||
return build_goal_continue_message(custom)
|
||||
@staticmethod
|
||||
def _build_continuation_message(spec: AgentRunSpec) -> dict[str, str] | None:
|
||||
callback = spec.continuation_callback
|
||||
if callback is None:
|
||||
return None
|
||||
try:
|
||||
content = callback()
|
||||
except Exception:
|
||||
logger.exception("continuation_callback failed")
|
||||
return None
|
||||
if content is None or not content.strip():
|
||||
return None
|
||||
return {"role": "user", "content": content}
|
||||
|
||||
async def _drain_injections(
|
||||
self,
|
||||
@@ -770,7 +771,7 @@ class AgentRunner:
|
||||
conversation_state=conversation_state,
|
||||
phase="after final response",
|
||||
iteration=iteration,
|
||||
allow_goal_continue=(
|
||||
allow_continuation=(
|
||||
response.finish_reason not in {"refusal", "content_filter"}
|
||||
),
|
||||
wait_at_terminal=(
|
||||
@@ -953,7 +954,6 @@ class AgentRunner:
|
||||
progress_callback = spec.progress_callback
|
||||
wants_progress_streaming = (
|
||||
not wants_streaming
|
||||
and spec.stream_progress_deltas
|
||||
and progress_callback is not None
|
||||
and getattr(spec.runtime.provider, "supports_progress_deltas", False) is True
|
||||
)
|
||||
|
||||
@@ -40,13 +40,6 @@ LENGTH_RECOVERY_PROMPT = (
|
||||
"existing text, recap, or apologize."
|
||||
)
|
||||
|
||||
SUSTAINED_GOAL_CONTINUE_PROMPT = (
|
||||
"You have an active sustained goal. Please continue working toward the "
|
||||
"objective using your tools, or call update_goal with action='complete' "
|
||||
"if the work is truly finished."
|
||||
)
|
||||
|
||||
|
||||
def empty_tool_result_message(tool_name: str) -> str:
|
||||
"""Short prompt-safe marker for tools that completed without visible output."""
|
||||
return f"({tool_name} completed with no output)"
|
||||
@@ -97,11 +90,6 @@ def build_length_recovery_message(content: str) -> dict[str, str]:
|
||||
return {"role": "user", "content": prompt}
|
||||
|
||||
|
||||
def build_goal_continue_message(custom: str | None = None) -> dict[str, str]:
|
||||
"""Prompt the model to continue when a sustained goal is still active."""
|
||||
return {"role": "user", "content": custom or SUSTAINED_GOAL_CONTINUE_PROMPT}
|
||||
|
||||
|
||||
def external_lookup_signature(tool_name: str, arguments: Any) -> str | None:
|
||||
"""Stable signature for repeated external lookups we want to throttle."""
|
||||
if not isinstance(arguments, dict):
|
||||
|
||||
Reference in New Issue
Block a user