-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtranscript-normalizer.js
More file actions
54 lines (50 loc) · 1.88 KB
/
transcript-normalizer.js
File metadata and controls
54 lines (50 loc) · 1.88 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
function cleanAgentText(presetId, text) {
let out = String(text || '').split('\n').map(l => l.replace(/[ \t]+$/g, '')).join('\n').trim();
out = out.replace(/\n\n─{5,}\s*$/u, '').trim();
if (presetId === 'codex' || presetId === 'claude-code') {
const cut = Math.max(out.lastIndexOf('›'), out.lastIndexOf('❯'));
if (cut !== -1) out = out.slice(0, cut).trim();
}
if (presetId === 'gemini-cli') {
const cut = out.lastIndexOf('\n >');
if (cut !== -1) out = out.slice(0, cut).trim();
out = out.replace(/\n\s*[⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏]?\s*Executing Hook:[\s\S]*$/, '').trim();
}
if (presetId === 'claude-code') {
out = out.replace(/\n\s*.*\(running stop hook\)[\s\S]*$/, '').trim();
out = out.replace(/\n\s*\?\s*for shortcuts[\s\S]*$/, '').trim();
out = out.replace(/\n\s*esc to interrupt[\s\S]*$/, '').trim();
out = out.replace(/\n\n\s*[✻✢✣✤✥✦✧][\s\S]*$/, '').trim();
out = out.replace(/\n\n─{5,}[\s\S]*$/, '').trim();
}
return out;
}
function normalizeEntry(entry, presetId) {
const text = entry.role === 'agent'
? cleanAgentText(presetId, entry.text)
: String(entry.text || '').trim();
return text ? { ...entry, text } : null;
}
function addEntry(entries, entry, presetId) {
const next = normalizeEntry(entry, presetId);
if (!next) return entries;
const prev = entries[entries.length - 1];
if (prev && prev.role === 'user' && next.role === 'user') {
prev.text += '\n' + next.text;
prev.ts = next.ts;
return entries;
}
if (prev && prev.role === 'agent' && next.role === 'agent') {
prev.text = next.text;
prev.ts = next.ts;
return entries;
}
entries.push(next);
return entries;
}
function compactEntries(entries, presetId) {
const out = [];
for (const entry of entries || []) addEntry(out, entry, presetId);
return out;
}
module.exports = { addEntry, compactEntries, cleanAgentText };