MCPcopy Create free account
hub / github.com/Eversmile12/sharedcontext / parseClaudeCodeJSONL

Function parseClaudeCodeJSONL

src/core/parsers/claude-code.ts:17–69  ·  view source on GitHub ↗
(
  text: string,
  fileId: string,
  project: string
)

Source from the content-addressed store, hash-verified

15 * Tool use blocks and tool results are skipped.
16 */
17export function parseClaudeCodeJSONL(
18 text: string,
19 fileId: string,
20 project: string
21): Conversation {
22 const messages: ConversationMessage[] = [];
23 const lines = text.split("\n").filter((l) => l.trim());
24
25 let firstTimestamp: string | null = null;
26 let lastTimestamp: string | null = null;
27
28 for (const line of lines) {
29 let obj: Record<string, unknown>;
30 try {
31 obj = JSON.parse(line);
32 } catch {
33 continue; // skip malformed lines
34 }
35
36 const type = obj.type as string;
37 const timestamp = (obj.timestamp as string) ?? null;
38
39 if (timestamp) {
40 if (!firstTimestamp) firstTimestamp = timestamp;
41 lastTimestamp = timestamp;
42 }
43
44 if (type === "user") {
45 const content = extractUserContent(obj);
46 if (content) {
47 messages.push({ role: "user", content, timestamp: timestamp ?? undefined });
48 }
49 } else if (type === "assistant") {
50 const content = extractAssistantContent(obj);
51 if (content) {
52 messages.push({ role: "assistant", content, timestamp: timestamp ?? undefined });
53 }
54 }
55 // Skip file-history-snapshot, system, etc.
56 }
57
58 const merged = mergeConsecutiveMessages(messages);
59
60 const now = new Date().toISOString();
61 return {
62 id: fileId,
63 client: "claude-code",
64 project,
65 messages: merged,
66 startedAt: firstTimestamp ?? now,
67 updatedAt: lastTimestamp ?? now,
68 };
69}
70
71function extractUserContent(obj: Record<string, unknown>): string | null {
72 const message = obj.message as Record<string, unknown> | undefined;

Callers 3

checkFileMethod · 0.85
loadLocalConversationsFunction · 0.85
startMcpServerFunction · 0.85

Calls 3

extractUserContentFunction · 0.85
extractAssistantContentFunction · 0.85
mergeConsecutiveMessagesFunction · 0.85

Tested by

no test coverage detected