refactor: centralize history context wrapping

This commit is contained in:
Peter Steinberger
2026-01-10 19:09:06 +01:00
parent b977ae19af
commit 82f71d25e5
8 changed files with 137 additions and 83 deletions

View File

@@ -43,6 +43,41 @@ export function appendHistoryEntry(params: {
return history;
}
export function buildHistoryContextFromMap(params: {
historyMap: Map<string, HistoryEntry[]>;
historyKey: string;
limit: number;
entry?: HistoryEntry;
currentMessage: string;
formatEntry: (entry: HistoryEntry) => string;
lineBreak?: string;
excludeLast?: boolean;
}): string {
if (params.limit <= 0) return params.currentMessage;
const entries = params.entry
? appendHistoryEntry({
historyMap: params.historyMap,
historyKey: params.historyKey,
entry: params.entry,
limit: params.limit,
})
: (params.historyMap.get(params.historyKey) ?? []);
return buildHistoryContextFromEntries({
entries,
currentMessage: params.currentMessage,
formatEntry: params.formatEntry,
lineBreak: params.lineBreak,
excludeLast: params.excludeLast,
});
}
export function clearHistoryEntries(params: {
historyMap: Map<string, HistoryEntry[]>;
historyKey: string;
}): void {
params.historyMap.set(params.historyKey, []);
}
export function buildHistoryContextFromEntries(params: {
entries: HistoryEntry[];
currentMessage: string;