| 1 | import path from "path"; |
| 2 | |
| 3 | import { diffLines } from "diff"; |
| 4 | |
| 5 | /** |
| 6 | * Calculate lines of code added/removed from a file diff |
| 7 | */ |
| 8 | export function calculateLinesOfCodeDiff( |
| 9 | oldContent: string, |
| 10 | newContent: string, |
| 11 | ): { added: number; removed: number } { |
| 12 | const diff = diffLines(oldContent, newContent); |
| 13 | let added = 0; |
| 14 | let removed = 0; |
| 15 | |
| 16 | for (const part of diff) { |
| 17 | if (part.added) { |
| 18 | added += part.count || 0; |
| 19 | } else if (part.removed) { |
| 20 | removed += part.count || 0; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | return { added, removed }; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Extract programming language from file path |
| 29 | */ |
| 30 | export function getLanguageFromFilePath(filePath: string): string { |
| 31 | const ext = path.extname(filePath).toLowerCase(); |
| 32 | |
| 33 | const languageMap: Record<string, string> = { |
| 34 | ".js": "JavaScript", |
| 35 | ".jsx": "JavaScript", |
| 36 | ".ts": "TypeScript", |
| 37 | ".tsx": "TypeScript", |
| 38 | ".py": "Python", |
| 39 | ".java": "Java", |
| 40 | ".cpp": "C++", |
| 41 | ".c": "C", |
| 42 | ".cs": "C#", |
| 43 | ".php": "PHP", |
| 44 | ".rb": "Ruby", |
| 45 | ".go": "Go", |
| 46 | ".rs": "Rust", |
| 47 | ".swift": "Swift", |
| 48 | ".kt": "Kotlin", |
| 49 | ".scala": "Scala", |
| 50 | ".clj": "Clojure", |
| 51 | ".hs": "Haskell", |
| 52 | ".ml": "OCaml", |
| 53 | ".elm": "Elm", |
| 54 | ".dart": "Dart", |
| 55 | ".lua": "Lua", |
| 56 | ".r": "R", |
| 57 | ".m": "Objective-C", |
| 58 | ".mm": "Objective-C++", |
| 59 | ".sh": "Shell", |
| 60 | ".bash": "Bash", |
| 61 | ".zsh": "Zsh", |
| 62 | ".fish": "Fish", |
| 63 | ".ps1": "PowerShell", |
| 64 | ".html": "HTML", |
| 65 | ".css": "CSS", |
| 66 | ".scss": "SCSS", |
| 67 | ".sass": "Sass", |
| 68 | ".less": "Less", |
| 69 | ".vue": "Vue", |
| 70 | ".svelte": "Svelte", |
| 71 | ".md": "Markdown", |
| 72 | ".json": "JSON", |
| 73 | ".xml": "XML", |
| 74 | ".yaml": "YAML", |
| 75 | ".yml": "YAML", |
| 76 | ".toml": "TOML", |
| 77 | ".ini": "INI", |
| 78 | ".cfg": "Config", |
| 79 | ".conf": "Config", |
| 80 | ".sql": "SQL", |
| 81 | ".dockerfile": "Dockerfile", |
| 82 | }; |
| 83 | |
| 84 | return languageMap[ext] || "unknown"; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Extract the first word (command type) from a shell command |
| 89 | */ |
| 90 | export function extractCommandType(command: string): string { |
| 91 | return command.trim().split(/\s+/)[0] || "unknown"; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Check if a command is a git commit command |
| 96 | */ |
| 97 | export function isGitCommitCommand(command: string): boolean { |
| 98 | const trimmed = command.trim().toLowerCase(); |
| 99 | return trimmed.startsWith("git commit") || trimmed.startsWith("git-commit"); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Check if a command is a pull request creation command |
| 104 | */ |
| 105 | export function isPullRequestCommand(command: string): boolean { |
| 106 | const trimmed = command.trim().toLowerCase(); |
| 107 | return ( |
| 108 | trimmed.includes("gh pr create") || |
| 109 | (trimmed.includes("git push") && trimmed.includes("pull-request")) || |
| 110 | trimmed.includes("hub pull-request") || |
| 111 | trimmed.includes("gitlab mr create") |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Simple cost calculation based on tokens and model |
| 117 | * This should be updated with actual pricing from model providers |
| 118 | */ |
| 119 | export function calculateTokenCost( |
| 120 | inputTokens: number, |
| 121 | outputTokens: number, |
| 122 | model: string, |
| 123 | ): number { |
| 124 | // Pricing per 1K tokens (these are approximate and should be updated) |
| 125 | const pricingMap: Record<string, { input: number; output: number }> = { |
| 126 | "gpt-4": { input: 0.03, output: 0.06 }, |
| 127 | "gpt-4-turbo": { input: 0.01, output: 0.03 }, |
| 128 | "gpt-3.5-turbo": { input: 0.0015, output: 0.002 }, |
| 129 | "claude-3-5-sonnet-20241022": { input: 0.003, output: 0.015 }, |
| 130 | "claude-3-sonnet": { input: 0.003, output: 0.015 }, |
| 131 | "claude-3-haiku": { input: 0.00025, output: 0.00125 }, |
| 132 | "claude-3-opus": { input: 0.015, output: 0.075 }, |
| 133 | }; |
| 134 | |
| 135 | // Find pricing for model (case insensitive partial match) |
| 136 | const modelKey = Object.keys(pricingMap).find((key) => |
| 137 | model.toLowerCase().includes(key.toLowerCase()), |
| 138 | ); |
| 139 | |
| 140 | if (!modelKey) { |
| 141 | // Default pricing if model not found |
| 142 | return (inputTokens * 0.001 + outputTokens * 0.002) / 1000; |
| 143 | } |
| 144 | |
| 145 | const pricing = pricingMap[modelKey]; |
| 146 | return (inputTokens * pricing.input + outputTokens * pricing.output) / 1000; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get file type from extension for metrics |
| 151 | */ |
| 152 | export function getFileTypeFromPath(filePath: string): string { |
| 153 | const ext = path.extname(filePath).toLowerCase(); |
| 154 | return ext.startsWith(".") ? ext.substring(1) : "unknown"; |
| 155 | } |