export const KNOWN_AGENT_ENV_VARS: Record = { CLAUDECODE: 'claude_code', CLAUDE_CODE_ENTRYPOINT: 'claude_code', CURSOR_AGENT: 'cursor', CODEX_SANDBOX: 'codex_cli', CLINE_ACTIVE: 'cline', GEMINI_CLI: 'gemini_cli', AUGMENT_AGENT: 'auggie_cli', OPENCODE_CLIENT: 'opencode_client', TRAE_AI_SHELL_ID: 'trae_ai', GOOSE_TERMINAL: 'goose', GOOSE_AGENT: 'goose', AI_AGENT: 'ai_agent', // Generic fallback. }; export function getAiAgent( env: NodeJS.ProcessEnv = process.env ): string | undefined { for (const [key, agent] of Object.entries(KNOWN_AGENT_ENV_VARS)) { const value = env[key]; if (!value) continue; // If the value is a descriptive name it is used directly, // otherwise 'ai_agent' is returned. if (key === 'AI_AGENT') { const v = value.toLowerCase(); return v === '1' || v === 'true' ? 'ai_agent' : v; } return agent; } return undefined; } /** * A helper class for keeping track of how often specific events occurred. */ export class MultiSet> { _entries: Map = new Map(); add(entry: T): void { const key = JSON.stringify(Object.entries(entry).sort()); this._entries.set(key, (this._entries.get(key) ?? 0) + 1); } clear(): void { this._entries.clear(); } *[Symbol.iterator](): Iterator<[T, number]> { for (const [key, count] of this._entries) { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument yield [Object.fromEntries(JSON.parse(key)) as T, count]; } } } /** * It transforms a random string into snake case. Snake case is completely * lowercase and uses '_' to separate words. For example: * * This function defines a "word" as a sequence of characters until the next `.` or capital letter. * * 'Random String' => 'random_string' * * It also removes any non alphanumeric characters. For example: * * 'Node.js REPL Instantiation' => 'node_js_repl_instantiation' * * @param str Any non snake-case formatted string * @returns The snake-case formatted string */ export function toSnakeCase(str: string): string { const matches = str.match( /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g ); if (!matches) { return str; } return matches.map((x) => x.toLowerCase()).join('_'); }