-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.ps1
More file actions
175 lines (153 loc) · 6.91 KB
/
generate.ps1
File metadata and controls
175 lines (153 loc) · 6.91 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
# Agent Log Viewer - Generate HTML from Claude Code JSONL session files
param(
[string]$TargetDir = ""
)
$ErrorActionPreference = "SilentlyContinue"
if (-not $TargetDir) {
$TargetDir = (Get-Location).Path
}
$ClaudeProjectsDir = Join-Path $env:USERPROFILE ".claude\projects"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$TemplateFile = Join-Path $ScriptDir "viewer.html"
# --- Find matching project folder ---
function Find-ProjectFolder {
param([string]$DirPath)
if (-not (Test-Path $ClaudeProjectsDir)) { return $null }
$encoded = $DirPath -replace ':', '-' -replace '\\', '-'
$target = Join-Path $ClaudeProjectsDir $encoded
if ((Test-Path $target) -and (Get-ChildItem $target -Filter *.jsonl -ErrorAction SilentlyContinue | Measure-Object).Count -gt 0) {
return $target
}
$dirNorm = $DirPath.TrimEnd('\').ToLower()
foreach ($projDir in (Get-ChildItem $ClaudeProjectsDir -Directory -ErrorAction SilentlyContinue)) {
foreach ($jsonl in (Get-ChildItem $projDir.FullName -Filter *.jsonl -ErrorAction SilentlyContinue | Select-Object -First 1)) {
try {
$firstCwdLine = Get-Content $jsonl.FullName -TotalCount 50 -Encoding UTF8 |
Where-Object { $_ -match '"cwd"' } | Select-Object -First 1
if ($firstCwdLine) {
$obj = $firstCwdLine | ConvertFrom-Json
if ($obj.cwd -and $obj.cwd.TrimEnd('\').ToLower() -eq $dirNorm) {
return $projDir.FullName
}
}
} catch {}
}
}
return $null
}
# --- Extract messages from JSONL ---
function Get-Conversations {
param([string]$ProjectFolder)
$sessions = @()
foreach ($jsonlFile in (Get-ChildItem $ProjectFolder -Filter *.jsonl -ErrorAction SilentlyContinue | Sort-Object Name)) {
$userMsgs = [System.Collections.ArrayList]::new()
$assistantGroups = @{}
$assistantOrder = [System.Collections.ArrayList]::new()
$sessionStart = $null
$lineIdx = 0
foreach ($line in (Get-Content $jsonlFile.FullName -Encoding UTF8)) {
if (-not $line.Trim()) { continue }
try {
$obj = $line | ConvertFrom-Json
} catch { continue }
$ts = if ($obj.timestamp) { $obj.timestamp } else { '' }
$lineIdx++
if (-not $sessionStart -and $ts) { $sessionStart = $ts }
if ($obj.type -eq 'user') {
$content = $obj.message.content
if ($content -is [string] -and $content.Trim()) {
$userMsgs.Add(@{role='user'; content=$content; ts=$ts; idx=$lineIdx}) | Out-Null
}
}
elseif ($obj.type -eq 'assistant') {
$msgId = $obj.message.id
if (-not $msgId) { continue }
$blocks = $obj.message.content
if ($blocks -is [System.Array]) {
foreach ($block in $blocks) {
if ($block.type -eq 'text' -and $block.text -and $block.text.Trim()) {
if (-not $assistantGroups.ContainsKey($msgId)) {
$assistantGroups[$msgId] = @{content=''; ts=$ts; idx=$lineIdx}
$assistantOrder.Add($msgId) | Out-Null
}
$assistantGroups[$msgId].content += $block.text
}
}
}
}
}
$allMsgs = [System.Collections.ArrayList]::new()
$allMsgs.AddRange($userMsgs)
foreach ($mid in $assistantOrder) {
$g = $assistantGroups[$mid]
$allMsgs.Add(@{role='assistant'; content=$g.content; ts=$g.ts; idx=$g.idx}) | Out-Null
}
$sorted = $allMsgs | Sort-Object { $_['ts'] }, { $_['idx'] }
if ($sorted.Count -gt 0) {
$sessions += ,@{
id = $jsonlFile.BaseName
startTime = $sessionStart
messages = $sorted
}
}
}
$sessions | Sort-Object startTime
}
# --- Get known project paths ---
function Get-KnownProjects {
$result = @()
if (-not (Test-Path $ClaudeProjectsDir)) { return $result }
foreach ($pd in (Get-ChildItem $ClaudeProjectsDir -Directory -ErrorAction SilentlyContinue)) {
foreach ($jf in (Get-ChildItem $pd.FullName -Filter *.jsonl -ErrorAction SilentlyContinue | Select-Object -First 1)) {
try {
$line = Get-Content $jf.FullName -TotalCount 50 -Encoding UTF8 |
Where-Object { $_ -match '"cwd"' } | Select-Object -First 1
if ($line) {
$o = $line | ConvertFrom-Json
if ($o.cwd) { $result += $o.cwd; break }
}
} catch {}
}
}
$result
}
# --- Main ---
$template = [System.IO.File]::ReadAllText($TemplateFile, [System.Text.Encoding]::UTF8)
$projectFolder = Find-ProjectFolder -DirPath $TargetDir
if (-not $projectFolder) {
$knownProjs = Get-KnownProjects
$knownJson = $knownProjs | ConvertTo-Json -Compress
if ($knownProjs.Count -eq 1) { $knownJson = "[""$($knownProjs[0])""]" }
$html = $template.Replace("'__MODE__'", "'notfound'").Replace("__DATA__", $knownJson).Replace("__PATH__", $TargetDir.Replace('\', '\\')).Replace("__NAME__", "")
} else {
$projectName = Split-Path $TargetDir -Leaf
$sessions = Get-Conversations -ProjectFolder $projectFolder
if ($sessions.Count -eq 0) {
$knownProjs = Get-KnownProjects
$knownJson = $knownProjs | ConvertTo-Json -Compress
if ($knownProjs.Count -eq 1) { $knownJson = "[""$($knownProjs[0])""]" }
$html = $template.Replace("'__MODE__'", "'notfound'").Replace("__DATA__", $knownJson).Replace("__PATH__", $TargetDir.Replace('\', '\\')).Replace("__NAME__", "")
} else {
$sessionsArray = @()
foreach ($s in $sessions) {
$msgsArray = @()
foreach ($m in $s.messages) {
$msgsArray += @{role=$m.role; content=$m.content; ts=$m.ts}
}
$sessionsArray += @{
id = $s.id
startTime = $s.startTime
messages = $msgsArray
}
}
$sessionsJson = ConvertTo-Json @($sessionsArray) -Depth 5 -Compress
$html = $template.Replace("'__MODE__'", "'normal'").Replace("__DATA__", $sessionsJson).Replace("__PATH__", $TargetDir.Replace('\', '\\')).Replace("__NAME__", $projectName)
}
}
# Clean old temp files, write new one, open in browser
Get-ChildItem $env:TEMP -Filter "agent_log_*.html" -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddHours(-1) } |
Remove-Item -Force -ErrorAction SilentlyContinue
$tempFile = Join-Path $env:TEMP "agent_log_$(Get-Random).html"
[System.IO.File]::WriteAllText($tempFile, $html, [System.Text.Encoding]::UTF8)
Start-Process $tempFile