refactor: extract memory section from system prompts into conditional addons (#947)

Co-authored-by: Letta <noreply@letta.com>
Co-authored-by: letta-code <248085862+letta-code@users.noreply.github.com>
Co-authored-by: Charles Packer <cpacker@users.noreply.github.com>
This commit is contained in:
Sarah Wooders
2026-02-12 19:29:42 -08:00
committed by GitHub
parent 415b1a403b
commit bf272ef009
9 changed files with 62 additions and 76 deletions

View File

@@ -245,8 +245,13 @@ export async function updateAgentSystemPrompt(
systemPromptId: string,
): Promise<UpdateSystemPromptResult> {
try {
const { resolveSystemPrompt } = await import("./promptAssets");
const systemPromptContent = await resolveSystemPrompt(systemPromptId);
const { resolveSystemPrompt, SYSTEM_PROMPT_MEMORY_ADDON } = await import(
"./promptAssets"
);
const baseContent = await resolveSystemPrompt(systemPromptId);
// Append the non-memfs memory section by default.
// If memfs is enabled, the caller should follow up with updateAgentSystemPromptMemfs().
const systemPromptContent = `${baseContent}\n${SYSTEM_PROMPT_MEMORY_ADDON}`;
const updateResult = await updateAgentSystemPromptRaw(
agentId,
@@ -279,7 +284,10 @@ export async function updateAgentSystemPrompt(
}
/**
* Updates an agent's system prompt to include or exclude the memfs addon section.
* Updates an agent's system prompt to swap between the memfs and non-memfs memory sections.
*
* When enabling memfs: strips any existing # Memory section, appends the memfs memory addon.
* When disabling memfs: strips any existing # Memory section, appends the non-memfs memory addon.
*
* @param agentId - The agent ID to update
* @param enableMemfs - Whether to enable (add) or disable (remove) the memfs addon
@@ -294,17 +302,21 @@ export async function updateAgentSystemPromptMemfs(
const agent = await client.agents.retrieve(agentId);
let currentSystemPrompt = agent.system || "";
const { SYSTEM_PROMPT_MEMFS_ADDON } = await import("./promptAssets");
const { SYSTEM_PROMPT_MEMFS_ADDON, SYSTEM_PROMPT_MEMORY_ADDON } =
await import("./promptAssets");
// Remove any existing memfs addon section (to avoid duplicates)
// Look for the "## Memory Filesystem" header
const memfsHeaderRegex = /\n## Memory Filesystem[\s\S]*?(?=\n# |$)/;
currentSystemPrompt = currentSystemPrompt.replace(memfsHeaderRegex, "");
// Strip any existing memory section (covers both old inline "# Memory" / "## Memory"
// sections and the new addon format including "## Memory Filesystem" subsections).
// Matches from "# Memory" or "## Memory" to the next top-level heading or end of string.
const memoryHeaderRegex =
/\n#{1,2} Memory\b[\s\S]*?(?=\n#{1,2} (?!Memory|Filesystem|Structure|How It Works|Syncing|History)[^\n]|$)/;
currentSystemPrompt = currentSystemPrompt.replace(memoryHeaderRegex, "");
// If enabling, append the memfs addon
if (enableMemfs) {
currentSystemPrompt = `${currentSystemPrompt}${SYSTEM_PROMPT_MEMFS_ADDON}`;
}
// Append the appropriate memory section
const addon = enableMemfs
? SYSTEM_PROMPT_MEMFS_ADDON
: SYSTEM_PROMPT_MEMORY_ADDON;
currentSystemPrompt = `${currentSystemPrompt}\n${addon}`;
await client.agents.update(agentId, {
system: currentSystemPrompt,
@@ -314,7 +326,7 @@ export async function updateAgentSystemPromptMemfs(
success: true,
message: enableMemfs
? "System prompt updated to include Memory Filesystem section"
: "System prompt updated to remove Memory Filesystem section",
: "System prompt updated to include standard Memory section",
};
} catch (error) {
return {