Files
magpie/internal/provider/compact.go
T
yetone 1aaac93b52 provider, gui: Compact at — where Codex and Claude Code compact a long conversation, set per provider or per model in its editor and to any size in Settings → Long conversations (#876, hisiling)
Until now the only choice was 272K for every model or Full window for every model. The 272K default stays as it is (e950eb08: OpenAI runs its own 1M models at 272K in Codex, and Chen saw 70–90s to a first token at 550K).

What changes:

- A provider's editor has Compact at beside Context window and Max output, in the same form: "500k" for all of its models, "deepseek-v4-flash=272k" for one, and empty to follow Settings. A size at or above a model's window means the whole window. The values are kept in settings.ModelCompacts.
- Settings → Long conversations takes a size of its own beside Compact at / Full window (settings.CompactAt).
- `magpie model compact` takes a size such as 500k.

Order: the model's own value, then its provider's "*", then Settings. Full window turns off only the Settings value. A group uses the smallest value among its members.

Where it applies:

- Codex's model_catalog is told the threshold as context_window, with the whole window as max_context_window.
- Claude Code's CLAUDE_CODE_AUTO_COMPACT_WINDOW follows the main model. A Claude model now takes its provider's value when one is set.
- The Codex list tag changes when the Settings size or Full window changes.

Tests:

- New: TestModelCompactsSetFromTheEditor, TestSetCompactAt, TestCodexCatalogCompactAt and TestClaudeCompactAt.
- TestSettingsSaveKeepsWhatItDoesNotSend now covers the two new fields.
- compact-at.test.cjs (WebKit, en/zh) checks the editor field (shown, refused, chips, saved) and the Settings size (posted, hidden under Full window, no scroll).
- go vet ./..., go test -tags nogui of the root package, provider, codexcat, agent, gui, gateway, settings and catalog under a temp HOME, and GOOS=linux/windows builds all pass.
2026-10-05 15:20:05 +08:00

149 lines
4.3 KiB
Go

package provider
import (
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// Where Codex and Claude Code compact a long conversation (#876): the
// window they are told for a model whose own is longer. A model's own
// (settings.ModelCompacts, "provider/model"), else its provider's
// ("provider/*"), else the one for every model (settings.Compact: 272K, a
// number the user gave, or none under Full window).
// compactOf is the user's threshold for a provider's model: its own, else
// its provider's "*"; 0 when neither is set.
func compactOf(s settings.Settings, providerID, model string) int {
if n := s.ModelCompacts[providerID+"/"+model]; n > 0 {
return n
}
return s.ModelCompacts[providerID+"/*"]
}
// CompactSet is the threshold the user set on a model, by its catalog id
// ("provider/model", magpie/ and a [1m] mark taken off), or on its
// provider; for a group, the smallest its models have, so none of them
// outgrows its own. 0 when none is set: settings.Compact then answers.
func CompactSet(id string) int {
return compactSet(settings.Load(), id, GroupFinder())
}
func compactSet(s settings.Settings, id string, find func(string) (Group, []Member, bool)) int {
if len(s.ModelCompacts) == 0 {
return 0
}
id = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(id), "magpie/"), "[1m]")
if strings.HasPrefix(id, GroupPrefix) {
_, ms, _ := find(id)
least := 0
for _, m := range ms {
if n := compactOf(s, m.Provider.ID, entryModel(m)); n > 0 && (least == 0 || n < least) {
least = n
}
}
return least
}
pid, model, ok := strings.Cut(id, "/")
if !ok {
return 0
}
return compactOf(s, pid, model)
}
// entryModel is the model id a group's member is listed under: the last
// of its path, the provider's id taken off, which is not always what the
// vendor is asked for (a wire name).
func entryModel(m Member) string {
if n := len(m.Path); n > 0 {
if model, ok := strings.CutPrefix(m.Path[n-1], m.Provider.ID+"/"); ok {
return model
}
}
return m.Model
}
// CompactsOf is the thresholds the user set on a provider's models, by
// model id, "*" for all of them: what its editor's Compact at shows.
func CompactsOf(providerID string) map[string]int {
out := map[string]int{}
for k, n := range settings.Load().ModelCompacts {
if model, ok := strings.CutPrefix(k, providerID+"/"); ok && model != "" && n > 0 {
out[model] = n
}
}
return out
}
// SetModelCompacts makes a provider's thresholds cs, by model id, "*" for
// all of them, as its editor's Compact at says: those it leaves out are
// taken away, and every one set has to be a model the provider serves, as
// for a reply limit (SetModelOutputs). Codex's and Claude Code's lists are
// written again when anything changed.
func SetModelCompacts(providerID string, cs map[string]int) error {
p, err := Find(providerID)
if err != nil {
return err
}
for model, n := range cs {
if n < 0 {
return errorf("a compaction threshold is a number of tokens, not %d", n)
}
if err := settings.CheckModelKey("a compaction threshold", p.ID+"/"+model); err != nil {
return err
}
if model != "*" && n > 0 && !p.serves(model) {
return errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID)
}
}
s := settings.Load()
changed := false
for k := range s.ModelCompacts {
if model, ok := strings.CutPrefix(k, p.ID+"/"); ok && cs[model] <= 0 {
delete(s.ModelCompacts, k)
changed = true
}
}
for model, n := range cs {
if n <= 0 || s.ModelCompacts[p.ID+"/"+model] == n {
continue
}
if s.ModelCompacts == nil {
s.ModelCompacts = map[string]int{}
}
s.ModelCompacts[p.ID+"/"+model] = n
changed = true
}
if !changed {
return nil
}
if err := settings.Save(s); err != nil {
return err
}
catalog.Touched()
return nil
}
// SetCompactAt has every model without a threshold of its own compacted at
// n tokens, Full window turned off; 0 is the working window again
// (settings.WorkingWindow).
func SetCompactAt(n int) error {
if n < 0 {
return errorf("a compaction threshold is a number of tokens, not %d", n)
}
s := settings.Load()
if n == settings.WorkingWindow {
n = 0
}
if s.CompactAt == n && !s.FullContext {
return nil
}
s.CompactAt, s.FullContext = n, false
if err := settings.Save(s); err != nil {
return err
}
catalog.Touched()
return nil
}