forked from jameswh3/MW-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertFrom-AgentTranscript.ps1
More file actions
393 lines (334 loc) · 16.2 KB
/
ConvertFrom-AgentTranscript.ps1
File metadata and controls
393 lines (334 loc) · 16.2 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<#
.SYNOPSIS
Converts conversation transcript data from Power Platform to human-readable format.
.DESCRIPTION
Parses conversation transcript files and reconstructs them in a chronological, human-readable format
showing the flow of conversation between users and bots.
.PARAMETER InputFile
Path to the conversation transcript file to parse.
.PARAMETER OutputFile
Path where the human-readable transcript should be saved.
.EXAMPLE
ConvertFrom-AgentTranscripts -InputFile "C:\temp\conversationtranscripts.txt" -OutputFile "C:\temp\readable_transcripts.txt"
#>
param(
[Parameter(Mandatory = $true)]
[string]$InputFile,
[Parameter(Mandatory = $true)]
[string]$OutputFile
)
function Get-ActivityType {
param($activity)
if ($activity.type -eq "message") {
if ($activity.from.role -eq 1) {
return "User Message"
} else {
return "Bot Message"
}
}
elseif ($activity.type -eq "event") {
switch ($activity.name) {
"startConversation" { return "Session Start" }
"ResponseData" { return "Bot Response" }
"DynamicPlanReceived" { return "Bot Planning" }
"UniversalSearchToolTraceData" { return "Knowledge Search" }
"pvaSetContext" { return "Context Set" }
default { return "Event: $($activity.name)" }
}
}
elseif ($activity.type -eq "trace") {
switch ($activity.valueType) {
"SessionInfo" { return "Session Info" }
"KnowledgeTraceData" { return "Knowledge Search" }
"VariableAssignment" { return "Variable Assignment" }
"GPTAnswer" { return "AI Response" }
default { return "Trace: $($activity.valueType)" }
}
}
else {
return "Unknown: $($activity.type)"
}
}
function Parse-ConversationTranscript {
param([string]$transcriptBlock)
$lines = $transcriptBlock -split "`n"
$transcript = @{}
$currentKey = ""
$currentValue = ""
foreach ($line in $lines) {
$line = $line.Trim()
if ($line -match "^([^:]+)\s*:\s*(.*)$") {
# Save previous key-value pair if exists
if ($currentKey) {
$transcript[$currentKey] = $currentValue.Trim()
}
$currentKey = $matches[1].Trim()
$currentValue = $matches[2].Trim()
} else {
# Continue multi-line value
if ($currentKey) {
$currentValue += "`n" + $line
}
}
}
# Save last key-value pair
if ($currentKey) {
$transcript[$currentKey] = $currentValue.Trim()
}
return $transcript
}
function ConvertFrom-UnixTimestamp {
param($UnixTimestamp)
if (-not $UnixTimestamp -or $UnixTimestamp -eq 0) {
return "N/A"
}
try {
# Handle different timestamp formats
$timestamp = $null
if ($UnixTimestamp -is [string]) {
# Try to parse as number
if ([long]::TryParse($UnixTimestamp, [ref]$timestamp)) {
# Success - use the parsed value
} else {
return "Invalid Timestamp (string: $UnixTimestamp)"
}
} elseif ($UnixTimestamp -is [long] -or $UnixTimestamp -is [int]) {
$timestamp = [long]$UnixTimestamp
} else {
return "Invalid Timestamp (type: $($UnixTimestamp.GetType()))"
}
# Convert Unix timestamp to DateTime (without -Kind parameter for compatibility)
$epoch = [DateTime]::new(1970, 1, 1, 0, 0, 0, [DateTimeKind]::Utc)
$dateTime = $epoch.AddSeconds($timestamp)
return $dateTime.ToLocalTime().ToString("MM-dd-yyyy HH:mm:ss")
}
catch {
return "Invalid Timestamp (error: $($_.Exception.Message))"
}
}
function Parse-ISO8601DateTime {
param([string]$DateTimeString)
if ([string]::IsNullOrWhiteSpace($DateTimeString)) {
return "N/A"
}
try {
$dateTime = [DateTime]::Parse($DateTimeString)
return $dateTime.ToString("MM-dd-yyyy HH:mm:ss")
}
catch {
return "Invalid DateTime: $DateTimeString"
}
}
# Read the input file
if (-not (Test-Path $InputFile)) {
Write-Error "Input file not found: $InputFile"
return
}
$content = Get-Content $InputFile -Raw
# Split on @odata.etag but keep the delimiter
$transcriptBlocks = $content -split '(?=@odata\.etag)' | Where-Object { $_.Trim() -ne "" }
$output = @()
Write-Host "Found $($transcriptBlocks.Count) potential transcript blocks" -ForegroundColor Yellow
foreach ($block in $transcriptBlocks) {
if ([string]::IsNullOrWhiteSpace($block)) { continue }
# Clean up the block
$block = $block.Trim()
$transcript = Parse-ConversationTranscript $block
if (-not $transcript.content) {
Write-Warning "No content found in transcript block"
continue
}
Write-Host "Processing conversation: $($transcript.conversationtranscriptid)" -ForegroundColor Green
try {
# Sanitize JSON content to fix common issues
$sanitizedContent = $transcript.content
# More aggressive Unicode escape fixing
# Replace any \u that doesn't have exactly 4 hex digits following it
# This handles: \u123, \u12, \u1, \u (at any position)
while ($sanitizedContent -match '\\u(?![0-9a-fA-F]{4})') {
$sanitizedContent = $sanitizedContent -replace '\\u(?![0-9a-fA-F]{4})[0-9a-fA-F]{0,3}', ''
}
# Remove any orphaned backslashes at the end of strings
$sanitizedContent = $sanitizedContent -replace '\\(?=[",\]\}])', ''
$sanitizedContent = $sanitizedContent -replace '\\$', ''
# Remove control characters except newlines, carriage returns, and tabs
$sanitizedContent = $sanitizedContent -replace '[\x00-\x08\x0B\x0C\x0E-\x1F]', ''
# Try to parse with more robust error recovery
try {
$contentJson = $sanitizedContent | ConvertFrom-Json -ErrorAction Stop
}
catch {
# If still failing, use character-by-character cleanup to find and fix the issue
Write-Warning "First parse attempt failed, trying aggressive cleanup..."
# Find the position of the error (around position 503 based on error message)
# Remove problematic characters around that position in each text field
$sanitizedContent = $sanitizedContent -replace '("text"\s*:\s*"[^"\\]*)(\\[^"]{0,10})([^"]*")', '$1$3'
# Also try to fix any remaining backslash-related issues
$sanitizedContent = $sanitizedContent -replace '\\(?!")', ''
try {
$contentJson = $sanitizedContent | ConvertFrom-Json -ErrorAction Stop
}
catch {
# Last resort - strip out all text fields entirely
Write-Warning "Second parse attempt failed, removing all text fields..."
# More comprehensive cleanup - handle unterminated strings
# Remove entire activities array items that have corrupted text
$sanitizedContent = $sanitizedContent -replace '"text"\s*:\s*"[^"]*$', '"text":""'
$sanitizedContent = $sanitizedContent -replace '"text"\s*:\s*"[^"\\]*(?:\\.[^"\\]*)*$', '"text":""'
# Try to fix unterminated strings by closing them
$sanitizedContent = $sanitizedContent -replace '("text"\s*:\s*"[^"]*)$', '$1"'
# If still broken, just remove all text field content
$sanitizedContent = $sanitizedContent -replace '"text"\s*:\s*"(?:[^"\\]|\\.)*', '"text":"'
# Ensure proper closing
$sanitizedContent = $sanitizedContent -replace '"text"\s*:\s*""', '"text":""'
try {
$contentJson = $sanitizedContent | ConvertFrom-Json -ErrorAction Stop
}
catch {
# Ultimate fallback - skip this transcript entirely
Write-Warning "All parse attempts failed, skipping this conversation: $($_.Exception.Message)"
continue
}
}
}
# Parse metadata if available
$metadata = $null
if ($transcript.metadata) {
try {
$metadata = $transcript.metadata | ConvertFrom-Json -ErrorAction Stop
}
catch {
Write-Warning "Could not parse metadata: $($_.Exception.Message)"
}
}
# Extract session info
$sessionInfo = $contentJson.activities | Where-Object { $_.valueType -eq "SessionInfo" } | Select-Object -First 1
$conversationInfo = $contentJson.activities | Where-Object { $_.valueType -eq "ConversationInfo" } | Select-Object -First 1
$output += "=" * 80
$output += "Bot Name: $(if ($metadata -and $metadata.BotName) { $metadata.BotName } else { 'Unknown' })"
$output += "Conversation ID: $($transcript.conversationtranscriptid)"
$output += "Created On: $(Parse-ISO8601DateTime $transcript.createdon)"
if ($sessionInfo) {
$output += "Session Start: $(Parse-ISO8601DateTime $sessionInfo.value.startTimeUtc)"
$output += "Session End: $(Parse-ISO8601DateTime $sessionInfo.value.endTimeUtc)"
$output += "Session Type: $($sessionInfo.value.type)"
$output += "Session Outcome: $($sessionInfo.value.outcome)"
$output += "Session Outcome Reason: $($sessionInfo.value.outcomeReason)"
$output += "Turn Count: $($sessionInfo.value.turnCount)"
}
if ($conversationInfo) {
$output += "Last Session Outcome: $($conversationInfo.value.lastSessionOutcome)"
$output += "Last Session Outcome Reason: $($conversationInfo.value.lastSessionOutcomeReason)"
$output += "Design Mode: $($conversationInfo.value.isDesignMode)"
$output += "Locale: $($conversationInfo.value.locale)"
}
$output += "-" * 40
# Sort activities by timestamp and filter out activities without timestamps
$activities = $contentJson.activities | Where-Object { $_.timestamp } | Sort-Object {
try { [long]$_.timestamp } catch { 0 }
}
if ($activities.Count -eq 0) {
$output += "No timestamped activities found in this conversation."
} else {
Write-Host "Found $($activities.Count) activities in this conversation" -ForegroundColor Cyan
}
foreach ($activity in $activities) {
$timestamp = ConvertFrom-UnixTimestamp $activity.timestamp
# Debug: Show raw timestamp value
Write-Debug "Raw timestamp: $($activity.timestamp) (type: $($activity.timestamp.GetType().Name))"
switch ($activity.type) {
"message" {
if ($activity.from.role -eq 1) {
$userInfo = if ($activity.from.aadObjectId) { " ($($activity.from.aadObjectId))" } else { " ($($activity.from.id))" }
$output += "[$timestamp] User$userInfo`: $($activity.text)"
} else {
$output += "[$timestamp] Bot: $($activity.text)"
}
}
"event" {
switch ($activity.name) {
"ResponseData" {
# Include full response text without truncation
$responseText = $activity.value
$output += "[$timestamp] Bot Response: $responseText"
}
"DynamicPlanReceived" {
if ($activity.value -and $activity.value.steps) {
$steps = $activity.value.steps -join ", "
$output += "[$timestamp] Bot Planning: Using tools [$steps]"
}
}
"DynamicPlanStepTriggered" {
if ($activity.value -and $activity.value.taskDialogId) {
$output += "[$timestamp] Bot Action: Starting $($activity.value.taskDialogId)"
}
}
"DynamicPlanStepFinished" {
if ($activity.value -and $activity.value.taskDialogId) {
$output += "[$timestamp] Bot Action: Completed $($activity.value.taskDialogId)"
}
}
"UniversalSearchToolTraceData" {
if ($activity.value -and $activity.value.knowledgeSources) {
$sources = ($activity.value.knowledgeSources | ForEach-Object { $_.Split('.')[-1] }) -join ", "
$output += "[$timestamp] Knowledge Search: Searched sources [$sources]"
}
}
"pvaSetContext" {
$output += "[$timestamp] Context Set (Channel: $($activity.channelId))"
}
default {
# Show other events that might be important
if ($activity.name -match "Plan|Search|Response|Step") {
$output += "[$timestamp] Event: $($activity.name)"
}
}
}
}
"trace" {
switch ($activity.valueType) {
"ConversationInfo" {
$output += "[$timestamp] Conversation Info: Outcome=$($activity.value.lastSessionOutcome), Locale=$($activity.value.locale)"
}
"KnowledgeTraceData" {
if ($activity.value -and $activity.value.citedKnowledgeSources) {
$sources = ($activity.value.citedKnowledgeSources | ForEach-Object { $_.Split('.')[-1] }) -join ", "
$output += "[$timestamp] Knowledge Search: Found sources [$sources], Status: $($activity.value.completionState)"
}
}
"VariableAssignment" {
if ($activity.value -and $activity.value.name -eq "Answer" -and $activity.value.newValue) {
# Include full answer without truncation
$output += "[$timestamp] Bot Generated Answer: $($activity.value.newValue)"
}
}
"GPTAnswer" {
if ($activity.value) {
$output += "[$timestamp] AI Processing: $($activity.value.gptAnswerState)"
}
}
"DynamicPlanReceived" {
if ($activity.value -and $activity.value.steps) {
$steps = $activity.value.steps -join ", "
$output += "[$timestamp] Bot Planning: Received plan with steps [$steps]"
}
}
"DynamicPlanFinished" {
$output += "[$timestamp] Bot Planning: Plan execution completed"
}
}
}
}
}
$output += ""
}
catch {
Write-Warning "Failed to parse transcript block: $($_.Exception.Message)"
Write-Warning "Block starts with: $($block.Substring(0, [Math]::Min(100, $block.Length)))"
continue
}
}
# Write output to file
$output | Out-File -FilePath $OutputFile -Encoding UTF8
Write-Host "Converted transcript saved to: $OutputFile" -ForegroundColor Green
Write-Host "Total conversations processed: $(($output | Where-Object { $_ -eq ('=' * 80) }).Count)" -ForegroundColor Cyan