Add context compaction service: schema validation, token estimation, and message normalization with tests
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {buildCompactionRequest, estimateTokens, normalizeCompaction} from "../src/services/contextCompaction.js";
|
||||
|
||||
const valid = {
|
||||
summary: "The user is implementing context compaction.",
|
||||
current_goals: ["Keep recent messages verbatim"],
|
||||
important_facts: [], decisions: ["Use the existing Ollama client"], constraints: [],
|
||||
technical_state: ["The context model is configured separately"], open_tasks: [],
|
||||
problems_and_solutions: [], user_preferences: [], important_exact_values: ["CONTEXT_MODEL"]
|
||||
};
|
||||
|
||||
test("compaction output is strictly validated and bounded", () => {
|
||||
assert.deepEqual(normalizeCompaction(JSON.stringify(valid), 5000), valid);
|
||||
assert.equal(normalizeCompaction("not json"), null);
|
||||
assert.equal(normalizeCompaction(JSON.stringify({...valid, summary: "", current_goals: [], important_facts: [], decisions: [], constraints: [], technical_state: [], open_tasks: [], problems_and_solutions: [], user_preferences: [], important_exact_values: []}), 5000), null);
|
||||
assert.equal(normalizeCompaction(JSON.stringify(valid), 10), null);
|
||||
});
|
||||
|
||||
test("compaction request merges existing state with only expired records", () => {
|
||||
const request = buildCompactionRequest("existing fact", [{role: "user", content: "old request"}]);
|
||||
assert.match(request, /existing fact/);
|
||||
assert.match(request, /old request/);
|
||||
assert.match(request, /Newly expired conversation records/);
|
||||
assert.equal(estimateTokens("1234"), 1);
|
||||
});
|
||||
Reference in New Issue
Block a user