feat(webui): add lightweight session messaging via mentions
This commit is contained in:
1 parent
2bdb11eeba
commit
0e184965e8
76 files changed
+8297
-658
No files matched your search
+44
-7
@@ -85,6 +85,11 @@ from nanobot.session.model_selection import (
|
||||
SESSION_MODEL_PRESET_METADATA_KEY,
|
||||
model_preset_from_metadata,
|
||||
)
|
||||
from nanobot.session.session_messages import (
|
||||
is_session_input,
|
||||
session_input_history_extra,
|
||||
)
|
||||
from nanobot.session.webui_turns import project_session_message_input
|
||||
from nanobot.triggers.local_turns import LocalTriggerTurnCoordinator
|
||||
from nanobot.utils.cancellation import task_is_cancelling
|
||||
from nanobot.utils.document import reference_non_image_attachments
|
||||
@@ -162,6 +167,7 @@ class TurnContext:
|
||||
|
||||
turn_wall_started_at: float = field(default_factory=time.time)
|
||||
visible_run_started_at: float | None = None
|
||||
run_status_started: bool = False
|
||||
turn_latency_ms: int | None = None
|
||||
usage: dict[str, int] = field(default_factory=dict)
|
||||
|
||||
@@ -1016,7 +1022,11 @@ class AgentLoop:
|
||||
if isinstance(metadata_value, dict)
|
||||
else {}
|
||||
)
|
||||
if pending_msg.channel != "system":
|
||||
session_input = is_session_input(pending_msg)
|
||||
if session_input:
|
||||
session_metadata = session_input_history_extra(pending_msg)
|
||||
row.update(session_metadata)
|
||||
if pending_msg.channel != "system" or session_input:
|
||||
scope = self.workspace_scopes.for_turn(
|
||||
channel=pending_msg.channel,
|
||||
message_metadata=metadata,
|
||||
@@ -1257,8 +1267,13 @@ class AgentLoop:
|
||||
msg.require_existing_session
|
||||
and self.sessions.get_cached(effective_key) is None
|
||||
):
|
||||
continue
|
||||
if await asyncio.to_thread(
|
||||
self.sessions.read_session_metadata,
|
||||
effective_key,
|
||||
) is None:
|
||||
continue
|
||||
if self.commands.is_priority(raw):
|
||||
await project_session_message_input(self.bus, msg, effective_key)
|
||||
await self._dispatch_command_inline(
|
||||
msg, effective_key, raw,
|
||||
self.commands.dispatch_priority,
|
||||
@@ -1287,6 +1302,7 @@ class AgentLoop:
|
||||
# Non-priority commands must not be queued for injection;
|
||||
# dispatch them directly (same pattern as priority commands).
|
||||
if self.commands.is_dispatchable_command(raw):
|
||||
await project_session_message_input(self.bus, msg, effective_key)
|
||||
await self._dispatch_command_inline(
|
||||
msg, effective_key, raw,
|
||||
self.commands.dispatch,
|
||||
@@ -1306,6 +1322,7 @@ class AgentLoop:
|
||||
effective_key,
|
||||
)
|
||||
else:
|
||||
await project_session_message_input(self.bus, msg, effective_key)
|
||||
logger.info(
|
||||
"Routed follow-up message to pending queue for session {}",
|
||||
effective_key,
|
||||
@@ -1517,7 +1534,11 @@ class AgentLoop:
|
||||
attributes: Mapping[str, Any] | None = None,
|
||||
) -> OutboundMessage | None:
|
||||
"""Process a single inbound message and return the response."""
|
||||
kind = TurnKind.SYSTEM if msg.channel == "system" else TurnKind.USER
|
||||
kind = (
|
||||
TurnKind.SYSTEM
|
||||
if msg.channel == "system" and not is_session_input(msg)
|
||||
else TurnKind.USER
|
||||
)
|
||||
if kind is TurnKind.SYSTEM:
|
||||
destination = (
|
||||
msg.chat_id.split(":", 1) if ":" in msg.chat_id else ("cli", msg.chat_id)
|
||||
@@ -1697,7 +1718,10 @@ class AgentLoop:
|
||||
|
||||
if ctx.session is None:
|
||||
if msg.require_existing_session:
|
||||
ctx.session = self.sessions.get_cached(ctx.session_key)
|
||||
ctx.session = await asyncio.to_thread(
|
||||
self.sessions.get_existing,
|
||||
ctx.session_key,
|
||||
)
|
||||
if ctx.session is None:
|
||||
raise RuntimeError("required session is not active")
|
||||
else:
|
||||
@@ -1728,6 +1752,12 @@ class AgentLoop:
|
||||
is_user_turn=ctx.original_user_text is not None,
|
||||
)
|
||||
await ctx.delivery.started()
|
||||
if is_session_input(ctx.msg):
|
||||
if ctx.visible_run_started_at is None:
|
||||
ctx.visible_run_started_at = time.time()
|
||||
await ctx.delivery.running(started_at=ctx.visible_run_started_at)
|
||||
ctx.run_status_started = True
|
||||
await project_session_message_input(self.bus, ctx.msg, ctx.session_key)
|
||||
if ctx.kind is TurnKind.USER:
|
||||
self.workspace_scopes.persist_message_scope(session, msg)
|
||||
|
||||
@@ -1904,6 +1934,7 @@ class AgentLoop:
|
||||
ctx.msg,
|
||||
session,
|
||||
runtime_context_blocks=ctx.runtime_context_blocks,
|
||||
**session_input_history_extra(ctx.msg),
|
||||
)
|
||||
if staged_provider_state and not ctx.input_persisted_early:
|
||||
session.provider_state = stored_state
|
||||
@@ -1922,7 +1953,9 @@ class AgentLoop:
|
||||
runtime = ctx.require_runtime()
|
||||
if ctx.visible_run_started_at is None:
|
||||
ctx.visible_run_started_at = time.time()
|
||||
await ctx.delivery.running(started_at=ctx.visible_run_started_at)
|
||||
if not ctx.run_status_started:
|
||||
await ctx.delivery.running(started_at=ctx.visible_run_started_at)
|
||||
ctx.run_status_started = True
|
||||
result = await self._run_agent_loop(
|
||||
ctx.initial_messages,
|
||||
runtime=runtime,
|
||||
@@ -1968,7 +2001,8 @@ class AgentLoop:
|
||||
and not ctx.suppress_response
|
||||
):
|
||||
ctx.final_content = EMPTY_FINAL_RESPONSE_MESSAGE
|
||||
|
||||
if session.discarded:
|
||||
raise RuntimeError("session was deleted while the turn was running")
|
||||
latency_started_at = (
|
||||
ctx.visible_run_started_at
|
||||
if (
|
||||
@@ -2022,8 +2056,11 @@ class AgentLoop:
|
||||
latency_ms=ctx.turn_latency_ms,
|
||||
)
|
||||
return
|
||||
outbound_input = (
|
||||
ctx.delivery.delivery_message if ctx.msg.channel == "system" else ctx.msg
|
||||
)
|
||||
ctx.outbound = self._assemble_outbound(
|
||||
ctx.msg,
|
||||
outbound_input,
|
||||
cast(str, ctx.final_content),
|
||||
ctx.stop_reason,
|
||||
ctx.had_injections,
|
||||
|
||||
Reference in new issue
Block a user