opencode-usage-v2 0.2.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opencode_usage/__init__.py +18 -0
- opencode_usage/__main__.py +5 -0
- opencode_usage/_opencode_cli.py +158 -0
- opencode_usage/auth.py +82 -0
- opencode_usage/cli.py +271 -0
- opencode_usage/db.py +712 -0
- opencode_usage/insights/__init__.py +19 -0
- opencode_usage/insights/analyze.py +384 -0
- opencode_usage/insights/cache.py +60 -0
- opencode_usage/insights/extract.py +600 -0
- opencode_usage/insights/orchestrator.py +159 -0
- opencode_usage/insights/prompts.py +306 -0
- opencode_usage/insights/report.py +574 -0
- opencode_usage/insights/types.py +75 -0
- opencode_usage/llm.py +93 -0
- opencode_usage/models.py +145 -0
- opencode_usage/render.py +240 -0
- opencode_usage_v2-0.2.5.dist-info/METADATA +217 -0
- opencode_usage_v2-0.2.5.dist-info/RECORD +22 -0
- opencode_usage_v2-0.2.5.dist-info/WHEEL +4 -0
- opencode_usage_v2-0.2.5.dist-info/entry_points.txt +3 -0
- opencode_usage_v2-0.2.5.dist-info/licenses/LICENSE +22 -0
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
"""HTML report generation for OpenCode usage insights."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from opencode_usage.insights.types import AggregatedStats
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def generate_css() -> str:
|
|
12
|
+
"""Terminal-hacker-aesthetic CSS for the insights report."""
|
|
13
|
+
return """
|
|
14
|
+
:root {
|
|
15
|
+
--bg-primary: #0d1117;
|
|
16
|
+
--bg-secondary: #161b22;
|
|
17
|
+
--bg-tertiary: #21262d;
|
|
18
|
+
--text-primary: #c9d1d9;
|
|
19
|
+
--text-muted: #8b949e;
|
|
20
|
+
--border: #30363d;
|
|
21
|
+
--accent-green: #3fb950;
|
|
22
|
+
--accent-cyan: #58a6ff;
|
|
23
|
+
--accent-amber: #d29922;
|
|
24
|
+
--accent-red: #f85149;
|
|
25
|
+
--accent-purple: #bc8cff;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
29
|
+
|
|
30
|
+
body {
|
|
31
|
+
background: var(--bg-primary);
|
|
32
|
+
color: var(--text-primary);
|
|
33
|
+
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
|
|
34
|
+
font-size: 14px;
|
|
35
|
+
line-height: 1.6;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.container {
|
|
39
|
+
max-width: 1200px;
|
|
40
|
+
margin: 0 auto;
|
|
41
|
+
padding: 0 24px;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
nav {
|
|
45
|
+
position: sticky;
|
|
46
|
+
top: 0;
|
|
47
|
+
background: var(--bg-secondary);
|
|
48
|
+
border-bottom: 1px solid var(--border);
|
|
49
|
+
padding: 12px 0;
|
|
50
|
+
z-index: 100;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
nav .container {
|
|
54
|
+
display: flex;
|
|
55
|
+
gap: 16px;
|
|
56
|
+
flex-wrap: wrap;
|
|
57
|
+
align-items: center;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
nav a {
|
|
61
|
+
color: var(--text-muted);
|
|
62
|
+
text-decoration: none;
|
|
63
|
+
font-size: 12px;
|
|
64
|
+
padding: 4px 8px;
|
|
65
|
+
border-radius: 4px;
|
|
66
|
+
transition: color 0.2s;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
nav a:hover { color: var(--accent-cyan); }
|
|
70
|
+
|
|
71
|
+
header {
|
|
72
|
+
padding: 48px 0 32px;
|
|
73
|
+
border-bottom: 1px solid var(--border);
|
|
74
|
+
margin-bottom: 48px;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
header h1 {
|
|
78
|
+
font-size: 24px;
|
|
79
|
+
color: var(--accent-green);
|
|
80
|
+
margin-bottom: 8px;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
header p { color: var(--text-muted); }
|
|
84
|
+
|
|
85
|
+
section {
|
|
86
|
+
margin-bottom: 48px;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
h2 {
|
|
90
|
+
font-size: 18px;
|
|
91
|
+
color: var(--accent-cyan);
|
|
92
|
+
margin-bottom: 16px;
|
|
93
|
+
padding-bottom: 8px;
|
|
94
|
+
border-bottom: 1px solid var(--border);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
h3 {
|
|
98
|
+
font-size: 14px;
|
|
99
|
+
color: var(--text-primary);
|
|
100
|
+
margin-bottom: 8px;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.card {
|
|
104
|
+
background: var(--bg-secondary);
|
|
105
|
+
border: 1px solid var(--border);
|
|
106
|
+
border-radius: 6px;
|
|
107
|
+
padding: 16px;
|
|
108
|
+
margin-bottom: 12px;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.stat-card {
|
|
112
|
+
text-align: center;
|
|
113
|
+
padding: 20px 16px;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
.stat-value {
|
|
117
|
+
font-size: 28px;
|
|
118
|
+
font-weight: 700;
|
|
119
|
+
color: var(--accent-green);
|
|
120
|
+
display: block;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
.stat-label {
|
|
124
|
+
font-size: 11px;
|
|
125
|
+
text-transform: uppercase;
|
|
126
|
+
color: var(--text-muted);
|
|
127
|
+
letter-spacing: 0.05em;
|
|
128
|
+
margin-top: 4px;
|
|
129
|
+
display: block;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
.stat-subtitle {
|
|
133
|
+
font-size: 11px;
|
|
134
|
+
color: var(--text-muted);
|
|
135
|
+
margin-top: 4px;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
.stats-row {
|
|
139
|
+
display: flex;
|
|
140
|
+
gap: 12px;
|
|
141
|
+
flex-wrap: wrap;
|
|
142
|
+
margin-bottom: 24px;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.stats-row .stat-card {
|
|
146
|
+
flex: 1;
|
|
147
|
+
min-width: 120px;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.bar-chart { margin-bottom: 16px; }
|
|
151
|
+
|
|
152
|
+
.bar-chart-title {
|
|
153
|
+
font-size: 12px;
|
|
154
|
+
color: var(--text-muted);
|
|
155
|
+
text-transform: uppercase;
|
|
156
|
+
margin-bottom: 8px;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.bar-row {
|
|
160
|
+
display: flex;
|
|
161
|
+
align-items: center;
|
|
162
|
+
gap: 8px;
|
|
163
|
+
margin-bottom: 6px;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
.bar-label {
|
|
167
|
+
width: 120px;
|
|
168
|
+
font-size: 12px;
|
|
169
|
+
color: var(--text-primary);
|
|
170
|
+
text-align: right;
|
|
171
|
+
flex-shrink: 0;
|
|
172
|
+
overflow: hidden;
|
|
173
|
+
text-overflow: ellipsis;
|
|
174
|
+
white-space: nowrap;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.bar-track {
|
|
178
|
+
flex: 1;
|
|
179
|
+
height: 8px;
|
|
180
|
+
background: var(--bg-tertiary);
|
|
181
|
+
border-radius: 4px;
|
|
182
|
+
overflow: hidden;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.bar-fill {
|
|
186
|
+
height: 100%;
|
|
187
|
+
border-radius: 4px;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.bar-value {
|
|
191
|
+
width: 60px;
|
|
192
|
+
font-size: 11px;
|
|
193
|
+
color: var(--text-muted);
|
|
194
|
+
flex-shrink: 0;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
.narrative { margin-bottom: 16px; }
|
|
198
|
+
|
|
199
|
+
.narrative p {
|
|
200
|
+
color: var(--text-primary);
|
|
201
|
+
margin-bottom: 8px;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
.key-insight {
|
|
205
|
+
background: var(--bg-tertiary);
|
|
206
|
+
border-left: 3px solid var(--accent-amber);
|
|
207
|
+
padding: 12px 16px;
|
|
208
|
+
margin-top: 12px;
|
|
209
|
+
border-radius: 0 4px 4px 0;
|
|
210
|
+
font-size: 13px;
|
|
211
|
+
color: var(--accent-amber);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
.accent-card {
|
|
215
|
+
border-left: 3px solid var(--accent-green);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
.list-items { list-style: none; padding: 0; }
|
|
219
|
+
|
|
220
|
+
.list-items li {
|
|
221
|
+
padding: 6px 0;
|
|
222
|
+
border-bottom: 1px solid var(--border);
|
|
223
|
+
font-size: 13px;
|
|
224
|
+
color: var(--text-primary);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
.list-items li:last-child { border-bottom: none; }
|
|
228
|
+
|
|
229
|
+
.list-items li::before {
|
|
230
|
+
content: '\\25b8 ';
|
|
231
|
+
color: var(--accent-green);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
.tag {
|
|
235
|
+
display: inline-block;
|
|
236
|
+
background: var(--bg-tertiary);
|
|
237
|
+
border: 1px solid var(--border);
|
|
238
|
+
border-radius: 12px;
|
|
239
|
+
padding: 2px 8px;
|
|
240
|
+
font-size: 11px;
|
|
241
|
+
color: var(--text-muted);
|
|
242
|
+
margin: 2px;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
footer {
|
|
246
|
+
border-top: 1px solid var(--border);
|
|
247
|
+
padding: 24px 0;
|
|
248
|
+
margin-top: 48px;
|
|
249
|
+
color: var(--text-muted);
|
|
250
|
+
font-size: 12px;
|
|
251
|
+
text-align: center;
|
|
252
|
+
}
|
|
253
|
+
"""
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def generate_html_skeleton(title: str, body_html: str) -> str:
|
|
257
|
+
"""Wrap body HTML in a complete HTML document with embedded CSS."""
|
|
258
|
+
css = generate_css()
|
|
259
|
+
return (
|
|
260
|
+
"<!DOCTYPE html>\n"
|
|
261
|
+
'<html lang="en">\n'
|
|
262
|
+
"<head>\n"
|
|
263
|
+
'<meta charset="utf-8">\n'
|
|
264
|
+
'<meta name="viewport" content="width=device-width, initial-scale=1">\n'
|
|
265
|
+
f"<title>{title}</title>\n"
|
|
266
|
+
"<style>\n"
|
|
267
|
+
f"{css}\n"
|
|
268
|
+
"</style>\n"
|
|
269
|
+
"</head>\n"
|
|
270
|
+
"<body>\n"
|
|
271
|
+
f"{body_html}\n"
|
|
272
|
+
"</body>\n"
|
|
273
|
+
"</html>"
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def render_stat_card(label: str, value: str, subtitle: str = "") -> str:
|
|
278
|
+
"""Render a single stat card with a prominent value."""
|
|
279
|
+
sub = f'<span class="stat-subtitle">{subtitle}</span>' if subtitle else ""
|
|
280
|
+
return (
|
|
281
|
+
'<div class="card stat-card">'
|
|
282
|
+
f'<span class="stat-value">{value}</span>'
|
|
283
|
+
f'<span class="stat-label">{label}</span>'
|
|
284
|
+
f"{sub}"
|
|
285
|
+
"</div>"
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def render_bar_chart(title: str, items: list[tuple[str, float, str]]) -> str:
|
|
290
|
+
"""Render a CSS-only horizontal bar chart. items = [(label, value, color)]."""
|
|
291
|
+
if not items:
|
|
292
|
+
return (
|
|
293
|
+
f'<div class="bar-chart">'
|
|
294
|
+
f'<p class="bar-chart-title">{title}</p>'
|
|
295
|
+
'<p style="color:var(--text-muted)">No data</p>'
|
|
296
|
+
"</div>"
|
|
297
|
+
)
|
|
298
|
+
max_val = max(v for _, v, _ in items) or 1
|
|
299
|
+
rows = []
|
|
300
|
+
for label, value, color in items:
|
|
301
|
+
pct = round((value / max_val) * 100)
|
|
302
|
+
rows.append(
|
|
303
|
+
'<div class="bar-row">'
|
|
304
|
+
f'<span class="bar-label">{label}</span>'
|
|
305
|
+
'<div class="bar-track">'
|
|
306
|
+
f'<div class="bar-fill" style="width:{pct}%;background:{color}"></div>'
|
|
307
|
+
"</div>"
|
|
308
|
+
f'<span class="bar-value">{int(value):,}</span>'
|
|
309
|
+
"</div>"
|
|
310
|
+
)
|
|
311
|
+
return f'<div class="bar-chart"><p class="bar-chart-title">{title}</p>{"".join(rows)}</div>'
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def render_stats_row(stats: list[tuple[str, str]]) -> str:
|
|
315
|
+
"""Render a flex row of stat cards."""
|
|
316
|
+
cards = "".join(render_stat_card(label, value) for label, value in stats)
|
|
317
|
+
return f'<div class="stats-row">{cards}</div>'
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def render_section(section_id: str, title: str, content_html: str) -> str:
|
|
321
|
+
"""Render a report section with anchor id and h2 heading."""
|
|
322
|
+
return f'<section id="{section_id}"><h2>{title}</h2>{content_html}</section>'
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def render_narrative(paragraphs: list[str], key_insight: str = "") -> str:
|
|
326
|
+
"""Render narrative paragraphs with optional highlighted key insight."""
|
|
327
|
+
paras = "".join(f"<p>{p}</p>" for p in paragraphs)
|
|
328
|
+
insight_html = f'<div class="key-insight">{key_insight}</div>' if key_insight else ""
|
|
329
|
+
return f'<div class="narrative">{paras}{insight_html}</div>'
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def render_card(title: str, description: str, accent_color: str = "") -> str:
|
|
333
|
+
"""Render a generic info card with optional left-border accent."""
|
|
334
|
+
style = (
|
|
335
|
+
f' style="border-left-color:{accent_color};border-left-width:3px;border-left-style:solid"'
|
|
336
|
+
)
|
|
337
|
+
accent_cls = " accent-card" if accent_color else ""
|
|
338
|
+
border_style = style if accent_color else ""
|
|
339
|
+
return f'<div class="card{accent_cls}"{border_style}><h3>{title}</h3><p>{description}</p></div>'
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def render_nav_toc(sections: list[tuple[str, str]]) -> str:
|
|
343
|
+
"""Render a sticky nav bar with section links."""
|
|
344
|
+
links = "".join(f'<a href="#{sid}">{label}</a>' for sid, label in sections)
|
|
345
|
+
return f'<nav><div class="container">{links}</div></nav>'
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _fmt_cost(cost: float) -> str:
|
|
349
|
+
"""Format cost as dollar string."""
|
|
350
|
+
if cost == 0:
|
|
351
|
+
return "$0.00"
|
|
352
|
+
return f"${cost:.2f}"
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def _fmt_date_range(date_range: tuple[int, int]) -> str:
|
|
356
|
+
"""Format ms epoch date range as human-readable string."""
|
|
357
|
+
try:
|
|
358
|
+
start = datetime.fromtimestamp(date_range[0] / 1000, tz=timezone.utc).strftime("%Y-%m-%d")
|
|
359
|
+
end = datetime.fromtimestamp(date_range[1] / 1000, tz=timezone.utc).strftime("%Y-%m-%d")
|
|
360
|
+
return f"{start} \u2192 {end}"
|
|
361
|
+
except Exception:
|
|
362
|
+
return "\u2014"
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def generate_report(insights_data: dict[str, Any]) -> str:
|
|
366
|
+
"""Assemble the full 9-section terminal-style HTML report."""
|
|
367
|
+
stats: AggregatedStats = insights_data.get("aggregated_stats") or AggregatedStats(
|
|
368
|
+
total_sessions=0,
|
|
369
|
+
analyzed_sessions=0,
|
|
370
|
+
date_range=(0, 0),
|
|
371
|
+
total_messages=0,
|
|
372
|
+
total_cost=0.0,
|
|
373
|
+
)
|
|
374
|
+
at_a_glance = insights_data.get("at_a_glance") or {}
|
|
375
|
+
project_areas = insights_data.get("project_areas") or {}
|
|
376
|
+
interaction_style = insights_data.get("interaction_style") or {}
|
|
377
|
+
agent_performance = insights_data.get("agent_performance") or {}
|
|
378
|
+
friction = insights_data.get("friction") or {}
|
|
379
|
+
suggestions = insights_data.get("suggestions") or {}
|
|
380
|
+
tool_health = insights_data.get("tool_health") or {}
|
|
381
|
+
horizon = insights_data.get("horizon") or {}
|
|
382
|
+
delegation_stats = insights_data.get("delegation_stats") or {}
|
|
383
|
+
|
|
384
|
+
nav_sections = [
|
|
385
|
+
("at-a-glance", "At a Glance"),
|
|
386
|
+
("work-areas", "Work Areas"),
|
|
387
|
+
("agent-performance", "Agents"),
|
|
388
|
+
("cost-intelligence", "Cost"),
|
|
389
|
+
("interaction-friction", "Interaction"),
|
|
390
|
+
("suggestions", "Suggestions"),
|
|
391
|
+
("delegation-topology", "Delegation"),
|
|
392
|
+
("tool-health", "Tools"),
|
|
393
|
+
("horizon", "Horizon"),
|
|
394
|
+
]
|
|
395
|
+
|
|
396
|
+
nav_html = render_nav_toc(nav_sections)
|
|
397
|
+
|
|
398
|
+
date_str = _fmt_date_range(stats.date_range)
|
|
399
|
+
header_html = (
|
|
400
|
+
'<header><div class="container">'
|
|
401
|
+
"<h1>\u25b8 OpenCode Usage Insights</h1>"
|
|
402
|
+
f"<p>{date_str} \u00b7 {stats.total_sessions} sessions"
|
|
403
|
+
f" \u00b7 {stats.total_messages} messages"
|
|
404
|
+
f" \u00b7 {_fmt_cost(stats.total_cost)}</p>"
|
|
405
|
+
"</div></header>"
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
stats_html = render_stats_row(
|
|
409
|
+
[
|
|
410
|
+
("Sessions", str(stats.total_sessions)),
|
|
411
|
+
("Analyzed", str(stats.analyzed_sessions)),
|
|
412
|
+
("Messages", f"{stats.total_messages:,}"),
|
|
413
|
+
("Total Cost", _fmt_cost(stats.total_cost)),
|
|
414
|
+
]
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
# Section 1: At a Glance
|
|
418
|
+
if at_a_glance:
|
|
419
|
+
working = at_a_glance.get("whats_working", "")
|
|
420
|
+
hindering = at_a_glance.get("whats_hindering", "")
|
|
421
|
+
quick_wins = at_a_glance.get("quick_wins") or []
|
|
422
|
+
ambitious = at_a_glance.get("ambitious_workflows") or []
|
|
423
|
+
wins_html = ""
|
|
424
|
+
if quick_wins:
|
|
425
|
+
items = "".join(f"<li>{w}</li>" for w in quick_wins)
|
|
426
|
+
wins_html = f'<h3>Quick Wins</h3><ul class="list-items">{items}</ul>'
|
|
427
|
+
amb_html = ""
|
|
428
|
+
if ambitious:
|
|
429
|
+
items = "".join(f"<li>{a}</li>" for a in ambitious)
|
|
430
|
+
amb_html = f'<h3>Ambitious Workflows</h3><ul class="list-items">{items}</ul>'
|
|
431
|
+
aag_content = (
|
|
432
|
+
stats_html
|
|
433
|
+
+ (render_card("What's Working", working, "#3fb950") if working else "")
|
|
434
|
+
+ (render_card("What's Hindering", hindering, "#f85149") if hindering else "")
|
|
435
|
+
+ wins_html
|
|
436
|
+
+ amb_html
|
|
437
|
+
)
|
|
438
|
+
else:
|
|
439
|
+
aag_content = (
|
|
440
|
+
stats_html
|
|
441
|
+
+ '<p style="color:var(--text-muted)">Install opencode for AI-powered analysis</p>'
|
|
442
|
+
)
|
|
443
|
+
s1 = render_section("at-a-glance", "At a Glance", aag_content)
|
|
444
|
+
|
|
445
|
+
# Section 2: What You Work On
|
|
446
|
+
areas = project_areas.get("areas") or []
|
|
447
|
+
if areas:
|
|
448
|
+
area_cards = "".join(
|
|
449
|
+
render_card(
|
|
450
|
+
a.get("name", ""),
|
|
451
|
+
f"{a.get('session_count', 0)} sessions \u2014 {a.get('description', '')}",
|
|
452
|
+
"#58a6ff",
|
|
453
|
+
)
|
|
454
|
+
for a in areas
|
|
455
|
+
)
|
|
456
|
+
s2_content = area_cards
|
|
457
|
+
else:
|
|
458
|
+
s2_content = '<p style="color:var(--text-muted)">No project area data available</p>'
|
|
459
|
+
s2 = render_section("work-areas", "What You Work On", s2_content)
|
|
460
|
+
|
|
461
|
+
# Section 3: Agent Performance
|
|
462
|
+
agent_items = [(a, float(c), "#3fb950") for a, c in stats.top_agents]
|
|
463
|
+
agent_chart = render_bar_chart("Agent Usage (calls)", agent_items) if agent_items else ""
|
|
464
|
+
agent_insight = agent_performance.get("insights", "")
|
|
465
|
+
s3_content = agent_chart + (
|
|
466
|
+
f'<div class="card"><p>{agent_insight}</p></div>' if agent_insight else ""
|
|
467
|
+
)
|
|
468
|
+
s3 = render_section("agent-performance", "Agent Performance", s3_content)
|
|
469
|
+
|
|
470
|
+
# Section 4: Cost Intelligence
|
|
471
|
+
model_items = [(m, float(c), "#58a6ff") for m, c in stats.top_models]
|
|
472
|
+
model_chart = render_bar_chart("Model Usage (calls)", model_items) if model_items else ""
|
|
473
|
+
s4_content = (
|
|
474
|
+
render_stats_row(
|
|
475
|
+
[("Total Cost", _fmt_cost(stats.total_cost)), ("Sessions", str(stats.total_sessions))]
|
|
476
|
+
)
|
|
477
|
+
+ model_chart
|
|
478
|
+
)
|
|
479
|
+
s4 = render_section("cost-intelligence", "Cost Intelligence", s4_content)
|
|
480
|
+
|
|
481
|
+
# Section 5: Interaction & Friction
|
|
482
|
+
narrative_text = interaction_style.get("narrative", "")
|
|
483
|
+
key_pattern = interaction_style.get("key_pattern", "")
|
|
484
|
+
narrative_html = render_narrative([narrative_text], key_pattern) if narrative_text else ""
|
|
485
|
+
friction_cats = friction.get("categories") or []
|
|
486
|
+
friction_html = ""
|
|
487
|
+
if friction_cats:
|
|
488
|
+
cards = "".join(
|
|
489
|
+
render_card(
|
|
490
|
+
c.get("name", ""),
|
|
491
|
+
f"Count: {c.get('count', 0)} \u2014 {c.get('example', '')}",
|
|
492
|
+
"#f85149",
|
|
493
|
+
)
|
|
494
|
+
for c in friction_cats
|
|
495
|
+
)
|
|
496
|
+
friction_html = f"<h3>Friction Patterns</h3>{cards}"
|
|
497
|
+
s5_content = narrative_html + friction_html or (
|
|
498
|
+
'<p style="color:var(--text-muted)">No interaction data available</p>'
|
|
499
|
+
)
|
|
500
|
+
s5 = render_section("interaction-friction", "Interaction & Friction", s5_content)
|
|
501
|
+
|
|
502
|
+
# Section 6: Suggestions
|
|
503
|
+
agents_md = suggestions.get("agents_md_additions") or []
|
|
504
|
+
skills = suggestions.get("skill_candidates") or []
|
|
505
|
+
workflows = suggestions.get("workflow_patterns") or []
|
|
506
|
+
s6_parts = []
|
|
507
|
+
if agents_md:
|
|
508
|
+
items = "".join(f"<li>{s}</li>" for s in agents_md)
|
|
509
|
+
s6_parts.append(f'<h3>AGENTS.md Additions</h3><ul class="list-items">{items}</ul>')
|
|
510
|
+
if skills:
|
|
511
|
+
tags = "".join(f'<span class="tag">{s}</span>' for s in skills)
|
|
512
|
+
s6_parts.append(f"<h3>Skill Candidates</h3><p>{tags}</p>")
|
|
513
|
+
if workflows:
|
|
514
|
+
items = "".join(f"<li>{w}</li>" for w in workflows)
|
|
515
|
+
s6_parts.append(f'<h3>Workflow Patterns</h3><ul class="list-items">{items}</ul>')
|
|
516
|
+
s6_content = "".join(s6_parts) or (
|
|
517
|
+
'<p style="color:var(--text-muted)">No suggestions available</p>'
|
|
518
|
+
)
|
|
519
|
+
s6 = render_section("suggestions", "Suggestions", s6_content)
|
|
520
|
+
|
|
521
|
+
# Section 7: Delegation Topology
|
|
522
|
+
sub_types = delegation_stats.get("sub_types") or {}
|
|
523
|
+
deleg_items = [(k, float(v), "#bc8cff") for k, v in sub_types.items()]
|
|
524
|
+
deleg_chart = render_bar_chart("Sub-session Types", deleg_items) if deleg_items else ""
|
|
525
|
+
deleg_stats_html = render_stats_row(
|
|
526
|
+
[
|
|
527
|
+
("Root Sessions", str(delegation_stats.get("root_sessions", 0))),
|
|
528
|
+
("Sub-sessions", str(delegation_stats.get("sub_sessions", 0))),
|
|
529
|
+
("Max Depth", str(delegation_stats.get("max_depth", 0))),
|
|
530
|
+
]
|
|
531
|
+
)
|
|
532
|
+
s7 = render_section(
|
|
533
|
+
"delegation-topology", "Delegation Topology", deleg_stats_html + deleg_chart
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
# Section 8: Tool Health
|
|
537
|
+
tool_items = [(t, float(c), "#d29922") for t, c in stats.top_tools]
|
|
538
|
+
tool_chart = render_bar_chart("Tool Usage (calls)", tool_items) if tool_items else ""
|
|
539
|
+
tool_insight = tool_health.get("insights", "")
|
|
540
|
+
tool_tips = tool_health.get("tips") or []
|
|
541
|
+
tips_html = ""
|
|
542
|
+
if tool_tips:
|
|
543
|
+
items = "".join(f"<li>{t}</li>" for t in tool_tips)
|
|
544
|
+
tips_html = f'<ul class="list-items">{items}</ul>'
|
|
545
|
+
s8_content = (
|
|
546
|
+
tool_chart
|
|
547
|
+
+ (f'<div class="card"><p>{tool_insight}</p></div>' if tool_insight else "")
|
|
548
|
+
+ tips_html
|
|
549
|
+
)
|
|
550
|
+
s8 = render_section("tool-health", "Tool Health", s8_content)
|
|
551
|
+
|
|
552
|
+
# Section 9: On the Horizon
|
|
553
|
+
opportunities = horizon.get("opportunities") or []
|
|
554
|
+
if opportunities:
|
|
555
|
+
opp_cards = "".join(
|
|
556
|
+
render_card(o.get("title", ""), o.get("description", ""), "#bc8cff")
|
|
557
|
+
for o in opportunities
|
|
558
|
+
)
|
|
559
|
+
s9_content = opp_cards
|
|
560
|
+
else:
|
|
561
|
+
s9_content = '<p style="color:var(--text-muted)">No horizon data available</p>'
|
|
562
|
+
s9 = render_section("horizon", "On the Horizon", s9_content)
|
|
563
|
+
|
|
564
|
+
footer_html = (
|
|
565
|
+
'<footer><div class="container"><p>Generated by opencode-usage insights</p></div></footer>'
|
|
566
|
+
)
|
|
567
|
+
|
|
568
|
+
body = (
|
|
569
|
+
nav_html
|
|
570
|
+
+ header_html
|
|
571
|
+
+ f'<main class="container">{s1}{s2}{s3}{s4}{s5}{s6}{s7}{s8}{s9}</main>'
|
|
572
|
+
+ footer_html
|
|
573
|
+
)
|
|
574
|
+
return generate_html_skeleton("OpenCode Usage Insights", body)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Data types for OpenCode insights extraction and analysis."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class SessionFacet:
|
|
11
|
+
"""Extracted facet information from a single session."""
|
|
12
|
+
|
|
13
|
+
session_id: str
|
|
14
|
+
underlying_goal: str
|
|
15
|
+
goal_categories: dict[str, int] = field(default_factory=dict)
|
|
16
|
+
outcome: str = ""
|
|
17
|
+
satisfaction: dict[str, int] = field(default_factory=dict)
|
|
18
|
+
helpfulness: str = ""
|
|
19
|
+
session_type: str = ""
|
|
20
|
+
friction_counts: dict[str, int] = field(default_factory=dict)
|
|
21
|
+
friction_detail: str = ""
|
|
22
|
+
primary_success: str = ""
|
|
23
|
+
brief_summary: str = ""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class SessionMeta:
|
|
28
|
+
"""Metadata about a session for insights analysis."""
|
|
29
|
+
|
|
30
|
+
id: str
|
|
31
|
+
title: str
|
|
32
|
+
project_path: str | None = None
|
|
33
|
+
parent_id: str | None = None
|
|
34
|
+
duration_minutes: float = 0.0
|
|
35
|
+
user_msg_count: int = 0
|
|
36
|
+
assistant_msg_count: int = 0
|
|
37
|
+
input_tokens: int = 0
|
|
38
|
+
output_tokens: int = 0
|
|
39
|
+
total_tokens: int = 0
|
|
40
|
+
cost: float = 0.0
|
|
41
|
+
tool_counts: dict[str, int] = field(default_factory=dict)
|
|
42
|
+
languages: dict[str, int] = field(default_factory=dict)
|
|
43
|
+
agent_counts: dict[str, int] = field(default_factory=dict)
|
|
44
|
+
model_counts: dict[str, int] = field(default_factory=dict)
|
|
45
|
+
start_time: int = 0
|
|
46
|
+
end_time: int = 0
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass
|
|
50
|
+
class AggregatedStats:
|
|
51
|
+
"""Aggregated statistics across multiple sessions."""
|
|
52
|
+
|
|
53
|
+
total_sessions: int
|
|
54
|
+
analyzed_sessions: int
|
|
55
|
+
date_range: tuple[int, int]
|
|
56
|
+
total_messages: int
|
|
57
|
+
total_cost: float
|
|
58
|
+
top_tools: list[tuple[str, int]] = field(default_factory=list)
|
|
59
|
+
top_agents: list[tuple[str, int]] = field(default_factory=list)
|
|
60
|
+
top_models: list[tuple[str, int]] = field(default_factory=list)
|
|
61
|
+
outcome_dist: dict[str, int] = field(default_factory=dict)
|
|
62
|
+
satisfaction_dist: dict[str, int] = field(default_factory=dict)
|
|
63
|
+
friction_dist: dict[str, int] = field(default_factory=dict)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass
|
|
67
|
+
class InsightsConfig:
|
|
68
|
+
"""Configuration for the insights pipeline."""
|
|
69
|
+
|
|
70
|
+
model: str
|
|
71
|
+
days: int | None = None
|
|
72
|
+
since: datetime | None = None
|
|
73
|
+
force: bool = False
|
|
74
|
+
output_path: str = "./opencode-insights.html"
|
|
75
|
+
concurrency: int | None = None
|