Files
Codeman/src/utils/regex-patterns.ts
T
arkonandClaude Opus 4.7 02e2f3e8b5 refactor: remove dead code and narrow internal exports (knip sweep)
Knip-driven cleanup. All changes verified with tsc --noEmit, lint, and
build.

Removed (zero consumers):
- VERIFICATION_PROMPT constant + its barrel re-export
- createInitialOrchestratorPersistState factory
- transcriptWatcher singleton export
- createAnsiPatternFull / createAnsiPatternSimple factories
- TimerInfo interface + unused AiCheckResult/AiPlanCheckResult imports
  in respawn-controller.ts
- 35 unused Zod z.infer \`*Input\` types in schemas.ts
- Dead re-exports: SessionMode from session.ts, AuthSessionRecord from
  web/ports/index.ts, EnhancedPlanTask/CheckpointReview from
  ralph-tracker.ts, 7 unused entries in utils/index.ts
- 14 event/config interfaces that lived only as JSDoc hints (no TS type
  position usage): Session/Respawn/RalphLoop/RalphTracker/
  SessionManager/SessionAutoOps/Subagent/TaskQueue/TaskTracker/
  TranscriptWatcher/Image/OrchestratorLoop Events + RespawnPreset +
  SessionOutput

Narrowed to module scope (kept but no longer exported):
- buildPermissionArgs in session-cli-builder.ts
- 28 type/interface declarations used only within their own file:
  Ai{Idle,Plan}Check{Config,State}, BashToolParser{Events,Config},
  FileStream/CreateStream{Options,Result}, PlanSubagentEvent,
  SubagentCallback, RalphLoopConfig, RalphLoop{Events,Options},
  ActiveTimerInfo, DetectionStatus, ActionLogEntry, AutoOpsCallbacks,
  TunnelStatus, Timer/LRUMap/StaleExpirationMap Options, AuthState,
  SessionListenerDeps, SseStreamManagerDeps, and 8 more

Docs: CLAUDE.md advice for global-regex `lastIndex` now points to the
remaining `execPattern()` helper instead of the deleted factories.

Knip delta: unused files 42→0, unused exports 161→16, unused types 92→0.
The 16 remaining exports are a mobile-test helper toolkit intentionally
kept for upcoming tests.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 11:57:00 +02:00

76 lines
2.6 KiB
TypeScript

/**
* @fileoverview Shared regex patterns for terminal and token parsing.
*
* Pre-compiled patterns avoid re-compilation overhead on each use.
* Import these patterns instead of defining them locally.
*
* @module utils/regex-patterns
*/
/**
* Comprehensive ANSI escape pattern that handles:
* - SGR (colors/styles): ESC [ params m
* - CSI sequences (cursor, scroll, etc.): ESC [ params letter
* - OSC sequences (title, etc.): ESC ] ... BEL or ESC ] ... ST
* - Single-char escapes: ESC = or ESC >
*
* Use this when you need complete ANSI stripping including OSC sequences.
* Note: Has global flag - reset lastIndex before exec() if reusing.
*/
// eslint-disable-next-line no-control-regex
export const ANSI_ESCAPE_PATTERN_FULL = /\x1b(?:\[[0-9;?]*[A-Za-z]|\][^\x07\x1b]*(?:\x07|\x1b\\)|[=>])/g;
/**
* Simple ANSI CSI-only pattern for basic escape code stripping.
* Matches: ESC [ params letter (e.g., colors, cursor movement)
*
* Use this for faster stripping when OSC sequences aren't a concern.
* Note: Has global flag - reset lastIndex before exec() if reusing.
*/
// eslint-disable-next-line no-control-regex
export const ANSI_ESCAPE_PATTERN_SIMPLE = /\x1b\[[0-9;]*[A-Za-z]/g;
/**
* Pattern to extract token count from Claude's status line.
* Matches: "123.4k tokens", "5234 tokens", "1.2M tokens"
*
* Capture groups:
* - Group 1: The numeric value (e.g., "123.4", "5234", "1.2")
* - Group 2: Optional suffix (k, K, m, M) or undefined
*/
export const TOKEN_PATTERN = /(\d+(?:\.\d+)?)\s*([kKmM])?\s*tokens/;
/**
* Strips ANSI escape codes from text using the comprehensive pattern.
* @param text - Text containing ANSI escape codes
* @returns Text with ANSI codes removed
*/
export function stripAnsi(text: string): string {
return text.replace(ANSI_ESCAPE_PATTERN_FULL, '');
}
/**
* Regex to validate safe file paths (no shell metacharacters).
* Shared by schemas.ts and tmux-manager.ts for consistent path validation.
*/
/**
* Braille spinner characters used by Claude Code's Ink UI.
* Matches any of: ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧
* Used on the hot path (every PTY chunk) — pre-compiled for performance.
*/
export const SPINNER_PATTERN = /[⠋⠙⠹⠸⠼⠴⠦⠧]/;
export const SAFE_PATH_PATTERN = /^[a-zA-Z0-9_/\-. ~]+$/;
/**
* Execute a global regex pattern against data, calling the callback for each match.
* Automatically resets lastIndex before execution.
*/
export function execPattern(pattern: RegExp, data: string, callback: (match: RegExpExecArray) => void): void {
pattern.lastIndex = 0;
let match: RegExpExecArray | null;
while ((match = pattern.exec(data)) !== null) {
callback(match);
}
}