Files
magpie/internal/gateway/compacting.go
T
yetone 3fe2ff9958 rules: a routing group's rule can be for the agent compacting its conversation, so a cheaper, faster model writes the summary while the conversation stays on its own (X, MR.Guo: 希望 Magpie 能像 OpenCode 一样支持单独指定 context compression model:当 Agent 需要做上下文压缩时…指定一个更便宜、更快的小模型专门负责总结和压缩历史上下文) — a compaction went to whatever model the conversation was on, the priciest in the group as often as not, and a long session paid for each summary at that price. A rule's new condition, compact (provider.Rule.Compact, "compacting"; magpie group rule add <group> use=<model> compact, compact=yes|no; the GUI editor's 压缩上下文时 / compacting toggle; the TUI hint), matches a request that is an agent compacting, told from the agent's own prompt (internal/gateway/compacting.go, read from each one's release): the system prompt of Claude Code 2.1's /compact and auto-compact, OpenCode's compaction agent, Pi, Gemini CLI's state_snapshot and Qwen Code, or the last user message of Claude Code's, Codex's (and magpie's own for a Codex compaction_trigger) and Kimi Code's; a prompt only quoted earlier in a conversation doesn't count. Only in a group with such a rule is a request looked at this way, so nothing else changes. A compaction is decided on its own, never held by the turn's rule, its classifier never asked; it isn't remembered for the turn, and neither the conversation's affinity nor the turn's size is taken from its answer, so the requests after it go on on the model the turn was on and its cache (without that, the next request of a Claude Code turn followed the summary to the cheap model). A member whose context is less than the compaction is passed over for the next rule that matches, or the group's order (RuleHit.Small). The trace tells it ("The agent is compacting the conversation, so rule 1 sends the summary to …", the models passed over named), and the editor notes a compact rule's model taking less than another member. Docs: start#rules (en, zh). Tested: internal/gateway/compacting_test.go — each agent's prompt taken, a plain and a quoted one not; in a group with rules compact→a/small and tokens≥2000→b/big, a long Claude Code turn on b, its /compact to a (Compact, N=1), the turn's next request back on b held, Codex's compaction to a too; a 70k-token compaction skipping a (64k) for rule 2 with Small [a/small]; a group with no compact rule routing a compaction as before; ParseRule/Line/Conditions/Matches for compact. TestRuleCompact failed with the affinity kept from the summary. internal/gui/tests/routing-compact.test.cjs, Chromium and WebKit, en and zh: the compaction in the trace, the rule's words, the toggle on and off with nothing moved, the room note, a compacting-only rule saved as compact:true and not refused. routing-kind/served/signin/steady, click-scroll and old-webkit pass. go vet, go build -tags nogui (and GOOS=linux, GOOS=windows) and go test -tags nogui ./... pass. Not tried against real agents' compactions through the gateway (the prompts are matched as read from their releases) nor in the real Wails window.
2026-09-30 10:24:18 +08:00

63 lines
1.9 KiB
Go

package gateway
import "strings"
// An agent compacting its conversation asks a model to summarize it, with
// a prompt of its own in the system prompt or as the last user message; a
// rule may send that request to a cheaper, faster model (provider.Rule's
// Compact). The prompts are the agents' own, read from each one's release.
// compactSystems open (or are) the system prompt of a compaction request.
var compactSystems = []string{
// Claude Code 2.1, and OpenCode's before its own agent
"You are a helpful AI assistant tasked with summarizing conversations.",
// OpenCode's compaction agent (agent/prompt/compaction.txt)
"You are a context summarization agent.",
// Pi
"You are a context summarization assistant.",
// Gemini CLI
"You are a specialized system component responsible for distilling chat history into a structured XML <state_snapshot>",
// Qwen Code
"You are the component that summarizes a conversation when its context window is about to overflow.",
}
// compactAsks are in the last user message of a compaction request.
var compactAsks = []string{
// Claude Code's /compact and auto-compact
"Your task is to create a detailed summary of the conversation so far",
// Codex (and magpie's own for a Codex compaction_trigger)
"You are performing a CONTEXT CHECKPOINT COMPACTION.",
// Kimi Code (prompts/compact.md)
"You are now given a task to compact this conversation context",
}
// compacting reports whether req is an agent compacting its conversation.
func compacting(req *Request) bool {
if req == nil {
return false
}
for _, p := range compactSystems {
if strings.Contains(req.System, p) {
return true
}
}
for i := len(req.Messages) - 1; i >= 0; i-- {
m := req.Messages[i]
if m.Role != "user" {
continue
}
for _, p := range m.Parts {
if p.Kind != Text {
continue
}
for _, a := range compactAsks {
if strings.Contains(p.Text, a) {
return true
}
}
}
break
}
return false
}