-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcursor-agent-stream-format.py
More file actions
executable file
·134 lines (117 loc) · 4.72 KB
/
cursor-agent-stream-format.py
File metadata and controls
executable file
·134 lines (117 loc) · 4.72 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
"""
Pretty-print Cursor Agent CLI stream-json (NDJSON) for humans.
Reads one JSON object per line from stdin; prints assistant text to stdout
and progress (session, tools) to stderr. See:
https://cursor.com/docs/cli/reference/output-format
"""
from __future__ import annotations
import json
import sys
def _assistant_text(obj: dict) -> str:
parts: list[str] = []
for block in (obj.get("message") or {}).get("content") or []:
if isinstance(block, dict) and block.get("type") == "text":
parts.append(str(block.get("text") or ""))
return "".join(parts)
def _describe_tool_call(tool_call: dict, *, completed: bool) -> str:
if not isinstance(tool_call, dict) or not tool_call:
return "?"
if "readToolCall" in tool_call:
inner = tool_call["readToolCall"]
args = inner.get("args") or {}
path = args.get("path", "?")
if completed:
res = inner.get("result") or {}
succ = (res.get("success") or {}) if isinstance(res, dict) else {}
lines = succ.get("totalLines")
if lines is not None:
return f"read {path} ({lines} lines)"
return f"read {path} (done)"
return f"read {path}"
if "writeToolCall" in tool_call:
inner = tool_call["writeToolCall"]
args = inner.get("args") or {}
path = args.get("path", "?")
if completed:
res = inner.get("result") or {}
succ = (res.get("success") or {}) if isinstance(res, dict) else {}
lines = succ.get("linesCreated")
if lines is not None:
return f"write {path} (+{lines} lines)"
return f"write {path} (done)"
return f"write {path}"
fn = tool_call.get("function")
if isinstance(fn, dict):
name = fn.get("name") or "function"
raw = fn.get("arguments")
if isinstance(raw, str):
arg_s = raw.replace("\n", " ").strip()
if len(arg_s) > 160:
arg_s = arg_s[:160] + "…"
else:
arg_s = json.dumps(raw, default=str) if raw is not None else ""
if len(arg_s) > 160:
arg_s = arg_s[:160] + "…"
return f"{name} {arg_s}".strip()
for key, inner in tool_call.items():
if isinstance(inner, dict) and key.endswith("ToolCall"):
label = key[: -len("ToolCall")] or key
args = inner.get("args")
if isinstance(args, dict):
return f"{label} {json.dumps(args, default=str)[:200]}"
return f"{label} …"
return json.dumps(tool_call, default=str)[:200]
def main() -> None:
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
o = json.loads(line)
except json.JSONDecodeError:
print(line, file=sys.stderr, flush=True)
continue
t = o.get("type")
if t == "thinking":
continue
if t == "system":
sid = str(o.get("session_id") or "")
sid_short = (sid[:8] + "…") if len(sid) > 8 else sid
print(
f"\n[cursor-agent] session {sid_short} model={o.get('model', '?')!s} cwd={o.get('cwd', '')}",
file=sys.stderr,
flush=True,
)
elif t == "user":
txt = _assistant_text(o)
if len(txt) > 600:
txt = txt[:600] + "…"
print(f"\n[cursor-agent] --- prompt ({len(txt)} chars) ---\n{txt}\n", file=sys.stderr, flush=True)
elif t == "assistant":
sys.stdout.write(_assistant_text(o))
sys.stdout.flush()
elif t == "tool_call":
sub = o.get("subtype")
tc = o.get("tool_call") if isinstance(o.get("tool_call"), dict) else {}
desc = _describe_tool_call(tc, completed=(sub == "completed"))
if sub == "started":
print(f"\n[cursor-agent] tool ▶ {desc}", file=sys.stderr, flush=True)
elif sub == "completed":
print(f"[cursor-agent] tool ✓ {desc}", file=sys.stderr, flush=True)
else:
print(f"[cursor-agent] tool {sub!s} {desc}", file=sys.stderr, flush=True)
elif t == "result":
dur = o.get("duration_ms")
err = o.get("is_error")
sub = o.get("subtype")
print(
f"\n[cursor-agent] result subtype={sub!s} duration_ms={dur!s} is_error={err!s}",
file=sys.stderr,
flush=True,
)
else:
# Forward-compatible: show unknown types lightly
print(f"[cursor-agent] event type={t!s}", file=sys.stderr, flush=True)
if __name__ == "__main__":
main()