Nowledge Mem asks for "选择一个 AI" on its first page and in Settings >
AI, and Mem 0.10 has a provider type of magpie's own. Picking a magpie
model for the new agent nowledge-mem now sets it up from magpie:
- Mem keeps its providers in remote_llm.json with each key encrypted, so
the file is only read. Changes go through its backend (nmem-server,
127.0.0.1:14242, $NMEM_API_URL when it names this computer), as its
Settings and `nmem config provider` do: POST /remote-llm/config saves
provider "magpie" at the gateway's /v1 with agentKey("nowledge-mem")
and the model, which makes it active and turns the AI on.
- Mem puts temperature, max_tokens and timeout back to its defaults
when a save leaves them out, so every save sends the provider's own.
- What Mem had is stashed once: the active provider, whether the AI was
on, and a "magpie" provider the user made, which keeps its key and
gets its model, base and parameters back. Stepping out activates the
provider Mem had (unless one was picked in Mem since), removes
magpie's provider and turns the AI off again if it was off.
- Mem has to run to change it (errMemDown); a sync moves the base only
when the gateway's port moved. Detected by its config folder or the
installed app (co.nowledge.mem.desktop). Icon from its icon.icns.
Run (mote):
- go test -count=1 -run 'NowledgeMem|TestEveryAgent|TestListOnly|TestSync|Desktop'
./internal/agent ./internal/library ./internal/agentenv ok; -run Icon
./internal/gui ok.
- GUI: icon-one-tone, icon-tile, routing-client-icons, i18n-keys in
Chromium and WebKit, 58 of 59; the red is icon-tile's dim-color, red
on origin/main since 2649d5d72 (#6759), not this icon.
- Real Nowledge Mem 0.10.121 (free tier) in a sandbox HOME on mote, its
nmem-server on its own port behind a logging proxy, and a sandbox
magpie (go build of this tree) with a fake OpenAI vendor:
`magpie nowledge-mem fakeoa/fake-model` made magpie Mem's active
provider at the sandbox gateway; Mem's own `nmem config provider test`
answered "Connection OK — magpie/fakeoa/fake-model", the vendor got
the request, and `magpie usage` counted it under Nowledge Mem.
`magpie nowledge-mem default` put openai_compatible/u2 back as active,
removed magpie's provider, and Mem's test passed on it again.
- That run found NMEM_API_URL dropped as "not an absolute path";
NotPaths has it now (the AGY sibling is the commit before).
- On 681f2ee7f, then rebased onto 6af38c0ee (internal/gui providers
only, none of these packages): gofmt -l clean; go vet, GOOS=linux
and GOOS=windows go vet of ./internal/agent ./internal/agentenv
./internal/library ok; the tests above again ok.
- Not run: the full go test ./... suite. mote had another session's full
suite running and mica's disk is full (323 MB free); it runs on mote
once that one ends, and a red gets its own issue and fix.
Broken by hand: without the check that magpie's provider is still the
active one, TestNowledgeMemKeepsALaterPick is red; without re-sending
temperature, or sending a key over the user's own magpie provider,
TestNowledgeMemKeepsTheUsersMagpie is red; with NMEM_API_URL out of
NotPaths, TestNowledgeMemOnlyOnThisComputer is red.
Not tried: Mem's window itself (the run used its API and CLI), magpie's
GUI connect row clicked, Windows and Linux (config folder per OS is
from Mem's source, not a real install), a paid Mem plan.
455 lines
14 KiB
Go
455 lines
14 KiB
Go
package agent
|
|
|
|
// Nowledge Mem (mem.nowledge.co, a Tauri app, co.nowledge.mem.desktop;
|
|
// xiaomaomiloulou on X) is a personal memory app whose own AI (search,
|
|
// distilling, the chat) runs on a remote LLM picked on its first page,
|
|
// "选择一个 AI", and in Settings > AI. Mem 0.10 has a provider of magpie's
|
|
// own among its types: "magpie", at the gateway's default address
|
|
// http://127.0.0.1:3425/v1, which sends Bearer magpie-nowledge-mem when no
|
|
// key is set. So magpie's gateway already reads its requests as the agent
|
|
// nowledge-mem; what it lacked was the setting.
|
|
//
|
|
// Mem keeps its providers in remote_llm.json in its config folder
|
|
// (~/Library/Application Support/co.nowledge.mem.desktop on a Mac,
|
|
// %APPDATA%\co.nowledge.mem.desktop on Windows, the XDG config folder on
|
|
// Linux; $NMEM_APP_CONFIG_DIR), each key encrypted there (api_key_enc and
|
|
// api_key_nonce), so the file is never written by magpie. Mem's backend,
|
|
// nmem-server, changes it through its HTTP API on 127.0.0.1:14242 (the
|
|
// nmem CLI's $NMEM_API_URL), which takes loopback requests without a key,
|
|
// as Mem's own Settings and `nmem config provider` do (read at 0.10.121):
|
|
//
|
|
// GET /remote-llm/config enabled, active_provider, providers{id: provider_type, model, api_base, has_api_key, …}
|
|
// POST /remote-llm/config {enabled, provider, provider_type, model, api_base, api_key, temperature, max_tokens, timeout}
|
|
// POST /remote-llm/activate {provider}
|
|
// POST /remote-llm/provider/remove {provider}
|
|
// POST /remote-llm/enabled {enabled}
|
|
//
|
|
// POST /remote-llm/config saves a provider and always makes it the active
|
|
// one; api_base and api_key left out keep what was stored, but
|
|
// temperature, max_tokens and timeout left out go back to 0.7, 8192 and
|
|
// 60, so the provider's own are sent again with every change.
|
|
//
|
|
// Picking a magpie model for Mem saves the provider "magpie" (type magpie)
|
|
// at the gateway's /v1 with Mem's own agent key and that model, and turns
|
|
// Mem's AI on. What Mem had before is stashed: its active provider,
|
|
// whether its AI was on, and a "magpie" provider the user had made
|
|
// themselves, with its values. Stepping out activates that provider again,
|
|
// puts the user's "magpie" back as it was or removes magpie's, and turns
|
|
// the AI off again if it was off.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"maps"
|
|
"net/http"
|
|
"net/url"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
// memBundle is Mem's bundle id, the name of its config folder.
|
|
const memBundle = "co.nowledge.mem.desktop"
|
|
|
|
// memAddr is where Mem's backend listens unless $NMEM_API_URL says.
|
|
const memAddr = "http://127.0.0.1:14242"
|
|
|
|
// memDefaultBase is where Mem's magpie type goes with no api_base.
|
|
const memDefaultBase = "http://127.0.0.1:3425/v1"
|
|
|
|
// errMemDown is a change asked of a Mem that isn't running.
|
|
var errMemDown = errors.New("Nowledge Mem changes its AI only while it runs: open Nowledge Mem, then try again")
|
|
|
|
// memConfigDir is Mem's config folder: $NMEM_APP_CONFIG_DIR, else the
|
|
// app's folder in the system's config folder.
|
|
func memConfigDir(home string) string {
|
|
if d := strings.TrimSpace(appdir.Getenv("NMEM_APP_CONFIG_DIR")); d != "" {
|
|
return d
|
|
}
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
return filepath.Join(home, "Library", "Application Support", memBundle)
|
|
case "windows":
|
|
if d := appdir.Getenv("APPDATA"); d != "" {
|
|
return filepath.Join(d, memBundle)
|
|
}
|
|
return filepath.Join(home, "AppData", "Roaming", memBundle)
|
|
}
|
|
if d := appdir.Getenv("XDG_CONFIG_HOME"); d != "" {
|
|
return filepath.Join(d, memBundle)
|
|
}
|
|
return filepath.Join(home, ".config", memBundle)
|
|
}
|
|
|
|
// memProvider is one of Mem's providers, as its API (provider_type) and
|
|
// its file (type) give it.
|
|
type memProvider struct {
|
|
Type string `json:"provider_type,omitempty"`
|
|
FileType string `json:"type,omitempty"`
|
|
Model string `json:"model"`
|
|
APIBase string `json:"api_base,omitempty"`
|
|
HasKey bool `json:"has_api_key,omitempty"`
|
|
Temperature *float64 `json:"temperature,omitempty"`
|
|
MaxTokens *int `json:"max_tokens,omitempty"`
|
|
Timeout *float64 `json:"timeout,omitempty"`
|
|
}
|
|
|
|
func (p memProvider) kind() string {
|
|
if p.Type != "" {
|
|
return p.Type
|
|
}
|
|
return p.FileType
|
|
}
|
|
|
|
// memState is Mem's remote LLM setting.
|
|
type memState struct {
|
|
Enabled bool `json:"enabled"`
|
|
Active string `json:"active_provider"`
|
|
Providers map[string]memProvider `json:"providers"`
|
|
}
|
|
|
|
// memPrev is what Mem had before magpie, as stashed.
|
|
type memPrev struct {
|
|
Active string `json:"active,omitempty"`
|
|
Enabled bool `json:"enabled"`
|
|
Had *memProvider `json:"had,omitempty"`
|
|
}
|
|
|
|
// memOn says magpie's provider is the one Mem's AI uses.
|
|
func memOn(s memState) (memProvider, bool) {
|
|
p, ok := s.Providers[magpieID]
|
|
return p, ok && s.Enabled && s.Active == magpieID
|
|
}
|
|
|
|
// memBase is where p sends Mem's requests.
|
|
func memBase(p memProvider) string {
|
|
if p.APIBase == "" && p.kind() == magpieID {
|
|
return memDefaultBase
|
|
}
|
|
return strings.TrimSuffix(p.APIBase, "/")
|
|
}
|
|
|
|
// memRead is Mem's setting as remote_llm.json has it, for a Mem that
|
|
// isn't running.
|
|
func memRead(path string) (memState, bool) {
|
|
raw, err := edit.Read(path)
|
|
if err != nil {
|
|
return memState{}, false
|
|
}
|
|
var s memState
|
|
if json.Unmarshal(raw, &s) != nil {
|
|
return memState{}, false
|
|
}
|
|
return s, true
|
|
}
|
|
|
|
func nowledgeMem(home string) *Agent {
|
|
dir := memConfigDir(home)
|
|
path := filepath.Join(dir, "remote_llm.json")
|
|
keyPrev := "nowledge-mem.prev:" + dir
|
|
now := func() (memState, *memClient) {
|
|
if c := memLive(dir); c != nil {
|
|
if s, err := c.config(); err == nil {
|
|
return s, c
|
|
}
|
|
}
|
|
s, _ := memRead(path)
|
|
return s, nil
|
|
}
|
|
// wire makes magpie's provider Mem's AI, with model m
|
|
wire := func(c *memClient, s memState, m string) error {
|
|
had, there := s.Providers[magpieID]
|
|
if unstashPeek(keyPrev) == "" {
|
|
prev := memPrev{Enabled: s.Enabled}
|
|
if s.Active != magpieID {
|
|
prev.Active = s.Active
|
|
}
|
|
if there {
|
|
prev.Had = &had
|
|
}
|
|
b, _ := json.Marshal(prev)
|
|
stash(map[string]string{keyPrev: string(b)})
|
|
}
|
|
body := memBody(had, magpieID, m)
|
|
body["api_base"] = gatewayV1()
|
|
var prev memPrev
|
|
json.Unmarshal([]byte(unstashPeek(keyPrev)), &prev)
|
|
if prev.Had == nil {
|
|
// a key on the user's own "magpie" provider stays theirs
|
|
body["api_key"] = agentKey("nowledge-mem")
|
|
}
|
|
return c.post("/remote-llm/config", body)
|
|
}
|
|
// unwire takes magpie's provider out of Mem's AI, putting back what Mem
|
|
// had, with model v on it where v isn't ""
|
|
unwire := func(c *memClient, s memState, v string) error {
|
|
raw := unstashPeek(keyPrev)
|
|
var prev memPrev
|
|
if raw == "" {
|
|
// magpie never stashed: a "magpie" the user made by hand is
|
|
// theirs to keep; Mem goes to another provider of its own
|
|
prev.Had, prev.Enabled = &memProvider{}, true
|
|
for _, id := range slices.Sorted(maps.Keys(s.Providers)) {
|
|
if id != magpieID {
|
|
prev.Active = id
|
|
break
|
|
}
|
|
}
|
|
if prev.Active == "" {
|
|
prev.Enabled = false
|
|
}
|
|
} else {
|
|
json.Unmarshal([]byte(raw), &prev)
|
|
}
|
|
if p, ok := s.Providers[magpieID]; ok && prev.Had != nil && prev.Had.Model != "" {
|
|
back := memBody(p, magpieID, prev.Had.Model)
|
|
back["api_base"] = prev.Had.APIBase
|
|
for k, val := range map[string]any{"temperature": prev.Had.Temperature, "max_tokens": prev.Had.MaxTokens, "timeout": prev.Had.Timeout} {
|
|
if val != nil {
|
|
back[k] = val
|
|
}
|
|
}
|
|
if err := c.post("/remote-llm/config", back); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// a provider the user picked in Mem since stays picked
|
|
ours := s.Active == magpieID
|
|
if p, ok := s.Providers[prev.Active]; ok && prev.Active != magpieID && (ours || v != "") {
|
|
if v != "" && v != p.Model {
|
|
if err := c.post("/remote-llm/config", memBody(p, prev.Active, v)); err != nil {
|
|
return err
|
|
}
|
|
} else if err := c.post("/remote-llm/activate", map[string]any{"provider": prev.Active}); err != nil {
|
|
return err
|
|
}
|
|
} else if v != "" && ours {
|
|
return errors.New("Nowledge Mem had no AI of its own to use " + v + " with: pick one in its Settings > AI")
|
|
}
|
|
if _, ok := s.Providers[magpieID]; ok && prev.Had == nil {
|
|
if err := c.post("/remote-llm/provider/remove", map[string]any{"provider": magpieID}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if !prev.Enabled && ours {
|
|
if err := c.post("/remote-llm/enabled", map[string]any{"enabled": false}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
forget(keyPrev)
|
|
return nil
|
|
}
|
|
return &Agent{
|
|
ID: "nowledge-mem", Name: "Nowledge Mem", Icon: "nowledge-mem",
|
|
Dir: dir, Path: path,
|
|
detect: func() bool {
|
|
return isDir(dir) || (&Agent{ID: "nowledge-mem"}).HasApp()
|
|
},
|
|
// a gateway on a new port reaches magpie's provider
|
|
Sync: func() error {
|
|
c := memLive(dir)
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
s, err := c.config()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p, on := memOn(s)
|
|
if !on || !avibeMoved(memBase(p)) {
|
|
return nil
|
|
}
|
|
body := memBody(p, magpieID, p.Model)
|
|
body["api_base"] = gatewayV1()
|
|
return c.post("/remote-llm/config", body)
|
|
},
|
|
Check: func() string {
|
|
s, _ := now()
|
|
p, on := memOn(s)
|
|
if !on {
|
|
return ""
|
|
}
|
|
return wiringOff("Nowledge Mem", path, func(string) (string, bool) { return memBase(p), true }, "api_base", gatewayV1())
|
|
},
|
|
Fields: []Field{{
|
|
Key: "model", Label: "model",
|
|
Get: func() string {
|
|
s, _ := now()
|
|
if p, on := memOn(s); on {
|
|
return magpieID + "/" + p.Model
|
|
}
|
|
if p, ok := s.Providers[s.Active]; ok && s.Enabled {
|
|
return p.Model
|
|
}
|
|
return ""
|
|
},
|
|
Set: func(v string) error {
|
|
s, c := now()
|
|
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
|
|
if c == nil {
|
|
return errMemDown
|
|
}
|
|
return wire(c, s, ref)
|
|
}
|
|
_, on := memOn(s)
|
|
_, made := s.Providers[magpieID]
|
|
stashed := unstashPeek(keyPrev) != ""
|
|
if !on && !(stashed && made) {
|
|
if v == "" {
|
|
return nil
|
|
}
|
|
if p, ok := s.Providers[s.Active]; ok && p.Model == v {
|
|
return nil
|
|
}
|
|
}
|
|
if c == nil {
|
|
if !on && !stashed {
|
|
return nil
|
|
}
|
|
return errMemDown
|
|
}
|
|
if on || stashed {
|
|
return unwire(c, s, v)
|
|
}
|
|
p, ok := s.Providers[s.Active]
|
|
if !ok {
|
|
return errors.New("Nowledge Mem has no AI of its own to use " + v + " with: pick one in its Settings > AI")
|
|
}
|
|
return c.post("/remote-llm/config", memBody(p, s.Active, v))
|
|
},
|
|
Options: func(cur map[string]string) []Option {
|
|
var own []Option
|
|
if c := cur["model"]; c != "" && !usesMagpie(c) {
|
|
own = append(own, Option{Value: c, Icon: modelIcon("", c)})
|
|
}
|
|
return withVia(group("Nowledge Mem", own), "nowledge-mem", magpieID+"/")
|
|
},
|
|
}},
|
|
}
|
|
}
|
|
|
|
// memBody is the POST /remote-llm/config that saves provider id as p is,
|
|
// with model m, its parameters sent again (left out, Mem resets them).
|
|
func memBody(p memProvider, id, m string) map[string]any {
|
|
kind := p.kind()
|
|
if kind == "" {
|
|
kind = id
|
|
}
|
|
body := map[string]any{"enabled": true, "provider": id, "provider_type": kind, "model": m}
|
|
if p.Temperature != nil {
|
|
body["temperature"] = *p.Temperature
|
|
}
|
|
if p.MaxTokens != nil {
|
|
body["max_tokens"] = *p.MaxTokens
|
|
}
|
|
if p.Timeout != nil {
|
|
body["timeout"] = *p.Timeout
|
|
}
|
|
return body
|
|
}
|
|
|
|
// memProbe gives up quickly: a backend that doesn't answer at once isn't
|
|
// running.
|
|
const memProbe = 1500 * time.Millisecond
|
|
|
|
// memWait is how long a change may take.
|
|
const memWait = 20 * time.Second
|
|
|
|
// memClient is a running Mem's backend API.
|
|
type memClient struct {
|
|
http *http.Client
|
|
base string
|
|
}
|
|
|
|
// memLive is memConnect, for the tests to stand in for.
|
|
var memLive = memConnect
|
|
|
|
// memConnect is the backend of the Mem whose config folder is dir if it
|
|
// answers, nil if none runs: $NMEM_API_URL when it names this computer,
|
|
// else 127.0.0.1:14242.
|
|
func memConnect(dir string) *memClient {
|
|
base := memAddr
|
|
if u := strings.TrimSpace(appdir.Getenv("NMEM_API_URL")); u != "" {
|
|
p, err := url.Parse(u)
|
|
if err != nil || (p.Scheme != "http" && p.Scheme != "https") || !kunLoopback(p.Hostname()) {
|
|
return nil
|
|
}
|
|
base = strings.TrimRight(u, "/")
|
|
}
|
|
c := &memClient{http: &http.Client{}, base: base}
|
|
ctx, cancel := context.WithTimeout(context.Background(), memProbe)
|
|
defer cancel()
|
|
if _, err := c.do(ctx, "GET", "/remote-llm/status", nil); err != nil {
|
|
return nil
|
|
}
|
|
return c
|
|
}
|
|
|
|
func (c *memClient) do(ctx context.Context, method, path string, body any) ([]byte, error) {
|
|
var r io.Reader
|
|
if body != nil {
|
|
b, err := json.Marshal(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
r = bytes.NewReader(b)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, c.base+path, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
resp, err := c.http.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Nowledge Mem: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
out, err := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Nowledge Mem: %w", err)
|
|
}
|
|
if resp.StatusCode/100 != 2 {
|
|
var e struct {
|
|
Detail any `json:"detail"`
|
|
}
|
|
json.Unmarshal(out, &e)
|
|
if s, ok := e.Detail.(string); ok && s != "" {
|
|
return nil, errors.New("Nowledge Mem: " + s)
|
|
}
|
|
return nil, fmt.Errorf("Nowledge Mem: %s %s: HTTP %d", method, path, resp.StatusCode)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (c *memClient) config() (memState, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), memWait)
|
|
defer cancel()
|
|
b, err := c.do(ctx, "GET", "/remote-llm/config", nil)
|
|
if err != nil {
|
|
return memState{}, err
|
|
}
|
|
var s memState
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
return memState{}, fmt.Errorf("Nowledge Mem: /remote-llm/config: %w", err)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func (c *memClient) post(path string, body any) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), memWait)
|
|
defer cancel()
|
|
_, err := c.do(ctx, "POST", path, body)
|
|
return err
|
|
}
|