Files
magpie/internal/gui/builtins.go
T
yetone 3ccbe6bc15 plugins: a runtime for built-in plugins, and Plugins › Built-in to switch them (P0)
Lxxyx on X (https://x.com/Lxxyx_/status/2109506752656150974): magpie has
too much in it. This is P0 of making magpie's parts plugins, as DeepSeek
Harness does: a runtime, plugins.yml, and the first five built-in plugins
the user can switch off.

- internal/pluginrt: a registry (Register from init), a manifest (API
  caret range, Requires/Provides, Essential, InUse, Apply, Parts,
  Defaults) and Reconcile: the plugins no longer wanted, with whatever
  needs a service they provided, are disposed newest first; the wanted
  ones are applied once their services are there. A plugin that fails or
  panics is failed and the rest run. TestPluginsImportOnlyCore keeps the
  boundary: nothing outside plugins/ imports one, none imports another.
- plugins.yml (appdir.Config()): default → preset (essentials | all) →
  the user's rows → in use. A new install is written Essentials, a magpie
  that ran here before (settings.RanBefore) All, so existing users keep
  everything. Writes are locked, whole and keep a .bak; a file that
  doesn't parse is an error, never empty, and is never written over.
- plugins/groups, library, otel, sessions, warmups. In P0 a plugin off
  is the window's alone: its page, sections and rows (data-feature) leave
  the window; the gateway, TUI and CLI serve the same, nothing is
  deleted. One in use stays on.
- The window: Plugins › Built-in, the first tab, a switch per plugin;
  GET/POST /api/plugins/builtin; boot.js says which are off before the
  first paint. CLI: magpie plugin builtin [on|off <id>], plugin preset,
  plugin dump [--json].
- docs/subsystems/plugin-runtime.md, gui-shell.md.

Run: go test ./internal/pluginrt/ ./internal/settings/ ./internal/gui/;
gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows
go vet ./.... GUI: builtins.test.cjs in Chromium and WebKit, 5 languages,
1100/800/420; list-sort, mobile-web, routing-keys-fold, routing-sessions
pass. By hand under a sandbox HOME: a new install gets essentials, an
existing one all, a broken plugins.yml is refused and kept, dump works.
Full suite results after the rebase are in the push notes.
2026-10-13 16:19:13 +08:00

131 lines
4.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"encoding/json"
"fmt"
"net/http"
"github.com/yetone/magpie/internal/pluginrt"
)
// Plugins › Built-in (Lxxyx on X: "功能太多了"): magpie's built-in plugins
// (internal/pluginrt, plugins/), each with a switch kept in plugins.yml.
// Off, a plugin's parts leave the window: its page leaves the sidebar and
// the header's tabs, its sections leave the pages they are on, its rows
// leave Settings and Settings' search, and a link to it opens nothing. The
// page hides whatever is marked data-feature="<id>" while the plugin isn't
// active.
//
// One in use stays on: its switch can't be turned off, and its row says
// what uses it. A new install starts with the Essentials preset; one that
// ran before has them all on until the user says otherwise.
// TestBuiltinsTranslated asks each string of every plugin of every
// language.
// builtinJSON is a built-in plugin as the page is told it: whether it
// shows (On: active, which plugins.yml or its use makes it), the user's
// own choice, and what of it is in use.
type builtinJSON struct {
ID string `json:"id"`
Name string `json:"name"`
Sub string `json:"sub"`
On bool `json:"on"`
Chosen bool `json:"chosen"`
Source pluginrt.Source `json:"source"`
State pluginrt.State `json:"state"`
// Why is the reason it isn't running, when it is on and isn't
Why string `json:"why,omitempty"`
InUse int `json:"inUse"`
Use string `json:"use,omitempty"` // One or Many, by InUse
Parts []pluginrt.Part `json:"parts,omitempty"`
// Broken is plugins.yml's error when it can't be read: every
// plugin runs as it did, and no switch can be changed
Broken string `json:"broken,omitempty"`
}
// builtinsState composes plugins.yml again, brings the runtime to it, and
// says every built-in plugin, in their order.
func builtinsState() []builtinJSON {
cs, err := pluginrt.Refresh()
byID := map[string]pluginrt.Composed{}
for _, c := range cs {
byID[c.ID] = c
}
st := map[string]pluginrt.Status{}
if rt := pluginrt.Running(); rt != nil {
for _, s := range rt.Statuses() {
st[s.ID] = s
}
}
out := []builtinJSON{}
for _, m := range pluginrt.Registered() {
c, s := byID[m.ID], st[m.ID]
j := builtinJSON{ID: m.ID, Name: m.Name, Sub: m.Sub, On: s.State == pluginrt.Active, Chosen: c.Chosen, Source: c.Source, State: s.State, Why: s.Why, InUse: c.InUse, Parts: s.Parts}
if err != nil {
j.Broken, j.Chosen = err.Error(), j.On
}
switch {
case j.InUse == 1:
j.Use = m.One
case j.InUse > 1:
j.Use = m.Many
}
out = append(out, j)
}
return out
}
// builtinsOff are the ids of the plugins the window leaves out now, for
// boot.js to hide them before the page paints.
func builtinsOff(list []builtinJSON) []string {
out := []string{}
for _, b := range list {
if !b.On {
out = append(out, b.ID)
}
}
return out
}
// builtinRoutes: GET /api/plugins/builtin is every built-in plugin; POST
// {"id", "on"} switches one, kept in plugins.yml as the user's own choice.
// One in use is refused off.
func builtinRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/plugins/builtin", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, builtinsState())
})
mux.HandleFunc("POST /api/plugins/builtin", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
ID string `json:"id"`
On bool `json:"on"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
var b *builtinJSON
for _, x := range builtinsState() {
if x.ID == in.ID {
b = &x
break
}
}
switch {
case b == nil:
fail(rw, fmt.Errorf("no built-in plugin %q", in.ID))
return
case b.Broken != "":
fail(rw, fmt.Errorf("%s", b.Broken))
return
case !in.On && b.InUse > 0:
fail(rw, fmt.Errorf("%s is in use and stays on", b.Name))
return
}
if err := pluginrt.Set(in.ID, in.On); err != nil {
fail(rw, err)
return
}
writeJSON(rw, builtinsState())
})
}