@threadbase-sh/scanner 0.24.7 → 0.25.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 +45 -0
- package/dist/cli.js +91 -89
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +363 -363
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +351 -351
- package/dist/index.js.map +1 -1
- package/dist/parse-helpers.d.ts +14 -0
- package/dist/parse-helpers.d.ts.map +1 -0
- package/dist/parse.cjs +340 -0
- package/dist/parse.cjs.map +1 -0
- package/dist/parse.d.ts +5 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +311 -0
- package/dist/parse.js.map +1 -0
- package/dist/parser.d.ts +2 -13
- package/dist/parser.d.ts.map +1 -1
- package/package.json +7 -2
package/README.md
CHANGED
|
@@ -292,6 +292,26 @@ await scan({
|
|
|
292
292
|
})
|
|
293
293
|
```
|
|
294
294
|
|
|
295
|
+
### Parse only
|
|
296
|
+
|
|
297
|
+
To turn Claude messages into `ConversationMessage`s without the scanner, import the `parse` entry.
|
|
298
|
+
It loads no native module (`better-sqlite3`), no watcher or logger, and no Node file APIs.
|
|
299
|
+
The input is one transcript JSONL line, or an Agent SDK message of the same shape serialized with `JSON.stringify`.
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import { createJsonlParseState, parseJsonlLine } from '@threadbase-sh/scanner/parse'
|
|
303
|
+
|
|
304
|
+
// One state per conversation: it pairs each tool_result with its tool_use,
|
|
305
|
+
// so the result gets its tool's type (`bash`, `edit`, …) instead of `generic`.
|
|
306
|
+
const state = createJsonlParseState()
|
|
307
|
+
for (const line of lines) {
|
|
308
|
+
const message = parseJsonlLine(line, state) // null for rows that are not messages
|
|
309
|
+
if (message) render(message)
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
The names and types match the root entry, so code can switch between the two imports without other changes.
|
|
314
|
+
|
|
295
315
|
## CLI Usage
|
|
296
316
|
|
|
297
317
|
```bash
|
|
@@ -377,6 +397,31 @@ npm run build # build ESM + CJS + types
|
|
|
377
397
|
npm run lint # type check
|
|
378
398
|
```
|
|
379
399
|
|
|
400
|
+
### Capturing live Copilot events
|
|
401
|
+
|
|
402
|
+
The Copilot parser was written against a hand-sanitized fixture.
|
|
403
|
+
`scripts/capture-copilot-events.mjs` turns a real session into a safe fixture and reports the shapes the parser still has to confirm.
|
|
404
|
+
|
|
405
|
+
1. Start one Copilot CLI session and, in it, do all of these:
|
|
406
|
+
- answer an `ask_user` question;
|
|
407
|
+
- approve a shell command;
|
|
408
|
+
- deny a shell command and type feedback;
|
|
409
|
+
- cancel a prompt with Esc, if the CLI allows it.
|
|
410
|
+
2. Run the script on that session's `events.jsonl` (under the Copilot home's `session-state/<session id>/`; a session directory works too).
|
|
411
|
+
The script never scans for sessions itself, makes no network call, and writes only under `__fixtures__/copilot/live/`.
|
|
412
|
+
```bash
|
|
413
|
+
node scripts/capture-copilot-events.mjs <path to events.jsonl> --out __fixtures__/copilot/live/<name>.jsonl
|
|
414
|
+
```
|
|
415
|
+
3. Read the `REVIEW BEFORE COMMIT` notice it prints, then skim the output file.
|
|
416
|
+
Ids, home paths, usernames, hostnames, emails and token-shaped strings are redacted; free text you typed is kept as is.
|
|
417
|
+
4. Print the shape report:
|
|
418
|
+
```bash
|
|
419
|
+
node scripts/capture-copilot-events.mjs __fixtures__/copilot/live/<name>.jsonl --report
|
|
420
|
+
```
|
|
421
|
+
5. Paste the report output, not the raw file, back to the maintainer.
|
|
422
|
+
|
|
423
|
+
`npm test` runs every `*.jsonl` in that directory through the real parser (`__tests__/copilot-live-fixtures.test.ts`) and is skipped while there are none.
|
|
424
|
+
|
|
380
425
|
## Contributing
|
|
381
426
|
|
|
382
427
|
Small bugfixes and parser improvements are welcome. For design changes, please open an issue first to discuss the shape before opening a PR.
|
package/dist/cli.js
CHANGED
|
@@ -5,7 +5,7 @@ import { Command } from "commander";
|
|
|
5
5
|
import pino2 from "pino";
|
|
6
6
|
|
|
7
7
|
// package.json
|
|
8
|
-
var version = "0.
|
|
8
|
+
var version = "0.25.0";
|
|
9
9
|
|
|
10
10
|
// src/logger.ts
|
|
11
11
|
import pino from "pino";
|
|
@@ -786,6 +786,96 @@ import { createReadStream } from "fs";
|
|
|
786
786
|
import { basename as basename3 } from "path";
|
|
787
787
|
import { createInterface } from "readline";
|
|
788
788
|
|
|
789
|
+
// src/parse-helpers.ts
|
|
790
|
+
function asQueuedPromptUserEntry(entry) {
|
|
791
|
+
if (entry.type !== "attachment") return entry;
|
|
792
|
+
const att = entry.attachment;
|
|
793
|
+
if (att?.type !== "queued_command" || att.commandMode !== "prompt") return entry;
|
|
794
|
+
if (att.origin?.kind !== "human") return entry;
|
|
795
|
+
return {
|
|
796
|
+
...entry,
|
|
797
|
+
type: "user",
|
|
798
|
+
attachment: void 0,
|
|
799
|
+
message: { role: "user", content: att.prompt }
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
function extractTextContent(content) {
|
|
803
|
+
if (!content) return "";
|
|
804
|
+
if (typeof content === "string") return cleanSystemTags(content);
|
|
805
|
+
if (Array.isArray(content)) {
|
|
806
|
+
return content.map((item) => {
|
|
807
|
+
if (typeof item === "string") return item;
|
|
808
|
+
if (item?.type === "text" && item?.text) return item.text;
|
|
809
|
+
if (item?.type === "tool_result" && typeof item?.content === "string") return item.content;
|
|
810
|
+
return "";
|
|
811
|
+
}).filter(Boolean).map(cleanSystemTags).join(" ");
|
|
812
|
+
}
|
|
813
|
+
return "";
|
|
814
|
+
}
|
|
815
|
+
function extractToolUseNames(content) {
|
|
816
|
+
if (!Array.isArray(content)) return [];
|
|
817
|
+
return content.filter((item) => item?.type === "tool_use" && item?.name).map((item) => item.name);
|
|
818
|
+
}
|
|
819
|
+
function extractToolUseBlocks(content) {
|
|
820
|
+
if (!Array.isArray(content)) return [];
|
|
821
|
+
return content.filter((item) => item?.type === "tool_use" && item?.name && item?.id).map((item) => ({
|
|
822
|
+
id: item.id,
|
|
823
|
+
name: item.name,
|
|
824
|
+
input: item.input || {}
|
|
825
|
+
}));
|
|
826
|
+
}
|
|
827
|
+
var TOOL_NAME_TO_TYPE = {
|
|
828
|
+
Edit: "edit",
|
|
829
|
+
Write: "write",
|
|
830
|
+
Read: "read",
|
|
831
|
+
Bash: "bash",
|
|
832
|
+
Grep: "grep",
|
|
833
|
+
Glob: "glob",
|
|
834
|
+
Agent: "taskAgent",
|
|
835
|
+
TaskCreate: "taskCreate",
|
|
836
|
+
TaskUpdate: "taskUpdate"
|
|
837
|
+
};
|
|
838
|
+
function extractToolResultBlocks(content, pendingToolUses) {
|
|
839
|
+
if (!Array.isArray(content)) return [];
|
|
840
|
+
return content.filter((item) => item?.type === "tool_result" && item?.tool_use_id).map((item) => {
|
|
841
|
+
const toolName = pendingToolUses.get(item.tool_use_id)?.name ?? "";
|
|
842
|
+
return {
|
|
843
|
+
toolUseId: item.tool_use_id,
|
|
844
|
+
type: TOOL_NAME_TO_TYPE[toolName] ?? "generic",
|
|
845
|
+
content: typeof item.content === "string" ? { text: item.content } : item.content ?? {},
|
|
846
|
+
isError: typeof item.is_error === "boolean" ? item.is_error : void 0
|
|
847
|
+
};
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
function isOnlyToolResultContent(content) {
|
|
851
|
+
if (!Array.isArray(content)) return false;
|
|
852
|
+
return content.length > 0 && content.every((item) => item?.type === "tool_result");
|
|
853
|
+
}
|
|
854
|
+
function extractThinking(content) {
|
|
855
|
+
if (!Array.isArray(content)) return { content: "", signature: "" };
|
|
856
|
+
const blocks = content.filter((item) => item?.type === "thinking");
|
|
857
|
+
return {
|
|
858
|
+
content: blocks.map((b) => b.thinking).filter(Boolean).join("\n\n"),
|
|
859
|
+
signature: blocks.map((b) => b.signature).filter(Boolean).join("")
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
function hasImageBlocks(content) {
|
|
863
|
+
if (!Array.isArray(content)) return false;
|
|
864
|
+
return content.some(
|
|
865
|
+
(item) => item?.type === "image" && (item?.source?.type === "base64" || item?.file?.base64 !== void 0)
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
function parseTeammateMessageTag(content) {
|
|
869
|
+
const match = content.match(/<teammate-message\s+([^>]*)>/);
|
|
870
|
+
if (!match) return null;
|
|
871
|
+
const attrs = match[1];
|
|
872
|
+
const id = attrs.match(/teammate_id="([^"]*)"/)?.[1];
|
|
873
|
+
if (!id) return null;
|
|
874
|
+
const summary = attrs.match(/summary="([^"]*)"/)?.[1];
|
|
875
|
+
const color = attrs.match(/color="([^"]*)"/)?.[1];
|
|
876
|
+
return { teammateId: id, summary, color };
|
|
877
|
+
}
|
|
878
|
+
|
|
789
879
|
// src/persistent/claude-name-slots.ts
|
|
790
880
|
function userPromptText(entry) {
|
|
791
881
|
if (entry.isMeta || entry.isCompactSummary === true) return "";
|
|
@@ -1090,18 +1180,6 @@ function getShortProjectName(fullPath) {
|
|
|
1090
1180
|
}
|
|
1091
1181
|
|
|
1092
1182
|
// src/parser.ts
|
|
1093
|
-
function asQueuedPromptUserEntry(entry) {
|
|
1094
|
-
if (entry.type !== "attachment") return entry;
|
|
1095
|
-
const att = entry.attachment;
|
|
1096
|
-
if (att?.type !== "queued_command" || att.commandMode !== "prompt") return entry;
|
|
1097
|
-
if (att.origin?.kind !== "human") return entry;
|
|
1098
|
-
return {
|
|
1099
|
-
...entry,
|
|
1100
|
-
type: "user",
|
|
1101
|
-
attachment: void 0,
|
|
1102
|
-
message: { role: "user", content: att.prompt }
|
|
1103
|
-
};
|
|
1104
|
-
}
|
|
1105
1183
|
function turnDurationOf(entry) {
|
|
1106
1184
|
if (entry.type !== "system" || entry.subtype !== "turn_duration" || typeof entry.durationMs !== "number") {
|
|
1107
1185
|
return null;
|
|
@@ -1205,54 +1283,6 @@ async function parseConversation(filePath, account) {
|
|
|
1205
1283
|
lastPrompt: state.lastPrompt || void 0
|
|
1206
1284
|
};
|
|
1207
1285
|
}
|
|
1208
|
-
function extractTextContent(content) {
|
|
1209
|
-
if (!content) return "";
|
|
1210
|
-
if (typeof content === "string") return cleanSystemTags(content);
|
|
1211
|
-
if (Array.isArray(content)) {
|
|
1212
|
-
return content.map((item) => {
|
|
1213
|
-
if (typeof item === "string") return item;
|
|
1214
|
-
if (item?.type === "text" && item?.text) return item.text;
|
|
1215
|
-
if (item?.type === "tool_result" && typeof item?.content === "string") return item.content;
|
|
1216
|
-
return "";
|
|
1217
|
-
}).filter(Boolean).map(cleanSystemTags).join(" ");
|
|
1218
|
-
}
|
|
1219
|
-
return "";
|
|
1220
|
-
}
|
|
1221
|
-
function extractToolUseNames(content) {
|
|
1222
|
-
if (!Array.isArray(content)) return [];
|
|
1223
|
-
return content.filter((item) => item?.type === "tool_use" && item?.name).map((item) => item.name);
|
|
1224
|
-
}
|
|
1225
|
-
function extractToolUseBlocks(content) {
|
|
1226
|
-
if (!Array.isArray(content)) return [];
|
|
1227
|
-
return content.filter((item) => item?.type === "tool_use" && item?.name && item?.id).map((item) => ({
|
|
1228
|
-
id: item.id,
|
|
1229
|
-
name: item.name,
|
|
1230
|
-
input: item.input || {}
|
|
1231
|
-
}));
|
|
1232
|
-
}
|
|
1233
|
-
var TOOL_NAME_TO_TYPE = {
|
|
1234
|
-
Edit: "edit",
|
|
1235
|
-
Write: "write",
|
|
1236
|
-
Read: "read",
|
|
1237
|
-
Bash: "bash",
|
|
1238
|
-
Grep: "grep",
|
|
1239
|
-
Glob: "glob",
|
|
1240
|
-
Agent: "taskAgent",
|
|
1241
|
-
TaskCreate: "taskCreate",
|
|
1242
|
-
TaskUpdate: "taskUpdate"
|
|
1243
|
-
};
|
|
1244
|
-
function extractToolResultBlocks(content, pendingToolUses) {
|
|
1245
|
-
if (!Array.isArray(content)) return [];
|
|
1246
|
-
return content.filter((item) => item?.type === "tool_result" && item?.tool_use_id).map((item) => {
|
|
1247
|
-
const toolName = pendingToolUses.get(item.tool_use_id)?.name ?? "";
|
|
1248
|
-
return {
|
|
1249
|
-
toolUseId: item.tool_use_id,
|
|
1250
|
-
type: TOOL_NAME_TO_TYPE[toolName] ?? "generic",
|
|
1251
|
-
content: typeof item.content === "string" ? { text: item.content } : item.content ?? {},
|
|
1252
|
-
isError: typeof item.is_error === "boolean" ? item.is_error : void 0
|
|
1253
|
-
};
|
|
1254
|
-
});
|
|
1255
|
-
}
|
|
1256
1286
|
function collectToolNames(content, toolSet) {
|
|
1257
1287
|
if (!Array.isArray(content)) return;
|
|
1258
1288
|
for (const item of content) {
|
|
@@ -1261,34 +1291,6 @@ function collectToolNames(content, toolSet) {
|
|
|
1261
1291
|
}
|
|
1262
1292
|
}
|
|
1263
1293
|
}
|
|
1264
|
-
function isOnlyToolResultContent(content) {
|
|
1265
|
-
if (!Array.isArray(content)) return false;
|
|
1266
|
-
return content.length > 0 && content.every((item) => item?.type === "tool_result");
|
|
1267
|
-
}
|
|
1268
|
-
function extractThinking(content) {
|
|
1269
|
-
if (!Array.isArray(content)) return { content: "", signature: "" };
|
|
1270
|
-
const blocks = content.filter((item) => item?.type === "thinking");
|
|
1271
|
-
return {
|
|
1272
|
-
content: blocks.map((b) => b.thinking).filter(Boolean).join("\n\n"),
|
|
1273
|
-
signature: blocks.map((b) => b.signature).filter(Boolean).join("")
|
|
1274
|
-
};
|
|
1275
|
-
}
|
|
1276
|
-
function hasImageBlocks(content) {
|
|
1277
|
-
if (!Array.isArray(content)) return false;
|
|
1278
|
-
return content.some(
|
|
1279
|
-
(item) => item?.type === "image" && (item?.source?.type === "base64" || item?.file?.base64 !== void 0)
|
|
1280
|
-
);
|
|
1281
|
-
}
|
|
1282
|
-
function parseTeammateMessageTag(content) {
|
|
1283
|
-
const match = content.match(/<teammate-message\s+([^>]*)>/);
|
|
1284
|
-
if (!match) return null;
|
|
1285
|
-
const attrs = match[1];
|
|
1286
|
-
const id = attrs.match(/teammate_id="([^"]*)"/)?.[1];
|
|
1287
|
-
if (!id) return null;
|
|
1288
|
-
const summary = attrs.match(/summary="([^"]*)"/)?.[1];
|
|
1289
|
-
const color = attrs.match(/color="([^"]*)"/)?.[1];
|
|
1290
|
-
return { teammateId: id, summary, color };
|
|
1291
|
-
}
|
|
1292
1294
|
function getShortProjectName2(fullPath) {
|
|
1293
1295
|
const parts = fullPath.split("/").filter(Boolean);
|
|
1294
1296
|
return parts.slice(-3).join("/");
|