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.
24 lines
2.4 KiB
JSON
24 lines
2.4 KiB
JSON
{
|
|
"source": "https://github.com/cheaper-inference/OmniRoute",
|
|
"commit": "1ac00b05e2d507d416a3e9c4c278ab625ce1dbf2",
|
|
"version": "v3.8.53",
|
|
"license": "MIT",
|
|
"files": {
|
|
"open-sse/services/compression/lite.ts": {
|
|
"sha256": "0e857ed68a700e8000fa20cb7e3adddaf40c126149fe119024b4e4a2a8c317fd"
|
|
}
|
|
},
|
|
"changes": [
|
|
"Inlined the engine into one import-free middleware file that exports only onRequest.",
|
|
"Reads body.messages only. The protocol adapter that walks Chat, Anthropic, Responses and Gemini bodies is not ported, so a request without a messages array goes on unchanged instead of being rewritten segment by segment.",
|
|
"Dropped replaceImageUrls: which models take images is provider configuration, and its placeholders would only fire for some of them.",
|
|
"Dropped createCompressionStats and the telemetry that goes with it.",
|
|
"The four steps, their order, the word-boundary lookback of 80 characters and the [truncated] suffix are the engine's own. Four places differ on purpose, each because the engine's version drops or rewrites something the request needed:",
|
|
"A repeated system prompt is dropped only when its whole trimmed text is equal. The engine compares the first 200 trimmed characters, so a second system prompt that shares an opening — two rule sets, two agents — is dropped and the model never reads it.",
|
|
"A repeated message is dropped only when every field is equal, tool_calls and name included. The engine compares the content alone, so a second assistant message carrying different tool calls is dropped and the tool result answering it is left with no call, which an OpenAI-compatible upstream refuses with a 400.",
|
|
"All four steps leave the turn being sent alone — every message after the last assistant one. The engine skips that turn in the tool-result cut only, so its whitespace step rewrites a patch line, a Markdown hard line break or a trailing tab in the message about to go out. A history with no assistant message in it has no past the model has read: there this package touches nothing, while the engine treats every message as history to fold, dedupe and cut.",
|
|
"A cut steps back one unit when it lands between a high and a low surrogate. The engine's word-boundary back-off finds no space in emoji or CJK text, so it cuts a surrogate pair in half.",
|
|
"Options are read from ctx.options instead of a module-level configuration: maxToolLength, compressToolResults and preserveSystemPrompt."
|
|
]
|
|
}
|