@threadbase-sh/scanner 0.18.0 → 0.19.1

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/dist/index.js CHANGED
@@ -1040,12 +1040,88 @@ function parseCodexJsonlLine(line) {
1040
1040
  function codexEntryToMessage(entry) {
1041
1041
  const payload = entry.payload;
1042
1042
  if (!payload || typeof payload !== "object") return null;
1043
- if (entry.type !== "response_item" || payload.type !== "message") return null;
1044
- const role = payload.role;
1045
- if (role !== "user" && role !== "assistant") return null;
1046
- const text = extractCodexText(payload.content);
1047
- if (!text) return null;
1048
- return { role, text, timestamp: asString(entry.timestamp) };
1043
+ if (entry.type !== "response_item") return null;
1044
+ const timestamp = asString(entry.timestamp);
1045
+ const uuid = asString(payload.id) || void 0;
1046
+ const base = { timestamp, ...uuid ? { uuid } : {} };
1047
+ switch (payload.type) {
1048
+ case "message": {
1049
+ const role = payload.role;
1050
+ if (role !== "user" && role !== "assistant") return null;
1051
+ const text = extractCodexText(payload.content);
1052
+ if (!text) return null;
1053
+ return { role, text, ...base };
1054
+ }
1055
+ case "function_call":
1056
+ case "custom_tool_call":
1057
+ case "web_search_call": {
1058
+ const block = codexToolUseBlock(payload);
1059
+ if (!block) return null;
1060
+ return {
1061
+ role: "assistant",
1062
+ text: "",
1063
+ ...base,
1064
+ metadata: { toolUses: [block.name], toolUseBlocks: [block] }
1065
+ };
1066
+ }
1067
+ case "function_call_output":
1068
+ case "custom_tool_call_output": {
1069
+ const toolUseId = asString(payload.call_id);
1070
+ if (!toolUseId) return null;
1071
+ return {
1072
+ role: "user",
1073
+ text: "",
1074
+ ...base,
1075
+ isToolResult: true,
1076
+ metadata: {
1077
+ toolResults: [
1078
+ {
1079
+ toolUseId,
1080
+ type: "generic",
1081
+ content: { output: codexToolOutputText(payload.output) }
1082
+ }
1083
+ ]
1084
+ }
1085
+ };
1086
+ }
1087
+ case "reasoning": {
1088
+ const summary = Array.isArray(payload.summary) ? payload.summary : [];
1089
+ const thinkingContent = summary.map((s) => asString(s?.text)).filter((t) => t.trim()).join("\n\n");
1090
+ if (!thinkingContent) return null;
1091
+ return { role: "assistant", text: "", ...base, isThinking: true, thinkingContent };
1092
+ }
1093
+ default:
1094
+ return null;
1095
+ }
1096
+ }
1097
+ function codexToolUseBlock(payload) {
1098
+ if (payload.type === "web_search_call") {
1099
+ const action = payload.action;
1100
+ return {
1101
+ id: asString(payload.id),
1102
+ name: "web_search",
1103
+ input: action && typeof action === "object" ? action : {}
1104
+ };
1105
+ }
1106
+ const name = asString(payload.name);
1107
+ if (!name) return null;
1108
+ const id = asString(payload.call_id);
1109
+ if (payload.type === "custom_tool_call") {
1110
+ return { id, name, input: { input: asString(payload.input) } };
1111
+ }
1112
+ const args = asString(payload.arguments);
1113
+ let input = { arguments: args };
1114
+ try {
1115
+ const parsed = JSON.parse(args);
1116
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) input = parsed;
1117
+ } catch {
1118
+ }
1119
+ return { id, name, input };
1120
+ }
1121
+ function codexToolOutputText(output) {
1122
+ if (typeof output === "string") return output;
1123
+ if (!Array.isArray(output)) return "";
1124
+ return output.map((item) => typeof item === "string" ? item : asString(item?.text)).filter(Boolean).join("\n");
1049
1125
  }
1050
1126
  function reduceCodexEntry(acc, entry, tier) {
1051
1127
  const ts = asString(entry.timestamp);
@@ -1185,6 +1261,7 @@ async function parseCodexConversation(filePath, account) {
1185
1261
  const message = codexEntryToMessage(entry);
1186
1262
  if (!message) continue;
1187
1263
  messages.push(message);
1264
+ if (!message.text) continue;
1188
1265
  textParts.push(message.text);
1189
1266
  if (message.role === "user") lastUserText = message.text;
1190
1267
  }
@@ -1210,6 +1287,7 @@ async function parseCodexConversation(filePath, account) {
1210
1287
  }
1211
1288
 
1212
1289
  // src/providers/cursor.ts
1290
+ import { createHash } from "crypto";
1213
1291
  import fg2 from "fast-glob";
1214
1292
  import { createReadStream as createReadStream3, existsSync, statSync } from "fs";
1215
1293
  import { stat as stat2 } from "fs/promises";
@@ -1384,35 +1462,64 @@ function firstTimestampHint(content) {
1384
1462
  }
1385
1463
  return "";
1386
1464
  }
