Plugins could be subscription providers or gateway middleware. Now a third
kind: an agent plugin, so a user whose agent magpie doesn't ship can still
put it on magpie.
- internal/agentplug: a plugin's magpie.agent module (or a *.agent.js file)
exports agent = {id, name, config, format?, model, prefix?, bin?, dir?,
ua?, icon?, notice?} and an optional connect({gateway, model, models,
agent}) returning the other keys to write. Compiled in moejs, reloaded
when plugins.json or the file changes; why one didn't load is kept.
- internal/jsmod: module compiling (imports kept inside the plugin), moved
out of middleware so both share it.
- internal/agent/plugged.go: each becomes an Agent (Plugin set) after
magpie's own. JSON, YAML and env files are edited key by key, comments
kept; what each key held is stashed typed under plugin:<id>:<path>, and
its own model or Disconnect puts every key back. A built-in id is
refused; a connect that throws writes nothing. Key magpie-<id>; usage
names it by key or ua (usage.PluginAgents).
- GUI Plugins page: an Agent kind chip and glyph, a Discover "Agents"
section (market kind "agent"), and an installed row line "Adds X to
Agents" or why it didn't load. zh, zh-TW, ja, de strings.
- Docs: site Plugins page (en, zh, ja) "Agent plugins", reference.md,
agent-wiring.md, gateway-middleware.md.
Run:
- go test ./... (all ok); gofmt -l clean; GOOS=linux go vet -tags nogui
./... and GOOS=windows go vet ./... clean.
- TestPluggedYAML, TestPluggedJSONUnwire, TestPluggedRefused.
- GUI: plugin-agent.test.cjs 22/22 (en, zh, zh-TW, ja, de; 980 and 420;
chromium and webkit); all plugin-*.test.cjs, i18n-keys,
deprecated-subscriptions, filter-ime: 315 pass, 0 fail.
- Real use, in a sandbox HOME: an Aider plugin (~/.aider.conf.yml, prefix
openai/, connect writes openai-api-base/key) installed in a dev build;
picking DeepSeek V4.1 Flash on the Agents page wired the file keeping the
user's comment and keys; real aider 0.86.2 `--message` answered through
magpie from the real DeepSeek account; usage.jsonl recorded agent aider,
provider deepseek, 200; Disconnect restored the file byte for byte.
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package plugin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Agent is the agent a plugin at target adds magpie's wiring for: the
|
|
// file its package.json's magpie.agent names, or the file itself for a
|
|
// path to a *.agent.js (or .mjs). only is whether it is nothing else: no
|
|
// OpenCode plugin for the host to load (no main, no exports).
|
|
func Agent(target string) (file string, only bool) {
|
|
st, err := os.Stat(target)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
if !st.IsDir() {
|
|
base := strings.ToLower(filepath.Base(target))
|
|
if strings.HasSuffix(base, ".agent.js") || strings.HasSuffix(base, ".agent.mjs") {
|
|
return target, true
|
|
}
|
|
return "", false
|
|
}
|
|
b, err := os.ReadFile(filepath.Join(target, "package.json"))
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
var pkg struct {
|
|
Main string `json:"main"`
|
|
Exports json.RawMessage `json:"exports"`
|
|
Magpie struct {
|
|
Agent string `json:"agent"`
|
|
} `json:"magpie"`
|
|
}
|
|
if json.Unmarshal(b, &pkg) != nil || strings.TrimSpace(pkg.Magpie.Agent) == "" {
|
|
return "", false
|
|
}
|
|
file = filepath.Join(target, filepath.FromSlash(strings.TrimSpace(pkg.Magpie.Agent)))
|
|
return file, pkg.Main == "" && len(pkg.Exports) == 0
|
|
}
|
|
|
|
// InMagpieOnly is whether everything a plugin at target has runs in
|
|
// magpie's own process, its gateway middleware and its agent, so the
|
|
// host has nothing of it to load.
|
|
func InMagpieOnly(target string) bool {
|
|
mw, mwOnly := Middleware(target)
|
|
ag, agOnly := Agent(target)
|
|
if mw == "" && ag == "" {
|
|
return false
|
|
}
|
|
return (mw == "" || mwOnly) && (ag == "" || agOnly)
|
|
}
|