This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.py
More file actions
334 lines (263 loc) · 11.9 KB
/
format.py
File metadata and controls
334 lines (263 loc) · 11.9 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import typing as t
from datetime import datetime
from rich import box
from rich.console import Group, RenderableType
from rich.panel import Panel
from rich.pretty import Pretty
from rich.table import Table
from rich.text import Text
from dreadnode_cli import api
P = t.ParamSpec("P")
# um@ is added to indicate a user model
USER_MODEL_PREFIX: str = "um@"
def get_status_style(status: api.Client.StrikeRunStatus | api.Client.StrikeRunZoneStatus | None) -> str:
return (
{
"pending": "dim",
"running": "bold cyan",
"completed": "bold green",
"mixed": "bold gold3",
"terminated": "bold dark_orange3",
"failed": "bold red",
"timeout": "bold yellow",
}.get(status, "")
if status
else ""
)
def get_model_provider_style(provider: str) -> str:
return {
"OpenAI": "turquoise4",
"Hugging Face": "dark_cyan",
"Anthropic": "cornflower_blue",
"Google": "cyan",
"MistralAI": "light_salmon3",
"Groq": "grey63",
}.get(provider, "")
def pretty_container_logs(logs: str) -> str:
lines: list[str] = []
for line in logs.splitlines():
parts = line.split(" ", 1)
lines.append(f"[dim]{parts[0]}[/] {parts[1]}")
return "\n".join(lines)
def format_duration(start: datetime | None, end: datetime | None) -> str:
start = start.astimezone() if start else None
end = (end or datetime.now()).astimezone()
if not start:
return "..."
return f"{(end - start).total_seconds():.1f}s"
def format_time(dt: datetime | None) -> str:
return dt.astimezone().strftime("%c") if dt else "-"
def format_strike_models(models: list[api.Client.StrikeModel]) -> RenderableType:
table = Table(box=box.ROUNDED)
table.add_column("key")
table.add_column("name")
table.add_column("provider")
for model in models:
provider_style = get_model_provider_style(model.provider)
table.add_row(
Text(model.key),
Text(model.name, style=f"bold {provider_style}"),
Text(model.provider, style=provider_style),
)
return table
def format_strikes(strikes: list[api.Client.StrikeSummaryResponse]) -> RenderableType:
table = Table(box=box.ROUNDED)
table.add_column("key")
table.add_column("name")
table.add_column("type")
table.add_column("competitive", justify="center")
table.add_column("models")
def _format_model(model: api.Client.StrikeModel) -> str:
provider_style = get_model_provider_style(model.provider)
return f"[{provider_style}]{model.key}[/]"
for strike in strikes:
table.add_row(
Text(strike.key),
Text(strike.name, style="bold cyan"),
Text(strike.type, style="magenta"),
":skull:" if strike.competitive else "-",
", ".join(_format_model(model) for model in strike.models),
)
return table
def format_agent(agent: api.Client.StrikeAgentResponse) -> RenderableType:
table = Table(show_header=False, box=box.ROUNDED)
table.add_column("Property", justify="right")
table.add_column("Value")
table.add_row("id", f"[dim]{agent.id}[/]")
table.add_row("key", f"[magenta]{agent.key}[/]")
table.add_row("name", f"[magenta]{agent.name or '-'}[/]")
table.add_row("revision", f"[yellow]{agent.revision}[/]")
table.add_row("last status", Text(agent.latest_run_status or "-", style=get_status_style(agent.latest_run_status)))
table.add_row("created", f"[cyan]{agent.created_at.astimezone().strftime('%c')}[/]")
table.add_row("", "")
latest_table = Table(show_header=False, box=box.ROUNDED)
latest_table.add_column("Property", justify="right")
latest_table.add_column("Value")
latest_table.add_row("id", f"[dim]{agent.latest_version.id}[/]")
latest_table.add_row("created", f"[cyan]{agent.latest_version.created_at.astimezone().strftime('%c')}[/]")
latest_table.add_row("notes", agent.latest_version.notes or "-")
table.add_row("latest", latest_table)
return table
def format_agent_versions(agent: api.Client.StrikeAgentResponse) -> RenderableType:
table = Table(box=box.ROUNDED)
table.add_column("rev", style="yellow")
table.add_column("notes", style="cyan")
table.add_column("image")
table.add_column("created")
for i, version in enumerate(sorted(agent.versions, key=lambda v: v.created_at)):
latest = version.id == agent.latest_version.id
table.add_row(
str(i + 1) + ("*" if latest else ""),
version.notes or "-",
version.container.image,
f"[dim]{version.created_at.astimezone().strftime('%c')}[/]",
style="bold" if latest else None,
)
return table
def format_zones_summary(zones: list[api.Client.StrikeRunZone]) -> RenderableType:
table = Table(box=box.SIMPLE, padding=(0, 1))
table.add_column("zone", style="cyan")
table.add_column("status")
table.add_column("duration")
table.add_column("inferences", justify="center")
table.add_column("outputs", justify="center")
table.add_column("score", justify="center")
for zone in zones:
zone_score = sum(
output.score.value if hasattr(output, "score") and output.score else 0 for output in zone.outputs
)
if isinstance(zone_score, float):
zone_score = round(zone_score, 2)
table.add_row(
zone.key,
Text(zone.status, style=get_status_style(zone.status)),
Text(format_duration(zone.start, zone.end) if zone.end else "...", style="bold"),
Text(str(len(zone.inferences)) if zone.inferences else "-", style="dim"),
Text(str(len(zone.outputs)) if zone.outputs else "-", style="magenta"),
Text(str(zone_score), style="yellow" if zone_score > 0 else "dim"),
)
return table
def format_zones_verbose(zones: list[api.Client.StrikeRunZone], *, include_logs: bool = False) -> RenderableType:
components: list[RenderableType] = []
for zone in zones:
table = Table(show_header=False, box=box.SIMPLE)
table.add_column("Property", style="dim")
table.add_column("Value")
zone_score = sum(output.score.value if output.score else 0 for output in zone.outputs)
if isinstance(zone_score, float):
zone_score = round(zone_score, 2)
table.add_row("score", f"[yellow]{zone_score}[/]" if zone_score else "[dim]0[/]")
table.add_row("outputs", f"[magenta]{len(zone.outputs)}[/]" if zone.outputs else "[dim]0[/]")
table.add_row("inferences", f"[blue]{len(zone.inferences)}[/]" if zone.inferences else "[dim]0[/]")
table.add_row("start", format_time(zone.start))
table.add_row("end", format_time(zone.end))
table.add_row("duration", Text(format_duration(zone.start, zone.end), style="bold cyan"))
table.add_row("", "")
sub_components: list[RenderableType] = [table]
if zone.outputs:
outputs_table = Table(box=box.SIMPLE, padding=(0, 1))
outputs_table.add_column("score", justify="center", style="yellow")
outputs_table.add_column("output", style="cyan")
outputs_table.add_column("explanation")
for output in zone.outputs:
if output.score and isinstance(output.score.value, float):
output.score.value = round(output.score.value, 3)
outputs_table.add_row(
str(output.score.value) if output.score else "-",
Pretty(output.data),
output.score.explanation if output.score else "-",
)
outputs_panel = Panel(
outputs_table,
title="outputs",
title_align="left",
style="magenta",
)
sub_components.append(outputs_panel)
if include_logs and zone.agent_logs:
agent_log_panel = Panel(
pretty_container_logs(zone.agent_logs),
title="[dim]logs:[/] [bold]agent[/]",
title_align="left",
style="cyan",
)
sub_components.append(agent_log_panel)
if include_logs:
for container, logs in zone.container_logs.items():
container_log_panel = Panel(
pretty_container_logs(logs),
title=f"[dim]logs:[/] [bold]{container}[/]",
title_align="left",
style="blue",
)
sub_components.append(container_log_panel)
panel = Panel(
Group(*sub_components),
title=f"[dim]zone:[/] [bold]{zone.key} [{get_status_style(zone.status)}]({zone.status})[/]",
title_align="left",
border_style="cyan",
)
components.append(panel)
return Group(*components)
def format_run(
run: api.Client.StrikeRunResponse, *, verbose: bool = False, include_logs: bool = False, server_url: str = ""
) -> Panel:
# Main run information
table = Table(show_header=False, box=box.SIMPLE)
table.add_column("Property", style="dim")
table.add_column("Value")
table.add_row("status", Text(run.status, style=get_status_style(run.status)))
table.add_row("strike", f"[magenta]{run.strike_name}[/] ([dim]{run.strike_key}[/])")
table.add_row("type", run.strike_type)
if run.agent_name:
agent_name = f"[bold magenta]{run.agent_name}[/] [[dim]{run.agent_key}[/]]"
else:
agent_name = f"[bold magenta]{run.agent_key}[/]"
table.add_row("", "")
table.add_row("model", run.model.replace(USER_MODEL_PREFIX, "") if run.model else "<default>")
table.add_row("agent", f"{agent_name} ([dim]rev[/] [yellow]{run.agent_revision}[/])")
table.add_row("image", Text(run.agent_version.container.image, style="cyan"))
if server_url != "":
table.add_row(
"run url", Text(f"{server_url.rstrip('/')}/strikes/agents/{run.agent_key}/runs/{run.id}", style="cyan")
)
table.add_row("notes", run.agent_version.notes or "-")
table.add_row("", "")
table.add_row("duration", Text(format_duration(run.start, run.end), style="bold cyan"))
table.add_row("start", format_time(run.start))
table.add_row("end", format_time(run.end))
if run.context and (run.context.environment or run.context.parameters or run.context.command):
table.add_row("", "")
if run.context.environment:
table.add_row(
"environment", " ".join(f"[magenta]{k}[/]=[yellow]{v}[/]" for k, v in run.context.environment.items())
)
if run.context.parameters:
table.add_row(
"parameters", " ".join(f"[magenta]{k}[/]=[yellow]{v}[/]" for k, v in run.context.parameters.items())
)
if run.context.command:
table.add_row("command", f"[bold][red]{run.context.command}[/red][/bold]")
components: list[RenderableType] = [
table,
format_zones_verbose(run.zones, include_logs=include_logs) if verbose else format_zones_summary(run.zones),
]
return Panel(Group(*components), title=f"[bold]run [dim]{run.id}[/]", title_align="left", border_style="blue")
def format_runs(runs: list[api.Client.StrikeRunSummaryResponse]) -> RenderableType:
table = Table(box=box.ROUNDED)
table.add_column("id", style="dim")
table.add_column("agent")
table.add_column("status")
table.add_column("model")
table.add_column("started")
table.add_column("duration")
for run in runs:
table.add_row(
str(run.id),
f"[bold magenta]{run.agent_key}[/] [dim]:[/] [yellow]{run.agent_revision}[/]",
Text(run.status, style="bold " + get_status_style(run.status)),
Text(run.model.replace(USER_MODEL_PREFIX, "") if run.model else "-"),
format_time(run.start),
Text(format_duration(run.start, run.end), style="bold cyan"),
)
return table