1387
- function parseCursorJsonlLine(line) {
1388
- if (!line.trim()) return null;
1465
+ function parseCursorJsonlLine(line, index) {
1466
+ const trimmed = line.trim();
1467
+ if (!trimmed) return null;
1389
1468
  let entry;
1390
1469
  try {
1391
- entry = JSON.parse(line);
1470
+ entry = JSON.parse(trimmed);
1392
1471
  } catch {
1393
1472
  return null;
1394
1473
  }
1395
- return cursorEntryToMessage(entry);
1474
+ const message = cursorEntryToMessage(entry);
1475
+ if (!message || message.uuid) return message;
1476
+ return { ...message, uuid: cursorMessageUuid(trimmed, message.role, index) };
1477
+ }
1478
+ function cursorMessageUuid(line, role, index) {
1479
+ const hash = createHash("sha1").update(line).digest("hex").slice(0, 16);
1480
+ return index === void 0 ? `cursor-${role}-${hash}` : `cursor-${role}-${index}-${hash}`;
1396
1481
  }
1397
1482
  function cursorEntryToMessage(entry) {
1398
1483
  if (looksLikeCursorChatEntry(entry)) {
1399
1484
  const role = entry.role;
1400
1485
  const message = entry.message;
1401
- const text = extractCursorText(message?.content ?? entry.content);
1402
- if (!text) return null;
1403
- const ts = firstTimestampHint(message?.content ?? entry.content);
1404
- return { role, text, timestamp: ts };
1486
+ const content = message?.content ?? entry.content;
1487
+ return withToolUses(
1488
+ { role, text: extractCursorText(content), timestamp: firstTimestampHint(content) },
1489
+ content
1490
+ );
1405
1491
  }
1406
1492
  if (looksLikeImportedClaudeEntry(entry)) {
1407
1493
  const message = entry.message;
1408
1494
  const role = message?.role === "assistant" || entry.type === "assistant" ? "assistant" : "user";
1409
- const text = extractCursorText(message?.content ?? entry.content);
1410
- if (!text) return null;
1411
- const ts = asString2(entry.timestamp);
1412
- return { role, text, timestamp: ts };
1495
+ const content = message?.content ?? entry.content;
1496
+ return withToolUses(
1497
+ { role, text: extractCursorText(content), timestamp: asString2(entry.timestamp) },
1498
+ content
1499
+ );
1413
1500
  }
1414
1501
  return parseCodexJsonlLine(JSON.stringify(entry));
1415
1502
  }
1503
+ function withToolUses(message, content) {
1504
+ const blocks = extractCursorToolUseBlocks(content);
1505
+ if (blocks.length === 0) return message.text ? message : null;
1506
+ return {
1507
+ ...message,
1508
+ metadata: { toolUses: blocks.map((b) => b.name), toolUseBlocks: blocks }
1509
+ };
1510
+ }
1511
+ function extractCursorToolUseBlocks(content) {
1512
+ if (!Array.isArray(content)) return [];
1513
+ const blocks = [];
1514
+ for (const item of content) {
1515
+ const block = item;
1516
+ if (block?.type !== "tool_use" || typeof block.name !== "string" || !block.name) continue;
1517
+ const input = block.input && typeof block.input === "object" && !Array.isArray(block.input) ? block.input : {};
1518
+ const id = typeof block.id === "string" && block.id ? block.id : `cursor-tool-${createHash("sha1").update(`${block.name}\0${JSON.stringify(input)}`).digest("hex").slice(0, 16)}`;
1519
+ blocks.push({ id, name: block.name, input });
1520
+ }
1521
+ return blocks;
1522
+ }
1416
1523
  function foldCursorMessage(acc, role, text, timestamp, tier) {
1417
1524
  if (timestamp && (!acc.latestTimestamp || timestamp > acc.latestTimestamp)) {
1418
1525
  acc.latestTimestamp = timestamp;
@@ -1452,13 +1559,13 @@ function reduceCursorEntry(acc, entry, tier) {
1452
1559
  if (looksLikeImportedClaudeEntry(entry)) {
1453
1560
  collectCursorToolNames(content, acc.toolNames);
1454
1561
  const msg = cursorEntryToMessage(entry);
1455
- if (!msg) return;
1562
+ if (!msg?.text) return;
1456
1563
  foldCursorMessage(acc, msg.role, msg.text, msg.timestamp, tier);
1457
1564
  return;
1458
1565
  }
1459
1566
  if (looksLikeImportedCodexEntry(entry)) {
1460
1567
  const msg = parseCodexJsonlLine(JSON.stringify(entry));
1461
- if (!msg) return;
1568
+ if (!msg?.text) return;
1462
1569
  foldCursorMessage(acc, msg.role, msg.text, msg.timestamp, tier);
1463
1570
  }
1464
1571
  }
@@ -1573,12 +1680,13 @@ async function parseCursorConversation(filePath, account) {
1573
1680
  const rl = createInterface3({ input: createReadStream3(filePath), crlfDelay: Infinity });
1574
1681
  try {
1575
1682
  for await (const line of rl) {
1576
- const message = parseCursorJsonlLine(line);
1683
+ const message = parseCursorJsonlLine(line, messages.length);
1577
1684
  if (!message) continue;
1578
1685
  if (message.timestamp && (!latestTimestamp || message.timestamp > latestTimestamp)) {
1579
1686
  latestTimestamp = message.timestamp;
1580
1687
  }
1581
1688
  messages.push(message);
1689
+ if (!message.text) continue;
1582
1690
  textParts.push(message.text);
1583
1691
  if (message.role === "user") lastUserText = message.text;
1584
1692
  }
@@ -1954,11 +2062,11 @@ async function extendConversation(previous, resume, filePath, account) {
1954
2062
  }
1955
2063
 
1956
2064
  // src/persistent/cursor.ts
1957
- import { createHash } from "crypto";
2065
+ import { createHash as createHash2 } from "crypto";
1958
2066
  import { closeSync, openSync, readSync, statSync as statSync2 } from "fs";
1959
2067
  var FP_BYTES = 4096;
1960
2068
  function fingerprint(filePath, size) {
1961
- const hash = createHash("sha1");
2069
+ const hash = createHash2("sha1");
1962
2070
  hash.update(String(size));
1963
2071
  const fd = openSync(filePath, "r");
1964
2072
  try {