perf: use hash-based caching for toolset swap upserts (#414)

Co-authored-by: Letta <noreply@letta.com>
This commit is contained in:
Charles Packer
2025-12-29 11:00:54 -08:00
committed by GitHub
parent 9db539a6d8
commit ad3450ad8c
2 changed files with 21 additions and 7 deletions

View File

@@ -5,6 +5,19 @@ import { LETTA_CLOUD_API_URL, refreshAccessToken } from "../auth/oauth";
import { ensureAnthropicProviderToken } from "../providers/anthropic-provider";
import { settingsManager } from "../settings-manager";
/**
* Get the current Letta server URL from environment or settings.
* Used for cache keys and API operations.
*/
export function getServerUrl(): string {
const settings = settingsManager.getSettings();
return (
process.env.LETTA_BASE_URL ||
settings.env?.LETTA_BASE_URL ||
LETTA_CLOUD_API_URL
);
}
export async function getClient() {
const settings = settingsManager.getSettings();

View File

@@ -1,5 +1,5 @@
import type Letta from "@letta-ai/letta-client";
import { getClient } from "../agent/client";
import { getClient, getServerUrl } from "../agent/client";
import { resolveModel } from "../agent/model";
import { linkToolsToAgent, unlinkToolsFromAgent } from "../agent/modify";
import { toolFilter } from "./filter";
@@ -14,7 +14,7 @@ import {
loadTools,
OPENAI_DEFAULT_TOOLS,
OPENAI_PASCAL_TOOLS,
upsertToolsToServer,
upsertToolsIfNeeded,
} from "./manager";
// Use the same toolset definitions from manager.ts (single source of truth)
@@ -146,9 +146,10 @@ export async function forceToolsetSwitch(
await loadTools("anthropic/claude-sonnet-4");
}
// Upsert the new toolset to server
// Upsert the new toolset to server (with hash-based caching)
const client = await getClient();
await upsertToolsToServer(client);
const serverUrl = getServerUrl();
await upsertToolsIfNeeded(client, serverUrl);
// Remove old Letta tools and add new ones (or just remove if none)
await unlinkToolsFromAgent(agentId);
@@ -158,7 +159,6 @@ export async function forceToolsetSwitch(
// Ensure base memory tool uses memory_apply_patch instead of legacy memory
try {
const client = await getClient();
const agent = await client.agents.retrieve(agentId, {
include: ["agent.tools"],
});
@@ -250,9 +250,10 @@ export async function switchToolsetForModel(
}
}
// Upsert the new toolset (stored in the tool registry) to server
// Upsert the new toolset (stored in the tool registry) to server (with hash-based caching)
const client = await getClient();
await upsertToolsToServer(client);
const serverUrl = getServerUrl();
await upsertToolsIfNeeded(client, serverUrl);
// Remove old Letta tools and add new ones
await unlinkToolsFromAgent(agentId);