|
| 1 | +# SPDX-FileCopyrightText: 2026 Hari Srinivasan <harisrini21@gmail.com> |
| 2 | +# SPDX-License-Identifier: AGPL-3.0-only |
| 3 | + |
| 4 | +"""Antigravity CLI JSONL session parser. |
| 5 | +
|
| 6 | +Real transcript format (brain/<id>/.system_generated/logs/transcript.jsonl): |
| 7 | + { |
| 8 | + "step_index": 0, |
| 9 | + "source": "USER_EXPLICIT" | "MODEL" | "SYSTEM", |
| 10 | + "type": "USER_INPUT" | "PLANNER_RESPONSE" | "LIST_DIRECTORY" | "CONVERSATION_HISTORY" | ..., |
| 11 | + "status": "DONE" | "IN_PROGRESS" | "ERROR", |
| 12 | + "created_at": "2026-05-31T17:54:04Z", |
| 13 | + "content": "...", # present on USER_INPUT and PLANNER_RESPONSE |
| 14 | + "tool_calls": [ # present on PLANNER_RESPONSE when tools are invoked |
| 15 | + {"name": "list_dir", "args": {...}} |
| 16 | + ] |
| 17 | + } |
| 18 | +
|
| 19 | +Tool results come as separate rows with type matching the tool name |
| 20 | +(e.g. "LIST_DIRECTORY") and content holding the output. |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import json |
| 26 | +import re |
| 27 | + |
| 28 | +from .base import basic_event, pick_timestamp |
| 29 | + |
| 30 | +# Types that represent user prompts |
| 31 | +_USER_TYPES = {"USER_INPUT"} |
| 32 | + |
| 33 | +# Types that represent model text responses (no tool calls) |
| 34 | +_ASSISTANT_TYPES = {"PLANNER_RESPONSE"} |
| 35 | + |
| 36 | +# Types that are tool results (source=MODEL, type != USER_INPUT/PLANNER_RESPONSE/SYSTEM types) |
| 37 | +_SYSTEM_TYPES = {"CONVERSATION_HISTORY", "SYSTEM_PROMPT"} |
| 38 | + |
| 39 | +_USER_REQUEST_RE = re.compile(r"<USER_REQUEST>\s*(.*?)\s*</USER_REQUEST>", re.DOTALL) |
| 40 | + |
| 41 | + |
| 42 | +def _extract_user_text(content: str) -> str: |
| 43 | + """Strip XML wrapper tags agy injects around user prompts.""" |
| 44 | + m = _USER_REQUEST_RE.search(content) |
| 45 | + return m.group(1).strip() if m else content.strip() |
| 46 | + |
| 47 | + |
| 48 | +def parse_rows(rows: list[dict]) -> list[dict]: |
| 49 | + """Parse raw_line Antigravity transcript rows into normalised frontend events.""" |
| 50 | + events: list[dict] = [] |
| 51 | + # Maps step_index of a PLANNER_RESPONSE with tool_calls -> event index |
| 52 | + # so we can attach tool results back to the tool call event |
| 53 | + tool_step_index: dict[int, int] = {} |
| 54 | + |
| 55 | + for row in rows: |
| 56 | + raw_line = row.get("raw_line", "") |
| 57 | + ingested_at = row.get("ingested_at", "") |
| 58 | + row_ts = row.get("timestamp", "") |
| 59 | + ide = row.get("ide", "antigravity") |
| 60 | + |
| 61 | + if not raw_line: |
| 62 | + events.append(basic_event(row)) |
| 63 | + continue |
| 64 | + |
| 65 | + try: |
| 66 | + line = json.loads(raw_line) |
| 67 | + except (json.JSONDecodeError, ValueError): |
| 68 | + events.append(basic_event(row)) |
| 69 | + continue |
| 70 | + |
| 71 | + line_type = line.get("type", "") |
| 72 | + source = line.get("source", "") |
| 73 | + status = line.get("status", "") |
| 74 | + content = line.get("content", "") |
| 75 | + tool_calls = line.get("tool_calls", []) |
| 76 | + step_index = line.get("step_index", -1) |
| 77 | + jsonl_ts = line.get("created_at") |
| 78 | + ts = pick_timestamp(jsonl_ts, row_ts, ingested_at) |
| 79 | + |
| 80 | + # Skip system/history lines |
| 81 | + if line_type in _SYSTEM_TYPES or source == "SYSTEM": |
| 82 | + continue |
| 83 | + |
| 84 | + # User prompt |
| 85 | + if line_type in _USER_TYPES and source in ("USER_EXPLICIT", "USER_IMPLICIT"): |
| 86 | + text = _extract_user_text(content) if content else "" |
| 87 | + if text: |
| 88 | + events.append({ |
| 89 | + "timestamp": ts, |
| 90 | + "event_name": "hook_userpromptsubmit", |
| 91 | + "body": text[:120], |
| 92 | + "attributes": {"tool_input": text}, |
| 93 | + "service_name": ide, |
| 94 | + }) |
| 95 | + |
| 96 | + # Model response with tool calls |
| 97 | + elif line_type in _ASSISTANT_TYPES and tool_calls: |
| 98 | + for tc in tool_calls: |
| 99 | + tool_name = tc.get("name", "") |
| 100 | + args = tc.get("args", {}) |
| 101 | + idx = len(events) |
| 102 | + events.append({ |
| 103 | + "timestamp": ts, |
| 104 | + "event_name": "hook_posttooluse", |
| 105 | + "body": tool_name, |
| 106 | + "attributes": { |
| 107 | + "tool_name": tool_name, |
| 108 | + "tool_input": json.dumps(args) if isinstance(args, dict) else str(args), |
| 109 | + }, |
| 110 | + "service_name": ide, |
| 111 | + }) |
| 112 | + tool_step_index[step_index] = idx |
| 113 | + |
| 114 | + # Model text response (no tool calls) |
| 115 | + elif line_type in _ASSISTANT_TYPES and content and not tool_calls: |
| 116 | + events.append({ |
| 117 | + "timestamp": ts, |
| 118 | + "event_name": "hook_assistant_response", |
| 119 | + "body": content[:120], |
| 120 | + "attributes": {"tool_response": content}, |
| 121 | + "service_name": ide, |
| 122 | + }) |
| 123 | + |
| 124 | + # Tool result — type is the tool name (e.g. LIST_DIRECTORY) |
| 125 | + # source=MODEL, step_index follows the PLANNER_RESPONSE that called it |
| 126 | + elif source == "MODEL" and line_type not in _ASSISTANT_TYPES and content: |
| 127 | + # Find the preceding tool call event (step_index - 1 or step_index - 2) |
| 128 | + parent_idx = tool_step_index.get(step_index - 1) or tool_step_index.get(step_index - 2) |
| 129 | + if parent_idx is not None and parent_idx < len(events): |
| 130 | + events[parent_idx]["attributes"]["tool_response"] = content[:500] |
| 131 | + if status == "ERROR": |
| 132 | + events[parent_idx]["attributes"]["tool_status"] = "error" |
| 133 | + else: |
| 134 | + # No matching tool call — emit as standalone result |
| 135 | + events.append({ |
| 136 | + "timestamp": ts, |
| 137 | + "event_name": "hook_posttooluse", |
| 138 | + "body": line_type, |
| 139 | + "attributes": { |
| 140 | + "tool_name": line_type, |
| 141 | + "tool_response": content[:500], |
| 142 | + }, |
| 143 | + "service_name": ide, |
| 144 | + }) |
| 145 | + |
| 146 | + else: |
| 147 | + events.append(basic_event(row)) |
| 148 | + |
| 149 | + return events |
0 commit comments