Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

refactor: extract shared emit_event helper, remove duplicate _emit

xf gonga5e40ccec309
diff --git a/cubepi/agent/__init__.py b/cubepi/agent/__init__.py
index eb59e6f..3c4f0e7 100644
--- a/cubepi/agent/__init__.py
+++ b/cubepi/agent/__init__.py
@@ -22,8 +22,8 @@ from cubepi.agent.types import (
ToolExecutionUpdateEvent,
TurnEndEvent,
TurnStartEvent,
- emit_event,
)
+from cubepi.utils import emit_event
__all__ = [
"Agent",
diff --git a/cubepi/agent/loop.py b/cubepi/agent/loop.py
index e655757..537ed1c 100644
--- a/cubepi/agent/loop.py
+++ b/cubepi/agent/loop.py
@@ -14,8 +14,8 @@ from cubepi.agent.types import (
ShouldStopAfterTurnContext,
TurnEndEvent,
TurnStartEvent,
- emit_event,
)
+from cubepi.utils import emit_event
from cubepi.providers.base import (
AssistantMessage,
Model,
@@ -154,8 +154,8 @@ async def _run_loop(
pending = await get_steering_messages() or []
if pending:
for msg in pending:
- await _emit(emit, MessageStartEvent(message=msg))
- await _emit(emit, MessageEndEvent(message=msg))
+ await emit_event(emit, MessageStartEvent(message=msg))
+ await emit_event(emit, MessageEndEvent(message=msg))
current_context.messages.append(msg)
new_messages.append(msg)
diff --git a/cubepi/agent/tools.py b/cubepi/agent/tools.py
index e7b2cf8..099f89b 100644
--- a/cubepi/agent/tools.py
+++ b/cubepi/agent/tools.py
@@ -18,8 +18,8 @@ from cubepi.agent.types import (
ToolExecutionEndEvent,
ToolExecutionStartEvent,
ToolExecutionUpdateEvent,
- emit_event,
)
+from cubepi.utils import emit_event
from cubepi.providers.base import (
AssistantMessage,
TextContent,
diff --git a/cubepi/agent/types.py b/cubepi/agent/types.py
index b0cdc74..c91f6ad 100644
--- a/cubepi/agent/types.py
+++ b/cubepi/agent/types.py
@@ -168,9 +168,3 @@ AgentEvent = (
AgentEventSink = Callable[[AgentEvent], Awaitable[None]]
AgentListener = Callable[[AgentEvent, asyncio.Event | None], Awaitable[None] | None]
-
-
-async def emit_event(emit_fn: Callable, event: AgentEvent) -> None:
- result = emit_fn(event)
- if asyncio.iscoroutine(result):
- await result
diff --git a/cubepi/utils/__init__.py b/cubepi/utils/__init__.py
index 3eb4fd5..d7db47c 100644
--- a/cubepi/utils/__init__.py
+++ b/cubepi/utils/__init__.py
@@ -1,5 +1,6 @@
"""cubepi.utils — utility modules."""
+from cubepi.utils.emit import emit_event
from cubepi.utils.json_parse import parse_streaming_json, repair_json
-__all__ = ["parse_streaming_json", "repair_json"]
+__all__ = ["emit_event", "parse_streaming_json", "repair_json"]
diff --git a/cubepi/utils/emit.py b/cubepi/utils/emit.py
new file mode 100644
index 0000000..485d092
--- /dev/null
+++ b/cubepi/utils/emit.py
@@ -0,0 +1,10 @@
+"""Event emission helper shared by the agent loop and tool executor."""
+
+import asyncio
+from collections.abc import Callable
+
+
+async def emit_event(emit_fn: Callable, event: object) -> None:
+ result = emit_fn(event)
+ if asyncio.iscoroutine(result):
+ await result