@stackmemoryai/stackmemory
Version:
Project-scoped memory for AI coding tools. Durable context across sessions with MCP integration, frames, smart retrieval, Claude Code skills, and automatic hooks.
86 lines (85 loc) • 2.68 kB
JavaScript
import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
function buildSweepPrompt(input) {
const parts = [];
if (input.contextFiles) {
for (const [path, content] of Object.entries(input.contextFiles)) {
parts.push(`<|file_sep|>${path}`);
parts.push(content);
}
}
const diffSection = buildDiffSection(input.recentDiffs);
if (diffSection) {
parts.push(diffSection);
}
parts.push(`<|file_sep|>original/${input.filePath}`);
parts.push(input.originalContent || input.currentContent);
parts.push(`<|file_sep|>current/${input.filePath}`);
parts.push(input.currentContent);
parts.push(`<|file_sep|>updated/${input.filePath}`);
return parts.join("\n");
}
function buildDiffSection(diffs) {
if (!diffs || diffs.length === 0) {
return "";
}
const parts = [];
for (const diff of diffs) {
if (!diff.original && !diff.updated) {
continue;
}
parts.push(`<|file_sep|>${diff.file_path}.diff`);
parts.push("original:");
parts.push(diff.original || "");
parts.push("updated:");
parts.push(diff.updated || "");
}
return parts.join("\n");
}
function trimContentAroundCursor(lines, cursorLine, cursorCol, maxTokens) {
const totalChars = lines.join("\n").length;
const estimatedTokens = Math.ceil(totalChars / 4);
if (estimatedTokens <= maxTokens) {
return { lines, offset: 0, didTrim: false };
}
const targetChars = maxTokens * 4;
const avgLineLength = totalChars / lines.length;
const windowSize = Math.floor(targetChars / avgLineLength);
const halfWindow = Math.floor(windowSize / 2);
let start = Math.max(0, cursorLine - halfWindow);
let end = Math.min(lines.length, start + windowSize);
if (end === lines.length) {
start = Math.max(0, end - windowSize);
}
return {
lines: lines.slice(start, end),
offset: start,
didTrim: true
};
}
function parseCompletion(completionText, originalLines, windowStart, windowEnd) {
let text = completionText.replace(/<\|file_sep\|>$/, "").replace(/<\/s>$/, "").trimEnd();
if (!text || text.trim().length === 0) {
return null;
}
const newLines = text.split("\n");
const oldLines = originalLines.slice(windowStart, windowEnd);
const oldText = oldLines.join("\n").trimEnd();
if (text === oldText) {
return null;
}
return {
lines: newLines,
startLine: windowStart + 1,
// Convert to 1-indexed
endLine: windowEnd
};
}
export {
buildSweepPrompt,
parseCompletion,
trimContentAroundCursor
};
//# sourceMappingURL=prompt-builder.js.map