UNPKG

better-claude-code

Version:

CLI auxiliary tools for Claude Code

120 lines (119 loc) 4.48 kB
import { readdirSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; const MESSAGE_PATTERNS = { FILE_OR_FOLDER_SLASH: /\/([\w\-./]+)/g, FILE_OR_FOLDER_AT: /(^|\s)@([a-zA-Z][\w\-.]*(?:\/[\w\-./]+)*)/g }; export function extractPathsFromText(text) { const paths = new Set(); const slashMatches = text.match(MESSAGE_PATTERNS.FILE_OR_FOLDER_SLASH); if (slashMatches) { for (const match of slashMatches) { paths.add(match); } } const atRegex = new RegExp(MESSAGE_PATTERNS.FILE_OR_FOLDER_AT.source, 'g'); const atMatches = text.matchAll(atRegex); for (const match of atMatches) { const cleanMatch = match[0].trim().substring(1); paths.add(cleanMatch); } return Array.from(paths); } export async function getRealPathFromSession(folderPath) { try { const files = readdirSync(folderPath); const sessionFiles = files.filter((f) => f.endsWith('.jsonl') && !f.startsWith('agent-')); if (sessionFiles.length === 0) return null; const firstSession = join(folderPath, sessionFiles[0]); const content = readFileSync(firstSession, 'utf-8'); const lines = content.split('\n'); for (const line of lines) { if (!line.trim()) continue; try { const parsed = JSON.parse(line); if (parsed.cwd) { return parsed.cwd; } } catch { } } return null; } catch { return null; } } export function parseCommandFromContent(content) { const commandNameMatch = content.match(/<command-name>\/?([^<]+)<\/command-name>/); const commandArgsMatch = content.match(/<command-args>([^<]+)<\/command-args>/); if (commandNameMatch) { const cmdName = commandNameMatch[1]; const cmdArgs = commandArgsMatch ? commandArgsMatch[1] : ''; return cmdArgs ? `/${cmdName} ${cmdArgs}` : `/${cmdName}`; } return null; } export function extractTextContent(content) { if (typeof content === 'string') { return content; } if (Array.isArray(content)) { const textParts = []; for (const item of content) { if (item.type === 'text' && item.text) { textParts.push(item.text); } else if (item.type === 'tool_use') { const toolName = item.name; const input = item.input || {}; let toolInfo = `[Tool: ${toolName}]`; if (toolName === 'Edit' || toolName === 'Read' || toolName === 'Write') { if (input.file_path) { toolInfo = `[Tool: ${toolName}] ${input.file_path}`; } } else if (toolName === 'Glob') { const parts = []; if (input.pattern) parts.push(`pattern: "${input.pattern}"`); if (input.path) parts.push(`path: ${input.path}`); if (parts.length > 0) { toolInfo = `[Tool: ${toolName}] ${parts.join(', ')}`; } } else if (toolName === 'Grep') { const parts = []; if (input.pattern) parts.push(`pattern: "${input.pattern}"`); if (input.path) parts.push(`path: ${input.path}`); if (parts.length > 0) { toolInfo = `[Tool: ${toolName}] ${parts.join(', ')}`; } } else if (toolName === 'Task') { if (input.description) { toolInfo = `[Tool: ${toolName}] ${input.description}`; } } else if (toolName === 'WebSearch') { if (input.query) { toolInfo = `[Tool: ${toolName}] "${input.query}"`; } } else if (toolName === 'Bash') { if (input.description) { toolInfo = `[Tool: ${toolName}] ${input.description}`; } } textParts.push(toolInfo); } } return textParts.join('\n'); } return ''; }