sidekick-shared 0.27.6 → 0.28.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/CHANGELOG.md +28 -0
- package/README.md +58 -18
- package/dist/browser.d.ts +13 -2
- package/dist/browser.js +30 -2
- package/dist/claudePaths.d.ts +11 -0
- package/dist/claudePaths.js +61 -0
- package/dist/claudeProfiles.d.ts +2 -9
- package/dist/claudeProfiles.js +10 -25
- package/dist/codexPaths.d.ts +22 -0
- package/dist/codexPaths.js +67 -0
- package/dist/codexProfiles.d.ts +1 -4
- package/dist/codexProfiles.js +5 -43
- package/dist/doctor.js +2 -2
- package/dist/esm/accountChangeSignal.js +15 -0
- package/dist/esm/accountChanges.js +152 -0
- package/dist/esm/accountHealth.js +288 -0
- package/dist/esm/accountLaunch.js +145 -0
- package/dist/esm/accountLease.js +60 -0
- package/dist/esm/accountManager.js +606 -0
- package/dist/esm/accountRegistry.js +214 -0
- package/dist/esm/accountStatus.js +51 -0
- package/dist/esm/accountSwitch.js +74 -0
- package/dist/esm/accountSync.js +156 -0
- package/dist/esm/accountSyncTypes.js +4 -0
- package/dist/esm/accounts.js +1291 -0
- package/dist/esm/aggregation/EventAggregator.js +2034 -0
- package/dist/esm/aggregation/FrequencyTracker.js +69 -0
- package/dist/esm/aggregation/HeatmapTracker.js +89 -0
- package/dist/esm/aggregation/PatternExtractor.js +168 -0
- package/dist/esm/aggregation/costProvenance.js +31 -0
- package/dist/esm/aggregation/snapshot.js +161 -0
- package/dist/esm/aggregation/types.js +5 -0
- package/dist/esm/analytics/codeImpact.js +18 -0
- package/dist/esm/analytics/compactionLedger.js +37 -0
- package/dist/esm/analytics/qualityScore.js +112 -0
- package/dist/esm/autoSwitch.js +185 -0
- package/dist/esm/browser.js +34 -0
- package/dist/esm/claudeCredentialDoctor.js +239 -0
- package/dist/esm/claudeCredentialLedger.js +91 -0
- package/dist/esm/claudeCredentials.js +111 -0
- package/dist/esm/claudeLiveGuard.js +43 -0
- package/dist/esm/claudePaths.js +22 -0
- package/dist/esm/claudeProfiles.js +142 -0
- package/dist/esm/claudeRefreshLock.js +106 -0
- package/dist/esm/codexAuth.js +101 -0
- package/dist/esm/codexCredentialDoctor.js +265 -0
- package/dist/esm/codexCredentialStore.js +164 -0
- package/dist/esm/codexKeepAlive.js +112 -0
- package/dist/esm/codexPaths.js +172 -0
- package/dist/esm/codexProfiles.js +1251 -0
- package/dist/esm/codexQuota.js +564 -0
- package/dist/esm/codexQuotaWatcher.js +363 -0
- package/dist/esm/context/composer.js +71 -0
- package/dist/esm/context/sessionContext.js +546 -0
- package/dist/esm/credentialIO.js +283 -0
- package/dist/esm/credentials.js +49 -0
- package/dist/esm/doctor.js +305 -0
- package/dist/esm/ensureDefaultAccounts.js +62 -0
- package/dist/esm/errorHistory.js +56 -0
- package/dist/esm/extractors/errorTaxonomy.js +43 -0
- package/dist/esm/extractors/gatherAssets.js +32 -0
- package/dist/esm/extractors/sessionAssets.js +176 -0
- package/dist/esm/extractors/sources/claudeAssets.js +158 -0
- package/dist/esm/extractors/sources/codexAssets.js +234 -0
- package/dist/esm/extractors/tokenUsage.js +41 -0
- package/dist/esm/extractors/toolCall.js +142 -0
- package/dist/esm/failingTools.js +65 -0
- package/dist/esm/fileHead.js +27 -0
- package/dist/esm/formatters/eventHighlighter.js +332 -0
- package/dist/esm/formatters/noiseClassifier.js +209 -0
- package/dist/esm/formatters/sessionDump.js +328 -0
- package/dist/esm/formatters/toolSummary.js +226 -0
- package/dist/esm/formatting.js +82 -0
- package/dist/esm/handoffUrl.js +23 -0
- package/dist/esm/historicalStore.js +305 -0
- package/dist/esm/humanPrompt.js +88 -0
- package/dist/esm/index.js +213 -0
- package/dist/esm/keepAlive.js +189 -0
- package/dist/esm/liveCredentialGuard.js +33 -0
- package/dist/esm/modelAliases.js +51 -0
- package/dist/esm/modelCatalog.js +139 -0
- package/dist/esm/modelContext.js +280 -0
- package/dist/esm/modelInfo.js +832 -0
- package/dist/esm/multiProviderQuotaService.js +450 -0
- package/dist/esm/node.js +46 -0
- package/dist/esm/observedContextWindows.js +165 -0
- package/dist/esm/observedSessionCollector.js +939 -0
- package/dist/esm/parsers/changelogParser.js +71 -0
- package/dist/esm/parsers/codexHistory.js +89 -0
- package/dist/esm/parsers/codexParser.js +906 -0
- package/dist/esm/parsers/debugLogParser.js +126 -0
- package/dist/esm/parsers/jsonl.js +66 -0
- package/dist/esm/parsers/mcpToolName.js +28 -0
- package/dist/esm/parsers/openCodeParser.js +625 -0
- package/dist/esm/parsers/planExtractor.js +335 -0
- package/dist/esm/parsers/sessionActivityDetector.js +246 -0
- package/dist/esm/parsers/sessionPathResolver.js +713 -0
- package/dist/esm/parsers/subagentScanner.js +360 -0
- package/dist/esm/parsers/subagentStatsCache.js +45 -0
- package/dist/esm/parsers/subagentTraceParser.js +325 -0
- package/dist/esm/paths.js +134 -0
- package/dist/esm/peakHours.js +194 -0
- package/dist/esm/phrases.js +1330 -0
- package/dist/esm/pricingCatalog.js +247 -0
- package/dist/esm/processDetection.js +200 -0
- package/dist/esm/projectMigration.js +59 -0
- package/dist/esm/promptHistory.js +1221 -0
- package/dist/esm/providerFailure.js +362 -0
- package/dist/esm/providerIds.js +6 -0
- package/dist/esm/providerQuota.js +1 -0
- package/dist/esm/providerServiceStatus.js +145 -0
- package/dist/esm/providerServiceStatusTypes.js +1 -0
- package/dist/esm/providerStatus.js +117 -0
- package/dist/esm/providers/claudeCode.js +575 -0
- package/dist/esm/providers/claudeListing.js +121 -0
- package/dist/esm/providers/codex.js +1034 -0
- package/dist/esm/providers/codexDatabase.js +297 -0
- package/dist/esm/providers/codexRollouts.js +215 -0
- package/dist/esm/providers/detect.js +185 -0
- package/dist/esm/providers/diagnostics.js +83 -0
- package/dist/esm/providers/directoryListingCache.js +206 -0
- package/dist/esm/providers/factory.js +54 -0
- package/dist/esm/providers/openCode.js +2008 -0
- package/dist/esm/providers/openCodeDatabase.js +419 -0
- package/dist/esm/providers/rolloutWalker.js +252 -0
- package/dist/esm/providers/types.js +12 -0
- package/dist/esm/quota.js +182 -0
- package/dist/esm/quotaHistory.js +364 -0
- package/dist/esm/quotaPoller.js +230 -0
- package/dist/esm/quotaPresentation.js +97 -0
- package/dist/esm/quotaResolver.js +231 -0
- package/dist/esm/quotaSnapshots.js +130 -0
- package/dist/esm/quotaThresholds.js +85 -0
- package/dist/esm/readers/decisions.js +24 -0
- package/dist/esm/readers/handoff.js +20 -0
- package/dist/esm/readers/helpers.js +27 -0
- package/dist/esm/readers/history.js +9 -0
- package/dist/esm/readers/notes.js +48 -0
- package/dist/esm/readers/plans.js +225 -0
- package/dist/esm/readers/tasks.js +19 -0
- package/dist/esm/report/htmlHelpers.js +280 -0
- package/dist/esm/report/htmlReportGenerator.js +704 -0
- package/dist/esm/report/index.js +7 -0
- package/dist/esm/report/logo.js +2 -0
- package/dist/esm/report/openBrowser.js +19 -0
- package/dist/esm/report/sessionReportInputs.js +33 -0
- package/dist/esm/report/transcriptParser.js +83 -0
- package/dist/esm/report/types.js +4 -0
- package/dist/esm/schemas/accountManager.js +204 -0
- package/dist/esm/schemas/accountStatus.js +25 -0
- package/dist/esm/schemas/assistantTurn.js +74 -0
- package/dist/esm/schemas/index.js +25 -0
- package/dist/esm/schemas/observedSessionV1.js +77 -0
- package/dist/esm/schemas/providerFailure.js +37 -0
- package/dist/esm/schemas/quota.js +132 -0
- package/dist/esm/schemas/quotaHistory.js +40 -0
- package/dist/esm/schemas/sessionEvent.js +216 -0
- package/dist/esm/schemas/stateFile.js +75 -0
- package/dist/esm/schemas/tokenEstimation.js +7 -0
- package/dist/esm/schemas/transcript.js +86 -0
- package/dist/esm/schemas/usageNormalization.js +38 -0
- package/dist/esm/search/advancedFilter.js +197 -0
- package/dist/esm/search/sessionSearch.js +66 -0
- package/dist/esm/sessionFingerprint.js +40 -0
- package/dist/esm/sessionMonitor.js +198 -0
- package/dist/esm/sessionPreviews.js +522 -0
- package/dist/esm/sessionPromptHistory.js +254 -0
- package/dist/esm/sessionPromptSignals.js +364 -0
- package/dist/esm/sessionStats.js +160 -0
- package/dist/esm/sessionTokenTotals.js +98 -0
- package/dist/esm/sessionTranscripts.js +49 -0
- package/dist/esm/stateFile.js +119 -0
- package/dist/esm/statusline/BurnRateCalculator.js +40 -0
- package/dist/esm/statusline/claudeStatuslinePayload.js +148 -0
- package/dist/esm/statusline/formatter.js +122 -0
- package/dist/esm/statusline/index.js +8 -0
- package/dist/esm/terminalSync.js +95 -0
- package/dist/esm/testing/credentialGuard.js +197 -0
- package/dist/esm/testing/index.js +12 -0
- package/dist/esm/testing/sessionLogFixtures.js +128 -0
- package/dist/esm/tokenEstimation.js +67 -0
- package/dist/esm/tokenSummary.js +113 -0
- package/dist/esm/toolCategory.js +82 -0
- package/dist/esm/transcript.js +257 -0
- package/dist/esm/turns/assistantTurn.js +373 -0
- package/dist/esm/types/codex.js +23 -0
- package/dist/esm/types/decisionLog.js +5 -0
- package/dist/esm/types/historicalData.js +47 -0
- package/dist/esm/types/knowledgeNote.js +15 -0
- package/dist/esm/types/observedSessionV1.js +257 -0
- package/dist/esm/types/opencode.js +12 -0
- package/dist/esm/types/plan.js +7 -0
- package/dist/esm/types/sessionEvent.js +10 -0
- package/dist/esm/types/taskPersistence.js +12 -0
- package/dist/esm/usage/billingBlocks.js +148 -0
- package/dist/esm/usage/claudeUsageDedupe.js +189 -0
- package/dist/esm/usage/importSessionHistory.js +84 -0
- package/dist/esm/usage/subagentUsage.js +18 -0
- package/dist/esm/usage/usageEvents.js +402 -0
- package/dist/esm/usage/usageReports.js +202 -0
- package/dist/esm/usageNormalization.js +95 -0
- package/dist/esm/watchers/eventBridge.js +219 -0
- package/dist/esm/watchers/factory.js +63 -0
- package/dist/esm/watchers/jsonlTail.js +289 -0
- package/dist/esm/watchers/jsonlWatcher.js +435 -0
- package/dist/esm/watchers/providerReaderWatcher.js +118 -0
- package/dist/esm/watchers/sqliteWatcher.js +285 -0
- package/dist/esm/watchers/types.js +4 -0
- package/dist/esm/workspaceRoots.js +56 -0
- package/dist/esm/writers/atomic.js +375 -0
- package/dist/esm/writers/decisions.js +51 -0
- package/dist/esm/writers/notes.js +46 -0
- package/dist/esm/writers/tasks.js +63 -0
- package/dist/esm/zaiQuota.js +323 -0
- package/dist/esm/zaiQuotaApi.js +271 -0
- package/dist/esm/zaiQuotaWatcher.js +265 -0
- package/dist/fileHead.d.ts +22 -0
- package/dist/fileHead.js +33 -0
- package/dist/index.d.ts +16 -7
- package/dist/index.js +49 -15
- package/dist/node.d.ts +13 -1
- package/dist/node.js +43 -2
- package/dist/parsers/codexHistory.js +2 -2
- package/dist/parsers/codexParser.d.ts +12 -0
- package/dist/parsers/codexParser.js +34 -12
- package/dist/parsers/debugLogParser.js +2 -2
- package/dist/parsers/sessionPathResolver.js +5 -4
- package/dist/promptHistory.d.ts +32 -0
- package/dist/promptHistory.js +115 -59
- package/dist/providerFailure.d.ts +12 -6
- package/dist/providerFailure.js +45 -0
- package/dist/providers/claudeCode.js +7 -99
- package/dist/providers/claudeListing.d.ts +22 -0
- package/dist/providers/claudeListing.js +158 -0
- package/dist/providers/codex.d.ts +3 -1
- package/dist/providers/codex.js +26 -227
- package/dist/providers/codexRollouts.d.ts +44 -0
- package/dist/providers/codexRollouts.js +261 -0
- package/dist/providers/detect.js +4 -4
- package/dist/readers/plans.js +3 -3
- package/dist/schemas/index.d.ts +2 -0
- package/dist/schemas/index.js +7 -2
- package/dist/schemas/providerFailure.d.ts +154 -0
- package/dist/schemas/providerFailure.js +40 -0
- package/dist/sessionPromptHistory.d.ts +12 -7
- package/dist/sessionPromptHistory.js +15 -3
- package/dist/sessionPromptSignals.d.ts +9 -0
- package/dist/sessionPromptSignals.js +13 -0
- package/dist/testing/credentialGuard.d.ts +23 -0
- package/dist/testing/credentialGuard.js +233 -0
- package/dist/testing/index.d.ts +14 -0
- package/dist/testing/index.js +22 -0
- package/dist/testing/sessionLogFixtures.d.ts +28 -0
- package/dist/testing/sessionLogFixtures.js +169 -0
- package/dist/tokenEstimation.d.ts +2 -0
- package/dist/tokenEstimation.js +21 -11
- package/dist/toolCategory.d.ts +18 -0
- package/dist/toolCategory.js +86 -0
- package/dist/types/codex.d.ts +4 -0
- package/dist/usage/usageEvents.d.ts +16 -0
- package/dist/usage/usageEvents.js +53 -9
- package/dist/watchers/jsonlTail.d.ts +26 -1
- package/dist/watchers/jsonlTail.js +136 -28
- package/dist/watchers/jsonlWatcher.js +7 -0
- package/dist/watchers/providerReaderWatcher.js +1 -0
- package/dist/watchers/types.d.ts +6 -0
- package/dist/workspaceRoots.d.ts +23 -0
- package/dist/workspaceRoots.js +93 -0
- package/package.json +22 -2
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Low-level JSONL tail reader for consumers that need raw parsed events plus
|
|
3
|
+
* their own aggregation lifecycle.
|
|
4
|
+
*/
|
|
5
|
+
import * as fs from 'fs';
|
|
6
|
+
import { fileHeadOf, HEAD_BYTES, headMatches } from '../fileHead';
|
|
7
|
+
import { JsonlParser } from '../parsers/jsonl';
|
|
8
|
+
const DEFAULT_DEBOUNCE_MS = 100;
|
|
9
|
+
const DEFAULT_CATCHUP_INTERVAL_MS = 30_000;
|
|
10
|
+
/** Default bytes read from the file per chunk. */
|
|
11
|
+
export const DEFAULT_JSONL_TAIL_READ_BYTES = 1024 * 1024;
|
|
12
|
+
/** Default longest line parsed; longer lines are skipped and reported. */
|
|
13
|
+
export const DEFAULT_JSONL_TAIL_MAX_LINE_BYTES = 16 * 1024 * 1024;
|
|
14
|
+
/** Skip old complete events while retaining an in-progress final line. */
|
|
15
|
+
function lastCompleteLineOffset(filePath) {
|
|
16
|
+
const fd = fs.openSync(filePath, 'r');
|
|
17
|
+
try {
|
|
18
|
+
let end = fs.fstatSync(fd).size;
|
|
19
|
+
const buffer = Buffer.alloc(64 * 1024);
|
|
20
|
+
while (end > 0) {
|
|
21
|
+
const start = Math.max(0, end - buffer.length);
|
|
22
|
+
const count = fs.readSync(fd, buffer, 0, end - start, start);
|
|
23
|
+
const newline = buffer.subarray(0, count).lastIndexOf(0x0a);
|
|
24
|
+
if (newline >= 0)
|
|
25
|
+
return start + newline + 1;
|
|
26
|
+
end = start;
|
|
27
|
+
}
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
fs.closeSync(fd);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function createJsonlTail(options) {
|
|
35
|
+
return new JsonlTailReader(options);
|
|
36
|
+
}
|
|
37
|
+
class JsonlTailReader {
|
|
38
|
+
options;
|
|
39
|
+
active = false;
|
|
40
|
+
/** Physical byte position already read from the file. */
|
|
41
|
+
readOffset;
|
|
42
|
+
/** Resume-safe byte position ending immediately after a newline. */
|
|
43
|
+
committedOffset;
|
|
44
|
+
pendingBytes = Buffer.alloc(0);
|
|
45
|
+
/** Inside a line over `maxLineBytes`: drop bytes until its newline. */
|
|
46
|
+
skippingLine = false;
|
|
47
|
+
currentLine = { offset: 0, length: 0 };
|
|
48
|
+
/** Identity and prefix of the file the offsets refer to, once read. */
|
|
49
|
+
identity = null;
|
|
50
|
+
head = null;
|
|
51
|
+
lastMtimeMs = -1;
|
|
52
|
+
maxReadBytes;
|
|
53
|
+
maxLineBytes;
|
|
54
|
+
fsWatcher = null;
|
|
55
|
+
debounceTimer = null;
|
|
56
|
+
catchupTimer = null;
|
|
57
|
+
eventsInCurrentBatch = 0;
|
|
58
|
+
parser;
|
|
59
|
+
constructor(options) {
|
|
60
|
+
this.options = options;
|
|
61
|
+
this.readOffset = options.startOffset ?? 0;
|
|
62
|
+
this.committedOffset = this.readOffset;
|
|
63
|
+
this.maxReadBytes = positiveInteger(options.maxReadBytes, DEFAULT_JSONL_TAIL_READ_BYTES);
|
|
64
|
+
this.maxLineBytes = positiveInteger(options.maxLineBytes, DEFAULT_JSONL_TAIL_MAX_LINE_BYTES);
|
|
65
|
+
this.parser = new JsonlParser({
|
|
66
|
+
onEvent: (event) => {
|
|
67
|
+
this.eventsInCurrentBatch += 1;
|
|
68
|
+
this.options.onEvent(event, this.currentLine);
|
|
69
|
+
},
|
|
70
|
+
onError: (error, line) => this.options.onError?.(error, line),
|
|
71
|
+
}, { schema: options.schema });
|
|
72
|
+
}
|
|
73
|
+
get isActive() {
|
|
74
|
+
return this.active;
|
|
75
|
+
}
|
|
76
|
+
start() {
|
|
77
|
+
if (this.active)
|
|
78
|
+
return;
|
|
79
|
+
this.active = true;
|
|
80
|
+
if (this.options.startAtEnd && this.options.startOffset === undefined) {
|
|
81
|
+
try {
|
|
82
|
+
this.readOffset = lastCompleteLineOffset(this.options.path);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
this.readOffset = 0;
|
|
86
|
+
}
|
|
87
|
+
this.committedOffset = this.readOffset;
|
|
88
|
+
}
|
|
89
|
+
this.readNow();
|
|
90
|
+
this.watchFile();
|
|
91
|
+
const catchupIntervalMs = this.options.catchupIntervalMs ?? DEFAULT_CATCHUP_INTERVAL_MS;
|
|
92
|
+
if (catchupIntervalMs > 0) {
|
|
93
|
+
this.catchupTimer = setInterval(() => this.readNow(), catchupIntervalMs);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
stop() {
|
|
97
|
+
if (!this.active)
|
|
98
|
+
return;
|
|
99
|
+
this.active = false;
|
|
100
|
+
if (this.debounceTimer) {
|
|
101
|
+
clearTimeout(this.debounceTimer);
|
|
102
|
+
this.debounceTimer = null;
|
|
103
|
+
}
|
|
104
|
+
if (this.fsWatcher) {
|
|
105
|
+
this.fsWatcher.close();
|
|
106
|
+
this.fsWatcher = null;
|
|
107
|
+
}
|
|
108
|
+
if (this.catchupTimer) {
|
|
109
|
+
clearInterval(this.catchupTimer);
|
|
110
|
+
this.catchupTimer = null;
|
|
111
|
+
}
|
|
112
|
+
this.parser.flush();
|
|
113
|
+
}
|
|
114
|
+
dispose() {
|
|
115
|
+
this.stop();
|
|
116
|
+
}
|
|
117
|
+
readNow() {
|
|
118
|
+
let fd = null;
|
|
119
|
+
try {
|
|
120
|
+
fd = fs.openSync(this.options.path, 'r');
|
|
121
|
+
const stat = fs.fstatSync(fd);
|
|
122
|
+
this.checkContinuity(fd, stat);
|
|
123
|
+
if (stat.size <= this.readOffset)
|
|
124
|
+
return;
|
|
125
|
+
this.eventsInCurrentBatch = 0;
|
|
126
|
+
let bytesRead = 0;
|
|
127
|
+
const chunk = Buffer.alloc(Math.min(this.maxReadBytes, stat.size - this.readOffset));
|
|
128
|
+
while (this.readOffset < stat.size) {
|
|
129
|
+
const want = Math.min(chunk.length, stat.size - this.readOffset);
|
|
130
|
+
const count = fs.readSync(fd, chunk, 0, want, this.readOffset);
|
|
131
|
+
if (count <= 0)
|
|
132
|
+
break;
|
|
133
|
+
this.consume(chunk.subarray(0, count), this.readOffset);
|
|
134
|
+
this.readOffset += count;
|
|
135
|
+
bytesRead += count;
|
|
136
|
+
}
|
|
137
|
+
if (bytesRead <= 0)
|
|
138
|
+
return;
|
|
139
|
+
this.options.onBatchComplete?.({
|
|
140
|
+
bytesRead,
|
|
141
|
+
eventsRead: this.eventsInCurrentBatch,
|
|
142
|
+
offset: this.committedOffset,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
this.options.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
if (fd !== null) {
|
|
150
|
+
try {
|
|
151
|
+
fs.closeSync(fd);
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
/* ignore close errors */
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Starts over when the path now names another file, the file shrank, its
|
|
161
|
+
* first bytes changed, or the resume point no longer follows a newline.
|
|
162
|
+
*/
|
|
163
|
+
checkContinuity(fd, stat) {
|
|
164
|
+
let reason = null;
|
|
165
|
+
if (this.identity && (stat.dev !== this.identity.dev || stat.ino !== this.identity.ino)) {
|
|
166
|
+
reason = 'replaced';
|
|
167
|
+
}
|
|
168
|
+
else if (stat.size < this.readOffset) {
|
|
169
|
+
reason = 'truncated';
|
|
170
|
+
}
|
|
171
|
+
if (reason === null && this.identity && stat.mtimeMs !== this.lastMtimeMs) {
|
|
172
|
+
const prefix = readAt(fd, 0, Math.min(HEAD_BYTES, stat.size));
|
|
173
|
+
const before = this.committedOffset > 0 ? readAt(fd, this.committedOffset - 1, 1) : null;
|
|
174
|
+
if ((this.head && !headMatches(this.head, prefix)) || (before && before[0] !== 0x0a)) {
|
|
175
|
+
reason = 'rewritten';
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (reason !== null)
|
|
179
|
+
this.reset(reason);
|
|
180
|
+
this.identity = { dev: stat.dev, ino: stat.ino };
|
|
181
|
+
this.lastMtimeMs = stat.mtimeMs;
|
|
182
|
+
if (!this.head || this.head.bytes < Math.min(HEAD_BYTES, stat.size)) {
|
|
183
|
+
this.head = fileHeadOf(readAt(fd, 0, Math.min(HEAD_BYTES, stat.size)));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
reset(reason) {
|
|
187
|
+
const previousOffset = this.committedOffset;
|
|
188
|
+
this.readOffset = 0;
|
|
189
|
+
this.committedOffset = 0;
|
|
190
|
+
this.pendingBytes = Buffer.alloc(0);
|
|
191
|
+
this.skippingLine = false;
|
|
192
|
+
this.head = null;
|
|
193
|
+
this.parser.reset();
|
|
194
|
+
if (reason === 'replaced' && this.active) {
|
|
195
|
+
// fs.watch follows the old inode; watch the file now at the path.
|
|
196
|
+
this.fsWatcher?.close();
|
|
197
|
+
this.fsWatcher = null;
|
|
198
|
+
this.watchFile();
|
|
199
|
+
}
|
|
200
|
+
this.options.onReset?.({ reason, previousOffset });
|
|
201
|
+
}
|
|
202
|
+
/** Parses the complete lines in `bytes`, which start at file offset `position`. */
|
|
203
|
+
consume(bytes, position) {
|
|
204
|
+
let start = 0;
|
|
205
|
+
if (this.skippingLine) {
|
|
206
|
+
const newline = bytes.indexOf(0x0a);
|
|
207
|
+
if (newline < 0)
|
|
208
|
+
return;
|
|
209
|
+
this.skippingLine = false;
|
|
210
|
+
start = newline + 1;
|
|
211
|
+
this.committedOffset = position + start;
|
|
212
|
+
}
|
|
213
|
+
let lineStart = this.committedOffset;
|
|
214
|
+
let cursor = start;
|
|
215
|
+
for (;;) {
|
|
216
|
+
const newline = bytes.indexOf(0x0a, cursor);
|
|
217
|
+
if (newline < 0)
|
|
218
|
+
break;
|
|
219
|
+
const piece = bytes.subarray(cursor, newline);
|
|
220
|
+
const line = this.pendingBytes.length > 0 ? Buffer.concat([this.pendingBytes, piece]) : piece;
|
|
221
|
+
this.pendingBytes = Buffer.alloc(0);
|
|
222
|
+
const lineEnd = position + newline + 1;
|
|
223
|
+
if (line.length > this.maxLineBytes) {
|
|
224
|
+
this.reportOversized(lineStart, line.length);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
this.currentLine = { offset: lineStart, length: line.length };
|
|
228
|
+
this.parser.processChunk(line.toString('utf8') + '\n');
|
|
229
|
+
}
|
|
230
|
+
this.committedOffset = lineEnd;
|
|
231
|
+
lineStart = lineEnd;
|
|
232
|
+
cursor = newline + 1;
|
|
233
|
+
}
|
|
234
|
+
const rest = bytes.subarray(cursor);
|
|
235
|
+
if (rest.length === 0)
|
|
236
|
+
return;
|
|
237
|
+
if (this.pendingBytes.length + rest.length > this.maxLineBytes) {
|
|
238
|
+
this.reportOversized(lineStart, this.pendingBytes.length + rest.length);
|
|
239
|
+
this.pendingBytes = Buffer.alloc(0);
|
|
240
|
+
this.skippingLine = true;
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
this.pendingBytes = Buffer.concat([this.pendingBytes, rest]);
|
|
244
|
+
}
|
|
245
|
+
reportOversized(offset, atLeast) {
|
|
246
|
+
this.options.onError?.(new Error(`JSONL line at byte ${offset} is over ${this.maxLineBytes} bytes (${atLeast} so far); skipped.`));
|
|
247
|
+
}
|
|
248
|
+
getOffset() {
|
|
249
|
+
return this.committedOffset;
|
|
250
|
+
}
|
|
251
|
+
seekTo(offset) {
|
|
252
|
+
this.readOffset = Math.max(0, offset);
|
|
253
|
+
this.committedOffset = this.readOffset;
|
|
254
|
+
this.pendingBytes = Buffer.alloc(0);
|
|
255
|
+
this.skippingLine = false;
|
|
256
|
+
this.parser.reset();
|
|
257
|
+
}
|
|
258
|
+
watchFile() {
|
|
259
|
+
try {
|
|
260
|
+
this.fsWatcher = fs.watch(this.options.path, { persistent: false }, () => {
|
|
261
|
+
this.debouncedRead();
|
|
262
|
+
});
|
|
263
|
+
this.fsWatcher.on('error', (error) => {
|
|
264
|
+
this.fsWatcher?.close();
|
|
265
|
+
this.fsWatcher = null;
|
|
266
|
+
this.options.onError?.(error);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
// Polling still covers filesystems where fs.watch is unavailable.
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
debouncedRead() {
|
|
274
|
+
if (this.debounceTimer)
|
|
275
|
+
clearTimeout(this.debounceTimer);
|
|
276
|
+
const debounceMs = this.options.debounceMs ?? DEFAULT_DEBOUNCE_MS;
|
|
277
|
+
this.debounceTimer = setTimeout(() => this.readNow(), debounceMs);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function positiveInteger(value, fallback) {
|
|
281
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 1
|
|
282
|
+
? Math.floor(value)
|
|
283
|
+
: fallback;
|
|
284
|
+
}
|
|
285
|
+
function readAt(fd, position, length) {
|
|
286
|
+
const buffer = Buffer.alloc(length);
|
|
287
|
+
const count = length > 0 ? fs.readSync(fd, buffer, 0, length, position) : 0;
|
|
288
|
+
return buffer.subarray(0, count);
|
|
289
|
+
}
|