Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 1x 1x 34x 34x 1x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 2x 62x 62x 62x 62x 1x 14x 14x 1x 1x 1x 14x 1x 1x 12x 14x 1x 45x 45x 1x 1x 1x 45x 1x 1x 43x 43x 43x 43x 43x 43x 43x 45x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 45x 25x 25x 20x 20x 45x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 45x 1x 24x 24x 24x 24x 24x 1x 3x 3x 3x 1x 1x 1x 1x 1x 37x 17x 17x 37x 37x 37x 37x 1x 2x 2x 1x 6x 6x 1x 6x 6x 1x 3x 3x 1x | /**
* Logging System for AAC Voice API
*
* This module provides comprehensive session logging for voice transcriptions,
* command matching, synonym resolution, and confidence tracking.
*
* **When to use `Logger` vs `CommandHistory`:**
*
* - **Use `Logger`** for comprehensive session analytics including:
* - Full transcription text with timestamps
* - Detailed synonym resolution tracking (library vs API)
* - Confidence scores for all matches
* - Error messages for debugging
* - JSON export capabilities for data analysis
* - Multi-speaker support in online mode
* - Suitable for researchers, developers, and analytics
*
* - **Use `CommandHistory`** (see CommandHistory.ts) for:
* - Lightweight command-only tracking
* - UI features like command history popup
* - Simple execution tracking without context
* - End-user facing features
*
* @module Logging
*/
/**
* Details about a matched command within a transcription.
* Records how the command was matched and its execution result.
*/
export interface MatchedCommand {
/** Name of the command that was matched */
commandName: string;
/** The actual word from the transcription that triggered the match */
matchedWord: string;
/** The synonym word that was used to match (only for synonym matches) */
matchedSynonym?: string;
/** Source of the match */
synonymSource: 'direct' | 'library-synonym' | 'api-synonym' | 'phonetic';
/** Confidence level of the match (0-1 scale, where 1 is 100% confident) */
confidence: number;
/** Whether the command executed successfully or failed */
status: 'success' | 'failed';
/** Error message if command execution failed (only for failed status) */
error?: string;
}
/**
* A complete log entry representing one transcription and its matched commands.
* Uses pending pattern: created → matches added → finalized.
*/
export interface LogEntry {
/** Sequential numeric ID for this log entry */
id: number;
/** ISO 8601 timestamp when the transcription was received */
timestamp: string;
/** The full transcribed text from speech recognition */
transcriptionText: string;
/** Speaker identifier (only available in online multi-speaker mode) */
speakerId?: string;
/** Array of all commands that were matched in this transcription */
matchedCommands: MatchedCommand[];
/** Whether this entry has been finalized (locked from further modifications) */
finalized: boolean;
}
/**
* Configuration options for the Logger.
*/
export interface LoggerConfig {
/** Maximum number of log entries to keep. When exceeded, oldest entries are batch-pruned. */
maxLogSize?: number;
/** Number of entries to remove when pruning (batch size) */
pruneCount?: number;
}
/**
* Internal tracking for pending entries with auto-finalization.
*/
interface PendingEntry {
entry: LogEntry;
timeoutId: ReturnType<typeof setTimeout>;
}
/**
* Logger singleton class for tracking voice command sessions.
*
* Uses a pending entry pattern:
* 1. `createEntry()` - Creates a new log entry and starts 60-second auto-finalization timer
* 2. `addMatch()` - Appends matched commands to pending entry
* 3. `finalizeEntry()` - Marks entry complete and prevents further modifications
*
* Auto-finalization after 60 seconds helps prevent memory leaks from forgotten entries.
*
* @example
* ```typescript
* const logger = Logger.getInstance();
*
* // Start logging a transcription
* const entryId = logger.createEntry("jump forward", "speaker-1");
*
* // Add matched commands
* logger.addMatch(entryId, {
* commandName: "jump",
* matchedWord: "jump",
* synonymSource: "direct",
* confidence: 1.0,
* status: "success"
* });
*
* // Finalize when done
* logger.finalizeEntry(entryId);
*
* // Export to JSON
* const json = logger.exportToJSON();
* ```
*/
export class Logger {
/** Singleton instance */
private static instance: Logger;
/** All log entries (both pending and finalized) */
private entries: Map<number, LogEntry>;
/** Pending entries with auto-finalization timeouts */
private pendingEntries: Map<number, PendingEntry>;
/** Sequential ID counter */
private nextId: number;
/** Configuration */
private config: Required<LoggerConfig>;
/** Auto-finalization timeout in milliseconds (60 seconds) */
private readonly AUTO_FINALIZE_TIMEOUT = 60000;
/** Private constructor for singleton pattern */
private constructor(config: LoggerConfig = {}) {
this.entries = new Map();
this.pendingEntries = new Map();
this.nextId = 1;
this.config = {
maxLogSize: config.maxLogSize ?? Infinity,
pruneCount: config.pruneCount ?? 10
};
}
/**
* Gets the singleton instance of Logger.
* @returns The Logger instance
*/
public static getInstance(config?: LoggerConfig): Logger {
if (!Logger.instance) {
Logger.instance = new Logger(config);
}
return Logger.instance;
}
/**
* Creates a new log entry for a transcription.
* Starts a 60-second auto-finalization timer.
*
* @param transcriptionText - The transcribed speech text
* @param speakerId - Optional speaker identifier (for multi-speaker mode)
* @returns The numeric ID of the created entry
*
* @example
* ```typescript
* const entryId = logger.createEntry("jump and run");
* ```
*/
public createEntry(transcriptionText: string, speakerId?: string): number {
const id = this.nextId++;
const entry: LogEntry = {
id,
timestamp: new Date().toISOString(),
transcriptionText,
speakerId,
matchedCommands: [],
finalized: false
};
this.entries.set(id, entry);
// Start auto-finalization timer
const timeoutId = setTimeout(() => {
this.autoFinalizeEntry(id);
}, this.AUTO_FINALIZE_TIMEOUT);
this.pendingEntries.set(id, { entry, timeoutId });
return id;
}
/**
* Adds a matched command to a pending log entry.
* Silently ignores if the entry is already finalized.
*
* @param entryId - The ID of the log entry
* @param matchDetails - Details about the matched command
*
* @example
* ```typescript
* logger.addMatch(entryId, {
* commandName: "jump",
* matchedWord: "leap",
* matchedSynonym: "leap",
* synonymSource: "library-synonym",
* confidence: 1.0,
* status: "success"
* });
* ```
*/
public addMatch(entryId: number, matchDetails: MatchedCommand): void {
const entry = this.entries.get(entryId);
if (!entry) {
console.warn(`Logger: Cannot add match to non-existent entry ${entryId}`);
return;
}
if (entry.finalized) {
// Silently ignore attempts to modify finalized entries
return;
}
entry.matchedCommands.push(matchDetails);
}
/**
* Finalizes a log entry, preventing further modifications.
* Clears the auto-finalization timeout.
*
* @param entryId - The ID of the log entry to finalize
*
* @example
* ```typescript
* logger.finalizeEntry(entryId);
* ```
*/
public finalizeEntry(entryId: number): void {
const entry = this.entries.get(entryId);
if (!entry) {
console.warn(`Logger: Cannot finalize non-existent entry ${entryId}`);
return;
}
if (entry.finalized) {
return; // Already finalized
}
entry.finalized = true;
// Clear the auto-finalization timeout
const pending = this.pendingEntries.get(entryId);
if (pending) {
clearTimeout(pending.timeoutId);
this.pendingEntries.delete(entryId);
}
// Check if we need to prune old entries
this.checkAndPrune();
}
/**
* Auto-finalizes an entry when timeout is reached.
* Logs a warning to help developers catch missing finalizeEntry() calls.
*
* @private
* @param entryId - The ID of the entry to auto-finalize
*/
private autoFinalizeEntry(entryId: number): void {
const entry = this.entries.get(entryId);
if (!entry || entry.finalized) {
return;
}
console.warn(
`Logger: Entry ${entryId} was auto-finalized after 60 seconds. ` +
`This may indicate a missing finalizeEntry() call. ` +
`Transcription: "${entry.transcriptionText.substring(0, 50)}..."`
);
entry.finalized = true;
this.pendingEntries.delete(entryId);
// Check if we need to prune old entries
this.checkAndPrune();
}
/**
* Checks if log size exceeds limit and batch-prunes oldest entries if needed.
* Removes multiple entries at once for efficiency.
*
* @private
*/
private checkAndPrune(): void {
if (this.config.maxLogSize === Infinity) {
return; // No limit set
}
const finalizedCount = Array.from(this.entries.values())
.filter(e => e.finalized).length;
if (finalizedCount > this.config.maxLogSize) {
// Get all finalized entries sorted by ID (oldest first)
const finalized = Array.from(this.entries.values())
.filter(e => e.finalized)
.sort((a, b) => a.id - b.id);
// Batch prune oldest entries
const entriesToRemove = finalized.slice(0, this.config.pruneCount);
for (const entry of entriesToRemove) {
this.entries.delete(entry.id);
}
console.log(
`Logger: Pruned ${entriesToRemove.length} oldest entries ` +
`(${finalizedCount} -> ${finalizedCount - entriesToRemove.length})`
);
}
}
/**
* Gets all finalized log entries.
* Returns an immutable copy to prevent external modifications.
*
* @returns Array of finalized log entries
*
* @example
* ```typescript
* const logs = logger.getAllLogs();
* console.log(`Total logs: ${logs.length}`);
* ```
*/
public getAllLogs(): LogEntry[] {
return Array.from(this.entries.values())
.filter(entry => entry.finalized)
.sort((a, b) => a.id - b.id)
.map(entry => ({ ...entry, matchedCommands: [...entry.matchedCommands] }));
}
/**
* Exports all finalized logs as a formatted JSON string.
* Uses 2-space indentation for readability.
*
* @returns Pretty-printed JSON string
*
* @example
* ```typescript
* const json = logger.exportToJSON();
* console.log(json);
* ```
*/
public exportToJSON(): string {
const logs = this.getAllLogs();
return JSON.stringify(logs, null, 2);
}
/**
* Gets the log data as a plain JavaScript object.
* Useful for Node.js environments where manual serialization is needed.
*
* @returns Plain object containing all finalized log entries
*
* @example
* ```typescript
* // Node.js usage
* const fs = require('fs');
* const logData = logger.getJSONBlob();
* fs.writeFileSync('logs.json', JSON.stringify(logData, null, 2));
* ```
*/
public getJSONBlob(): LogEntry[] {
return this.getAllLogs();
}
/**
* Saves logs to a file using browser download.
* Only works in browser environments.
*
* @param filename - Name of the file to download
* @throws Error if called in Node.js environment
*
* @example
* ```typescript
* // Browser usage
* logger.saveToFile('session-2024-01-15.json');
* ```
*/
public saveToFile(filename: string = 'aac-session-log.json'): void {
// Check if we're in a browser environment
if (typeof document === 'undefined') {
throw new Error(
'saveToFile() is only available in browser environments. ' +
'Use getJSONBlob() for Node.js and handle file writing manually.'
);
}
const json = this.exportToJSON();
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
// Clean up
URL.revokeObjectURL(url);
}
/**
* Clears all log entries and resets the ID counter.
* Also cancels all pending auto-finalization timeouts.
*
* @example
* ```typescript
* logger.clear(); // Start fresh
* ```
*/
public clear(): void {
// Cancel all pending timeouts
for (const pending of this.pendingEntries.values()) {
clearTimeout(pending.timeoutId);
}
this.entries.clear();
this.pendingEntries.clear();
this.nextId = 1;
}
/**
* Gets the total number of log entries (including pending).
*
* @returns Total count of entries
*/
public getTotalCount(): number {
return this.entries.size;
}
/**
* Gets the number of finalized entries.
*
* @returns Count of finalized entries
*/
public getFinalizedCount(): number {
return Array.from(this.entries.values()).filter(e => e.finalized).length;
}
/**
* Gets the number of pending (not yet finalized) entries.
*
* @returns Count of pending entries
*/
public getPendingCount(): number {
return this.pendingEntries.size;
}
/**
* Updates the configuration.
*
* @param config - Partial configuration to update
*/
public updateConfig(config: Partial<LoggerConfig>): void {
this.config = { ...this.config, ...config };
}
}
|