summaryrefslogtreecommitdiffhomepage
path: root/packages/api/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/api/src')
-rw-r--r--packages/api/src/agent-manager.ts13
-rw-r--r--packages/api/src/routes/tabs.ts24
2 files changed, 37 insertions, 0 deletions
diff --git a/packages/api/src/agent-manager.ts b/packages/api/src/agent-manager.ts
index f7975d1..5f2027d 100644
--- a/packages/api/src/agent-manager.ts
+++ b/packages/api/src/agent-manager.ts
@@ -799,6 +799,9 @@ export class AgentManager {
if (tabAgent.currentAssistantId) {
snap.currentAssistantId = tabAgent.currentAssistantId;
}
+ if (tabAgent.currentTurnId) {
+ snap.currentTurnId = tabAgent.currentTurnId;
+ }
}
result[tabId] = snap;
}
@@ -1119,6 +1122,10 @@ export class AgentManager {
// chunk rows — shares one `turn_id`.
const turnId = crypto.randomUUID();
tabAgent.currentTurnId = turnId;
+ // Announce the turn so the frontend can tag its live chunks with this
+ // turn_id (stable render keys → flicker-free reconcile when the turn
+ // seals). Emitted before any content delta.
+ this.emit({ type: "turn-start", turnId }, tabId);
appendChunks(tabId, explodeUserText(turnId, message));
// Store agent models on the tab if provided (defines fallback order)
@@ -1284,6 +1291,12 @@ export class AgentManager {
this.emit({ type: "status", status: "error" }, tabId);
break;
}
+ // Turn fully settled and its chunks are now persisted (flushAssistant ran
+ // above). Signal the frontend that the turn's rows — with real seqs — are
+ // durable so it can fold its live representation into the sealed log.
+ // Emitted AFTER status:idle/error (which fire before the DB write).
+ this.emit({ type: "turn-sealed", turnId }, tabId);
+
// Turn fully settled — clear the shared turn id.
tabAgent.currentTurnId = null;
diff --git a/packages/api/src/routes/tabs.ts b/packages/api/src/routes/tabs.ts
index e9265ec..b1e9659 100644
--- a/packages/api/src/routes/tabs.ts
+++ b/packages/api/src/routes/tabs.ts
@@ -93,6 +93,30 @@ tabsRoutes.get("/:id/messages", (c) => {
return c.json({ messages, total, oldestSeq });
});
+// Raw chunk window for a tab — the chunk-native frontend's load/paginate
+// source. Same `limit`/`before` chunk-`seq` windowing as `/messages`, but
+// returns the flat `ChunkRow[]` WITHOUT server-side grouping (the frontend
+// groups for render and evicts/paginates on the flat list). Dedupe on the
+// client by `seq` when overlap-fetching.
+tabsRoutes.get("/:id/chunks", (c) => {
+ const id = c.req.param("id");
+ const limitRaw = c.req.query("limit");
+ const beforeRaw = c.req.query("before");
+ const limit = limitRaw !== undefined ? Number(limitRaw) : undefined;
+ const before = beforeRaw !== undefined ? Number(beforeRaw) : undefined;
+ const options =
+ limit !== undefined || before !== undefined
+ ? {
+ ...(limit !== undefined && Number.isFinite(limit) ? { limit } : {}),
+ ...(before !== undefined && Number.isFinite(before) ? { before } : {}),
+ }
+ : undefined;
+ const chunks = getChunksForTab(id, options);
+ const oldestSeq = chunks.length > 0 ? (chunks[0]?.seq ?? null) : null;
+ const total = getTotalChunkCount(id);
+ return c.json({ chunks, total, oldestSeq });
+});
+
tabsRoutes.patch("/:id", async (c) => {
const id = c.req.param("id");
const body = await c.req.json<{