-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Expand file tree
/
Copy pathgstack-codex-jsonl-parser
More file actions
executable file
·64 lines (59 loc) · 2.05 KB
/
gstack-codex-jsonl-parser
File metadata and controls
executable file
·64 lines (59 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
"""
gstack-codex-jsonl-parser: parse Codex CLI --json streaming output to human-readable form.
Usage: gstack-codex-jsonl-parser [--mode challenge|consult]
Reads JSONL from stdin (codex exec --json output), prints formatted lines to stdout.
Modes:
challenge Track turn.completed count; warn on disconnect (no events received).
consult Extract SESSION_ID from thread.started for follow-up sessions.
"""
import sys
import json
mode = "consult"
args = sys.argv[1:]
i = 0
while i < len(args):
if args[i] == "--mode" and i + 1 < len(args):
mode = args[i + 1]
i += 2
else:
i += 1
turn_completed_count = 0
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
t = obj.get("type", "")
if t == "thread.started" and mode == "consult":
tid = obj.get("thread_id", "")
if tid:
print(f"SESSION_ID:{tid}", flush=True)
elif t == "item.completed" and "item" in obj:
item = obj["item"]
itype = item.get("type", "")
text = item.get("text", "")
if itype == "reasoning" and text:
print(f"[codex thinking] {text}", flush=True)
print(flush=True)
elif itype == "agent_message" and text:
print(text, flush=True)
elif itype == "command_execution":
cmd = item.get("command", "")
if cmd:
print(f"[codex ran] {cmd}", flush=True)
elif t == "turn.completed":
turn_completed_count += 1
usage = obj.get("usage", {})
tokens = usage.get("input_tokens", 0) + usage.get("output_tokens", 0)
if tokens:
print(f"\ntokens used: {tokens}", flush=True)
except Exception:
pass
if mode == "challenge" and turn_completed_count == 0:
print(
"[codex warning] No turn.completed event received — possible mid-stream disconnect.",
flush=True,
file=sys.stderr,
)