A new middleware package, @magpie-community/middleware-history-trim 0.1.0,
ported from OmniRoute's lite engine (MIT). It makes a request's history
smaller in four steps:
- folds runs of blank lines;
- drops a system prompt whose whole text repeats an earlier one;
- drops a message equal in every field to the one before it (tool results
excepted);
- cuts a history tool result over maxToolLength at a word boundary.
None of the steps touch the turn being sent (after the last assistant), and
a request with no assistant goes on unchanged. The README states the
prompt-cache cost.
Reviewed at 8af038a60201728f00ee350f56eb35228049baa3, merged locally with
main b2fe48e:
- bun test packages/history-trim: 21 pass.
- On the Mac mini, sandbox HOME under TMPDIR: bun scripts/check.mjs ok; full
bun test 2919 pass, 8 skip, 0 fail.
- Broken by hand, each turning cases.json red:
- the current-turn guard in dropRepeatedSystemPrompts;
- the current-turn guard in dropRepeatedMessages;
- a 200-char system key;
- a content-only compare;
- folding the current turn;
- the surrogate guard;
- turnStart returning length with no assistant;
- the cut's turn guard.
- Probes against the earlier review findings, each now as expected:
- two system prompts under a shared header both kept;
- tool_calls assistants that differ both kept;
- alice and bob kept apart;
- a patch with trailing spaces left whole;
- emoji and astral CJK cuts well-formed;
- the current turn's Anthropic tool_result left whole.
- npm and the dist index have no history-trim package yet, so 0.1.0 is free.
Not run: GitHub CI. The fork's pr workflow is waiting for approval
(action_required); the Mac mini run above stands in for it. No real
upstream request was sent through the middleware.