Zion Boggan
repos/TreeTrace/src/adapters/index.js
zionboggan.com ↗
59 lines · javascript
History for this file →
1
import { basename } from 'node:path';
2
import { detectCodex, parseCodex } from './codex.js';
3
import { detectGemini, detectGeminiJson, parseGemini, parseGeminiJson } from './gemini.js';
4
import { detectChatGPT, parseChatGPT } from './chatgpt.js';
5
import { detectCopilot, parseCopilot } from './copilot.js';
6
import { detectGrok, parseGrok } from './grok.js';
7
import { detectCursor, parseCursor } from './cursor.js';
8
import { TreetraceError, ExitCode } from '../util.js';
9
 
10
export const TOOLS = ['claude', 'codex', 'chatgpt', 'gemini', 'copilot', 'grok', 'cursor', 'transcript'];
11
 
12
function tryParseJson(text) {
13
  const head = text.trimStart()[0];
14
  if (head !== '{' && head !== '[') return null;
15
  try {
16
    return JSON.parse(text);
17
  } catch {
18
    return null;
19
  }
20
}
21
 
22
export function adaptFrom(tool, text, path) {
23
  const id = basename(path).replace(/\.(jsonl?|txt|md)$/i, '');
24
  const json = tryParseJson(text);
25
  switch (tool) {
26
    case 'codex':
27
      return [parseCodex(text, path, id)];
28
    case 'gemini':
29
      return json ? [parseGeminiJson(json, path, id)] : [parseGemini(text, path, id)];
30
    case 'chatgpt':
31
      return parseChatGPT(json, path);
32
    case 'copilot':
33
      return [parseCopilot(json, path, id)];
34
    case 'grok':
35
      return [parseGrok(json, path, id)];
36
    case 'cursor':
37
      return [parseCursor(json, path, id)];
38
    default:
39
      throw new TreetraceError(`unknown --from tool "${tool}" (expected one of: ${TOOLS.join(', ')})`, ExitCode.USAGE);
40
  }
41
}
42
 
43
export function autoAdapt(text, path) {
44
  const id = basename(path).replace(/\.(jsonl?|txt|md)$/i, '');
45
  const json = tryParseJson(text);
46
 
47
  if (json !== null) {
48
    if (detectChatGPT(json)) return { tool: 'chatgpt', sessions: parseChatGPT(json, path) };
49
    if (detectCopilot(json)) return { tool: 'copilot', sessions: [parseCopilot(json, path, id)] };
50
    if (detectGeminiJson(json)) return { tool: 'gemini', sessions: [parseGeminiJson(json, path, id)] };
51
    if (detectCursor(json)) return { tool: 'cursor', sessions: [parseCursor(json, path, id)] };
52
    if (detectGrok(json)) return { tool: 'grok', sessions: [parseGrok(json, path, id)] };
53
    return null;
54
  }
55
 
56
  if (detectCodex(text)) return { tool: 'codex', sessions: [parseCodex(text, path, id)] };
57
  if (detectGemini(text)) return { tool: 'gemini', sessions: [parseGemini(text, path, id)] };
58
  return null;
59
}