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.
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
// Package jsmod compiles a plugin's JavaScript module for moejs, the
|
|
// JavaScript engine written in Go that magpie runs plugin code in its own
|
|
// process with: gateway middleware (internal/middleware) and agents
|
|
// (internal/agentplug).
|
|
package jsmod
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Calcium-Ion/moejs"
|
|
)
|
|
|
|
// Compile compiles the module in file and the modules it imports, which
|
|
// are files under root: there is no node_modules here, so a package is
|
|
// bundled into the module. what names the kind of module in an error
|
|
// ("a middleware").
|
|
func Compile(root, file, what string) (*moejs.Module, error) {
|
|
src, err := os.ReadFile(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
entry, err := moejs.Compile(file, string(src))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mods := map[string]*moejs.Module{file: entry}
|
|
return moejs.Link(entry, func(ref moejs.Referrer, spec string) (*moejs.Module, error) {
|
|
if !strings.HasPrefix(spec, "./") && !strings.HasPrefix(spec, "../") {
|
|
return nil, fmt.Errorf("%q: %s imports only files beside it; bundle packages into it", spec, what)
|
|
}
|
|
from := file
|
|
if ref != nil {
|
|
from = ref.Name()
|
|
}
|
|
p := filepath.Clean(filepath.Join(filepath.Dir(from), filepath.FromSlash(spec)))
|
|
if rel, err := filepath.Rel(root, p); err != nil || strings.HasPrefix(rel, "..") {
|
|
return nil, fmt.Errorf("%q is outside the plugin", spec)
|
|
}
|
|
if m, ok := mods[p]; ok {
|
|
return m, nil
|
|
}
|
|
src, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
m, err := moejs.Compile(p, string(src))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mods[p] = m
|
|
return m, nil
|
|
})
|
|
}
|