package chat import ( "strings" "testing" "github.com/samcharles93/ai-sdk/chat" ) // Guards the replay budget: a model's own window decides how much history it // carries, and a model that published none is still answered within a sane // prompt instead of failing the request on its window. func TestHistoryBudgetFor(t *testing.T) { cases := []struct { name string contextWindow int maxOutput int want int }{ {name: "no published window falls back to the assumed budget", contextWindow: 0, want: assumedHistoryBudget}, {name: "a large window reserves what the reply needs", contextWindow: 200_000, maxOutput: 8_000, want: 200_000 - 8_000 - 25_000}, {name: "a small window still replays some history", contextWindow: 8_000, maxOutput: 4_000, want: 8_000 - 4_000 - 1_000}, {name: "a window the reply alone fills keeps an eighth", contextWindow: 1_000, maxOutput: 8_000, want: 125}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { if got := historyBudgetFor(tc.contextWindow, tc.maxOutput); got != tc.want { t.Errorf("historyBudgetFor(%d, %d) = %d, want %d", tc.contextWindow, tc.maxOutput, got, tc.want) } }) } } // Guards the replay of a long conversation: the newest messages and the // question being answered survive, and what is sent fits the budget, so an old // conversation is answered instead of failing the request on its window. func TestSummariseHistory(t *testing.T) { // aTurn is one exchange: the operator's message and Archie's reply, each // about 2000 tokens, so sixty turns are well past the replay budget. pad := strings.Repeat("x", 8_000) aTurn := func(i int) []chat.Message { return []chat.Message{ {Role: chat.RoleUser, Content: "question " + string(rune('a'+i%26)) + " " + pad}, {Role: chat.RoleAssistant, Content: "answer " + string(rune('a'+i%26)) + " " + pad}, } } many := make([]chat.Message, 0, 120) for i := range 60 { many = append(many, aTurn(i)...) } // A message too large to fit beside anything else, and a question after it. huge := make([]chat.Message, 0, 40) for i := range 20 { huge = append(huge, chat.Message{Role: chat.RoleAssistant, Content: strings.Repeat("x", 40_000)}, chat.Message{Role: chat.RoleUser, Content: "and then " + string(rune('a'+i))}) } const question = "what did we decide about the retry policy?" cases := []struct { name string messages []chat.Message budget int // kept is how many trailing messages must survive unchanged. kept int // fits reports whether the result must be inside the budget. fits bool // compressed reports whether the middle must have been dropped. compressed bool }{ {name: "a short history is replayed untouched", messages: []chat.Message{{Role: chat.RoleUser, Content: "hi"}, {Role: chat.RoleAssistant, Content: "hello"}, {Role: chat.RoleUser, Content: question}}, budget: assumedHistoryBudget, kept: 3, fits: true}, {name: "a long history keeps its newest messages", messages: many, budget: assumedHistoryBudget, kept: keepRecent, fits: true, compressed: true}, {name: "a small budget keeps at least the question", messages: many, budget: 2_000, kept: 1, fits: false, compressed: true}, {name: "a history whose own messages overflow keeps the question", messages: append(huge, chat.Message{Role: chat.RoleUser, Content: question}), budget: 2_000, kept: 1, fits: false, compressed: true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { out := summariseHistory(tc.messages, tc.budget) if len(out) < tc.kept { t.Fatalf("kept %d messages, want at least %d", len(out), tc.kept) } if got, want := out[len(out)-1], tc.messages[len(tc.messages)-1]; got.Content != want.Content { t.Errorf("last message is %q, want the question %q", got.Content, want.Content) } for i := 1; i <= tc.kept; i++ { if got, want := out[len(out)-i].Content, tc.messages[len(tc.messages)-i].Content; got != want { t.Errorf("message %d from the end is %q, want %q unchanged", i, got, want) } } if got := len(out) < len(tc.messages); got != tc.compressed { t.Fatalf("history compressed = %t, want %t (%d of %d messages sent)", got, tc.compressed, len(out), len(tc.messages)) } if tc.compressed && (out[0].Role != chat.RoleSystem || !strings.Contains(out[0].Content, summarisedMarker)) { t.Errorf("history was shortened without a summary in front of it: %q", out[0].Content) } if tc.fits && historyCost(out) > tc.budget { t.Errorf("history is %d tokens, over the %d budget", historyCost(out), tc.budget) } }) } }