Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(agent-chat): let a session read return only its newest events

rubenluetke10-beepaee40c63f3f4
diff --git a/jarvis/agent_chat/store.py b/jarvis/agent_chat/store.py
index 769df9eb63..cf2ce5959b 100644
--- a/jarvis/agent_chat/store.py
+++ b/jarvis/agent_chat/store.py
@@ -375,17 +375,31 @@ class AgentChatStore:
if row is None:
return None
return {
- "seq": int(row["seq"]), "ts_ms": int(row["ts_ms"]), "kind": row["kind"],
+ "seq": int(row["seq"]),
+ "ts_ms": int(row["ts_ms"]),
+ "kind": row["kind"],
"payload": json.loads(row["payload"]),
}
- def list_events(self, session_id: str, *, after_seq: int = 0) -> list[dict[str, Any]]:
+ def list_events(
+ self, session_id: str, *, after_seq: int = 0, tail: int | None = None
+ ) -> list[dict[str, Any]]:
+ """Events in order; ``tail`` keeps only the newest N (still oldest first)."""
with self._lock:
- rows = self._conn.execute(
- "SELECT seq, ts_ms, kind, payload FROM agent_chat_events "
- "WHERE session_id = ? AND seq > ? ORDER BY seq ASC",
- (session_id, int(after_seq)),
- ).fetchall()
+ if tail is not None:
+ rows = self._conn.execute(
+ "SELECT seq, ts_ms, kind, payload FROM ("
+ "SELECT seq, ts_ms, kind, payload FROM agent_chat_events "
+ "WHERE session_id = ? AND seq > ? ORDER BY seq DESC LIMIT ?"
+ ") ORDER BY seq ASC",
+ (session_id, int(after_seq), max(0, int(tail))),
+ ).fetchall()
+ else:
+ rows = self._conn.execute(
+ "SELECT seq, ts_ms, kind, payload FROM agent_chat_events "
+ "WHERE session_id = ? AND seq > ? ORDER BY seq ASC",
+ (session_id, int(after_seq)),
+ ).fetchall()
out: list[dict[str, Any]] = []
for r in rows:
try:
diff --git a/jarvis/ui/web/agent_chat_routes.py b/jarvis/ui/web/agent_chat_routes.py
index 74987bcbf1..a53e2cb091 100644
--- a/jarvis/ui/web/agent_chat_routes.py
+++ b/jarvis/ui/web/agent_chat_routes.py
@@ -687,14 +687,20 @@ async def create_session(body: CreateSessionBody, request: Request) -> dict[str,
@router.get("/sessions/{session_id}")
-async def get_session(session_id: str, request: Request) -> dict[str, Any]:
+async def get_session(
+ session_id: str,
+ request: Request,
+ tail: int | None = Query(
+ None, ge=1, le=500, description="Only the newest N events (e.g. a live preview)."
+ ),
+) -> dict[str, Any]:
svc = _service(request)
session = svc.store.get_session(session_id)
if session is None:
raise HTTPException(status_code=404, detail="session not found")
d = session.to_dict()
d["running"] = svc.is_running(session_id)
- return {"session": d, "events": svc.store.list_events(session_id)}
+ return {"session": d, "events": svc.store.list_events(session_id, tail=tail)}
@router.patch("/sessions/{session_id}")
diff --git a/jarvis/ui/web/frontend/src/components/society/office/useDeskChats.ts b/jarvis/ui/web/frontend/src/components/society/office/useDeskChats.ts
index c0e5d8b2ec..6260094c19 100644
--- a/jarvis/ui/web/frontend/src/components/society/office/useDeskChats.ts
+++ b/jarvis/ui/web/frontend/src/components/society/office/useDeskChats.ts
@@ -8,6 +8,9 @@
import { useEffect, useRef, useState } from "react";
import { fetchAgentChatSession } from "@/lib/agentChatApi";
import { chatLines, chatVersion, type ChatLine } from "./deskChat";
+
+/** Events fetched per poll: enough for a full screen after tool noise is filtered out. */
+const TAIL_EVENTS = 60;
import { seatedAtDesk } from "./walkerRegistry";
const POLL_MS = 2500;
@@ -29,7 +32,7 @@ export function useDeskChats(sessions: ReadonlyMap<string, string>, awake: boole
const due = [...seatedAtDesk].filter((id) => sessionsRef.current.has(id)).slice(0, MAX_PER_TICK);
const results = await Promise.all(due.map(async (agentId) => {
try {
- const detail = await fetchAgentChatSession(sessionsRef.current.get(agentId)!);
+ const detail = await fetchAgentChatSession(sessionsRef.current.get(agentId)!, { tail: TAIL_EVENTS });
return [agentId, { lines: chatLines(detail.events), version: chatVersion(detail.events) }] as const;
} catch (error) {
// The monitor keeps its last good frame; the chat view itself reports real failures.
diff --git a/jarvis/ui/web/frontend/src/lib/agentChatApi.ts b/jarvis/ui/web/frontend/src/lib/agentChatApi.ts
index 49a82a1efe..5dc99a8e2f 100644
--- a/jarvis/ui/web/frontend/src/lib/agentChatApi.ts
+++ b/jarvis/ui/web/frontend/src/lib/agentChatApi.ts
@@ -358,9 +358,11 @@ export async function deleteAgentChatSession(sessionId: string): Promise<void> {
export async function fetchAgentChatSession(
sessionId: string,
+ options: { tail?: number } = {},
): Promise<{ session: AgentChatSession; events: AgentChatEvent[] }> {
+ const query = options.tail ? `?tail=${Math.max(1, Math.round(options.tail))}` : "";
return json(
- await fetch(`/api/agent-chat/sessions/${encodeURIComponent(sessionId)}`),
+ await fetch(`/api/agent-chat/sessions/${encodeURIComponent(sessionId)}${query}`),
"session-failed",
);
}
diff --git a/tests/unit/agent_chat/test_agent_chat_core.py b/tests/unit/agent_chat/test_agent_chat_core.py
index c0267dc800..c90d557bd4 100644
--- a/tests/unit/agent_chat/test_agent_chat_core.py
+++ b/tests/unit/agent_chat/test_agent_chat_core.py
@@ -89,6 +89,9 @@ def test_store_round_trips_sessions_and_events(tmp_path: Path):
events = store.list_events(s.session_id)
assert [e["kind"] for e in events] == ["user_message", "assistant_text"]
assert store.list_events(s.session_id, after_seq=1)[0]["kind"] == "assistant_text"
+ # A live preview asks for the newest events only, still oldest first.
+ assert [e["kind"] for e in store.list_events(s.session_id, tail=1)] == ["assistant_text"]
+ assert [e["seq"] for e in store.list_events(s.session_id, tail=5)] == [1, 2]
# The store keeps whatever id the route validated against the runner's
# ladder (jarvis/agent_chat/permissions.py); it does not judge it.