import type { MemoryBlock } from "./memory";
import { MEMORY_INSTRUCTIONS } from "./letta";
const LINE_NUMBER_WARNING =
"# NOTE: Line numbers shown below (with arrows like '1→') are to help during editing. Do NOT include line number prefixes in your memory edit tool calls.";
function renderMemoryMetadata(blocks: MemoryBlock[]): string {
const now = new Date();
const lastModified = blocks.reduce(
(latest, block) => (block.lastModified > latest ? block.lastModified : latest),
new Date(0)
);
return `
- The current system date is: ${now.toISOString()}
- Memory blocks were last modified: ${lastModified.toISOString()}
- Use memory tools to manage your memory blocks
`;
}
export function renderMemoryBlocks(blocks: MemoryBlock[]): string {
if (blocks.length === 0) {
return "";
}
const parts: string[] = [
MEMORY_INSTRUCTIONS,
"",
"",
"The following memory blocks are currently engaged in your core memory unit:",
"",
];
for (const block of blocks) {
// escape xml
const desc = block.description
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">");
const numberedValue = block.value
? block.value.split("\n").map((line, i) => `${i + 1}→ ${line}`).join("\n")
: "";
const memoryBlock = `<${block.label}>
${desc}
- chars_current=${block.value.length}
- chars_limit=${block.limit}
- read_only=${block.readOnly}
- scope=${block.scope}
${LINE_NUMBER_WARNING}
${numberedValue}
${block.label}>`;
parts.push(memoryBlock);
}
parts.push("");
parts.push("");
parts.push(renderMemoryMetadata(blocks));
return parts.join("\n");
}