Add mode options to /compact (#921)

Co-authored-by: Amy Guan <amy@letta.com>
This commit is contained in:
amysguan
2026-02-11 19:18:44 -08:00
committed by GitHub
parent 9ea10bf2ff
commit 7f83035a09
2 changed files with 50 additions and 5 deletions

View File

@@ -1250,6 +1250,10 @@ export default function App({
llmConfigRef.current = llmConfig;
}, [llmConfig]);
const [currentModelId, setCurrentModelId] = useState<string | null>(null);
// Full model handle for API calls (e.g., "anthropic/claude-sonnet-4-5-20251101")
const [currentModelHandle, setCurrentModelHandle] = useState<string | null>(
null,
);
// Derive agentName from agentState (single source of truth)
const agentName = agentState?.name ?? null;
const [agentDescription, setAgentDescription] = useState<string | null>(null);
@@ -2605,6 +2609,8 @@ export default function App({
} else {
setCurrentModelId(agentModelHandle || null);
}
// Store full handle for API calls (e.g., compaction)
setCurrentModelHandle(agentModelHandle || null);
// Derive toolset from agent's model (not persisted, computed on resume)
if (agentModelHandle) {
@@ -6423,10 +6429,28 @@ export default function App({
}
// Special handling for /compact command - summarize conversation history
if (msg.trim() === "/compact") {
// Supports: /compact, /compact all, /compact sliding_window
if (msg.trim().startsWith("/compact")) {
const parts = msg.trim().split(/\s+/);
const modeArg = parts[1] as "all" | "sliding_window" | undefined;
const validModes = ["all", "sliding_window"];
// Validate mode if provided
if (modeArg && !validModes.includes(modeArg)) {
const cmd = commandRunner.start(
msg.trim(),
`Invalid mode "${modeArg}". Valid modes: ${validModes.join(", ")}`,
);
cmd.fail(
`Invalid mode "${modeArg}". Valid modes: ${validModes.join(", ")}`,
);
return { submitted: true };
}
const modeDisplay = modeArg ? ` (mode: ${modeArg})` : "";
const cmd = commandRunner.start(
msg.trim(),
"Compacting conversation history...",
`Compacting conversation history${modeDisplay}...`,
);
setCommandRunning(true);
@@ -6448,18 +6472,38 @@ export default function App({
}
const client = await getClient();
// Compute model handle from llmConfig
const modelHandle =
llmConfig?.model_endpoint_type && llmConfig?.model
? `${llmConfig.model_endpoint_type}/${llmConfig.model}`
: llmConfig?.model || null;
// Build compaction settings if mode was specified
// Pass mode-specific prompt to override any agent defaults
const compactParams =
modeArg && modelHandle
? {
compaction_settings: {
mode: modeArg,
model: modelHandle,
},
}
: undefined;
// Use agent-level compact API for "default" conversation,
// otherwise use conversation-level API
const result =
conversationIdRef.current === "default"
? await client.agents.messages.compact(agentId)
? await client.agents.messages.compact(agentId, compactParams)
: await client.conversations.messages.compact(
conversationIdRef.current,
compactParams,
);
// Format success message with before/after counts and summary
const outputLines = [
`Compaction completed. Message buffer length reduced from ${result.num_messages_before} to ${result.num_messages_after}.`,
`Compaction completed${modeDisplay}. Message buffer length reduced from ${result.num_messages_before} to ${result.num_messages_after}.`,
"",
`Summary: ${result.summary}`,
];

View File

@@ -366,7 +366,8 @@ export const commands: Record<string, Command> = {
},
},
"/compact": {
desc: "Summarize conversation history (compaction)",
desc: "Summarize conversation history (compaction) with optional mode",
args: "[all|sliding_window]",
handler: () => {
// Handled specially in App.tsx to access client and agent ID
return "Compacting conversation...";