ul3140773077392 on X: dsh compacts earlier than other harnesses at the same
window, so one global size per model fits none. An agent's sheet (Agents,
the agent's Settings line) now holds "Context window": a size for every
model, or model=size pairs (provider/model, group/<id>, provider/*, a bare
model id, *). Empty follows each model's own window. It is applied in
provider.CatalogFor, the choke point every agent's model list goes through,
so dsh's contextWindow, Codex's catalog and Claude Code's window all read
it, and a window set this way makes Codex/Claude compact at that window
(Entry.WindowSet). Stored in settings.AgentContexts, kept by a general
save (settingsapi); POST /api/settings/agent-contexts and
`magpie model window <agent> [model=size...|off]`.
NovaStudiosss on X: Routing, a group's Edit, Advanced already had "Context"
(the window agents are told). It now has "Compact at" beside it
(Group.Compact; compactSet reads it before the members' smallest), also
`magpie group ... compact=`.
GUI: agentwindow.js, loaded on demand through agentcompact.js so app.js
stays inside its budget (TestCoreScriptBudget passes; the "Context window
in {agent}" label reuses the existing "Context window" key). Every agent
with fields now has a sheet; agent-prefs' Pi expectation says so.
Run at the pushed head, on mote: go vet (darwin; linux -tags nogui;
windows) on provider, agent, cli, gui, settings, settingsapi; gofmt clean;
go test provider, cli, settings, settingsapi, gui (TestCoreScriptBudget
and cli's route table fixed here), agent -run Dsh; node --test agent-window
and group-compact (chromium and webkit, en zh zh-TW ja de, 900 and 420px),
agent-compact, agent-prefs, compact-at. Reds seen identically on a
detached origin/main 4e0026661: agent-prefs webkit "in the panel a
Settings line..." (#6139) and old-webkit "compat.js is the first" / "styles
hold up in Safari 15.0" (#5919, #3403).
Broken by hand, each red then restored: withAgentWindows out of CatalogFor
(TestAgentOwnContextWindow, TestDshOwnContextWindow); the group branch in
compactSet (TestGroupOwnCompactAt); the settingsapi keep line
(TestSettingsSaveKeepsWhatItDoesNotSend); the window row in the sheet
(agent-window tests); compact dropped from the group save
(group-compact tests).
Not tried: a real dsh with a real account (its compaction is dsh's own;
the test reads the window magpie hands it), the full GUI suite (queued
behind another run on mote), Windows and Linux runs of the new tests.
116 lines
3.5 KiB
Go
116 lines
3.5 KiB
Go
package provider
|
|
|
|
import (
|
|
"errors"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// The window an agent is told for a model, when it is not the model's own
|
|
// for every agent (settings.AgentContexts, ul3140773077392 on X): agents
|
|
// compact by different rules (dsh at the smaller of 80% of the window and
|
|
// window - max tokens - 64K, Codex at its 272K), so one window fits some
|
|
// and has others compact too early. An agent's entry comes before the
|
|
// provider's and the group's own size, and is a window the user typed
|
|
// (Entry.WindowSet): Codex and Claude Code are then told it whole.
|
|
|
|
// agentKey is the id an agent's windows are kept under.
|
|
func agentKey(agent string) string { return strings.ToLower(strings.TrimSpace(agent)) }
|
|
|
|
// agentWindow is the window agent was given for e, and whether it has one.
|
|
// The most specific key wins: the model as the catalog has it, its
|
|
// provider's "*", the bare model id, then "*" for all, which only shortens
|
|
// a window that is known (as a provider's "*" does).
|
|
func agentWindow(own map[string]int, e Entry) (int, bool) {
|
|
if len(own) == 0 {
|
|
return 0, false
|
|
}
|
|
if n := own[e.ID]; n > 0 {
|
|
return n, true
|
|
}
|
|
if e.Group == "" {
|
|
if n := own[e.Provider.ID+"/*"]; n > 0 {
|
|
return n, true
|
|
}
|
|
if n := own[e.Model]; n > 0 {
|
|
return n, true
|
|
}
|
|
}
|
|
if n := own["*"]; n > 0 && e.Context > 0 && n < e.Context {
|
|
return n, true
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
// withAgentWindows gives the entries the windows agent was set.
|
|
func withAgentWindows(s settings.Settings, agent string, es []Entry) []Entry {
|
|
own := s.AgentContexts[agentKey(agent)]
|
|
if len(own) == 0 {
|
|
return es
|
|
}
|
|
// copied: the list may be the catalog's own, which other agents read
|
|
es = slices.Clone(es)
|
|
for i := range es {
|
|
if n, ok := agentWindow(own, es[i]); ok {
|
|
es[i].Context, es[i].WindowSet = n, true
|
|
}
|
|
}
|
|
return es
|
|
}
|
|
|
|
// AgentContexts are the windows agent (its list id, Agent.ListsFor) was set.
|
|
func AgentContexts(agent string) map[string]int {
|
|
return maps.Clone(settings.Load().AgentContexts[agentKey(agent)])
|
|
}
|
|
|
|
// SetAgentContexts gives agent the windows in cs, replacing those it had;
|
|
// none takes them away, and it is told each model's own again. A key is a
|
|
// model as the catalog has it ("provider/model", "group/<id>"), a provider's
|
|
// "provider/*", a bare model id or "*". The agents are told when it changes.
|
|
func SetAgentContexts(agent string, cs map[string]int) error {
|
|
agent = agentKey(agent)
|
|
if agent == "" {
|
|
return errors.New("no agent")
|
|
}
|
|
clean := map[string]int{}
|
|
for k, n := range cs {
|
|
k = strings.TrimPrefix(strings.TrimSpace(k), "magpie/")
|
|
switch {
|
|
case n < 0:
|
|
return errorf("a context window is a number of tokens, not %d", n)
|
|
case n == 0:
|
|
case k == "":
|
|
return errors.New("a context window needs a model, provider/* or * to hold for")
|
|
default:
|
|
clean[k] = n
|
|
}
|
|
}
|
|
s := settings.Load()
|
|
if maps.Equal(s.AgentContexts[agent], clean) || len(clean) == 0 && len(s.AgentContexts[agent]) == 0 {
|
|
return nil
|
|
}
|
|
s.AgentContexts = maps.Clone(s.AgentContexts)
|
|
if len(clean) == 0 {
|
|
delete(s.AgentContexts, agent)
|
|
} else {
|
|
if s.AgentContexts == nil {
|
|
s.AgentContexts = map[string]map[string]int{}
|
|
}
|
|
s.AgentContexts[agent] = clean
|
|
}
|
|
if err := settings.Save(s); err != nil {
|
|
return err
|
|
}
|
|
catalog.Touched()
|
|
return nil
|
|
}
|
|
|
|
// AgentContextAgents are the agents that have a window of their own, sorted.
|
|
func AgentContextAgents() []string {
|
|
return slices.Sorted(maps.Keys(settings.Load().AgentContexts))
|
|
}
|