Files
magpie/internal/gui/memory.go
T
yetone 6a253f8af7 memory: pick several memories and keep, archive or delete them at once (RealZeroP27 on X)
Library -> Memory reviewed one memory at a time: To review held
candidates that had to be opened and kept or deleted one by one.

- Select (beside the filters, shown when two or more are listed) puts a
  box on each memory. A click picks one, Shift-click the range from the
  last one clicked, up/down move between the boxes (Shift picks as it
  goes), Cmd/Ctrl+A picks all shown, Escape ends it. A search or filter
  unpicks what it hides.
- The bar under the list (the library's lib-pickbar, as Skills' Select)
  keeps (To review), archives (In use) or restores (Archived) the picked
  ones, and deletes them on a second click.
- POST /api/memory/status {ids, status}: memory.SetStatus changes them in
  one write, each as Edit by the user (an archived inferred memory is
  dismissed, so it isn't proposed again). Unknown ids are skipped; none
  found, or a bad status, is a 400 and changes nothing.
  Delete reuses POST /api/memory/remove {ids}.

Run (mote, at this head):
- go test ./internal/memory ./internal/memdistill ./internal/library: ok
- go test -run 'Memory|Distill|Dream' ./internal/gui: ok
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./..., and the three builds: ok. gofmt -l: clean.
- library-memory-select.test.cjs (new): 22/22, chromium and webkit,
  en/zh/zh-TW/ja/de at 980 and 420 wide (no native select, no sideways
  overflow).
- Every library-*, i18n-* and gui-* GUI file (67), both engines: 1309 of 1309 pass.
- Rebased on 2219fdd49: i18n-keys, library-memory, library-memory-select
  and library-bili 132/132; go test ./internal/memory and -run Memory
  ./internal/gui ok (at d374168d0; only a
  client_icons test changed in those packages since).

Broken by hand:
- SetStatus without editIn: TestSetStatusOfSome red ("after keeping two:
  candidate candidate candidate candidate"), and
  TestMemoryStatusRouteActsOnSome red.
- memPick with no Shift range: the GUI test red (got [c1, c4], wanted
  c1..c4).

Not tried: the real app with a real memory.json, the WebKitGTK Linux app,
Windows' WebView2.
2026-11-10 16:00:30 +08:00

145 lines
3.6 KiB
Go

package gui
import (
"encoding/json"
"errors"
"net/http"
"github.com/yetone/magpie/internal/library"
"github.com/yetone/magpie/internal/memory"
)
// memoryView is the Library's Memory tab: every memory, newest change
// first, the projects they are kept for, and whether the agents ticked
// for instructions are given the digest.
type memoryView struct {
Memories []*memory.Memory `json:"memories"`
Projects map[string]*memory.Project `json:"projects"`
Digest bool `json:"digest"`
Agents []string `json:"agents"` // ticked for instructions
}
func readMemory() (*memoryView, error) {
ms, ps, err := memory.All()
if err != nil {
return nil, err
}
on, agents, err := library.MemoryInInstructions()
if err != nil {
return nil, err
}
if ms == nil {
ms = []*memory.Memory{}
}
if ps == nil {
ps = map[string]*memory.Project{}
}
return &memoryView{Memories: ms, Projects: ps, Digest: on, Agents: agents}, nil
}
// memoryChanged publishes the user's change to the agents' instructions
// and answers with the tab as it is now. A publish that fails is the
// change kept and not yet given out, said beside it.
func memoryChanged(rw http.ResponseWriter) {
_, perr := library.PublishMemory(false)
v, err := readMemory()
if err != nil {
fail(rw, err)
return
}
if perr != nil {
writeJSON(rw, struct {
*memoryView
Problem string `json:"problem"`
}{v, perr.Error()})
return
}
writeJSON(rw, v)
}
func memoryRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/memory", func(rw http.ResponseWriter, r *http.Request) {
v, err := readMemory()
if err != nil {
fail(rw, err)
return
}
writeJSON(rw, v)
})
// a memory added, or one changed by its id: the user's own from then on
mux.HandleFunc("POST /api/memory", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
ID string `json:"id"`
memory.Input
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
var err error
if in.ID != "" {
_, err = memory.Edit(in.ID, in.Input, true)
} else {
_, _, err = memory.Save(in.Input, memory.FromUser, nil, nil)
}
if err != nil {
fail(rw, err)
return
}
memoryChanged(rw)
})
mux.HandleFunc("POST /api/memory/remove", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
IDs []string `json:"ids"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
if len(in.IDs) == 0 {
fail(rw, errors.New("name the memories to remove"))
return
}
if err := memory.Remove(in.IDs...); err != nil {
fail(rw, err)
return
}
memoryChanged(rw)
})
// some memories given one status at once: kept, archived or brought
// back from the Memory tab's Select (RealZeroP27 on X)
mux.HandleFunc("POST /api/memory/status", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
IDs []string `json:"ids"`
Status string `json:"status"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
if len(in.IDs) == 0 {
fail(rw, errors.New("name the memories to change"))
return
}
if _, err := memory.SetStatus(in.Status, in.IDs...); err != nil {
fail(rw, err)
return
}
memoryChanged(rw)
})
mux.HandleFunc("POST /api/memory/digest", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
On bool `json:"on"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
if _, err := library.SetMemoryInInstructions(in.On); err != nil {
fail(rw, err)
return
}
memoryChanged(rw)
})
}