Files
magpie/internal/gateway/side_effort.go
T
yetone 0e68455863 memory: pick how far back conversations are read, and the model's reasoning level (RealZeroP27 on X)
Learn from conversations read the last 7 days only (Back, fixed at
switch-on) and asked its model with no reasoning level. Its row now has
two more pickers, beside the model:

- How far back it reads: 7 days (default), 30, 90, 1 year or All
  (State.Days; 0 stays 7 days, so an existing distill.json keeps its own
  Since until a range is picked). A longer range reads at once. Every
  range reads from the latest 400 conversations (memdistill.Listed) at
  10 an hour (PerRun), so All is ~240 requests a day at most until the
  backlog is read; All's menu note says so.
- Thinking: Default (none sent, as before) or one of the model's own
  levels, fitted and sent with room to think the way Vision and Searcher
  do (gateway.WithSideEffort over sideEffort/replyRoom/withFixedEffort).
  The nightly tidy uses the same level, and its row shows the picker
  when distilling is off.

Fixed on the way, as the longer ranges depend on it: a conversation's
mark was dropped after 30 days while Since stayed put, so anything
older than 30 days inside Since was read again every hour. A mark is now
dropped once it is before Since, or older than 30 days and past the
oldest conversation listed.

POST /api/memory/distill takes effort and days; a bad one is a 400.
docs/subsystems/memory.md says all of this.

Run (mote; first at base 4bfac1268, then memdistill, gui Distill|Dream|Budget and both GUI files again at this head on e697e7ddf: ok, 26 pass):
- go test -count=1 ./internal/memdistill ./internal/memory ./internal/gui: ok
- go test -count=1 -run 'Distill|Dream|Effort' ./internal/gateway: ok
- node --test library-memory.test.cjs i18n-keys.test.cjs, chromium and
  webkit, 980 and 420 wide, every language: 26 pass, 0 fail
- go vet darwin; GOOS=linux -tags nogui; GOOS=windows (memdistill, gui,
  gateway): ok. gofmt -l: clean.
- win-run go test ./internal/memdistill: ok

Broken by hand:
- the old 30-day mark prune: TestOldConversationReadOnce red
- distillComplete without WithSideEffort: TestDistillCompleteThroughGateway
  red (high sent with thinking disabled, max_tokens 4096)

Not tried: a real distill run with a real account at a picked level or
over All; the full suite (started after the tag).
2026-11-10 12:16:51 +08:00

146 lines
4.7 KiB
Go

package gateway
import (
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// The reasoning level picked in Settings for a model magpie asks on its
// own, beside the model in use: Image recognition's and the searcher's
// (MOMO on Discord, "外置模型能不能提供思考深度选项"). Only those calls
// are asked so; every other request goes as it did.
// ModelEfforts are the reasoning levels model ("<provider>/<model>") has,
// weakest first, for Settings to offer beside it; none for one whose
// levels aren't known, or a routing group.
func ModelEfforts(model string) []string {
p, m, ok := provider.Resolve(model)
if !ok {
return nil
}
return ByStrength(p.Efforts(m))
}
// sideEffort is want fitted to model's own levels: "" when none is
// wanted, or the model's levels aren't known, so nothing is sent that it
// might refuse.
func sideEffort(model, want string) string {
levels := ModelEfforts(model)
if want == "" || len(levels) == 0 {
return ""
}
return fitEffort(want, levels)
}
// visionEffort is the level the Image recognition model is asked to read
// an image at: the one picked in Settings, else magpie's own, the least
// it has (leastEffort), as a description is wanted quickly.
func visionEffort(model string) string {
if e := sideEffort(model, provider.HeldSettings().VisionEffort); e != "" {
return e
}
return leastEffort(model)
}
// leastEffort is the least reasoning model takes: none when it can go
// without, else its lowest level; "" when its levels aren't known, which
// leaves it to the vendor.
func leastEffort(model string) string {
p, m, ok := provider.Resolve(model)
if !ok {
return ""
}
levels := p.Efforts(m)
if len(levels) == 0 {
return ""
}
return fitEffort("none", levels)
}
// VisionEffortNow is the level the Image recognition model in use is
// asked at when one is picked in Settings ("" when none is): the picked
// one fitted to the model's levels, for the row to show.
func VisionEffortNow() string {
m, ok := seer()
if !ok {
return ""
}
return sideEffort(m, provider.HeldSettings().VisionEffort)
}
// VisionEfforts are the levels of the model that describes images now.
func VisionEfforts() []string {
if m, ok := seer(); ok {
return ModelEfforts(m)
}
return nil
}
// searchEffort is the level the searcher's model, provider p's model, is
// asked to search at: the one picked in Settings fitted to its levels,
// or "" to send none.
func searchEffort(p provider.Provider, model string) string {
if model == "" || model == codexSearchModel {
return ""
}
return sideEffort(p.ID+"/"+model, settings.Load().SearcherEffort)
}
// SearchEffortNow is the level the searcher in use is asked at, when one
// is picked in Settings.
func SearchEffortNow() string {
if p, m, ok := searcher(); ok {
return searchEffort(p, m)
}
return ""
}
// SearchEfforts are the levels of the model that searches now: none for
// a Kimi Code plan's search service, which has no model.
func SearchEfforts() []string {
if p, m, ok := searcher(); ok && m != "" && m != codexSearchModel {
return ModelEfforts(p.ID + "/" + m)
}
return nil
}
// replyRoom is the reply limit a side call asked to think at effort is
// sent with: its usual limit with room for the thinking above it, so a
// level picked higher doesn't spend the reply on thinking and leave no
// answer. The model's own limit still caps it (withMaxOutput).
func replyRoom(usual int, effort string) int {
switch effort {
case "", "none":
return usual
case "minimal":
return usual + 1024
}
return usual + budgetOf(effortOf(effort))
}
// SideEffort is the level a call magpie makes on its own with model is
// asked at when want is picked for it: want fitted to the model's own
// levels, "" when none is picked or its levels aren't known.
func SideEffort(model, want string) string { return sideEffort(model, want) }
// WithSideEffort is an Anthropic Messages request magpie makes on its own
// with model (memory's reading of conversations and its nightly tidy)
// asked to reason at want, fitted to the model's levels as SideEffort
// fits it, as a group's member fixed at a level is asked (withFixedEffort):
// none turns thinking off, any other level asks for it, with room above
// the reply's usual limit to think in (replyRoom). Nothing picked, or a
// model whose levels aren't known: the body as it was.
func WithSideEffort(body []byte, model, want string) []byte {
e := sideEffort(model, want)
if e == "" {
return body
}
var v struct {
MaxTokens int `json:"max_tokens"`
}
if decodeJSON(body, &v) == nil && v.MaxTokens > 0 {
body = withFields(body, map[string]any{"max_tokens": replyRoom(v.MaxTokens, e)})
}
return withFixedEffort(provider.Anthropic, body, e)
}