P2 of docs/subsystems/plugin-runtime.md, the fourth page after Sessions,
Library and Routing, and the largest part of app.js.
- The Usage page's code (~3000 lines: the Overview's cards, the
Requests ledger and heatmap, the Sessions tab, the Agents list) moves
from app.js to assets/plugins/usage/page.js; its markup, index.html's
<main id="view-usage">, is client.js's part html. app.js 25756 →
22675 lines (1,355,376 → 1,198,239 bytes).
- page.js builds on app.js at the top level (its timers, ?view=usage's
tab and filters), so it is the one plugin script that loads after
app.js. index.html: kernel.js, the plugins' other scripts, app.js and
the scripts beside it, each page.js, then start.js, which is app.js's
old tail (magpie.start(), navHides, OPEN_VIEWS, the first page shown,
load()), so the first page has its code. TestBuiltinClientsLoaded
holds that order.
- Hooks: open loads it, refresh(again) is load()'s read of it, reload is
Refresh now, costs redraws costs on a currency change, tab says the
tab for focusQuotaCard. app.js calls those through magpie.call. The
allowance cards, quotasOpened, panelQuotaFocus and panelUseFocus stay
in app.js: the tray panel draws them too.
- plugins/usage: Essential, Requires window, no InUse. Off, its nav tab
goes and so do the ways to it (data-feature="usage": the panel's Open
Usage, Routing's View usage); show("usage") gives way to Agents; the
panel's Allowances tab stays. builtins.test.cjs switches it off and
checks each.
- The 173 strings only Usage says move to plugins/usage/i18n.js;
i18n.js 15032 → 14344 lines. "{n}s" and
"All sessions" stay: context.js says them too since #1591.
- context.js: with context.js now in before the first page is shown,
the page's open and load()'s first refresh both reached loadContext,
and the second drew "Reading…" again over the advice the first had
just drawn, so its curves never played their entrance (context-tune,
3 of 3 red). loadContext draws the waiting pane once for the same
days. Its dead last line, which loaded the tab once context.js came
in late, goes.
- context-window.test.cjs's "arrives after the state" becomes "loads at
once though context.js is slow": start.js now waits for it.
usage-stale-answer reads page.js beside app.js. quota-refresh-order
(#1630) cut app.js at the period picker's comment, now in page.js; it
ends at the next comment that stays (5 of 5; without keepRefreshedQuotas'
call, 2 of 5 red).
- docs: plugin-runtime.md, gui-shell.md.
Run, at this tree on origin/main af27b70b4 (main's keepRefreshedQuotas
and quotaRefresh changes, #1630, stay in app.js beside loadQuotas: the
tray panel draws those cards too):
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows.
- go test -count=1 ./internal/gui ./internal/pluginrt ./plugins/...: ok
(-skip TestMainWindowCorners, #1936); gateway and usage as above.
- usage-card-refresh, quota-refresh-order, builtins, editor-picker-clicks,
context-window, usage-stale-answer: Chromium and WebKit green.
- The full GUI set: see the Routing commit; every Usage red there is the
same on main or red only under load.
16 KiB
Plugin runtime
magpie's built-in plugins are the parts of magpie not everyone needs, each
one a package under plugins/ that the user can switch off. The runtime in
internal/pluginrt keeps their registry, applies the ones that are on in the
order their services make, and disposes of what each registered when it is
turned off. plugins.yml says which are on. This page describes P0 (the
runtime, the file and the window's switch) and P1 (the window's kernel: a
plugin's page, its error boundary, the kit it draws with and its settings
form, with the Gateway page as the first plugin to draw its own page), and
P2 so far (a page moved out of the core whole: its code, styles and words:
Sessions, Library, Routing and Usage). OpenCode plugins (npm packages in
the plugin host) are a different thing; see
provider and plugin ownership and
gateway middleware.
Responsibilities and boundaries
| Part | What it does | Source |
|---|---|---|
| Registry | A plugin calls pluginrt.Register(Manifest) from init. Registered lists them by id; two with one id panic. plugins/all imports every plugin, and only main.go imports plugins/all, so core code and its tests run without any. |
pluginrt.go, plugins/all/all.go |
| Manifest | ID, API (a caret range of the host API, "^1"; pluginrt.API is "1.0"), Requires and Provides (services), Essential, Name/Sub (English; the window translates them), One/Many (what is in use, of 1 or {n}), InUse, Apply, Parts ({Slot, Key}: the window's parts it is), Defaults (its config), Settings (a struct of its settings, made into Defaults and a schema at Register) and Client (its files in the window, under internal/gui/assets/plugins/<id>/: scripts, its i18n.js and a .css). |
pluginrt.go |
| Lifecycle | Runtime.Reconcile(on, config). Off first: every plugin no longer wanted, and every one requiring a service one of those provided, is disposed newest first. Then on: each wanted plugin whose requirements are provided (by Core, settings, providers, gateway, window, or an active plugin) is applied, again and again until no more can be. States: off, pending (waiting for a service, logged), active, failed (Apply returned an error or panicked, or the API range doesn't match; what it registered before is undone), disposed. A failed plugin never stops the others. |
pluginrt.go |
| Ctx | What Apply gets: OnDispose(f), Contribute(slot, key), Provide(name, v) (withdrawn when it goes), Service(name), and its composed Config. Disposers run in reverse, each one's panic logged and the rest still run. |
pluginrt.go |
| plugins.yml | appdir.Config()/plugins.yml: preset (essentials or all) and plugins, rows of {id, enabled, config} holding only what the user set. |
config.go |
| Settings schema | SchemaOf(Settings) makes a JSON Schema object of the struct, in its fields' order (x-order): each exported field with a json name is a setting, of bool, an int, float64 or string, with tags title (English; the window translates it), description, enum (comma separated) and min/max. Its values are the defaults. A field a form can't draw, or a tag that doesn't parse, panics at Register. Schema.Validate takes a value only as the schema allows it (a JSON float64 or a YAML int for an integer); SetConfig(id, config) writes the user's row through it, and Compose leaves out a value edited into plugins.yml by hand that it doesn't allow, so the default stands. |
schema.go, config.go |
| Built-in plugins | groups, library, otel, sessions, warmups (P0), gateway (P1), the Gateway page, which is Essential and in use while a gateway key is there (access.List), and routing (P2), the Routing page, Essential and in use while the user has a routing group, since the page is where groups are made and changed. usage (P2), the Usage page, is Essential too, with no InUse: what it shows the gateway counts anyway, and the tray panel's Usage tab still shows the allowances with it off. The Gateway page's one setting, callsPage (10, 20, 50 or 100; 20), is how many recent calls the page shows at a time. The other five aren't Essential. Each one's InUse counts what the user made with it: routing groups not Auto and not hidden, library items given to agents, a collector sent to, gateway conversations recorded, warm-ups and check-ins on. |
plugins/ |
The boundary is checked by TestPluginsImportOnlyCore: no package outside
plugins/ imports one (but main.go, through plugins/all), a plugin never
imports another, and plugins/all imports every plugin folder. Plugins reach
each other only through services.
The window's kernel (P1)
internal/gui/assets/kernel.js loads after i18n.js and before the
plugins' scripts and app.js, all classic scripts sharing one scope. It is
window.magpie:
magpie.plugin(id, setup): a plugin's script registers, andsetupgets a context:config(from boot.js'spluginConfig, then Plugins › Built-in),onConfig(fn),onDispose(fn),slots.add(slot, part)andui.- Slots. The one slot is
page: a part{key, html, loads, mount, …hooks}becomes<main class="view" id="view-<key>" data-feature="<plugin>">where index.html's<template data-slot="page">is, at once, so app.js finds the page as it finds its own. app.js callsmagpie.start()when it is set up, which mounts every page, and then each hook throughmagpie.call(key, hook, …):loading,draftandrenderaround/api/providersfor a page thatloads: "providers",openwhenshowopens it (a promise it gives back puts the reader back where they were once it settles),relangwhen the language changes on it.titleis the page's header (English;showtranslates it).magpie.pages()adds the plugins' pages to the viewsshowand?view=know. - Words. A plugin's
i18n.jscallsmagpie.strings({zh, "zh-TW", ja, de}), added to i18n.js'sI18Nbefore app.js translates anything. A key already there is logged and not taken.tests/strings.cjsreads them as the page does, forgui-de,gui-ja,gui-zh-twandi18n-keys(no key in two files, every key of a plugin's file in all four languages). - Error boundary. A hook that throws, or whose promise fails, is caught
in
run: logged with[magpie] <plugin>: <key>.<hook>, and the page is replaced by a card saying "This page stopped working", the error's message and Try again. Further hooks of a broken page do nothing, so nothing half drawn comes back, and the other pages go on. Try again puts the page's markup back, translates it, mounts it and runs the hook that broke with what it was given. A plugin whosesetupthrows is logged and has no page. - Kit (
magpie.ui):el,button,toggle(the switch Plugins uses,role="switch"),menu(the app's menu throughopenProtoMenu, never a native<select>),row(a settings row) andform(schema, values, onChange): a switch for a boolean, a menu for an enum (numbers in the window's locale), a field for a number (kept to its range,aria-invalidwhen not) or text. - Settings. Plugins › Built-in draws the rows of each plugin on, with a
schema, from
ui.formunder its row (.feat-set), and a change posts{id, config};applyFeatureshands each plugin's config tomagpie.configure, which calls itsonConfigonly when it changed.
The Gateway page is plugins/gateway with Client: keys.js, client.js
(internal/gui/assets/plugins/gateway/): keys.js is the gateway-key
manager that was caller-keys.js, and client.js is the page's code that
was in app.js, ending in its magpie.plugin("gateway", …). Still in
app.js and app.css: its styles, its sidebar and header buttons
(data-feature="gateway" in index.html), and decideEntry, which the model
info card shares. TestBuiltinClientsLoaded holds index.html to load every
plugin's Client script between kernel.js and app.js, and its stylesheet in
the head, each with its ?v=.
Pages moved out (P2)
Each page moves in a commit of its own, whole: its code into
assets/plugins/<id>/client.js, its styles beside it, and the words only it
says from i18n.js into its i18n.js (a key app.js, another page, index.html
or Go also says stays in i18n.js). It stays so after: i18n-keys' "a
plugin's words are its own" fails when anything outside a plugin's folder
says one of its words, which goes back to i18n.js.
- Sessions (
plugins/sessions,Client: i18n.js, sessions.css, client.js): the page that wassessions.js, its markup all drawn by its code, so its part has nohtml. Its hooks:openandrelangload it, andreveal(s, e)is the Usage page's way in (a session's row and All sessions, #752), called throughmagpie.call("sessions", "reveal", …), so with no Sessions plugin it does nothing. Still in app.js: the Usage page's Sessions tab (/api/sessions) and Settings' session terminal row (data-feature="sessions"). - Library (
plugins/library,Client: i18n.js, library.css, client.js): the page that waslibrary.js. Its code runs atmountonly, where it wires its listeners (the dialog's backdrop and Escape, focus and visibility reading it again, its lists' scroll), since the script is in before app.js. Its hooks:openandrelangread/api/library,redrawdraws it again once the state says which agents are hidden, andclose,dirtyanddiscardare whatshow()asks before the reader leaves it.window.loadLibrary,redrawLibrary,closeLibraryModal,libraryDirtyanddiscardLibraryare gone. - Routing (
plugins/routing,Client: i18n.js, routing.css, client.js): the page that wasrouting.js, Essential. Its part'shtmlis the page's head and boxes that were index.html's; the rest of the page is drawn byroutingPage, run atmountbecause it builds on app.js. It keeps itself after that, as it did (its ownhidden, the language, the window's focus), so its hooks are onlydirtyanddiscard, a routing group being edited, whichshow()and the window's close ask throughmagpie.call("routing", …):window.routingDirtyanddiscardRoutingare gone. Its tab in the tray panel and the panel's section are markeddata-feature="routing". What other pages use of it is still onwindow(openRoutefor context.js;kindName,swapWhyandroutedWhyfor Usage's Requests;newGroupWithfor Providers;panelRoutingShownfor the panel). With the plugin off, what leads to the page goes with it: the nav and panel tabs, the guide's Open Routing, Providers' "Make a routing group of …" (data-feature="routing"), the Usage list's View routing and its time as a link, and the context card's Latest request; a panel left on its Routing tab shows Agents. - Usage (
plugins/usage,Client: i18n.js, client.js, page.js): the page that was the largest part of app.js (about 3000 lines: the Overview's cards, the Requests ledger, the Sessions tab and the Agents list). Its part'shtmlis the page's markup that was index.html's#view-usage. Its code builds on app.js at the top level (its timers,?view=usage's tab and filters), sopage.jsis the one script a plugin loads after app.js: index.html loads kernel.js, the plugins' other scripts, app.js and the scripts beside it, then eachpage.js, thenstart.js, which is app.js's old tail (magpie.start(), the first page shown, the firstload()), so the first page has its code.TestBuiltinClientsLoadedholds that order. Its hooks:openloads it,refresh(again)isload()'s read of it (coming back to the Overview is opening the allowances, #1588),reloadis Refresh now,costsdraws the costs again when Settings' currency changes, andtabsays which tab it is on, forfocusQuotaCard. Still in app.js, since the tray panel draws them too: the allowance cards,quotasOpened,panelQuotaFocusandpanelUseFocus. With the plugin off, its nav tab goes, and so do the ways to the page (data-feature="usage"): the tray panel's Open Usage and Routing's View usage. Ashow("usage")left anywhere gives way to Agents. The panel's Allowances tab stays.
Source of truth and composing
What a plugin runs with is composed field by field (Compose), each value
remembering its layer (Source):
default: every plugin on, itsManifest.Defaults;preset: Essentials leaves every plugin notEssentialoff;user: the rows of plugins.yml (enabled, andconfigmerged into the defaults key by key, maps deeply);in use: a plugin whoseInUseis above 0 is on whatever the rows say, since off its things would go on working with nowhere to see them. AnInUseerror counts as in use.
Composed.Chosen is plugins.yml's say before step 4. magpie plugin dump
(--json) prints the composed list with each plugin's state and why.
Runtime paths
- Start (
internal/cli/main.go, aftersettings.Migrate):pluginrt.Settle(!settings.RanBefore())writes a first plugins.yml when there is none:essentialson a new install,allon a magpie that ran here before (one ofsettings.ranBefore's files is there; one that can't be looked at counts as there), so an existing user keeps everything. Thenpluginrt.Refreshmakes the process's runtime and reconciles it. An error is printed and magpie starts. - Every read:
Refreshloads plugins.yml again, composes it and reconciles. The window's/api/state,/api/plugins/builtinand boot.js each call it (builtinsState), so a change made by the CLI shows on the next read. - Switch:
Set(id, on)(POST /api/plugins/builtin,magpie plugin builtin on|off <id>) writes the user's row;SetPresetthe preset (magpie plugin preset essentials|all). Off is refused while the plugin is in use. - The window: see GUI app shell, "Built-in plugins".
Failure and compatibility
- A plugins.yml that can't be read or doesn't parse, or names another
preset, is an error, never an empty file.
Refreshreturns it and leaves the runtime as it was; before the first good read every plugin is on, as before plugins existed.SetandSetPresetrefuse to write over it, the window says it above the list and disables every switch, and the CLI exits with it. The user fixes the file or removes it. - Writes hold
filelock.Mutex(plugins.yml.lock) against other magpies and a mutex in the process, are written whole (temp file, sync, rename), and keep the file they replace asplugins.yml.bak. - A plugin written for another major API, or a newer minor, is
failedwith the reason; the rest run. - A plugin's page whose code throws breaks only that page (the boundary above); a POST of a config the schema refuses is a 400 and writes nothing.
- A plugin's off is the window's alone: its page, sections and rows leave the window. The gateway, the TUI and the CLI serve the same either way, and nothing the user made is deleted. Davsync doesn't carry plugins.yml.
Verification
go test ./internal/pluginrt/(TestReconcile,TestCompatible,TestPluginsImportOnlyCore,TestCompose,TestSettleAndSet,TestBrokenFileIsKept),go test ./internal/settings/ -run TestRanBefore.go test ./internal/pluginrt/ -run 'TestSchemaOf|TestValidate|TestSetConfig'.go test ./internal/gui/ -run 'TestBuiltin|TestBootSaysBuiltinsOff'(TestBuiltinConfig,TestBuiltinClientsLoaded).i18n-keys,gui-de,gui-ja,gui-zh-tw, and a moved page's own tests (sessions-*,session-*,sess-*;library-*,skill-*,mcp-*,found-skill-*) in Chromium and WebKit.internal/gui/tests/builtins.test.cjs(the settings form) andplugin-kernel.test.cjs(the boundary, Try again,callsPagefrom boot.js and from a change) in Chromium and WebKit, every language, 1100, 800 and 420 points; the Gateway page's own tests (gateway-*,recent-calls-*) unchanged.- By hand, under a sandbox HOME:
magpie plugin builtin,magpie plugin builtin on groups,magpie plugin dump, and a plugins.yml broken by hand.