* Format resolved files for injection into the conversation. * Returns a string to append to the user message. * Capped at ~2000 tokens (8000 chars) to prevent context overflow when * the user types @dir/ on a large directory.
(files)
| 106 | * the user types @dir/ on a large directory. |
| 107 | */ |
| 108 | function formatReferencesForPrompt(files) { |
| 109 | if (files.length === 0) return ''; |
| 110 | |
| 111 | const MAX_REF_CHARS = 8000; // ~2000 tokens |
| 112 | let output = '\n\n--- Referenced files ---\n'; |
| 113 | let totalChars = output.length; |
| 114 | |
| 115 | for (const file of files) { |
| 116 | let entry = ''; |
| 117 | if (file.type === 'file') { |
| 118 | // Cap individual file content to 4000 chars |
| 119 | const cappedContent = file.content.length > 4000 |
| 120 | ? file.content.slice(0, 4000) + `\n... (${file.lines} lines total, truncated)` |
| 121 | : file.content; |
| 122 | entry = `\n๐ ${file.path} (${file.lines} lines):\n\`\`\`\n${cappedContent}\n\`\`\`\n`; |
| 123 | } else { |
| 124 | entry = `\n๐ ${file.path}/:\n${file.content}\n`; |
| 125 | } |
| 126 | |
| 127 | if (totalChars + entry.length > MAX_REF_CHARS) { |
| 128 | output += `\n... (${files.length - files.indexOf(file)} more files truncated to fit context budget)\n`; |
| 129 | break; |
| 130 | } |
| 131 | output += entry; |
| 132 | totalChars += entry.length; |
| 133 | } |
| 134 | return output; |
| 135 | } |
| 136 | |
| 137 | module.exports = { resolveReferences, formatReferencesForPrompt, FILE_REGEX }; |