spawned_live.py: the reporting children still at work, per parent (memoized), merged into runningAgents and _resolve_state (never over interrupted/review). spawnedChildren carry startedAt/messages/tokens/subagentCount/shortTitle/prompt/report; spawnedByChain for the breadcrumb; SSE parent cards + roots for child writes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
5.3 KiB
Python
139 lines
5.3 KiB
Python
"""One conversation's list card, built fresh from the store.
|
|
|
|
Shared by ``GET /api/conversation-summary`` and the transcript watcher: after
|
|
a live transcript grows the watcher re-indexes it and attaches the resulting
|
|
card to the SSE ``transcript`` event, so every open tab patches its cached
|
|
list row in place without a request each (the summary endpoint stays as the
|
|
fallback for a tab that missed the event)."""
|
|
|
|
from conversations import ids, spawned
|
|
from conversations.cards import _conv_meta, _conv_title, _stopped_on
|
|
from conversations.catalog import (
|
|
_CARD_KEYS,
|
|
_agents_of_conversation,
|
|
_conv_ids_by_session,
|
|
_resolve_transcript,
|
|
_skills_used,
|
|
)
|
|
from conversations.tree import (
|
|
_conv_id,
|
|
_is_deploying,
|
|
_is_sidechain_path,
|
|
_rollup_children,
|
|
_running_agents,
|
|
_subagent_descendants,
|
|
)
|
|
|
|
|
|
def conversation_card(deps, id: str) -> dict | None:
|
|
"""The card for transcript ``id`` — its uid, or (from the watcher) its rel
|
|
path — rollup of its subagent tree included, or None while the store
|
|
hasn't indexed it yet. The card always carries the uid."""
|
|
ap = _resolve_transcript(id)
|
|
id = _conv_id(str(ap))
|
|
s = deps.store.get_summary(str(ap))
|
|
if not s:
|
|
return None
|
|
children: list[dict] = []
|
|
paths = [str(ap)]
|
|
for child in _subagent_descendants(ap): # the whole tree, any depth
|
|
paths.append(str(child))
|
|
cs = deps.store.get_summary(str(child))
|
|
if cs:
|
|
children.append(cs)
|
|
skills_used = _skills_used(deps, paths)
|
|
s = _rollup_children(s, children)
|
|
m = _conv_meta(deps, s)
|
|
# Its hub children's spend, as the list route stamps it (spawned.py).
|
|
n, cost, live = spawned.descendant_stats(deps, s.get("sessionId"))
|
|
kids = {"childCount": n, "childCost": round(cost, 6), "childRunning": live} if n else {}
|
|
return {"id": id, **{k: s.get(k) for k in _CARD_KEYS}, **kids,
|
|
"title": _conv_title(deps, s, m),
|
|
"runningAgents": _running_agents(deps, s, m.get("state")),
|
|
"deploying": _is_deploying(deps, s, m.get("state")),
|
|
"stoppedOn": _stopped_on(s, m.get("state")),
|
|
"skillsUsed": skills_used,
|
|
"agentsUsed": _agents_of_conversation(deps, id, s.get("sessionId"), paths[1:]),
|
|
"meta": m}
|
|
|
|
|
|
def root_rel(rel: str) -> str | None:
|
|
"""A subagent transcript's rel path (any depth: Claude Code writes every
|
|
nested agent into the root's one ``subagents/`` dir) → its root's,
|
|
``<dir>/<conv>/subagents/…/agent-x.jsonl`` → ``<dir>/<conv>.jsonl``;
|
|
None for a root transcript. Internal paths only — a public id is a uid."""
|
|
i = rel.find("/subagents/")
|
|
return None if i == -1 else rel[:i] + ".jsonl"
|
|
|
|
|
|
def cards_for(deps, rels: list[str]) -> dict[str, dict]:
|
|
"""The cards the ``transcript`` event for the changed transcripts
|
|
(``rels``, archive-relative paths) should carry, keyed by uid: one per root
|
|
conversation touched — a subagent's write moves its *root's* card (the
|
|
rollup), and a subagent has no list row of its own."""
|
|
roots: list[str] = []
|
|
for rel in rels:
|
|
root = root_rel(rel) if _is_sidechain_path(rel) else None
|
|
root = root or rel
|
|
if root not in roots:
|
|
roots.append(root)
|
|
# A hub child's write moves its parents' cards too (their spend, their
|
|
# running agents) — up the spawn chain.
|
|
for up in spawn_ancestors(deps, roots):
|
|
if up not in roots:
|
|
roots.append(up)
|
|
out: dict[str, dict] = {}
|
|
for root in roots:
|
|
try:
|
|
card = conversation_card(deps, root)
|
|
except Exception:
|
|
card = None
|
|
if card:
|
|
out[card["id"]] = card
|
|
return out
|
|
|
|
|
|
def _parent_uid(deps, rel: str) -> str | None:
|
|
"""The uid of the conversation that started root transcript ``rel`` with
|
|
the ``ai-agent`` CLI, if any."""
|
|
s = deps.store.get_summary(ids.path_of(rel)) if deps and rel else None
|
|
up = spawned.spawn_parent(deps, (s or {}).get("sessionId"))
|
|
return _conv_ids_by_session(deps).get(up) if up else None
|
|
|
|
|
|
def spawn_ancestors(deps, rels: list[str]) -> list[str]:
|
|
"""The uids above root transcripts ``rels`` in their spawn chains."""
|
|
out: list[str] = []
|
|
for rel in rels:
|
|
try:
|
|
up, hops = _parent_uid(deps, rel), 0
|
|
while up and up not in out and hops < 8:
|
|
out.append(up)
|
|
up, hops = _parent_uid(deps, ids.rel_for(up) or ""), hops + 1
|
|
except Exception:
|
|
continue
|
|
return out
|
|
|
|
|
|
def transcript_event(rels: list[str], deps=None) -> dict:
|
|
"""The SSE ``transcript`` event for changed transcripts: their uids, and
|
|
for each subagent its root's (the client can't read it off an opaque id)
|
|
— for a hub child, the conversation that started it, so its open page
|
|
refetches the child's card as it runs."""
|
|
ev: dict = {"type": "transcript", "ids": [ids.uid_for_rel(r) for r in rels]}
|
|
roots = {ids.uid_for_rel(r): ids.uid_for_rel(root)
|
|
for r in rels if (root := root_rel(r))}
|
|
if deps is not None:
|
|
for r in rels:
|
|
if root_rel(r):
|
|
continue
|
|
try:
|
|
up = _parent_uid(deps, r)
|
|
except Exception:
|
|
up = None
|
|
if up:
|
|
roots[ids.uid_for_rel(r)] = up
|
|
if roots:
|
|
ev["roots"] = roots
|
|
return ev
|