feat: unify provider history context

This commit is contained in:
Peter Steinberger
2026-01-10 18:53:33 +01:00
parent 8c1d39064d
commit d41372b9d9
19 changed files with 718 additions and 80 deletions

View File

@@ -0,0 +1,63 @@
import { describe, expect, it } from "vitest";
import { CURRENT_MESSAGE_MARKER } from "./mentions.js";
import {
HISTORY_CONTEXT_MARKER,
appendHistoryEntry,
buildHistoryContext,
buildHistoryContextFromEntries,
} from "./history.js";
describe("history helpers", () => {
it("returns current message when history is empty", () => {
const result = buildHistoryContext({
historyText: " ",
currentMessage: "hello",
});
expect(result).toBe("hello");
});
it("wraps history entries and excludes current by default", () => {
const result = buildHistoryContextFromEntries({
entries: [
{ sender: "A", body: "one" },
{ sender: "B", body: "two" },
],
currentMessage: "current",
formatEntry: (entry) => `${entry.sender}: ${entry.body}`,
});
expect(result).toContain(HISTORY_CONTEXT_MARKER);
expect(result).toContain("A: one");
expect(result).not.toContain("B: two");
expect(result).toContain(CURRENT_MESSAGE_MARKER);
expect(result).toContain("current");
});
it("trims history to configured limit", () => {
const historyMap = new Map<string, { sender: string; body: string }[]>();
appendHistoryEntry({
historyMap,
historyKey: "room",
limit: 2,
entry: { sender: "A", body: "one" },
});
appendHistoryEntry({
historyMap,
historyKey: "room",
limit: 2,
entry: { sender: "B", body: "two" },
});
appendHistoryEntry({
historyMap,
historyKey: "room",
limit: 2,
entry: { sender: "C", body: "three" },
});
expect(historyMap.get("room")?.map((entry) => entry.body)).toEqual([
"two",
"three",
]);
});
});