Files
beaver-gateway/ui/src/lib/panel/history-cache.ts
T

29 lines
1003 B
TypeScript

import type { ContentBlock, HistoryMessage } from "$lib/api/types";
const MAX_BYTES = 250_000;
const TAIL = 200;
function slim(message: HistoryMessage): HistoryMessage {
if (typeof message.content === "string") {
return message;
}
const content: ContentBlock[] = message.content.map((block) =>
block.type === "tool_result" ? { ...block, content: "" } : block
);
return { ...message, content };
}
// The tail of a thread as it is worth keeping between opens: tool results
// dropped (hidden by default, and the bulk of the bytes), then trimmed from
// the head until it fits a slice of localStorage.
export function cacheableHistory(messages: HistoryMessage[]): HistoryMessage[] {
let tail = messages.slice(-TAIL).map(slim);
while (tail.length > 1 && JSON.stringify(tail).length > MAX_BYTES) {
tail = tail.slice(Math.ceil(tail.length / 4));
}
return tail;
}
export const historyKey = (id: string) => `history:${id}`;
export const infoKey = (id: string) => `info:${id}`;