Files
magpie/internal/provider/plugin_icon.go
T
yetone cb406c8425 plugins: a plugin can add an agent magpie has no setup of its own for
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.
2026-10-10 18:39:47 +08:00

189 lines
5.1 KiB
Go

package provider
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// PluginIcon is the icon a plugin's provider shows: the picture its plugin
// gives it (the auth hook's icon, or package.json's magpie.icon), kept as
// a picture given by hand is, else the one the plugin market gives it.
func PluginIcon(pp plugin.Provider) string {
if ic := pluginOwnIcon(pp.Icon); ic != "" {
return ic
}
return plugin.Icon(pp.Spec, pp.ID)
}
// PluginOwnIcon is the Icon value of a picture a plugin gave (a data:image
// URI or an https URL), "" while there is none to show: an agent a
// plugin adds shows it as a provider's does.
func PluginOwnIcon(said string) string { return pluginOwnIcon(said) }
// plugIcons are the pictures plugins gave, by their hash: the Icon value
// each is kept as, and when one was last tried (a URL being fetched or
// that failed, a data URI that isn't a picture).
var plugIcons struct {
sync.Mutex
from string // the file they were read from
kept map[string]string
tried map[string]time.Time
// the URLs being fetched now, each closed once its fetch is over
busy map[string]chan struct{}
}
// fetchPluginIcon fetches an https picture as an import link's is: a
// public host only, at most MaxIcon. Tests point it elsewhere.
var fetchPluginIcon = FetchIcon
func plugIconsPath() string { return filepath.Join(filepath.Dir(Path()), "plugin-icons.json") }
func loadPlugIcons() {
if plugIcons.from == plugIconsPath() {
return
}
plugIcons.from = plugIconsPath()
plugIcons.kept, plugIcons.tried = map[string]string{}, map[string]time.Time{}
if b, err := os.ReadFile(plugIcons.from); err == nil {
_ = json.Unmarshal(b, &plugIcons.kept)
}
}
func keepPlugIcon(k, icon string) {
plugIcons.kept[k] = icon
delete(plugIcons.tried, k)
if b, err := json.Marshal(plugIcons.kept); err == nil {
_ = os.MkdirAll(filepath.Dir(plugIcons.from), 0o755)
_ = os.WriteFile(plugIcons.from, b, 0o644)
}
}
// pluginOwnIcon is the Icon value of the picture a plugin gave, "" while
// there is none to show. A data:image URI is kept at once; an https URL
// is fetched in the background, through FetchIcon's checks, and shows
// once it is in (a failure is tried again in an hour). Anything else is
// refused.
func pluginOwnIcon(said string) string {
if said == "" {
return ""
}
sum := sha256.Sum256([]byte(said))
k := hex.EncodeToString(sum[:16])
plugIcons.Lock()
defer plugIcons.Unlock()
loadPlugIcons()
if ic := plugIcons.kept[k]; ic != "" {
if f := IconFile(strings.TrimPrefix(ic, iconPrefix)); f != "" {
if _, err := os.Stat(f); err == nil {
return ic
}
}
}
if t, ok := plugIcons.tried[k]; ok && time.Since(t) < time.Hour {
return ""
}
plugIcons.tried[k] = time.Now()
if len(said) > 11 && strings.EqualFold(said[:11], "data:image/") {
if b := dataURI(said[len("data:"):]); b != nil {
if ic, err := StoreIcon(b); err == nil {
keepPlugIcon(k, ic)
return ic
}
}
return ""
}
u, err := iconURL(said)
if err != nil {
return ""
}
if plugIcons.busy == nil {
plugIcons.busy = map[string]chan struct{}{}
}
done := make(chan struct{})
plugIcons.busy[k] = done
fetch := fetchPluginIcon
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
ic, err := fetch(ctx, u)
plugIcons.Lock()
if err == nil {
keepPlugIcon(k, ic)
}
delete(plugIcons.busy, k)
plugIcons.Unlock()
close(done)
}()
return ""
}
// RepoIcons are the Icon values of the pictures plugins not installed yet
// give in their package.json (repositories tagged magpie-plugin), checked
// and kept as an installed plugin's are: "" where there is none to show.
// An https picture not fetched before is waited for up to wait, so the
// list's first answer has it (yetone: 非官方插件显示不出来 logo); one
// slower than that shows the next time the list is asked.
func RepoIcons(said []string, wait time.Duration) []string {
out := make([]string, len(said))
var busy []chan struct{}
for i, s := range said {
out[i] = pluginOwnIcon(s)
if out[i] == "" && s != "" {
sum := sha256.Sum256([]byte(s))
plugIcons.Lock()
if ch := plugIcons.busy[hex.EncodeToString(sum[:16])]; ch != nil {
busy = append(busy, ch)
}
plugIcons.Unlock()
}
}
if len(busy) == 0 {
return out
}
t := time.NewTimer(wait)
defer t.Stop()
for _, ch := range busy {
select {
case <-ch:
case <-t.C:
return refill(out, said)
}
}
return refill(out, said)
}
// refill asks again for the pictures not in yet, which those fetched since
// now have
func refill(out, said []string) []string {
for i, s := range said {
if out[i] == "" && s != "" {
out[i] = pluginOwnIcon(s)
}
}
return out
}
// keptPluginIcons are the names of the stored pictures plugins gave, which
// pruneIcons leaves.
func keptPluginIcons() []string {
plugIcons.Lock()
defer plugIcons.Unlock()
loadPlugIcons()
var out []string
for _, ic := range plugIcons.kept {
if name, ok := strings.CutPrefix(ic, iconPrefix); ok {
out = append(out, name)
}
}
return out
}