Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281/** Activity word extraction from Claude transcripts. @module */
import { openAsBlob, } from 'node:fs';import { readFile, } from 'node:fs/promises';
//region Gerund filters
/** Words ending in "-ing" that are not meaningful activity verbs. */const NOISE_GERUNDS = new Set([ 'beginning', 'completing', 'continuing', 'ending', 'finishing', 'starting', 'stopping', 'waiting', 'pending', 'asking', 'calling', 'coming', 'doing', 'getting', 'giving', 'going', 'having', 'keeping', 'knowing', 'letting', 'looking', 'making', 'meaning', 'putting', 'saying', 'seeing', 'showing', 'telling', 'trying', 'turning', 'wanting', 'working', 'reading', 'searching', 'anything', 'everything', 'nothing', 'something', 'thing', 'according', 'assuming', 'concerning', 'considering', 'depending', 'during', 'excluding', 'following', 'including', 'providing', 'regarding', 'supposing', 'amazing', 'annoying', 'boring', 'confusing', 'corresponding', 'exciting', 'existing', 'frustrating', 'interesting', 'missing', 'lint-missing', 'outstanding', 'overwhelming', 'remaining', 'surprising', 'surrounding', 'underlying', 'bring', 'cling', 'fling', 'king', 'ring', 'sing', 'sling', 'spring', 'sting', 'string', 'swing', 'wing', 'wring', 'sibling', 'being', 'needing', 'running', 'thinking', 'using', 'quizzical-crafting', 'crafting', 'wild-nibbling', 'nibbling', 'purring', 'hatching', 'purring-hatching', 'beaming', 'hidden-beaming', 'nnothing', 'nstring',],);
/** Minimum word length to consider as a gerund candidate. */const MIN_GERUND_LENGTH = 5;
/** Matches words ending in "-ing", including hyphenated compounds. */// oxlint-disable-next-line no-restricted-syntax/no-regex -- bounded transcript tail, simple word-character class, global scan without nested quantifiers.const GERUND_PATTERN = /\b[a-z]+-?[a-z]*ing\b/gu;
/** Number of bytes to read from the end of the transcript. */const TAIL_BYTES = 8_192;
//endregion Gerund filters
//region Gerund extraction
/** Finds the last meaningful gerund in text. @param value - text to scan @returns capitalized gerund, or empty string when none is found @example ```ts findGerundInText('I am compiling and testing'); ``` */function findGerundInText(value: string,): string { /** Lowercased text for case-insensitive matching. */ const lowercaseValue = value.toLowerCase(); /** Raw `-ing` matches across lowercased text. */ const matches = lowercaseValue.match(GERUND_PATTERN,) ?? []; /** Matches that survive length and noise filters. */ const candidates = matches .filter(function isLongEnough(word,): boolean { return word.length >= MIN_GERUND_LENGTH; },) .filter(function isNotNoise(word,): boolean { return !NOISE_GERUNDS.has(word,); },);
/** Last surviving candidate. */ const last = candidates.at(-1,); if (last === undefined) return '';
return last.charAt(0,) .toUpperCase() + last.slice(1,);}
/** Reads transcript tail without loading the entire file. @param transcriptPath - path to session transcript JSONL @returns transcript tail text @example ```ts await readTranscriptTail({ transcriptPath: '/tmp/session.jsonl' }); ``` */async function readTranscriptTail({ transcriptPath,}: Readonly<{ transcriptPath: string;}>,): Promise<string> { /** Blob view of the transcript. */ const blob = await openAsBlob(transcriptPath,); /** Slice offset clamped to zero for short transcripts. */ const start = Math.max( 0, blob.size - TAIL_BYTES, ); return await blob .slice( start, blob.size, ) .text();}
/** Extracts a context-aware activity word from transcript path. @param transcriptPath - path to session transcript JSONL @returns capitalized activity word, or empty string when unavailable @example ```ts await readActivityWord({ transcriptPath: '/tmp/session.jsonl' }); ``` */async function readActivityWord({ transcriptPath,}: Readonly<{ transcriptPath: string;}>,): Promise<string> { try { /** Tail of the transcript decoded as UTF-8 text. */ const tail = await readTranscriptTail({ transcriptPath, },); return findGerundInText(tail,); } catch (_error: unknown) { return ''; }}
/** Reads complete transcript text for small test fixtures. @param transcriptPath - path to session transcript JSONL @returns transcript text, or empty string when unreadable @example ```ts await readTranscriptForTest({ transcriptPath: '/tmp/session.jsonl' }); ``` */async function readTranscriptForTest({ transcriptPath,}: Readonly<{ transcriptPath: string;}>,): Promise<string> { try { return await readFile( transcriptPath, 'utf8', ); } catch (_error: unknown) { return ''; }}
//endregion Gerund extraction
export { findGerundInText, readActivityWord, readTranscriptForTest, readTranscriptTail,};