|
| 1 | +import chalk from "chalk"; |
| 2 | +import ora, { type Ora } from "ora"; |
| 3 | + |
| 4 | +import type { Config } from "./config"; |
| 5 | + |
| 6 | +export enum LogLevel { |
| 7 | + DEBUG = "debug", // eslint-disable-line no-unused-vars |
| 8 | + INFO = "info", // eslint-disable-line no-unused-vars |
| 9 | + WARN = "warn", // eslint-disable-line no-unused-vars |
| 10 | + ERROR = "error", // eslint-disable-line no-unused-vars |
| 11 | +} |
| 12 | + |
| 13 | +export class Logger { |
| 14 | + private level: LogLevel; |
| 15 | + private color: boolean; |
| 16 | + private includeTimestamps: boolean; |
| 17 | + private spanStartTime: number | undefined = undefined; |
| 18 | + private spinner: Ora | undefined = undefined; |
| 19 | + private toolCallCount: number = 0; |
| 20 | + private updateInterval: NodeJS.Timeout | undefined = undefined; |
| 21 | + |
| 22 | + constructor(config: Config) { |
| 23 | + this.includeTimestamps = config.log_timestamps; |
| 24 | + this.level = config.log_level; |
| 25 | + this.color = config.log_color; |
| 26 | + } |
| 27 | + |
| 28 | + private getTimestamp() { |
| 29 | + const timestamp = new Date().toLocaleTimeString("en-US", { |
| 30 | + hour12: false, |
| 31 | + hour: "2-digit", |
| 32 | + minute: "2-digit", |
| 33 | + second: "2-digit", |
| 34 | + }); |
| 35 | + |
| 36 | + return this.includeTimestamps |
| 37 | + ? this.color |
| 38 | + ? chalk.gray(`[${timestamp}]`) |
| 39 | + : `[${timestamp}]` |
| 40 | + : ""; |
| 41 | + } |
| 42 | + |
| 43 | + private getSpanMarker() { |
| 44 | + return this.spanStartTime !== undefined ? " ├─" : ""; |
| 45 | + } |
| 46 | + |
| 47 | + private getDuration() { |
| 48 | + return Math.round((Date.now() - (this.spanStartTime ?? Date.now())) / 1000); |
| 49 | + } |
| 50 | + |
| 51 | + private getToolCallStats() { |
| 52 | + const duration = this.getDuration(); |
| 53 | + const toolCallText = ` 🕝 duration: ${duration}s | 🛠️ tool calls: ${this.toolCallCount}`; |
| 54 | + return this.color ? chalk.dim(toolCallText) : toolCallText; |
| 55 | + } |
| 56 | + |
| 57 | + private formatMessage(message: string, color: (text: string) => string) { |
| 58 | + return this.color ? color(message) : message; |
| 59 | + } |
| 60 | + |
| 61 | + private logToConsole( |
| 62 | + message: string, |
| 63 | + level: LogLevel, |
| 64 | + color: (text: string) => string, |
| 65 | + ) { |
| 66 | + const shouldSkip = |
| 67 | + (this.level === LogLevel.ERROR && level !== LogLevel.ERROR) || |
| 68 | + (this.level === LogLevel.WARN && |
| 69 | + (level === LogLevel.INFO || level === LogLevel.DEBUG)) || |
| 70 | + (this.level === LogLevel.INFO && level === LogLevel.DEBUG); |
| 71 | + if (shouldSkip) return; |
| 72 | + |
| 73 | + const timestamp = this.getTimestamp(); |
| 74 | + const spanMarker = this.getSpanMarker(); |
| 75 | + const formattedMessage = this.formatMessage(message, color); |
| 76 | + const output = `${timestamp}${spanMarker} ${formattedMessage}`; |
| 77 | + |
| 78 | + if (level === LogLevel.ERROR || level === LogLevel.WARN) { |
| 79 | + console.error(output); |
| 80 | + } else if (level === LogLevel.DEBUG) { |
| 81 | + console.debug(output); |
| 82 | + } else { |
| 83 | + console.log(output); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + info(message: string) { |
| 88 | + this.logToConsole(message, LogLevel.INFO, chalk.white); |
| 89 | + } |
| 90 | + |
| 91 | + warn(message: string) { |
| 92 | + this.logToConsole(message, LogLevel.WARN, chalk.yellow); |
| 93 | + } |
| 94 | + |
| 95 | + error(message: string) { |
| 96 | + this.logToConsole(message, LogLevel.ERROR, chalk.red); |
| 97 | + } |
| 98 | + |
| 99 | + debug(message: string) { |
| 100 | + this.logToConsole(message, LogLevel.DEBUG, chalk.gray); |
| 101 | + } |
| 102 | + |
| 103 | + incrementToolCalls() { |
| 104 | + this.toolCallCount++; |
| 105 | + this.updateSpanDisplay(); |
| 106 | + } |
| 107 | + |
| 108 | + private updateSpanDisplay() { |
| 109 | + if (!this.spinner) return; |
| 110 | + const mainMessage = this.spinner.text.split("\n")[0]; |
| 111 | + this.spinner.text = `${mainMessage}\n${this.getToolCallStats()}`; |
| 112 | + } |
| 113 | + |
| 114 | + startSpan(message: string) { |
| 115 | + this.info(message); |
| 116 | + this.spanStartTime = Date.now(); |
| 117 | + this.toolCallCount = 0; |
| 118 | + this.spinner = ora(this.formatMessage(message, chalk.cyan)).start(); |
| 119 | + |
| 120 | + this.updateInterval = setInterval(() => this.updateSpanDisplay(), 1000); |
| 121 | + } |
| 122 | + |
| 123 | + updateSpan(message: string, emoji: string) { |
| 124 | + if (!this.spinner) return; |
| 125 | + |
| 126 | + const originalText = this.spinner.text; |
| 127 | + const timestamp = this.getTimestamp(); |
| 128 | + const spanMarker = this.getSpanMarker(); |
| 129 | + const formattedMessage = this.formatMessage(message, chalk.white); |
| 130 | + |
| 131 | + this.spinner.stopAndPersist({ |
| 132 | + text: formattedMessage, |
| 133 | + symbol: `${timestamp}${spanMarker} ${emoji}`, |
| 134 | + }); |
| 135 | + |
| 136 | + this.spinner.start(this.formatMessage(originalText, chalk.cyan)); |
| 137 | + this.updateSpanDisplay(); |
| 138 | + } |
| 139 | + |
| 140 | + endSpan(message: string = "Completed with no output") { |
| 141 | + if (this.updateInterval) { |
| 142 | + clearInterval(this.updateInterval); |
| 143 | + this.updateInterval = undefined; |
| 144 | + } |
| 145 | + |
| 146 | + const doneMessage = "Done!"; |
| 147 | + const timestamp = this.getTimestamp(); |
| 148 | + const duration = this.getDuration(); |
| 149 | + |
| 150 | + this.spinner?.stopAndPersist({ |
| 151 | + text: this.formatMessage(`${doneMessage} (${duration}s)`, chalk.cyan), |
| 152 | + symbol: `${timestamp} ✅`, |
| 153 | + }); |
| 154 | + |
| 155 | + this.spinner = undefined; |
| 156 | + this.spanStartTime = undefined; |
| 157 | + this.toolCallCount = 0; |
| 158 | + |
| 159 | + this.info(`\r\n${message}\r\n`); |
| 160 | + } |
| 161 | +} |
0 commit comments