Repository navigation
Expand file tree
/
Copy pathpretty.jq
More file actions
51 lines (44 loc) · 2.47 KB
/
Copy pathpretty.jq
File metadata and controls
51 lines (44 loc) · 2.47 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
# One line of `ork agent sessions events stream` in, two lines out:
# 1. control fields, separated by \u001f: frame, event id, old?, type, stop reason, blocking ids, error
# 2. what to print for the participant (may be empty, may span lines)
# A stream line is either {"id": <frame>, "event": ..., "data": <event>} or the bare event.
# $since is the processed_at of our own message: anything older is from an earlier turn.
def wrapped: type == "object" and has("data") and (.data | type) == "object";
# Timestamps compare as strings once the fraction has exactly six digits.
def ts_key: (capture("^(?<base>[^.Z+]+)(\\.(?<frac>[0-9]+))?") // {base: ., frac: ""})
| .base + "." + ((.frac // "") + "000000")[0:6];
def shorten: if length > 160 then .[0:159] + "…" else . end;
# Python's json.dumps spacing, so every language prints the same line.
def pyjson:
if type == "object" then "{" + ([to_entries[] | (.key | tojson) + ": " + (.value | pyjson)] | join(", ")) + "}"
elif type == "array" then "[" + (map(pyjson) | join(", ")) + "]"
else tojson end;
def clean: tostring | gsub("[\n\r\u001f]"; " ");
# Servers report a retry in one of two places: beside the error, or inside it.
def will_retry: (.retry_status.will_retry == true) or (.error.retry_status.type == "retrying");
(if wrapped then (.id // "" | tostring) else "" end) as $frame
| (if wrapped then .data else . end) as $e
| ($e.processed_at // "") as $at
| ($e.error.message // $e.error.type // "unknown error") as $error
| [
$frame,
($e.id // ""),
(($since != "" and $at != "" and (($at | ts_key) < ($since | ts_key))) | tostring),
($e.type // ""),
(if $e.type == "session.status_idle" then $e.stop_reason.type // "" else "" end),
(if $e.type == "session.status_idle" then ($e.stop_reason.event_ids // []) | join(" ") else "" end),
(if $e.type == "session.error" then $error else "" end)
]
| map(clean) | join("\u001f"),
(
if $e.type == "agent.message" then
"[agent] " + ([($e.content // [])[] | select(type == "object" and .type == "text") | .text // ""] | join(""))
elif $e.type == "agent.mcp_tool_use" then
"[tool] \($e.name) \(($e.input // {}) | pyjson)" | shorten
elif $e.type == "agent.mcp_tool_result" then
((if $e.is_error then "[error] " else "[result] " end)
+ ([($e.content // [])[] | select(type == "object") | .text // ""] | join(" "))) | shorten
elif $e.type == "session.error" then
"[error] " + $error + (if ($e | will_retry) then " (retrying)" else "" end)
else "" end
)