refactor(events): unify scoped runtime notifications across clients (#5670)
* refactor(events): unify scoped runtime notifications across clients * style(tests): sort auto-compaction event imports * refactor(events): scope connections and remove retry callback plumbing * docs: remove runtime notification design write-up * refactor(bus): route operation events through the message bus * fix(channels): isolate outbound sends by destination * style(events): group shared notification imports * refactor(events): converge runtime and hook delivery on MessageBus * test(events): migrate fixtures to unified message bus
This commit is contained in:
1 parent
3495281282
commit
192e2e90dc
78 files changed
+1620
-1194
No files matched your search
@@ -7,14 +7,13 @@ from collections.abc import Awaitable, Callable
|
||||
from typing import Any, cast
|
||||
|
||||
from nanobot.agent.hook import AgentHookContext
|
||||
|
||||
|
||||
def on_progress_accepts_tool_events(cb: Callable[..., Any]) -> bool:
|
||||
return _on_progress_accepts(cb, "tool_events")
|
||||
|
||||
|
||||
def on_progress_accepts_file_edit_events(cb: Callable[..., Any]) -> bool:
|
||||
return _on_progress_accepts(cb, "file_edit_events")
|
||||
from nanobot.bus.outbound_events import (
|
||||
FileEditEvent,
|
||||
ProgressEvent,
|
||||
StreamDeltaEvent,
|
||||
StreamEndEvent,
|
||||
)
|
||||
from nanobot.events import NO_EVENTS, AgentEvent, EventSink
|
||||
|
||||
|
||||
def _on_progress_accepts(cb: Callable[..., Any], name: str) -> bool:
|
||||
@@ -27,26 +26,61 @@ def _on_progress_accepts(cb: Callable[..., Any], name: str) -> bool:
|
||||
return name in sig.parameters
|
||||
|
||||
|
||||
async def invoke_on_progress(
|
||||
on_progress: Callable[..., Awaitable[None]],
|
||||
content: str,
|
||||
def output_events(
|
||||
*,
|
||||
tool_hint: bool = False,
|
||||
tool_events: list[dict[str, Any]] | None = None,
|
||||
) -> None:
|
||||
if tool_events and on_progress_accepts_tool_events(on_progress):
|
||||
await on_progress(content, tool_hint=tool_hint, tool_events=tool_events)
|
||||
return
|
||||
await on_progress(content, tool_hint=tool_hint)
|
||||
default: EventSink = NO_EVENTS,
|
||||
on_progress: Callable[..., Awaitable[None]] | None = None,
|
||||
on_stream: Callable[[str], Awaitable[None]] | None = None,
|
||||
on_stream_end: Callable[..., Awaitable[None]] | None = None,
|
||||
) -> EventSink:
|
||||
"""Adapt direct-call output callbacks once, outside the runner and hooks."""
|
||||
if on_progress is None and on_stream is None and on_stream_end is None:
|
||||
return default
|
||||
progress_fields = {
|
||||
name for name in ("tool_events", "file_edit_events", "reasoning", "reasoning_end")
|
||||
if on_progress is not None and _on_progress_accepts(on_progress, name)
|
||||
}
|
||||
merge_next = on_stream_end is not None and _on_progress_accepts(on_stream_end, "merge_next")
|
||||
|
||||
def accepts(event_type: type[AgentEvent]) -> bool:
|
||||
if issubclass(event_type, FileEditEvent) and on_progress is not None:
|
||||
return "file_edit_events" in progress_fields
|
||||
if issubclass(event_type, ProgressEvent) and on_progress is not None:
|
||||
return True
|
||||
if issubclass(event_type, StreamDeltaEvent) and on_stream is not None:
|
||||
return True
|
||||
if issubclass(event_type, StreamEndEvent) and on_stream_end is not None:
|
||||
return True
|
||||
return default.accepts(event_type)
|
||||
|
||||
async def invoke_file_edit_progress(
|
||||
on_progress: Callable[..., Awaitable[None]],
|
||||
file_edit_events: list[dict[str, Any]],
|
||||
) -> None:
|
||||
if not file_edit_events or not on_progress_accepts_file_edit_events(on_progress):
|
||||
return
|
||||
await on_progress("", file_edit_events=file_edit_events)
|
||||
async def publish(event: AgentEvent) -> None:
|
||||
if isinstance(event, ProgressEvent) and on_progress is not None:
|
||||
if event.file_edit_events:
|
||||
if "file_edit_events" in progress_fields:
|
||||
await on_progress(event.content, file_edit_events=event.file_edit_events)
|
||||
return
|
||||
if event.reasoning_delta or event.reasoning_end:
|
||||
name = "reasoning" if event.reasoning_delta else "reasoning_end"
|
||||
if name in progress_fields:
|
||||
await on_progress(event.content, **{name: True})
|
||||
return
|
||||
if event.tool_events and not event.tool_hint and "tool_events" not in progress_fields:
|
||||
return
|
||||
kwargs: dict[str, Any] = {"tool_hint": event.tool_hint}
|
||||
if event.tool_events and "tool_events" in progress_fields:
|
||||
kwargs["tool_events"] = event.tool_events
|
||||
await on_progress(event.content, **kwargs)
|
||||
elif isinstance(event, StreamDeltaEvent) and on_stream is not None:
|
||||
await on_stream(event.content)
|
||||
elif isinstance(event, StreamEndEvent) and on_stream_end is not None:
|
||||
kwargs = {"resuming": event.resuming}
|
||||
if event.merge_next and merge_next:
|
||||
kwargs["merge_next"] = True
|
||||
await on_stream_end(**kwargs)
|
||||
elif default.publish is not None:
|
||||
await default.publish(event)
|
||||
|
||||
return EventSink(publish, accepts)
|
||||
|
||||
|
||||
def _tool_event_arguments(tool_call: Any) -> dict[str, Any]:
|
||||
|
||||
Reference in new issue
Block a user