@andersonbcdefg/transcript-viewer 0.0.0-stage → 0.2.0
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.
- package/README.md +44 -2
- package/dist/ConversationVisualizer.d.ts +15 -0
- package/dist/ConversationVisualizer.js +358 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +2 -0
- package/dist/parsers.d.ts +38 -0
- package/dist/parsers.js +1284 -0
- package/dist/styles.css +665 -0
- package/dist/types.d.ts +54 -0
- package/dist/types.js +1 -0
- package/package.json +47 -3
package/dist/parsers.js
ADDED
|
@@ -0,0 +1,1284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side JSONL parsers for Claude, Codex, and Claude Headless formats.
|
|
3
|
+
* These are TypeScript ports of the server-side parsers in conversation-sources.mjs,
|
|
4
|
+
* designed to run in the browser for URL mode.
|
|
5
|
+
*/
|
|
6
|
+
// Helper functions
|
|
7
|
+
const DEFAULT_SKIP_TYPES = new Set(["thinking", "tool_use"]);
|
|
8
|
+
function blocksToText(blocks, options = {}) {
|
|
9
|
+
if (!Array.isArray(blocks))
|
|
10
|
+
return "";
|
|
11
|
+
const skip = new Set(options.skipTypes || DEFAULT_SKIP_TYPES);
|
|
12
|
+
return blocks
|
|
13
|
+
.map((block) => {
|
|
14
|
+
if (!block || skip.has(block.type))
|
|
15
|
+
return "";
|
|
16
|
+
if (block.text)
|
|
17
|
+
return block.text;
|
|
18
|
+
if (typeof block.json === "string")
|
|
19
|
+
return block.json;
|
|
20
|
+
return "";
|
|
21
|
+
})
|
|
22
|
+
.join("")
|
|
23
|
+
.trim();
|
|
24
|
+
}
|
|
25
|
+
function normalizeRole(role, blocks) {
|
|
26
|
+
const normalized = role || "unknown";
|
|
27
|
+
if (normalized === "user" && Array.isArray(blocks) && blocks.length) {
|
|
28
|
+
const allToolResults = blocks.every((block) => block.type === "tool_result");
|
|
29
|
+
if (allToolResults)
|
|
30
|
+
return "tool";
|
|
31
|
+
}
|
|
32
|
+
return normalized;
|
|
33
|
+
}
|
|
34
|
+
function clipText(value, size = 120) {
|
|
35
|
+
if (!value)
|
|
36
|
+
return "";
|
|
37
|
+
if (value.length <= size)
|
|
38
|
+
return value;
|
|
39
|
+
return `${value.slice(0, size)}...`;
|
|
40
|
+
}
|
|
41
|
+
function guessTitleFromMessages(messages, fallback) {
|
|
42
|
+
const isCodexAgentsDump = (text) => text.includes("<INSTRUCTIONS>") || text.includes("<environment_context>");
|
|
43
|
+
const firstUser = messages.find((msg) => msg.role === "user" && msg.text && !isCodexAgentsDump(msg.text));
|
|
44
|
+
if (firstUser?.text)
|
|
45
|
+
return clipText(firstUser.text, 120);
|
|
46
|
+
// Skip assistant messages that are just tool calls (e.g., "Tool call: shell")
|
|
47
|
+
const isToolCallText = (text) => text.startsWith("Tool call:");
|
|
48
|
+
const firstAssistant = messages.find((msg) => msg.role === "assistant" && msg.text && !isToolCallText(msg.text));
|
|
49
|
+
if (firstAssistant?.text)
|
|
50
|
+
return clipText(firstAssistant.text, 120);
|
|
51
|
+
return fallback || "Conversation";
|
|
52
|
+
}
|
|
53
|
+
function derivePreview(messages) {
|
|
54
|
+
const isCodexAgentsDump = (text) => text.includes("<INSTRUCTIONS>") || text.includes("<environment_context>");
|
|
55
|
+
const firstUser = messages.find((msg) => msg.role === "user" && msg.text && !isCodexAgentsDump(msg.text));
|
|
56
|
+
const lastAssistant = [...messages]
|
|
57
|
+
.reverse()
|
|
58
|
+
.find((msg) => msg.role === "assistant" && msg.text);
|
|
59
|
+
return {
|
|
60
|
+
userPreview: firstUser?.text || "",
|
|
61
|
+
assistantPreview: lastAssistant?.text || "",
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function parseTimestamp(value) {
|
|
65
|
+
if (!value)
|
|
66
|
+
return null;
|
|
67
|
+
try {
|
|
68
|
+
const iso = new Date(value).toISOString();
|
|
69
|
+
return Number.isNaN(Date.parse(iso)) ? null : iso;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Quick check that a trimmed line looks like a JSON object (starts with { and ends with }). */
|
|
76
|
+
function looksLikeJsonObject(line) {
|
|
77
|
+
return (line.charCodeAt(0) === 0x7b && line.charCodeAt(line.length - 1) === 0x7d);
|
|
78
|
+
}
|
|
79
|
+
const CCREMOTE_EVENT_TYPE = "ccremote.event";
|
|
80
|
+
function toCcremoteEventMessage(entry) {
|
|
81
|
+
if (entry.type !== CCREMOTE_EVENT_TYPE)
|
|
82
|
+
return null;
|
|
83
|
+
const eventName = typeof entry.event === "string" ? entry.event : "unknown";
|
|
84
|
+
const providedText = typeof entry.text === "string" ? entry.text.trim() : "";
|
|
85
|
+
const text = providedText || `ccremote event: ${eventName}`;
|
|
86
|
+
return {
|
|
87
|
+
role: "system",
|
|
88
|
+
text,
|
|
89
|
+
blocks: [{ type: "text", text }],
|
|
90
|
+
timestamp: parseTimestamp(entry.timestamp) || null,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function findFirstDetectableJson(lines) {
|
|
94
|
+
for (const rawLine of lines) {
|
|
95
|
+
const line = rawLine.trim();
|
|
96
|
+
if (!line || !looksLikeJsonObject(line))
|
|
97
|
+
continue;
|
|
98
|
+
try {
|
|
99
|
+
const json = JSON.parse(line);
|
|
100
|
+
if (json.type === CCREMOTE_EVENT_TYPE)
|
|
101
|
+
continue;
|
|
102
|
+
return json;
|
|
103
|
+
}
|
|
104
|
+
catch { }
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
function takeDetectableJson(lines, maxEntries) {
|
|
109
|
+
const entries = [];
|
|
110
|
+
for (const rawLine of lines) {
|
|
111
|
+
if (entries.length >= maxEntries)
|
|
112
|
+
break;
|
|
113
|
+
const line = rawLine.trim();
|
|
114
|
+
if (!line || !looksLikeJsonObject(line))
|
|
115
|
+
continue;
|
|
116
|
+
try {
|
|
117
|
+
const json = JSON.parse(line);
|
|
118
|
+
if (json.type === CCREMOTE_EVENT_TYPE)
|
|
119
|
+
continue;
|
|
120
|
+
entries.push(json);
|
|
121
|
+
}
|
|
122
|
+
catch { }
|
|
123
|
+
}
|
|
124
|
+
return entries;
|
|
125
|
+
}
|
|
126
|
+
function buildMetadataDetails(meta, title = "Session metadata") {
|
|
127
|
+
const cleaned = Object.fromEntries(Object.entries(meta || {}).filter(([, value]) => value !== null && value !== undefined && value !== ""));
|
|
128
|
+
if (!Object.keys(cleaned).length)
|
|
129
|
+
return [];
|
|
130
|
+
return [
|
|
131
|
+
{
|
|
132
|
+
title,
|
|
133
|
+
content: JSON.stringify(cleaned, null, 2),
|
|
134
|
+
format: "json",
|
|
135
|
+
},
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
function buildConversation(params) {
|
|
139
|
+
const { userPreview, assistantPreview } = derivePreview(params.messages);
|
|
140
|
+
return {
|
|
141
|
+
id: String(params.id),
|
|
142
|
+
source: params.source,
|
|
143
|
+
title: params.title || "Conversation",
|
|
144
|
+
subtitle: params.subtitle || null,
|
|
145
|
+
createdAt: params.createdAt || null,
|
|
146
|
+
model: params.model || null,
|
|
147
|
+
status: params.status ?? null,
|
|
148
|
+
tags: params.tags || [],
|
|
149
|
+
messages: params.messages,
|
|
150
|
+
previewUser: userPreview,
|
|
151
|
+
previewAssistant: assistantPreview,
|
|
152
|
+
details: params.details,
|
|
153
|
+
parentSessionId: params.parentSessionId || null,
|
|
154
|
+
isSidechain: Boolean(params.isSidechain),
|
|
155
|
+
isComplete: params.isComplete ?? false,
|
|
156
|
+
resultSummary: params.resultSummary ?? null,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
// Block normalizers
|
|
160
|
+
function normalizeClaudeBlock(block, toolUseResultAgentId = null) {
|
|
161
|
+
if (!block)
|
|
162
|
+
return null;
|
|
163
|
+
if (typeof block === "string")
|
|
164
|
+
return { type: "text", text: block };
|
|
165
|
+
const b = block;
|
|
166
|
+
const type = b.type || "unknown";
|
|
167
|
+
if (type === "text")
|
|
168
|
+
return { type, text: b.text || "" };
|
|
169
|
+
if (type === "input_text" || type === "output_text")
|
|
170
|
+
return { type, text: b.text || "" };
|
|
171
|
+
if (type === "thinking")
|
|
172
|
+
return { type, text: b.thinking || b.text || "" };
|
|
173
|
+
if (type === "tool_use") {
|
|
174
|
+
return {
|
|
175
|
+
type,
|
|
176
|
+
text: b.name || "",
|
|
177
|
+
json: {
|
|
178
|
+
id: b.id,
|
|
179
|
+
name: b.name,
|
|
180
|
+
input: b.input,
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
if (type === "tool_result") {
|
|
185
|
+
const content = b.content;
|
|
186
|
+
const text = typeof content === "string"
|
|
187
|
+
? content
|
|
188
|
+
: Array.isArray(content)
|
|
189
|
+
? content
|
|
190
|
+
.map((item) => {
|
|
191
|
+
const i = item;
|
|
192
|
+
return i?.text ? i.text : JSON.stringify(item);
|
|
193
|
+
})
|
|
194
|
+
.join("")
|
|
195
|
+
: b.text || "";
|
|
196
|
+
return {
|
|
197
|
+
type,
|
|
198
|
+
text,
|
|
199
|
+
json: b,
|
|
200
|
+
agentId: toolUseResultAgentId,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
type,
|
|
205
|
+
text: b.text || b.content || "",
|
|
206
|
+
json: b,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function normalizeClaudeBlocks(content, toolUseResultAgentId = null) {
|
|
210
|
+
if (content == null)
|
|
211
|
+
return [];
|
|
212
|
+
if (typeof content === "string")
|
|
213
|
+
return [{ type: "text", text: content }];
|
|
214
|
+
if (!Array.isArray(content)) {
|
|
215
|
+
if (typeof content === "object") {
|
|
216
|
+
const c = content;
|
|
217
|
+
return [
|
|
218
|
+
{
|
|
219
|
+
type: c.type || "unknown",
|
|
220
|
+
text: c.text || "",
|
|
221
|
+
json: c,
|
|
222
|
+
},
|
|
223
|
+
];
|
|
224
|
+
}
|
|
225
|
+
return [{ type: "text", text: String(content) }];
|
|
226
|
+
}
|
|
227
|
+
return content
|
|
228
|
+
.map((block) => normalizeClaudeBlock(block, toolUseResultAgentId))
|
|
229
|
+
.filter(Boolean);
|
|
230
|
+
}
|
|
231
|
+
function normalizeCodexBlock(block) {
|
|
232
|
+
if (!block)
|
|
233
|
+
return null;
|
|
234
|
+
if (typeof block === "string")
|
|
235
|
+
return { type: "text", text: block };
|
|
236
|
+
const b = block;
|
|
237
|
+
const type = b.type || "unknown";
|
|
238
|
+
if (type === "input_text" || type === "output_text" || type === "text") {
|
|
239
|
+
return { type, text: b.text || "" };
|
|
240
|
+
}
|
|
241
|
+
return { type, text: b.text || "", json: b };
|
|
242
|
+
}
|
|
243
|
+
function normalizeCodexBlocks(content) {
|
|
244
|
+
if (content == null)
|
|
245
|
+
return [];
|
|
246
|
+
if (typeof content === "string")
|
|
247
|
+
return [{ type: "text", text: content }];
|
|
248
|
+
if (!Array.isArray(content)) {
|
|
249
|
+
if (typeof content === "object") {
|
|
250
|
+
const c = content;
|
|
251
|
+
return [
|
|
252
|
+
{
|
|
253
|
+
type: c.type || "unknown",
|
|
254
|
+
text: c.text || "",
|
|
255
|
+
json: c,
|
|
256
|
+
},
|
|
257
|
+
];
|
|
258
|
+
}
|
|
259
|
+
return [{ type: "text", text: String(content) }];
|
|
260
|
+
}
|
|
261
|
+
return content
|
|
262
|
+
.map((block) => normalizeCodexBlock(block))
|
|
263
|
+
.filter(Boolean);
|
|
264
|
+
}
|
|
265
|
+
// Main parsers
|
|
266
|
+
export function parseClaudeContent(content, sourceId = "url") {
|
|
267
|
+
const lines = content.split(/\r?\n/);
|
|
268
|
+
let sessionId = null;
|
|
269
|
+
let cwd = null;
|
|
270
|
+
let gitBranch = null;
|
|
271
|
+
let version = null;
|
|
272
|
+
let agentId = null;
|
|
273
|
+
let isSidechain = false;
|
|
274
|
+
let earliestTimestamp = null;
|
|
275
|
+
let model = null;
|
|
276
|
+
const messages = [];
|
|
277
|
+
const messageIndex = new Map();
|
|
278
|
+
for (let idx = 0; idx < lines.length; idx += 1) {
|
|
279
|
+
const line = lines[idx].trim();
|
|
280
|
+
if (!line || !looksLikeJsonObject(line))
|
|
281
|
+
continue;
|
|
282
|
+
let entry;
|
|
283
|
+
try {
|
|
284
|
+
entry = JSON.parse(line);
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (entry.sessionId && !sessionId)
|
|
290
|
+
sessionId = entry.sessionId;
|
|
291
|
+
if (entry.cwd && !cwd)
|
|
292
|
+
cwd = entry.cwd;
|
|
293
|
+
if (entry.gitBranch && !gitBranch)
|
|
294
|
+
gitBranch = entry.gitBranch;
|
|
295
|
+
if (entry.version && !version)
|
|
296
|
+
version = entry.version;
|
|
297
|
+
if (entry.agentId && !agentId)
|
|
298
|
+
agentId = entry.agentId;
|
|
299
|
+
if (typeof entry.isSidechain === "boolean")
|
|
300
|
+
isSidechain = isSidechain || entry.isSidechain;
|
|
301
|
+
if (entry.timestamp) {
|
|
302
|
+
const timestamp = parseTimestamp(entry.timestamp);
|
|
303
|
+
if (timestamp && (!earliestTimestamp || timestamp < earliestTimestamp))
|
|
304
|
+
earliestTimestamp = timestamp;
|
|
305
|
+
}
|
|
306
|
+
const message = entry.message;
|
|
307
|
+
if (!message || !message.role)
|
|
308
|
+
continue;
|
|
309
|
+
const toolUseResult = entry.toolUseResult;
|
|
310
|
+
const toolUseResultAgentId = toolUseResult?.agentId || null;
|
|
311
|
+
const blocks = normalizeClaudeBlocks(message.content, toolUseResultAgentId);
|
|
312
|
+
const role = normalizeRole(message.role, blocks);
|
|
313
|
+
const text = blocksToText(blocks);
|
|
314
|
+
const messageId = message.id || entry.uuid || `${sourceId}:${idx}`;
|
|
315
|
+
const existingIndex = messageIndex.get(messageId);
|
|
316
|
+
if (existingIndex !== undefined) {
|
|
317
|
+
const existing = messages[existingIndex];
|
|
318
|
+
const combinedBlocks = (existing.blocks || []).concat(blocks);
|
|
319
|
+
existing.blocks = combinedBlocks;
|
|
320
|
+
existing.text = blocksToText(combinedBlocks);
|
|
321
|
+
if (!existing.timestamp && entry.timestamp)
|
|
322
|
+
existing.timestamp = parseTimestamp(entry.timestamp);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
messages.push({
|
|
326
|
+
role,
|
|
327
|
+
text,
|
|
328
|
+
blocks: blocks.length ? blocks : null,
|
|
329
|
+
timestamp: entry.timestamp ? parseTimestamp(entry.timestamp) : null,
|
|
330
|
+
});
|
|
331
|
+
messageIndex.set(messageId, messages.length - 1);
|
|
332
|
+
if (!model && message.model)
|
|
333
|
+
model = message.model;
|
|
334
|
+
}
|
|
335
|
+
if (!messages.length)
|
|
336
|
+
return null;
|
|
337
|
+
const id = sessionId || sourceId;
|
|
338
|
+
const title = guessTitleFromMessages(messages, sourceId);
|
|
339
|
+
const subtitleParts = [];
|
|
340
|
+
if (cwd)
|
|
341
|
+
subtitleParts.push(cwd);
|
|
342
|
+
if (agentId)
|
|
343
|
+
subtitleParts.push(`agent:${agentId}`);
|
|
344
|
+
if (isSidechain)
|
|
345
|
+
subtitleParts.push("sidechain");
|
|
346
|
+
const subtitle = subtitleParts.join(" | ");
|
|
347
|
+
const details = buildMetadataDetails({
|
|
348
|
+
source: sourceId,
|
|
349
|
+
sessionId,
|
|
350
|
+
cwd,
|
|
351
|
+
gitBranch,
|
|
352
|
+
version,
|
|
353
|
+
agentId,
|
|
354
|
+
isSidechain,
|
|
355
|
+
});
|
|
356
|
+
const tags = [];
|
|
357
|
+
if (agentId)
|
|
358
|
+
tags.push("agent");
|
|
359
|
+
if (isSidechain)
|
|
360
|
+
tags.push("sidechain");
|
|
361
|
+
return buildConversation({
|
|
362
|
+
id: `claude-${id}`,
|
|
363
|
+
source: "claude-jsonl",
|
|
364
|
+
title,
|
|
365
|
+
subtitle,
|
|
366
|
+
createdAt: earliestTimestamp,
|
|
367
|
+
model,
|
|
368
|
+
tags,
|
|
369
|
+
messages,
|
|
370
|
+
details,
|
|
371
|
+
parentSessionId: isSidechain ? sessionId : null,
|
|
372
|
+
isSidechain,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
export function parseCodexContent(content, sourceId = "url") {
|
|
376
|
+
const lines = content.split(/\r?\n/);
|
|
377
|
+
let sessionMeta = null;
|
|
378
|
+
let turnContext = null;
|
|
379
|
+
let earliestTimestamp = null;
|
|
380
|
+
const messages = [];
|
|
381
|
+
for (let idx = 0; idx < lines.length; idx += 1) {
|
|
382
|
+
const line = lines[idx].trim();
|
|
383
|
+
if (!line || !looksLikeJsonObject(line))
|
|
384
|
+
continue;
|
|
385
|
+
let entry;
|
|
386
|
+
try {
|
|
387
|
+
entry = JSON.parse(line);
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (entry.timestamp) {
|
|
393
|
+
const timestamp = parseTimestamp(entry.timestamp);
|
|
394
|
+
if (timestamp && (!earliestTimestamp || timestamp < earliestTimestamp))
|
|
395
|
+
earliestTimestamp = timestamp;
|
|
396
|
+
}
|
|
397
|
+
if (entry.type === "session_meta" && entry.payload)
|
|
398
|
+
sessionMeta = entry.payload;
|
|
399
|
+
if (entry.type === "turn_context" && entry.payload && !turnContext)
|
|
400
|
+
turnContext = entry.payload;
|
|
401
|
+
if (entry.type !== "response_item" || !entry.payload)
|
|
402
|
+
continue;
|
|
403
|
+
const payload = entry.payload;
|
|
404
|
+
if (payload.type === "message") {
|
|
405
|
+
const blocks = normalizeCodexBlocks(payload.content);
|
|
406
|
+
const role = normalizeRole(payload.role, blocks);
|
|
407
|
+
const text = blocksToText(blocks, { skipTypes: [] });
|
|
408
|
+
messages.push({
|
|
409
|
+
role,
|
|
410
|
+
text,
|
|
411
|
+
blocks: blocks.length ? blocks : null,
|
|
412
|
+
timestamp: entry.timestamp ? parseTimestamp(entry.timestamp) : null,
|
|
413
|
+
});
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (payload.type === "function_call") {
|
|
417
|
+
const block = {
|
|
418
|
+
type: "tool_call",
|
|
419
|
+
text: payload.name || "tool_call",
|
|
420
|
+
json: {
|
|
421
|
+
name: payload.name,
|
|
422
|
+
arguments: payload.arguments,
|
|
423
|
+
call_id: payload.call_id,
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
messages.push({
|
|
427
|
+
role: "assistant",
|
|
428
|
+
text: `Tool call: ${payload.name || "unknown"}`,
|
|
429
|
+
blocks: [block],
|
|
430
|
+
timestamp: entry.timestamp ? parseTimestamp(entry.timestamp) : null,
|
|
431
|
+
});
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
if (payload.type === "function_call_output") {
|
|
435
|
+
const block = {
|
|
436
|
+
type: "tool_result",
|
|
437
|
+
text: payload.output || "",
|
|
438
|
+
json: payload,
|
|
439
|
+
};
|
|
440
|
+
messages.push({
|
|
441
|
+
role: "tool",
|
|
442
|
+
text: payload.output || "",
|
|
443
|
+
blocks: [block],
|
|
444
|
+
timestamp: entry.timestamp ? parseTimestamp(entry.timestamp) : null,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (!messages.length)
|
|
449
|
+
return null;
|
|
450
|
+
const sessionId = sessionMeta?.id || sourceId;
|
|
451
|
+
const model = turnContext?.model || null;
|
|
452
|
+
const createdAt = parseTimestamp(sessionMeta?.timestamp) || earliestTimestamp;
|
|
453
|
+
const title = guessTitleFromMessages(messages, sourceId);
|
|
454
|
+
const subtitle = sessionMeta?.cwd || "";
|
|
455
|
+
const details = buildMetadataDetails({
|
|
456
|
+
source: sourceId,
|
|
457
|
+
session: sessionMeta,
|
|
458
|
+
context: turnContext,
|
|
459
|
+
});
|
|
460
|
+
return buildConversation({
|
|
461
|
+
id: `codex-${sessionId}`,
|
|
462
|
+
source: "codex-jsonl",
|
|
463
|
+
title,
|
|
464
|
+
subtitle,
|
|
465
|
+
createdAt,
|
|
466
|
+
model,
|
|
467
|
+
tags: [],
|
|
468
|
+
messages,
|
|
469
|
+
details,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
export function parseHeadlessContent(content, sourceId = "url") {
|
|
473
|
+
const lines = content.split(/\r?\n/);
|
|
474
|
+
let initData = null;
|
|
475
|
+
let earliestTimestamp = null;
|
|
476
|
+
let model = null;
|
|
477
|
+
let isComplete = false;
|
|
478
|
+
let resultSummary = null;
|
|
479
|
+
const messages = [];
|
|
480
|
+
for (let idx = 0; idx < lines.length; idx += 1) {
|
|
481
|
+
const line = lines[idx].trim();
|
|
482
|
+
if (!line || !looksLikeJsonObject(line))
|
|
483
|
+
continue;
|
|
484
|
+
let entry;
|
|
485
|
+
try {
|
|
486
|
+
entry = JSON.parse(line);
|
|
487
|
+
}
|
|
488
|
+
catch {
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
const eventMessage = toCcremoteEventMessage(entry);
|
|
492
|
+
if (eventMessage) {
|
|
493
|
+
messages.push(eventMessage);
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
// Check for result entry which indicates conversation is complete
|
|
497
|
+
if (entry.type === "result") {
|
|
498
|
+
isComplete = true;
|
|
499
|
+
resultSummary = {
|
|
500
|
+
subtype: entry.subtype || "unknown",
|
|
501
|
+
is_error: Boolean(entry.is_error),
|
|
502
|
+
duration_ms: entry.duration_ms || 0,
|
|
503
|
+
duration_api_ms: entry.duration_api_ms,
|
|
504
|
+
num_turns: entry.num_turns || 0,
|
|
505
|
+
result: entry.result || "",
|
|
506
|
+
session_id: entry.session_id,
|
|
507
|
+
total_cost_usd: entry.total_cost_usd,
|
|
508
|
+
usage: entry.usage,
|
|
509
|
+
modelUsage: entry.modelUsage,
|
|
510
|
+
permission_denials: entry.permission_denials,
|
|
511
|
+
};
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
if (entry.type === "system" && entry.subtype === "init") {
|
|
515
|
+
initData = entry;
|
|
516
|
+
if (!model && entry.model)
|
|
517
|
+
model = entry.model;
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
if ((entry.type === "assistant" || entry.type === "user") &&
|
|
521
|
+
entry.message) {
|
|
522
|
+
const msg = entry.message;
|
|
523
|
+
if (!model && msg.model)
|
|
524
|
+
model = msg.model;
|
|
525
|
+
const existingIdx = messages.findIndex((m) => m._messageId === msg.id);
|
|
526
|
+
const blocks = normalizeClaudeBlocks(msg.content);
|
|
527
|
+
const role = normalizeRole(msg.role, blocks);
|
|
528
|
+
const text = blocksToText(blocks);
|
|
529
|
+
const messageObj = {
|
|
530
|
+
role,
|
|
531
|
+
text,
|
|
532
|
+
blocks: blocks.length ? blocks : null,
|
|
533
|
+
timestamp: entry.timestamp ? parseTimestamp(entry.timestamp) : null,
|
|
534
|
+
_messageId: msg.id,
|
|
535
|
+
};
|
|
536
|
+
if (existingIdx !== -1) {
|
|
537
|
+
messages[existingIdx] = messageObj;
|
|
538
|
+
}
|
|
539
|
+
else {
|
|
540
|
+
messages.push(messageObj);
|
|
541
|
+
}
|
|
542
|
+
if (entry.timestamp) {
|
|
543
|
+
const timestamp = parseTimestamp(entry.timestamp);
|
|
544
|
+
if (timestamp &&
|
|
545
|
+
(!earliestTimestamp || timestamp < earliestTimestamp)) {
|
|
546
|
+
earliestTimestamp = timestamp;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
// Clean up internal tracking field
|
|
552
|
+
for (const msg of messages) {
|
|
553
|
+
delete msg._messageId;
|
|
554
|
+
}
|
|
555
|
+
if (!messages.length)
|
|
556
|
+
return null;
|
|
557
|
+
const sessionId = initData?.session_id || sourceId;
|
|
558
|
+
const cwd = initData?.cwd || "";
|
|
559
|
+
const createdAt = earliestTimestamp;
|
|
560
|
+
const title = guessTitleFromMessages(messages, sourceId);
|
|
561
|
+
const subtitle = cwd;
|
|
562
|
+
const details = buildMetadataDetails({
|
|
563
|
+
source: sourceId,
|
|
564
|
+
session_id: sessionId,
|
|
565
|
+
cwd,
|
|
566
|
+
model,
|
|
567
|
+
claude_code_version: initData?.claude_code_version,
|
|
568
|
+
tools: initData?.tools,
|
|
569
|
+
});
|
|
570
|
+
return buildConversation({
|
|
571
|
+
id: `headless-${sessionId}`,
|
|
572
|
+
source: "claude-headless",
|
|
573
|
+
title,
|
|
574
|
+
subtitle,
|
|
575
|
+
createdAt,
|
|
576
|
+
model,
|
|
577
|
+
tags: [],
|
|
578
|
+
messages,
|
|
579
|
+
details,
|
|
580
|
+
isComplete,
|
|
581
|
+
resultSummary,
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Parse Codex headless format.
|
|
586
|
+
* Events: thread.started, turn.started, item.completed, item.started, item.updated, turn.completed
|
|
587
|
+
* item.type: reasoning, command_execution, file_change, todo_list, agent_message
|
|
588
|
+
*/
|
|
589
|
+
export function parseCodexHeadlessContent(content, sourceId = "url") {
|
|
590
|
+
const lines = content.split(/\r?\n/);
|
|
591
|
+
let threadId = null;
|
|
592
|
+
let model = null;
|
|
593
|
+
let isComplete = false;
|
|
594
|
+
let resultSummary = null;
|
|
595
|
+
const messages = [];
|
|
596
|
+
for (let idx = 0; idx < lines.length; idx += 1) {
|
|
597
|
+
const line = lines[idx].trim();
|
|
598
|
+
if (!line || !looksLikeJsonObject(line))
|
|
599
|
+
continue;
|
|
600
|
+
let entry;
|
|
601
|
+
try {
|
|
602
|
+
entry = JSON.parse(line);
|
|
603
|
+
}
|
|
604
|
+
catch {
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
const eventType = entry.type;
|
|
608
|
+
const eventMessage = toCcremoteEventMessage(entry);
|
|
609
|
+
if (eventMessage) {
|
|
610
|
+
messages.push(eventMessage);
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
// thread.started → extract thread_id as session ID
|
|
614
|
+
if (eventType === "thread.started") {
|
|
615
|
+
threadId = entry.thread_id || null;
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
// turn.completed → extract usage for token counts
|
|
619
|
+
if (eventType === "turn.completed") {
|
|
620
|
+
isComplete = true;
|
|
621
|
+
const usage = entry.usage;
|
|
622
|
+
if (usage) {
|
|
623
|
+
resultSummary = {
|
|
624
|
+
subtype: "success",
|
|
625
|
+
is_error: false,
|
|
626
|
+
duration_ms: 0,
|
|
627
|
+
num_turns: 1,
|
|
628
|
+
result: "",
|
|
629
|
+
usage,
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
// turn.started → can be used for timing but we skip for now
|
|
635
|
+
if (eventType === "turn.started") {
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
// item.started - can track in-progress state but we mainly care about completed items
|
|
639
|
+
// item.updated - updates to todo lists (skip for now)
|
|
640
|
+
if (eventType === "item.updated" || eventType === "item.started") {
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
// item.completed - main content
|
|
644
|
+
if (eventType === "item.completed") {
|
|
645
|
+
const item = entry.item;
|
|
646
|
+
if (!item)
|
|
647
|
+
continue;
|
|
648
|
+
const itemType = item.type;
|
|
649
|
+
// reasoning → render as thinking block
|
|
650
|
+
if (itemType === "reasoning") {
|
|
651
|
+
const text = item.text || "";
|
|
652
|
+
messages.push({
|
|
653
|
+
role: "assistant",
|
|
654
|
+
text: "",
|
|
655
|
+
blocks: [{ type: "thinking", text }],
|
|
656
|
+
timestamp: null,
|
|
657
|
+
});
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
// command_execution → render as tool_use (command) + tool_result (output)
|
|
661
|
+
if (itemType === "command_execution") {
|
|
662
|
+
const command = item.command || "";
|
|
663
|
+
const output = item.aggregated_output || "";
|
|
664
|
+
const exitCode = item.exit_code;
|
|
665
|
+
const status = item.status || "unknown";
|
|
666
|
+
// Tool use block (the command)
|
|
667
|
+
messages.push({
|
|
668
|
+
role: "assistant",
|
|
669
|
+
text: `Tool call: shell`,
|
|
670
|
+
blocks: [
|
|
671
|
+
{
|
|
672
|
+
type: "tool_use",
|
|
673
|
+
text: "shell",
|
|
674
|
+
json: {
|
|
675
|
+
name: "shell",
|
|
676
|
+
input: { command },
|
|
677
|
+
},
|
|
678
|
+
},
|
|
679
|
+
],
|
|
680
|
+
timestamp: null,
|
|
681
|
+
});
|
|
682
|
+
// Tool result block (the output)
|
|
683
|
+
const resultText = exitCode !== null && exitCode !== 0
|
|
684
|
+
? `${output}\n[exit code: ${exitCode}, status: ${status}]`
|
|
685
|
+
: output;
|
|
686
|
+
messages.push({
|
|
687
|
+
role: "tool",
|
|
688
|
+
text: resultText,
|
|
689
|
+
blocks: [
|
|
690
|
+
{
|
|
691
|
+
type: "tool_result",
|
|
692
|
+
text: resultText,
|
|
693
|
+
json: { exit_code: exitCode, status },
|
|
694
|
+
},
|
|
695
|
+
],
|
|
696
|
+
timestamp: null,
|
|
697
|
+
});
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
// file_change → render as tool_use showing file paths and change types
|
|
701
|
+
if (itemType === "file_change") {
|
|
702
|
+
const changes = item.changes || [];
|
|
703
|
+
const changesSummary = changes
|
|
704
|
+
.map((c) => `${c.kind}: ${c.path}`)
|
|
705
|
+
.join("\n");
|
|
706
|
+
messages.push({
|
|
707
|
+
role: "assistant",
|
|
708
|
+
text: `Tool call: file_change`,
|
|
709
|
+
blocks: [
|
|
710
|
+
{
|
|
711
|
+
type: "tool_use",
|
|
712
|
+
text: "file_change",
|
|
713
|
+
json: {
|
|
714
|
+
name: "file_change",
|
|
715
|
+
input: { changes },
|
|
716
|
+
},
|
|
717
|
+
},
|
|
718
|
+
],
|
|
719
|
+
timestamp: null,
|
|
720
|
+
});
|
|
721
|
+
// Add a result showing what changed
|
|
722
|
+
messages.push({
|
|
723
|
+
role: "tool",
|
|
724
|
+
text: changesSummary || "Files modified",
|
|
725
|
+
blocks: [
|
|
726
|
+
{
|
|
727
|
+
type: "tool_result",
|
|
728
|
+
text: changesSummary || "Files modified",
|
|
729
|
+
json: { changes, status: item.status },
|
|
730
|
+
},
|
|
731
|
+
],
|
|
732
|
+
timestamp: null,
|
|
733
|
+
});
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
// user_message → render as user text message (synthetic prompt)
|
|
737
|
+
if (itemType === "user_message") {
|
|
738
|
+
const text = item.text || "";
|
|
739
|
+
// Extract model from user_message if present
|
|
740
|
+
if (item.model && !model) {
|
|
741
|
+
model = item.model;
|
|
742
|
+
}
|
|
743
|
+
messages.push({
|
|
744
|
+
role: "user",
|
|
745
|
+
text,
|
|
746
|
+
blocks: [{ type: "text", text }],
|
|
747
|
+
timestamp: null,
|
|
748
|
+
});
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
// todo_list → render as collapsible tool call
|
|
752
|
+
if (itemType === "todo_list") {
|
|
753
|
+
const items = item.items || [];
|
|
754
|
+
const todoText = items
|
|
755
|
+
.map((t) => `${t.completed ? "✓" : "○"} ${t.text}`)
|
|
756
|
+
.join("\n");
|
|
757
|
+
messages.push({
|
|
758
|
+
role: "assistant",
|
|
759
|
+
text: `Tool call: todo_list`,
|
|
760
|
+
blocks: [
|
|
761
|
+
{
|
|
762
|
+
type: "tool_use",
|
|
763
|
+
text: "todo_list",
|
|
764
|
+
json: {
|
|
765
|
+
name: "todo_list",
|
|
766
|
+
input: { items },
|
|
767
|
+
},
|
|
768
|
+
},
|
|
769
|
+
],
|
|
770
|
+
timestamp: null,
|
|
771
|
+
});
|
|
772
|
+
messages.push({
|
|
773
|
+
role: "tool",
|
|
774
|
+
text: todoText || "No items",
|
|
775
|
+
blocks: [
|
|
776
|
+
{
|
|
777
|
+
type: "tool_result",
|
|
778
|
+
text: todoText || "No items",
|
|
779
|
+
json: { items },
|
|
780
|
+
},
|
|
781
|
+
],
|
|
782
|
+
timestamp: null,
|
|
783
|
+
});
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
// agent_message → render as assistant text message (final summary)
|
|
787
|
+
if (itemType === "agent_message") {
|
|
788
|
+
const text = item.text || "";
|
|
789
|
+
messages.push({
|
|
790
|
+
role: "assistant",
|
|
791
|
+
text,
|
|
792
|
+
blocks: [{ type: "text", text }],
|
|
793
|
+
timestamp: null,
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
if (!messages.length)
|
|
799
|
+
return null;
|
|
800
|
+
const id = threadId || sourceId;
|
|
801
|
+
const title = guessTitleFromMessages(messages, sourceId);
|
|
802
|
+
const details = buildMetadataDetails({
|
|
803
|
+
source: sourceId,
|
|
804
|
+
thread_id: threadId,
|
|
805
|
+
});
|
|
806
|
+
return buildConversation({
|
|
807
|
+
id: `codex-headless-${id}`,
|
|
808
|
+
source: "codex-headless",
|
|
809
|
+
title,
|
|
810
|
+
subtitle: "",
|
|
811
|
+
createdAt: null,
|
|
812
|
+
model,
|
|
813
|
+
tags: [],
|
|
814
|
+
messages,
|
|
815
|
+
details,
|
|
816
|
+
isComplete,
|
|
817
|
+
resultSummary,
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Parse Cursor headless format.
|
|
822
|
+
* Events: user, system/init, thinking/delta (skip), thinking/completed (skip if empty), assistant, tool_call/started, tool_call/completed, result
|
|
823
|
+
*/
|
|
824
|
+
export function parseCursorHeadlessContent(content, sourceId = "url") {
|
|
825
|
+
const lines = content.split(/\r?\n/);
|
|
826
|
+
let initData = null;
|
|
827
|
+
let earliestTimestamp = null;
|
|
828
|
+
let model = null;
|
|
829
|
+
let isComplete = false;
|
|
830
|
+
let resultSummary = null;
|
|
831
|
+
const messages = [];
|
|
832
|
+
const toolCallMap = new Map(); // Track tool calls by call_id
|
|
833
|
+
for (let idx = 0; idx < lines.length; idx += 1) {
|
|
834
|
+
const line = lines[idx].trim();
|
|
835
|
+
if (!line || !looksLikeJsonObject(line))
|
|
836
|
+
continue;
|
|
837
|
+
let entry;
|
|
838
|
+
try {
|
|
839
|
+
entry = JSON.parse(line);
|
|
840
|
+
}
|
|
841
|
+
catch {
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
const eventMessage = toCcremoteEventMessage(entry);
|
|
845
|
+
if (eventMessage) {
|
|
846
|
+
messages.push(eventMessage);
|
|
847
|
+
continue;
|
|
848
|
+
}
|
|
849
|
+
// Skip delta events
|
|
850
|
+
if (entry.type === "thinking" && entry.subtype === "delta") {
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
// Skip empty thinking blocks
|
|
854
|
+
if (entry.type === "thinking" && entry.subtype === "completed") {
|
|
855
|
+
const text = entry.text || "";
|
|
856
|
+
if (!text.trim()) {
|
|
857
|
+
continue;
|
|
858
|
+
}
|
|
859
|
+
// If thinking block has content, add it as a thinking block
|
|
860
|
+
messages.push({
|
|
861
|
+
role: "assistant",
|
|
862
|
+
text: "",
|
|
863
|
+
blocks: [{ type: "thinking", text }],
|
|
864
|
+
timestamp: entry.timestamp_ms
|
|
865
|
+
? parseTimestamp(entry.timestamp_ms)
|
|
866
|
+
: null,
|
|
867
|
+
});
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
870
|
+
// Check for result entry which indicates conversation is complete
|
|
871
|
+
if (entry.type === "result") {
|
|
872
|
+
isComplete = true;
|
|
873
|
+
resultSummary = {
|
|
874
|
+
subtype: entry.subtype || "unknown",
|
|
875
|
+
is_error: Boolean(entry.is_error),
|
|
876
|
+
duration_ms: entry.duration_ms || 0,
|
|
877
|
+
duration_api_ms: entry.duration_api_ms,
|
|
878
|
+
num_turns: entry.num_turns || 0,
|
|
879
|
+
result: entry.result || "",
|
|
880
|
+
session_id: entry.session_id,
|
|
881
|
+
total_cost_usd: entry.total_cost_usd,
|
|
882
|
+
usage: entry.usage,
|
|
883
|
+
modelUsage: entry.modelUsage,
|
|
884
|
+
permission_denials: entry.permission_denials,
|
|
885
|
+
};
|
|
886
|
+
continue;
|
|
887
|
+
}
|
|
888
|
+
// System init message contains session metadata
|
|
889
|
+
if (entry.type === "system" && entry.subtype === "init") {
|
|
890
|
+
initData = entry;
|
|
891
|
+
if (!model && entry.model)
|
|
892
|
+
model = entry.model;
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
// User messages
|
|
896
|
+
if (entry.type === "user" && entry.message) {
|
|
897
|
+
const msg = entry.message;
|
|
898
|
+
const blocks = normalizeClaudeBlocks(msg.content);
|
|
899
|
+
const role = normalizeRole(msg.role, blocks);
|
|
900
|
+
const text = blocksToText(blocks);
|
|
901
|
+
messages.push({
|
|
902
|
+
role,
|
|
903
|
+
text,
|
|
904
|
+
blocks: blocks.length ? blocks : null,
|
|
905
|
+
timestamp: entry.timestamp_ms
|
|
906
|
+
? parseTimestamp(entry.timestamp_ms)
|
|
907
|
+
: null,
|
|
908
|
+
});
|
|
909
|
+
if (entry.timestamp_ms) {
|
|
910
|
+
const timestamp = parseTimestamp(entry.timestamp_ms);
|
|
911
|
+
if (timestamp &&
|
|
912
|
+
(!earliestTimestamp || timestamp < earliestTimestamp)) {
|
|
913
|
+
earliestTimestamp = timestamp;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
continue;
|
|
917
|
+
}
|
|
918
|
+
// Assistant messages
|
|
919
|
+
if (entry.type === "assistant" && entry.message) {
|
|
920
|
+
const msg = entry.message;
|
|
921
|
+
if (!model && msg.model)
|
|
922
|
+
model = msg.model;
|
|
923
|
+
const blocks = normalizeClaudeBlocks(msg.content);
|
|
924
|
+
const role = normalizeRole(msg.role, blocks);
|
|
925
|
+
const text = blocksToText(blocks);
|
|
926
|
+
messages.push({
|
|
927
|
+
role,
|
|
928
|
+
text,
|
|
929
|
+
blocks: blocks.length ? blocks : null,
|
|
930
|
+
timestamp: entry.timestamp_ms
|
|
931
|
+
? parseTimestamp(entry.timestamp_ms)
|
|
932
|
+
: null,
|
|
933
|
+
});
|
|
934
|
+
if (entry.timestamp_ms) {
|
|
935
|
+
const timestamp = parseTimestamp(entry.timestamp_ms);
|
|
936
|
+
if (timestamp &&
|
|
937
|
+
(!earliestTimestamp || timestamp < earliestTimestamp)) {
|
|
938
|
+
earliestTimestamp = timestamp;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
943
|
+
// Tool call events
|
|
944
|
+
if (entry.type === "tool_call") {
|
|
945
|
+
const callId = entry.call_id;
|
|
946
|
+
const toolCall = entry.tool_call;
|
|
947
|
+
if (entry.subtype === "started") {
|
|
948
|
+
// Store tool call start
|
|
949
|
+
if (toolCall) {
|
|
950
|
+
// Extract tool name from tool_call structure (e.g., {readToolCall: {args: {...}}})
|
|
951
|
+
const toolName = Object.keys(toolCall)[0] || "unknown";
|
|
952
|
+
const toolArgs = toolCall[toolName]?.args || {};
|
|
953
|
+
toolCallMap.set(callId, {
|
|
954
|
+
name: toolName,
|
|
955
|
+
args: toolArgs,
|
|
956
|
+
started: true,
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
else if (entry.subtype === "completed") {
|
|
961
|
+
// Complete the tool call and add result
|
|
962
|
+
const stored = toolCallMap.get(callId);
|
|
963
|
+
if (stored && toolCall) {
|
|
964
|
+
const toolName = stored.name;
|
|
965
|
+
const toolResultObj = toolCall[Object.keys(toolCall)[0]];
|
|
966
|
+
const toolResult = toolResultObj?.result || {};
|
|
967
|
+
// Add tool_use block
|
|
968
|
+
messages.push({
|
|
969
|
+
role: "assistant",
|
|
970
|
+
text: `Tool call: ${toolName}`,
|
|
971
|
+
blocks: [
|
|
972
|
+
{
|
|
973
|
+
type: "tool_use",
|
|
974
|
+
text: toolName,
|
|
975
|
+
json: {
|
|
976
|
+
id: callId,
|
|
977
|
+
name: toolName,
|
|
978
|
+
input: stored.args,
|
|
979
|
+
},
|
|
980
|
+
},
|
|
981
|
+
],
|
|
982
|
+
timestamp: entry.timestamp_ms
|
|
983
|
+
? parseTimestamp(entry.timestamp_ms)
|
|
984
|
+
: null,
|
|
985
|
+
});
|
|
986
|
+
// Add tool_result block
|
|
987
|
+
const resultText = typeof toolResult === "string"
|
|
988
|
+
? toolResult
|
|
989
|
+
: JSON.stringify(toolResult, null, 2);
|
|
990
|
+
messages.push({
|
|
991
|
+
role: "tool",
|
|
992
|
+
text: resultText,
|
|
993
|
+
blocks: [
|
|
994
|
+
{
|
|
995
|
+
type: "tool_result",
|
|
996
|
+
text: resultText,
|
|
997
|
+
json: toolResult,
|
|
998
|
+
},
|
|
999
|
+
],
|
|
1000
|
+
timestamp: entry.timestamp_ms
|
|
1001
|
+
? parseTimestamp(entry.timestamp_ms)
|
|
1002
|
+
: null,
|
|
1003
|
+
});
|
|
1004
|
+
toolCallMap.delete(callId);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
if (!messages.length)
|
|
1010
|
+
return null;
|
|
1011
|
+
const sessionId = initData?.session_id || sourceId;
|
|
1012
|
+
const cwd = initData?.cwd || "";
|
|
1013
|
+
const createdAt = earliestTimestamp;
|
|
1014
|
+
const title = guessTitleFromMessages(messages, sourceId);
|
|
1015
|
+
const subtitle = cwd;
|
|
1016
|
+
const details = buildMetadataDetails({
|
|
1017
|
+
source: sourceId,
|
|
1018
|
+
session_id: sessionId,
|
|
1019
|
+
cwd,
|
|
1020
|
+
model,
|
|
1021
|
+
apiKeySource: initData?.apiKeySource,
|
|
1022
|
+
permissionMode: initData?.permissionMode,
|
|
1023
|
+
});
|
|
1024
|
+
return buildConversation({
|
|
1025
|
+
id: `cursor-headless-${sessionId}`,
|
|
1026
|
+
source: "cursor-headless",
|
|
1027
|
+
title,
|
|
1028
|
+
subtitle,
|
|
1029
|
+
createdAt,
|
|
1030
|
+
model,
|
|
1031
|
+
tags: [],
|
|
1032
|
+
messages,
|
|
1033
|
+
details,
|
|
1034
|
+
isComplete,
|
|
1035
|
+
resultSummary,
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Parse Gemini headless format.
|
|
1040
|
+
* Events: user, init, message, tool_use, tool_result, result
|
|
1041
|
+
*/
|
|
1042
|
+
export function parseGeminiHeadlessContent(content, sourceId = "url") {
|
|
1043
|
+
const lines = content.split(/\r?\n/);
|
|
1044
|
+
let sessionId = null;
|
|
1045
|
+
let model = null;
|
|
1046
|
+
let isComplete = false;
|
|
1047
|
+
let resultSummary = null;
|
|
1048
|
+
const messages = [];
|
|
1049
|
+
for (let idx = 0; idx < lines.length; idx += 1) {
|
|
1050
|
+
const line = lines[idx].trim();
|
|
1051
|
+
if (!line || !looksLikeJsonObject(line))
|
|
1052
|
+
continue;
|
|
1053
|
+
let entry;
|
|
1054
|
+
try {
|
|
1055
|
+
entry = JSON.parse(line);
|
|
1056
|
+
}
|
|
1057
|
+
catch {
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
const eventType = entry.type;
|
|
1061
|
+
const eventMessage = toCcremoteEventMessage(entry);
|
|
1062
|
+
if (eventMessage) {
|
|
1063
|
+
messages.push(eventMessage);
|
|
1064
|
+
continue;
|
|
1065
|
+
}
|
|
1066
|
+
// Initial user prompt with provider info
|
|
1067
|
+
if (eventType === "user" && entry.provider === "gemini") {
|
|
1068
|
+
const msg = entry.message;
|
|
1069
|
+
const text = msg?.content || "";
|
|
1070
|
+
if (text) {
|
|
1071
|
+
messages.push({
|
|
1072
|
+
role: "user",
|
|
1073
|
+
text,
|
|
1074
|
+
blocks: [{ type: "text", text }],
|
|
1075
|
+
timestamp: entry.timestamp || null,
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
continue;
|
|
1079
|
+
}
|
|
1080
|
+
// init → extract session_id and model
|
|
1081
|
+
if (eventType === "init") {
|
|
1082
|
+
sessionId = entry.session_id || null;
|
|
1083
|
+
model = entry.model || null;
|
|
1084
|
+
continue;
|
|
1085
|
+
}
|
|
1086
|
+
// message → user or assistant text
|
|
1087
|
+
if (eventType === "message") {
|
|
1088
|
+
const role = entry.role;
|
|
1089
|
+
const text = entry.content || "";
|
|
1090
|
+
if (role === "user" || role === "assistant") {
|
|
1091
|
+
messages.push({
|
|
1092
|
+
role,
|
|
1093
|
+
text,
|
|
1094
|
+
blocks: [{ type: "text", text }],
|
|
1095
|
+
timestamp: entry.timestamp || null,
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
continue;
|
|
1099
|
+
}
|
|
1100
|
+
// tool_use → render as tool_use block
|
|
1101
|
+
if (eventType === "tool_use") {
|
|
1102
|
+
const toolName = entry.tool_name || "unknown";
|
|
1103
|
+
const toolId = entry.tool_id || null;
|
|
1104
|
+
const parameters = entry.parameters || {};
|
|
1105
|
+
messages.push({
|
|
1106
|
+
role: "assistant",
|
|
1107
|
+
text: `Tool call: ${toolName}`,
|
|
1108
|
+
blocks: [
|
|
1109
|
+
{
|
|
1110
|
+
type: "tool_use",
|
|
1111
|
+
text: toolName,
|
|
1112
|
+
json: {
|
|
1113
|
+
name: toolName,
|
|
1114
|
+
id: toolId,
|
|
1115
|
+
input: parameters,
|
|
1116
|
+
},
|
|
1117
|
+
},
|
|
1118
|
+
],
|
|
1119
|
+
timestamp: entry.timestamp || null,
|
|
1120
|
+
});
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
// tool_result → render as tool_result block
|
|
1124
|
+
if (eventType === "tool_result") {
|
|
1125
|
+
const output = entry.output || "";
|
|
1126
|
+
const status = entry.status || "unknown";
|
|
1127
|
+
const toolId = entry.tool_id || null;
|
|
1128
|
+
messages.push({
|
|
1129
|
+
role: "tool",
|
|
1130
|
+
text: output,
|
|
1131
|
+
blocks: [
|
|
1132
|
+
{
|
|
1133
|
+
type: "tool_result",
|
|
1134
|
+
text: output,
|
|
1135
|
+
json: { tool_id: toolId, status },
|
|
1136
|
+
},
|
|
1137
|
+
],
|
|
1138
|
+
timestamp: entry.timestamp || null,
|
|
1139
|
+
});
|
|
1140
|
+
continue;
|
|
1141
|
+
}
|
|
1142
|
+
// result → final result with stats
|
|
1143
|
+
if (eventType === "result") {
|
|
1144
|
+
isComplete = true;
|
|
1145
|
+
const stats = entry.stats || {};
|
|
1146
|
+
const error = entry.error;
|
|
1147
|
+
resultSummary = {
|
|
1148
|
+
subtype: entry.status === "error" ? "error" : "success",
|
|
1149
|
+
is_error: entry.status === "error",
|
|
1150
|
+
duration_ms: stats.duration_ms || 0,
|
|
1151
|
+
num_turns: stats.tool_calls || 0,
|
|
1152
|
+
result: error?.message || "",
|
|
1153
|
+
usage: {
|
|
1154
|
+
input_tokens: stats.input_tokens || 0,
|
|
1155
|
+
output_tokens: stats.output_tokens || 0,
|
|
1156
|
+
total_tokens: stats.total_tokens || 0,
|
|
1157
|
+
cached_tokens: stats.cached || 0,
|
|
1158
|
+
},
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
if (!messages.length)
|
|
1163
|
+
return null;
|
|
1164
|
+
const id = sessionId || sourceId;
|
|
1165
|
+
const title = guessTitleFromMessages(messages, sourceId);
|
|
1166
|
+
const details = buildMetadataDetails({
|
|
1167
|
+
source: sourceId,
|
|
1168
|
+
session_id: sessionId,
|
|
1169
|
+
});
|
|
1170
|
+
return buildConversation({
|
|
1171
|
+
id: `gemini-headless-${id}`,
|
|
1172
|
+
source: "gemini-headless",
|
|
1173
|
+
title,
|
|
1174
|
+
subtitle: "",
|
|
1175
|
+
createdAt: null,
|
|
1176
|
+
model,
|
|
1177
|
+
tags: [],
|
|
1178
|
+
messages,
|
|
1179
|
+
details,
|
|
1180
|
+
isComplete,
|
|
1181
|
+
resultSummary,
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* Detect format and parse JSONL content.
|
|
1186
|
+
* Returns { conversations, kind } on success, { error } on failure.
|
|
1187
|
+
*/
|
|
1188
|
+
export function parseJsonlContent(content, sourceId) {
|
|
1189
|
+
// Remove BOM if present
|
|
1190
|
+
const cleanContent = content.replace(/^\uFEFF/, "");
|
|
1191
|
+
const lines = cleanContent.split(/\r?\n/);
|
|
1192
|
+
// Check multiple lines to better detect cursor format
|
|
1193
|
+
let detectedKind = null;
|
|
1194
|
+
for (const json of takeDetectableJson(lines, 10)) {
|
|
1195
|
+
// Cursor headless: has type="tool_call" with subtype="started"/"completed" or type="thinking" with subtype="delta"/"completed"
|
|
1196
|
+
if (json.type === "tool_call" &&
|
|
1197
|
+
(json.subtype === "started" || json.subtype === "completed")) {
|
|
1198
|
+
detectedKind = "cursor-headless";
|
|
1199
|
+
break;
|
|
1200
|
+
}
|
|
1201
|
+
if (json.type === "thinking" &&
|
|
1202
|
+
(json.subtype === "delta" || json.subtype === "completed")) {
|
|
1203
|
+
detectedKind = "cursor-headless";
|
|
1204
|
+
break;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
// Fall back to single-line detection
|
|
1208
|
+
if (!detectedKind) {
|
|
1209
|
+
const json = findFirstDetectableJson(lines);
|
|
1210
|
+
if (!json) {
|
|
1211
|
+
return { error: "Could not detect conversation format" };
|
|
1212
|
+
}
|
|
1213
|
+
// Gemini headless: has provider="gemini" or type="init" with session_id (check first - has "message" key that would match claude-jsonl)
|
|
1214
|
+
if (json.provider === "gemini") {
|
|
1215
|
+
detectedKind = "gemini-headless";
|
|
1216
|
+
}
|
|
1217
|
+
else if (json.type === "init" && json.session_id) {
|
|
1218
|
+
detectedKind = "gemini-headless";
|
|
1219
|
+
// Claude JSONL: has sessionId, message, summary, or file-history-snapshot
|
|
1220
|
+
}
|
|
1221
|
+
else if (json.type === "summary" ||
|
|
1222
|
+
json.sessionId ||
|
|
1223
|
+
json.message ||
|
|
1224
|
+
json.type === "file-history-snapshot") {
|
|
1225
|
+
detectedKind = "claude-jsonl";
|
|
1226
|
+
// Codex headless: thread.started, turn.started, or item.completed with codex-style item types
|
|
1227
|
+
// Check for item.completed with codex-specific item.type values to distinguish from other formats
|
|
1228
|
+
}
|
|
1229
|
+
else if (json.type === "thread.started" || json.type === "turn.started") {
|
|
1230
|
+
detectedKind = "codex-headless";
|
|
1231
|
+
}
|
|
1232
|
+
else if (json.type === "item.completed" &&
|
|
1233
|
+
json.item?.type &&
|
|
1234
|
+
[
|
|
1235
|
+
"reasoning",
|
|
1236
|
+
"command_execution",
|
|
1237
|
+
"file_change",
|
|
1238
|
+
"todo_list",
|
|
1239
|
+
"agent_message",
|
|
1240
|
+
"user_message",
|
|
1241
|
+
].includes(json.item.type)) {
|
|
1242
|
+
detectedKind = "codex-headless";
|
|
1243
|
+
// Codex: has session_meta, response_item, or payload at top level
|
|
1244
|
+
}
|
|
1245
|
+
else if (json.type === "session_meta" ||
|
|
1246
|
+
json.type === "response_item" ||
|
|
1247
|
+
json.payload) {
|
|
1248
|
+
detectedKind = "codex-jsonl";
|
|
1249
|
+
// Claude headless: system/init or assistant/user with message.content
|
|
1250
|
+
}
|
|
1251
|
+
else if (json.type === "system" && json.subtype === "init") {
|
|
1252
|
+
detectedKind = "claude-headless";
|
|
1253
|
+
}
|
|
1254
|
+
else if ((json.type === "assistant" || json.type === "user") &&
|
|
1255
|
+
json.message?.content) {
|
|
1256
|
+
detectedKind = "claude-headless";
|
|
1257
|
+
}
|
|
1258
|
+
if (!detectedKind) {
|
|
1259
|
+
return {
|
|
1260
|
+
error: `Could not detect format. Keys: ${Object.keys(json).join(", ")}. type=${json.type}`,
|
|
1261
|
+
};
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
let parsed = null;
|
|
1265
|
+
if (detectedKind === "claude-jsonl") {
|
|
1266
|
+
parsed = parseClaudeContent(content, sourceId);
|
|
1267
|
+
}
|
|
1268
|
+
else if (detectedKind === "gemini-headless") {
|
|
1269
|
+
parsed = parseGeminiHeadlessContent(content, sourceId);
|
|
1270
|
+
}
|
|
1271
|
+
else if (detectedKind === "codex-headless") {
|
|
1272
|
+
parsed = parseCodexHeadlessContent(content, sourceId);
|
|
1273
|
+
}
|
|
1274
|
+
else if (detectedKind === "codex-jsonl") {
|
|
1275
|
+
parsed = parseCodexContent(content, sourceId);
|
|
1276
|
+
}
|
|
1277
|
+
else if (detectedKind === "claude-headless") {
|
|
1278
|
+
parsed = parseHeadlessContent(content, sourceId);
|
|
1279
|
+
}
|
|
1280
|
+
else if (detectedKind === "cursor-headless") {
|
|
1281
|
+
parsed = parseCursorHeadlessContent(content, sourceId);
|
|
1282
|
+
}
|
|
1283
|
+
return { conversations: parsed ? [parsed] : [], kind: detectedKind };
|
|
1284
|
+
}
|