refactor(core): replace CommonJS requires with named imports (#31)

This commit is contained in:
tommy0103
2026-08-04 05:49:51 +08:00
committed by GitHub
parent 47df0fe76a
commit 1941e64572
6 changed files with 76 additions and 82 deletions
+15 -17
View File
@@ -6,11 +6,9 @@
// persist layer consumes them. Session aggregates here reflect only THIS chunk
// (started_at/ended_at/message_count); persist merges them with any existing row.
import { createRequire } from 'node:module';
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
import { homedir } from 'node:os';
import { dirname, isAbsolute, join, normalize, relative } from 'node:path';
const require = createRequire(import.meta.url);
const fs = require('node:fs');
import {
extractText, extractContentType, extractMessageIsMeta, isSkillInstructions,
@@ -65,7 +63,7 @@ function discoverAt(rootDir: string, ctx: DiscoverContext): IndexUnit[] {
const projectsDir = join(rootDir, 'projects');
const historyPath = normalize(join(rootDir, 'history.jsonl'));
const historyTitles = new Map<string, string>();
if (fs.existsSync(historyPath)) {
if (existsSync(historyPath)) {
readLines(historyPath, (line: string) => {
try {
const item = JSON.parse(line);
@@ -104,7 +102,7 @@ function discoverAt(rootDir: string, ctx: DiscoverContext): IndexUnit[] {
return historyChanged
|| forcedPaths.has(normalizedPath)
|| cursor === null
|| Number(cursor.split(':')[0]) < fs.statSync(file.path).mtimeMs;
|| Number(cursor.split(':')[0]) < statSync(file.path).mtimeMs;
}).map((f: any) => ({
key: f.path,
sessionId: f.sessionId,
@@ -118,20 +116,20 @@ function discoverAt(rootDir: string, ctx: DiscoverContext): IndexUnit[] {
}));
const workflowUnits: IndexUnit[] = [];
if (!fs.existsSync(projectsDir)) return transcriptUnits;
if (!existsSync(projectsDir)) return transcriptUnits;
let projects: string[];
try { projects = fs.readdirSync(projectsDir); } catch { return transcriptUnits; }
try { projects = readdirSync(projectsDir); } catch { return transcriptUnits; }
for (const project of projects) {
const projectPath = join(projectsDir, project);
if (!isDir(projectPath)) continue;
let sessionIds: string[];
try { sessionIds = fs.readdirSync(projectPath); } catch { continue; }
try { sessionIds = readdirSync(projectPath); } catch { continue; }
for (const sessionId of sessionIds) {
const workflowDir = join(projectPath, sessionId, 'workflows');
if (!isDir(workflowDir)) continue;
const mainTranscriptPath = join(projectPath, `${sessionId}.jsonl`);
let files: string[];
try { files = fs.readdirSync(workflowDir); } catch { continue; }
try { files = readdirSync(workflowDir); } catch { continue; }
for (const file of files) {
if (!file.endsWith('.json')) continue;
const workflowPath = join(workflowDir, file);
@@ -142,7 +140,7 @@ function discoverAt(rootDir: string, ctx: DiscoverContext): IndexUnit[] {
&& !changedWorkflowPaths.has(normalizedPath)
&& !relationshipChanged
) continue;
const mtime = fs.statSync(workflowPath).mtimeMs;
const mtime = statSync(workflowPath).mtimeMs;
const cursor = ctx.lastCursor(workflowPath);
if (!relationshipChanged && cursor !== null && Number(cursor.split(':')[0]) >= mtime) continue;
workflowUnits.push({
@@ -172,7 +170,7 @@ function workflowParentToolUseId(
runId: string,
workflowName: string | null,
): string | null {
if (!fs.existsSync(transcriptPath)) return null;
if (!existsSync(transcriptPath)) return null;
const workflowToolIds = new Set<string>();
let parentToolUseId: string | null = null;
readLines(transcriptPath, (line: string) => {
@@ -201,10 +199,10 @@ function workflowParentToolUseId(
}
function* parseWorkflow(unit: IndexUnit): Generator<TranscriptRecord, Cursor> {
const mtime = fs.statSync(unit.key).mtimeMs;
const mtime = statSync(unit.key).mtimeMs;
const outCursor = `${mtime}:1`;
let workflow: any;
try { workflow = JSON.parse(fs.readFileSync(unit.key, 'utf8')); } catch { return outCursor; }
try { workflow = JSON.parse(readFileSync(unit.key, 'utf8')); } catch { return outCursor; }
if (!workflow?.runId) return outCursor;
const meta = unit.meta as ClaudeWorkflowUnitMeta;
const progress = Array.isArray(workflow.workflowProgress) ? workflow.workflowProgress : [];
@@ -251,7 +249,7 @@ export function* parse(unit: IndexUnit, cursor: Cursor): Generator<TranscriptRec
return yield* parseWorkflow(unit);
}
const skip = cursorToSkip(cursor);
const mtime = fs.statSync(unit.key).mtimeMs;
const mtime = statSync(unit.key).mtimeMs;
const isSubagent = unit.isSubagent === true;
const records: TranscriptRecord[] = [];
const sm = {
@@ -339,9 +337,9 @@ export function* parse(unit: IndexUnit, cursor: Cursor): Generator<TranscriptRec
if (isSubagent && unit.agentId) {
const metaPath = unit.key.replace(/\.jsonl$/, '.meta.json');
if (fs.existsSync(metaPath)) {
if (existsSync(metaPath)) {
try {
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
const meta = JSON.parse(readFileSync(metaPath, 'utf8'));
const workflowRunId = (unit.meta as { workflowRunId?: string } | undefined)?.workflowRunId;
if (workflowRunId) {
records.push({
@@ -394,7 +392,7 @@ function rawClaude(input: RawLookup): RawRecord | null {
? join(dirname(mainPath), String(input.session?.id ?? ''), 'subagents', 'workflows', runId, `${input.agentId}.jsonl`)
: join(dirname(mainPath), String(input.session?.id ?? ''), 'subagents', `${input.agentId}.jsonl`);
}
if (!fs.existsSync(sourcePath)) return null;
if (!existsSync(sourcePath)) return null;
let found: string | null = null;
readLines(sourcePath, (line: string) => {
if (!line.includes(input.messageUuid)) return;