MCPcopy
hub / github.com/Doorman11991/smallcode / formatReferencesForPrompt

Function formatReferencesForPrompt

src/session/references.js:108โ€“135  ยท  view source on GitHub โ†—

* 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)

Source from the content-addressed store, hash-verified

106 * the user types @dir/ on a large directory.
107 */
108function 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
137module.exports = { resolveReferences, formatReferencesForPrompt, FILE_REGEX };

Callers 1

runAgentLoopFunction ยท 0.85

Calls

no outgoing calls

Tested by

no test coverage detected