Files
magpie/internal/gui/memory.go
T
yetone 2b2ee49ded gui: the Library's Memory tab
A tab after Instructions lists what the agents remember, in use, to
review (what an agent only guessed) and archived, pinned first then
newest; searches it, narrows it by where it applies, and adds, edits,
pins, keeps, archives and deletes memories (Delete asks with a second
click). Kind and scope are picked from the app's menu, never a <select>.
A card on top switches the digest in the ticked agents' instructions on
or off and names those agents, and offers Magpie Memory while it isn't
in the Library.

/api/memory (GET; POST saves the user's own or edits by id),
/api/memory/remove and /api/memory/digest; a change publishes the digest
at once and its reply carries `problem` when that failed.

Words other code also says (Archived, Pin, Keep, the kinds…) are in core
i18n.js; the rest in the library plugin's, in zh, zh-TW, ja and de.
docs/subsystems/memory.md describes the layer.

Run (mote-run):
- node --test internal/gui/tests/library-memory.test.cjs, Chromium and
  WebKit, 5 languages, 980 and 420 wide: ok
- whole GUI suite: not run at this commit; it is queued on mote for the stack head (034f249af before the rebase)
- go vet ./internal/gui, GOOS darwin, linux, windows: ok
Broken by hand: the pinned sort read `b.pinned - a.pinned` (NaN for an
unpinned one) and the test was red in every language until `!!`.
Looked at: screenshots en light 980, zh dark 980, en 420.
2026-11-10 03:25:44 +08:00

124 lines
3.0 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)
})
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)
})
}