feat: Add memory_apply_patch for non-Anthropic models (#149)

This commit is contained in:
Kevin Lin
2025-12-02 17:21:37 -08:00
committed by GitHub
parent a33160e029
commit baf3faf969
3 changed files with 150 additions and 3 deletions

View File

@@ -89,14 +89,44 @@ export async function createAgent(
getServerToolName(name),
);
const baseMemoryTool = modelHandle.startsWith("anthropic/")
? "memory"
: "memory_apply_patch";
const defaultBaseTools = baseTools ?? [
"memory",
baseMemoryTool,
"web_search",
"conversation_search",
"fetch_webpage",
];
const toolNames = [...serverToolNames, ...defaultBaseTools];
let toolNames = [...serverToolNames, ...defaultBaseTools];
// Fallback: if server doesn't have memory_apply_patch, use legacy memory tool
if (toolNames.includes("memory_apply_patch")) {
try {
const resp = await client.tools.list({ name: "memory_apply_patch" });
const hasMemoryApplyPatch =
Array.isArray(resp.items) && resp.items.length > 0;
if (!hasMemoryApplyPatch) {
console.warn(
"memory_apply_patch tool not found on server; falling back to 'memory' tool",
);
toolNames = toolNames.map((n) =>
n === "memory_apply_patch" ? "memory" : n,
);
}
} catch (err) {
// If the capability check fails for any reason, conservatively fall back to 'memory'
console.warn(
`Unable to verify memory_apply_patch availability (falling back to 'memory'): ${
err instanceof Error ? err.message : String(err)
}`,
);
toolNames = toolNames.map((n) =>
n === "memory_apply_patch" ? "memory" : n,
);
}
}
// Load memory blocks from .mdx files
const defaultMemoryBlocks =
@@ -312,6 +342,8 @@ export async function createAgent(
enable_sleeptime: enableSleeptime,
});
// Note: Preflight check above falls back to 'memory' when 'memory_apply_patch' is unavailable.
// Apply updateArgs if provided (e.g., reasoningEffort, verbosity, etc.)
// Skip if updateArgs only contains context_window (already set in create)
if (updateArgs && Object.keys(updateArgs).length > 0) {