Part 1 of 2, pruneDist (coffemon's first commit): pruneDist took out every tarball in plugins/dist that plugins/package.json doesn't name, as encoding/json reads it. Bun installs a package from the tarball bun.lock's packages has it at, and that can be one package.json has moved on from: gkd2323c's lock (#3622, Windows, Bun 1.4.4) had autoclaw, codearts and cloudflare-workers-ai at the tarballs before the ones package.json names, and those were gone from plugins/dist, where nothing but pruneDist takes a file out. With Bun's own copy of such a tarball, every bun command that changes plugins/ puts the older version back; with none, every add and removal, of any plugin, fails with "ENOENT extracting tarball from <that package>" (part 2 has the likely why there was none). Now a tarball the lock's packages has a package at stays, beside the ones package.json names. Kept, it is what Bun installs: a package whose lock is behind package.json stays at the older version without a word, where4e5b2ad8, once Bun had no copy, failed every command with ENOENT. part 2 sends such a lock back to package.json before each bun command when package.json has the package at a dist tarball too; with package.json at npm it stays behind (#3720). package.json is read as text, every dist/<file> in it counting, where encoding/json keeps one value of a key named twice: Bun 1.4.0 adds a tarball's package a second time instead of replacing it, the new tarball first, which Bun reads, and encoding/json reads the second, so4e5b2ad8took out the tarball Bun installs from. No magpie runs plugins on that Bun unless MAGPIE_BUN names it (BunVersion has been 1.4.2 since 10-03, before plugins/dist was), so that is how a decoded package.json loses it, not a path a user is known to have taken. A tarball only the lock's workspaces names goes: Bun 1.4.2 and 1.4.3 leave the one an add replaced there until their next command, and nothing installs from it. With no bun.lock, package.json alone decides, as before; with one that can't be read, nothing goes, as with a package.json that can't be read. This keeps the tarballs a lock has from going, and mends no lock: one behind package.json's dist tarball, the reporter's or one that falls behind later, is sent back by part 2, its tarball there or gone. What was run (tree 8f285445, on origin/main4e5b2ad8, go1.26.8, linux/amd64): - TestDistTarballTheLockNamesStays (new): the reporter's package.json and lock, built around the lines #3622 quotes; the package.json real Bun 1.4.2 writes for `bun add ./dist/<0.2.6>` over 0.2.1, and the one Bun 1.4.0 writes (the package named twice), each with the lock both write, and the Bun 1.4.0 one with no lock; no lock; a lock that can't be read (a folder in its place). The tarballs the lock's packages or package.json name stay, the one only the lock's workspaces name goes, and one nothing names goes; with no lock package.json alone decides, and with an unreadable one nothing goes. With the change the TestDist tests are ok on each Bun. Broken by hand, each put back after, with Bun 1.4.2, 1.4.3 and 1.4.4 (outside graftcp, the proxy wrapper this box runs commands in, umask 077, logs kept): - 4e5b2ad8's pruneDist: the reporter's, both named-twice and the unreadable-lock cases red on each ("<tarball> taken out"); - package.json decoded as4e5b2ad8did, the lock still read: the named-twice case with no lock red on each ("<0.2.6> taken out"); - the whole lock read for tarballs instead of its packages: the workspaces case red on each ("0.2.1 kept"), and TestDistInstallsAPackageNPMDoesntHave red on 1.4.2 and 1.4.3 (0.1.4's tarball stays after 0.1.3 is pinned: those two leave it in the workspaces). - internal/plugin, -tags nogui, with Bun 1.4.4, the one magpie runs now: ok but TestPluginSOCKSProxy, red the same at origin/main7a60e78c(#2207, open, filed for 1.4.3); TestFlowBigReplyProd and TestRenewOtherHost skip by their own condition. - gofmt -l lists nothing; GOOS=linux, windows and darwin go vet -tags nogui ./..., GOOS=windows go vet ./..., GOOS=linux and darwin go build -tags nogui ./... and GOOS=windows go build ./...: exit 0 each. go vet and go build without the tag on linux: exit 1 (no GTK, WebKitGTK or fontconfig headers on this box). - Full suite, -tags nogui (CI's Linux job tags gtk3), outside graftcp (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout 45m, log kept): 64 ok, internal/plugin among them, and 8 red, none in what this changes: - agent TestCodexDriftReached and TestCodexBehindReverseProxy: #3398; TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir: #3141; TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout: this box's /tmp/.git, #3641. - davsync TestSyncTeraCloudStillSeesOtherWrites: #3420. - edit TestWriteAtomicReadOnlyHardLink: this box's umask 077, #3640. - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git, #3675. - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie: this box's millions of open fds, #3649. - provider: "fatal error: close of synctest channel from outside bubble" in TestUnreadLoginsWriteErrorsKeepCredentials, main's CI red, #3632; the package's tests after it didn't run. - sessions TestAgyTitleLater: a same-size write in the same mtime tick, 2 of 200 on main alone, #3708. - testenv TestProgram: this box's umask 077, #3640. Not tried: Windows, where the reporter is (CI's Windows job runs none of these tests: it compiles them and runs a named few). package.json and the lock are read for the forward-slash form magpie hands Bun ("./dist/<file>"), which the reporter's Bun 1.4.4 on Windows kept in both. A tarball they name only with backslashes goes, where4e5b2ad8on Windows kept one package.json named so (it read package.json through filepath.ToSlash). Part 2 of 2, mendDist (coffemon's second commit): The commit before keeps the tarballs bun.lock has packages at, but a lock behind package.json stays behind: gkd2323c's (#3622, Windows, Bun 1.4.4) had autoclaw, codearts and cloudflare-workers-ai at tarballs pruneDist had taken, while package.json and the lock's workspaces had moved on. Bun installs what the lock has, at every command that changes plugins/. From that tarball, kept now, or from its own copy of it, it put the older version back without a word; with neither, every install, update and removal, of any plugin, failed with "ENOENT extracting tarball from <that package>". Bun keeps its copy of a local tarball in its install cache as @T@<16hex>@@@1 on 1.4.2 and 1.4.3, and as @T@sha512-<32hex>@@@1, the start of the tarball's sha512, on 1.4.4, so a Bun moved to 1.4.4 has no copy of one an older Bun made (magpie takes a Bun release two days after it is out, bunSettle: 1.4.4, out on 10-22, from 10-24). pruneInstallCache leaves these copies alone (no package name before their first @). On Linux, such a lock and copies made by 1.4.2 or by 1.4.3 give an add on 1.4.4 the reporter's error, for both packages locked at gone tarballs; the same Bun's add succeeds and puts both back at their older versions. Now mendDist runs before each bun command that changes plugins/ (in runBun and install, under installMu): the packages the lock has at a dist tarball package.json doesn't name, while package.json has them at one plugins/dist holds, get one `bun update --ignore-scripts` of them all, which resolves them from package.json again. It comes after a9242eb6's mendPackageJSON, which cuts what follows package.json's object (plugins#405): mendDist decodes package.json, and with that tail there it would return without a mend, leaving the lock behind for the command. A package.json that names the lock's tarball too, as Bun 1.4.0's names a package twice, isn't behind. Nothing is asked of the registry: no package on dist has a dependency (its index.json, 42 packages, on 10-26). `bun add` of them doesn't do it on Bun 1.4.2 and 1.4.3, which keep what the lock has, and with no copy of them, an update of one package alone fails on the others still locked at gone tarballs. Each mend is logged with the packages it sent back, so a lock that falls behind again, which nothing here explains, leaves a line saying when. What it can't mend is left to the command it comes before, as before: package.json's tarball gone too, or a package.json or bun.lock it can't read. A lock behind package.json across npm and plugins/dist it leaves alone, and Bun installs the lock's version (#3720). A lock with nothing stale costs reading the two files, decoding package.json and a scan of the lock; a tarball is looked at only where the lock and package.json name different ones. A lock already behind, the reporter's among them, is sent back at the next bun command magpie runs that changes plugins/, as package.json's tarball is in plugins/dist: pruneDist never takes one package.json names. What was run (tree 5ae0b4a1, on origin/main4e5b2ad8, go1.26.8, linux/amd64): - TestDistLockBehindPackageJSONIsMended (new), with real Bun: autoclaw and codearts installed from plugins/dist at 0.2.1 and 0.2.0, then at 0.2.6 and 0.2.1 (the reporter's), and the lock's packages given back the entries Bun wrote for the first ones; with Bun's copies kept, with them gone, with the older tarballs put back in plugins/dist, as the commit before keeps them, and with the copies gone and plugins#405's tail after package.json's object. From each state an unrelated plugin's add (zcode) and then its removal succeed and leave both packages at package.json's versions, in node_modules and alone in the lock, and package.json's specs for both as they were; the add's prune takes the older tarballs. With the change the TestDist tests are ok on each Bun, and the two new ones at -count=10 on 1.4.2 and 1.4.4. Broken by hand, each put back after, with Bun 1.4.2, 1.4.3 and 1.4.4 (outside graftcp, the proxy wrapper this box runs commands in, umask 077, logs kept): - no mendDist: every case red on each, the older versions back with the copies or the older tarballs kept (`is at "0.2.1", want 0.2.6`), the add failing with "ENOENT extracting tarball from" both packages with the copies gone; - mendDist before install's add only: every case red at the removal on each, the older versions back, or with the copies gone package.json still naming zcode after Remove; - one `bun update` a package: both copies-gone cases red on each, the add failing with "ENOENT extracting tarball from" both packages, as with no mendDist (each update alone fails on the other's gone tarball); - `bun add` of package.json's tarballs instead of `bun update`: every case red on 1.4.2 and 1.4.3, ok on 1.4.4; - the trigger as first written (the lock's tarball gone): the tarballs-kept case red on each, the older versions back (`is at "0.2.1", want 0.2.6`); - mendDist before package.json's tail is cut: in install, the tail case red on each at the add, "ENOENT extracting tarball from" both packages; in runBun, red on each at the removal, package.json still naming zcode after Remove. - internal/plugin, -tags nogui, with Bun 1.4.4, the one magpie runs now: ok but TestPluginSOCKSProxy, red the same at origin/main7a60e78c(#2207, open, filed for 1.4.3); TestFlowBigReplyProd and TestRenewOtherHost skip by their own condition. - gofmt -l lists nothing; GOOS=linux, windows and darwin go vet -tags nogui ./..., GOOS=windows go vet ./..., GOOS=linux and darwin go build -tags nogui ./... and GOOS=windows go build ./...: exit 0 each. go vet and go build without the tag on linux: exit 1 (no GTK, WebKitGTK or fontconfig headers on this box). - Full suite, -tags nogui (CI's Linux job tags gtk3), outside graftcp (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout 45m, log kept): 65 ok, internal/plugin among them, and 7 red, none in what this changes: - agent TestCodexDriftReached: #3398; TestInstalledVersion, TestHowInstalled, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir: #3141; TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout: this box's /tmp/.git, #3641. - davsync TestSync and TestSyncTeraCloudStillSeesOtherWrites: #3420. - edit TestWriteAtomicReadOnlyHardLink: this box's umask 077, #3640. - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git, #3675. - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie: this box's millions of open fds, #3649. - provider: "fatal error: close of synctest channel from outside bubble" in TestUnreadLoginsWriteErrorsKeepCredentials, main's CI red, #3632; the package's tests after it didn't run. - testenv TestProgram: this box's umask 077, #3640. Not tried: Windows, where the reporter is (CI's Windows job runs none of these tests: it compiles them and runs a named few); the reporter's own plugins/ folder; the step that left their lock's packages at the tarballs before while package.json and the workspaces moved on. On Linux each Bun from 1.4.2 to 1.4.4 writes the packages at the tarball an add names, so the test makes that state by hand. Review (macOS arm64, go1.26.8, squashed into one commit per issue, on origin/main1cbdda25b), plus the class plugins#405 belongs to, a file here written over a longer one without truncating: - bun.lock: real Bun 1.4.4 reads the lock's first object and goes on past bytes after it (a lock with an older lock's packages appended: `bun install` checked 1 install, no changes, tail left as it was). So a tail doesn't stop Bun, but pruneDist's and mendDist's regexp read the whole file: the tail's older tarball was kept, and sent to bun update before every command. Both now read lockHead, the first object, braces and quotes inside strings skipped. TestDistLockTailIsntRead (bun update run for a lock whose object is behind, not for one whose tail is, nor one with "}\"{]" in a string) and a TestDistTarballTheLockNamesStays case (the tail's tarball goes). Broken by hand: lockHead returning the whole file turned the tail case and both tail subtests red ("0.2.1 kept", "bun update run: true"); escapes not skipped turned the braces-in-strings subtest red. - package.json: every magpie write of it is temp+rename (mendPackageJSON sincea9242eb68) but the first one install makes, an os.WriteFile; that goes through edit.WriteAtomic now. bun's own writes are bun's. - TestDist* ok on Bun 1.4.2, 1.4.3 and 1.4.4; broken by hand with 1.4.4: mendDist's calls removed turned TestDistLockBehindPackageJSONIsMended red, and the old pruneDist turned TestDistTarballTheLockNamesStays red ("taken out"). - internal/plugin whole package ok (144s); gofmt clean; vet ok for linux nogui, windows, darwin nogui. Not tried: Windows, where both reports come from.
1017 lines
32 KiB
Go
1017 lines
32 KiB
Go
// Package plugin runs OpenCode's provider plugins for magpie.
|
|
//
|
|
// A subscription a vendor may object to being used outside its own app is
|
|
// not built into magpie: a plugin signs in to it and makes its requests,
|
|
// the way OpenCode's plugins do (opencode-gemini-auth and the like), and
|
|
// magpie runs those same plugins. They are npm packages (or local files)
|
|
// written against OpenCode's plugin API (@opencode-ai/plugin, v1): their
|
|
// auth hook gives the sign-in methods and a loader whose fetch carries the
|
|
// requests, their config and provider hooks the models. magpie runs them
|
|
// under Bun, downloaded the first time a plugin is added, in one process
|
|
// (host.js) it talks to over stdin and stdout.
|
|
//
|
|
// What magpie keeps: plugins.json, the plugins and the config handed to
|
|
// them (OpenCode's, its provider section: a plugin's options); the npm
|
|
// packages under plugins/; plugin-auth.json, the sign-ins, in OpenCode's
|
|
// auth.json shape (so one can be moved between the two).
|
|
package plugin
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/filememo"
|
|
"github.com/yetone/magpie/internal/lastgood"
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/steady"
|
|
"github.com/yetone/magpie/internal/update"
|
|
)
|
|
|
|
// Entry is one plugin the user added.
|
|
type Entry struct {
|
|
// Spec is how it was added: an npm package (name, name@version), a git
|
|
// repository (github:owner/repo, git+https://…) or a path to a file
|
|
// or folder.
|
|
Spec string `json:"spec"`
|
|
// Off is set on a plugin the user turned off.
|
|
Off bool `json:"off,omitempty"`
|
|
// Options are handed to the plugin as its second argument, as
|
|
// OpenCode's ["name", {options}] does.
|
|
Options map[string]any `json:"options,omitempty"`
|
|
}
|
|
|
|
// List is plugins.json.
|
|
type List struct {
|
|
Plugins []Entry `json:"plugins"`
|
|
// Config is OpenCode's config the plugins are handed: its provider
|
|
// section says what a plugin reads of it
|
|
// (provider.google.options.projectId).
|
|
Config map[string]any `json:"config,omitempty"`
|
|
// Prefer is the plugin the user picked for a provider id more than
|
|
// one plugin signs in to (a plugin of their own beside a third
|
|
// party's): provider id → that plugin's spec. Without it the last of
|
|
// them in Plugins serves the id, as in OpenCode.
|
|
Prefer map[string]string `json:"prefer,omitempty"`
|
|
}
|
|
|
|
var listMu sync.Mutex
|
|
|
|
// bunMu is held through each bun command that changes plugins/ (add,
|
|
// update, remove). Two at once read package.json and bun.lock before
|
|
// either writes them, so one's change is lost (#2002, anganing: after an
|
|
// update, the hourly plugin updates and the moved subscriptions' ran
|
|
// together at start). The dependency a git plugin is found by is then
|
|
// gone from package.json, and the plugin can't be loaded.
|
|
var installMu sync.Mutex
|
|
|
|
// runBun runs a bun command that changes plugins/, one at a time.
|
|
func runBun(ctx context.Context, bun string, args ...string) ([]byte, error) {
|
|
installMu.Lock()
|
|
defer installMu.Unlock()
|
|
mendPackageJSON()
|
|
defer mendPackageJSON()
|
|
mendDist(ctx, bun)
|
|
return bunCommand(ctx, bun, Dir(), args...).CombinedOutput()
|
|
}
|
|
|
|
// mendPackageJSON cuts what follows plugins/package.json's object off. Bun
|
|
// on Windows can write a shorter package.json over a longer one without
|
|
// truncating it (klmno1993, plugins#405: a bun remove left the old file's
|
|
// last 56 bytes after the closing brace), and every bun add after it
|
|
// fails on bun's "failed to read/parse package.json", whatever
|
|
// the plugin. The object is kept as written; the whole file is kept as
|
|
// package.json.broken. A file whose start isn't an object is left alone.
|
|
// installMu held.
|
|
func mendPackageJSON() {
|
|
pj := filepath.Join(Dir(), "package.json")
|
|
b, err := os.ReadFile(pj)
|
|
if err != nil || json.Valid(b) {
|
|
return
|
|
}
|
|
d := json.NewDecoder(bytes.NewReader(b))
|
|
var obj map[string]any
|
|
if d.Decode(&obj) != nil || obj == nil {
|
|
return
|
|
}
|
|
head := append(bytes.Clone(bytes.TrimRight(b[:d.InputOffset()], " \t\r\n")), '\n')
|
|
if !json.Valid(head) {
|
|
return
|
|
}
|
|
_ = os.WriteFile(pj+".broken", b, 0o644)
|
|
_ = edit.WriteAtomic(pj, head)
|
|
}
|
|
|
|
// Dir is the folder magpie installs npm plugins into.
|
|
func Dir() string { return filepath.Join(settings.Dir(), "plugins") }
|
|
|
|
// AuthPath is the plugins' sign-ins, OpenCode's auth.json in shape.
|
|
func AuthPath() string { return filepath.Join(settings.Dir(), "plugin-auth.json") }
|
|
|
|
// authLockStale is how long plugin-auth.json.lock is held at most: one
|
|
// older was left by a host or a magpie that died holding it.
|
|
const authLockStale = 10 * time.Second
|
|
|
|
// lockAuth takes plugin-auth.json.lock, which host.js takes too, for a
|
|
// change to plugin-auth.json read afresh under it: two hosts (one being
|
|
// restarted) and magpie never write each other's accounts away. It gives
|
|
// the unlock.
|
|
func lockAuth() func() {
|
|
lock := AuthPath() + ".lock"
|
|
start := time.Now()
|
|
for {
|
|
f, err := os.OpenFile(lock, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
|
if err == nil {
|
|
f.Close()
|
|
return func() { os.Remove(lock) }
|
|
}
|
|
if !os.IsExist(err) && !os.IsPermission(err) {
|
|
return func() {} // no folder to lock in: no file to change either
|
|
}
|
|
if fi, err := os.Stat(lock); err == nil && time.Since(fi.ModTime()) > authLockStale || time.Since(start) > 2*authLockStale {
|
|
os.Remove(lock)
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func listPath() string { return filepath.Join(settings.Dir(), "plugins.json") }
|
|
|
|
// ListPath is plugins.json's path.
|
|
func ListPath() string { return listPath() }
|
|
|
|
// pluginsList is plugins.json as last read, with the bytes it was parsed
|
|
// from. The file is read every time: an edit that keeps the file's size and
|
|
// time (a swap of two accounts of one length, a replaced file whose time is
|
|
// put back) would be missed by a stamp, and only the parse is reused.
|
|
var pluginsList struct {
|
|
sync.Mutex
|
|
path string
|
|
raw []byte
|
|
list List
|
|
}
|
|
|
|
// list is plugins.json, read now. What it gives is shared and must not be
|
|
// changed; Load gives the mutable copy.
|
|
func list() List {
|
|
path := listPath()
|
|
// one a crash left all zero (#1505) is its last good generation: the
|
|
// plugins installed are unknown, never none
|
|
b, err := lastgood.Read(path, lastgood.JSON)
|
|
if err != nil {
|
|
// Do not cache read failures; a later call will read the file again.
|
|
return List{}
|
|
}
|
|
pluginsList.Lock()
|
|
defer pluginsList.Unlock()
|
|
if pluginsList.path == path && bytes.Equal(pluginsList.raw, b) {
|
|
return pluginsList.list
|
|
}
|
|
var l List
|
|
// Preserve partially decoded values on type errors, as Load has always done.
|
|
_ = json.Unmarshal(b, &l)
|
|
pluginsList.path, pluginsList.raw, pluginsList.list = path, b, l
|
|
return l
|
|
}
|
|
|
|
// Load reads plugins.json. What it gives is the caller's to change at every
|
|
// level: the mutators (Add, Remove, SetOff, SetConfig) each get their own
|
|
// copy, so nothing they change reaches what list keeps.
|
|
func Load() List {
|
|
l := list()
|
|
return List{Plugins: clonePlugins(l.Plugins), Config: cloneMap(l.Config), Prefer: maps.Clone(l.Prefer)}
|
|
}
|
|
|
|
// clonePlugins copies the entries, their options with them.
|
|
func clonePlugins(es []Entry) []Entry {
|
|
if es == nil {
|
|
return nil
|
|
}
|
|
out := make([]Entry, len(es))
|
|
for i, e := range es {
|
|
out[i] = e
|
|
out[i].Options = cloneMap(e.Options)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// cloneMap copies a JSON object and everything under it.
|
|
func cloneMap(m map[string]any) map[string]any {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
out := make(map[string]any, len(m))
|
|
for k, v := range m {
|
|
out[k] = cloneJSON(v)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// cloneJSON copies a JSON value: Unmarshal into any gives only objects,
|
|
// arrays, strings, numbers, bools and nil, so this reaches every part of one.
|
|
func cloneJSON(v any) any {
|
|
switch x := v.(type) {
|
|
case map[string]any:
|
|
return cloneMap(x)
|
|
case []any:
|
|
out := make([]any, len(x))
|
|
for i, e := range x {
|
|
out[i] = cloneJSON(e)
|
|
}
|
|
return out
|
|
default:
|
|
return v
|
|
}
|
|
}
|
|
|
|
// writeWhole writes b to p by a rename, so a magpie or the host reading
|
|
// p meanwhile reads the old file or the new one, never one half-written,
|
|
// and flushed to the disk first, so a machine that goes down meanwhile
|
|
// doesn't leave p all zero (#1505); read back with steady.ReadFile, which
|
|
// waits out the rename on Windows.
|
|
func writeWhole(p string, b []byte) error {
|
|
return steady.WriteFile(p, b, 0o600)
|
|
}
|
|
|
|
func save(l List) error {
|
|
b, err := json.MarshalIndent(l, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(settings.Dir(), 0o700); err != nil {
|
|
return err
|
|
}
|
|
if err := lastgood.Keep(listPath(), lastgood.JSON, 0o600); err != nil {
|
|
return err
|
|
}
|
|
defer filememo.Forget() // what follows plugins.json looks again (Written)
|
|
if err := writeWhole(listPath(), append(b, '\n')); err != nil {
|
|
return err
|
|
}
|
|
listSeen.Lock()
|
|
listSeen.stamp, listSeen.set = listStamp(), true
|
|
listSeen.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// listSeen is plugins.json as this magpie last wrote or read it.
|
|
var listSeen struct {
|
|
sync.Mutex
|
|
stamp string
|
|
set bool
|
|
}
|
|
|
|
// listStamp is plugins.json as it is now, and what is installed: bun's
|
|
// package.json and lockfile in Dir(), which `magpie plugin update` changes
|
|
// without touching plugins.json (#952: a terminal's update to 0.1.18 left
|
|
// the app's host answering with 0.1.17 until it was killed).
|
|
func listStamp() string {
|
|
var b strings.Builder
|
|
for _, p := range []string{listPath(), filepath.Join(Dir(), "package.json"), filepath.Join(Dir(), "bun.lock"), filepath.Join(Dir(), "bun.lockb")} {
|
|
if fi, err := os.Stat(p); err == nil {
|
|
fmt.Fprint(&b, fi.ModTime().UnixNano(), " ", fi.Size())
|
|
}
|
|
b.WriteString(";")
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// hostStale is set when another magpie changed the plugins (magpie plugin
|
|
// add, remove, update or move in a terminal while the app runs): the host
|
|
// running has the old ones loaded.
|
|
var hostStale atomic.Bool
|
|
|
|
// checkList notices plugins.json changed by another magpie: the host is
|
|
// started again with the plugins as they are, and the providers the other
|
|
// magpie last saw asked for are the ones known meanwhile.
|
|
func checkList() {
|
|
st := listStamp()
|
|
listSeen.Lock()
|
|
moved := listSeen.set && listSeen.stamp != st
|
|
listSeen.stamp, listSeen.set = st, true
|
|
listSeen.Unlock()
|
|
if !moved {
|
|
return
|
|
}
|
|
provMu.Lock()
|
|
provCache = nil
|
|
provMu.Unlock()
|
|
hostStale.Store(true)
|
|
changed()
|
|
}
|
|
|
|
// IsPath is whether spec names a file or folder rather than a package,
|
|
// as OpenCode tells them apart. A path rooted the way any system roots
|
|
// one counts on every system, since plugins.json travels between
|
|
// machines in a backup or a sync: a leading / or \ (a UNC path among
|
|
// them) on Windows as well, and a drive letter off it (#2229).
|
|
func IsPath(spec string) bool {
|
|
return strings.HasPrefix(spec, "file://") || strings.HasPrefix(spec, ".") || strings.HasPrefix(spec, "/") || strings.HasPrefix(spec, `\`) || filepath.IsAbs(spec) || driveRooted.MatchString(spec)
|
|
}
|
|
|
|
var driveRooted = regexp.MustCompile(`^[A-Za-z]:[\\/]`)
|
|
|
|
var pkgName = regexp.MustCompile(`^(@[a-z0-9][a-z0-9._~-]*/)?[a-z0-9][a-z0-9._~-]*$`)
|
|
|
|
// gitSpec is a package bun fetches from a git repository, not npm:
|
|
// github:owner/repo[#ref] (gitlab: and bitbucket: alike), a git+https://,
|
|
// git+ssh://, git+file:// or git:// URL, a GitHub, GitLab or Bitbucket
|
|
// page's URL, or owner/repo, GitHub's shorthand.
|
|
var gitSpec = regexp.MustCompile(`^(?:(?:github|gitlab|bitbucket):|git\+[a-z]+://|git://)\S+$|^https?://(?:www\.)?(?:github\.com|gitlab\.com|bitbucket\.org)/\S+$|^[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9_.-]+(?:#\S+)?$`)
|
|
|
|
// IsGit is whether spec names a git repository rather than an npm package
|
|
// or a path. The package bun installs from it is named by its own
|
|
// package.json, not by spec.
|
|
func IsGit(spec string) bool { return !IsPath(spec) && gitSpec.MatchString(spec) }
|
|
|
|
// gitName is the package bun installed from the git spec: the dependency
|
|
// of plugins/package.json bun added it as (it keeps spec as given), ""
|
|
// when there is none.
|
|
func gitName(spec string) string {
|
|
var pj struct {
|
|
Dependencies map[string]string `json:"dependencies"`
|
|
}
|
|
b, err := os.ReadFile(filepath.Join(Dir(), "package.json"))
|
|
if err != nil || json.Unmarshal(b, &pj) != nil {
|
|
return ""
|
|
}
|
|
for name, s := range pj.Dependencies {
|
|
if s == spec {
|
|
return name
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Name is the package spec names, without its version: "@scope/x" of
|
|
// "@scope/x@1.2", the package installed from a git repository (the spec
|
|
// itself until it is), or the path.
|
|
func Name(spec string) string {
|
|
if IsPath(spec) {
|
|
return spec
|
|
}
|
|
if IsGit(spec) {
|
|
if n := gitName(spec); n != "" {
|
|
return n
|
|
}
|
|
return spec
|
|
}
|
|
at := strings.LastIndex(spec, "@")
|
|
if at > 0 {
|
|
return spec[:at]
|
|
}
|
|
return spec
|
|
}
|
|
|
|
// Target is where the plugin is to load from: the installed package's
|
|
// folder, or the path.
|
|
func Target(spec string) string {
|
|
if IsPath(spec) {
|
|
p := strings.TrimPrefix(spec, "file://")
|
|
if abs, err := filepath.Abs(p); err == nil {
|
|
return abs
|
|
}
|
|
return p
|
|
}
|
|
return filepath.Join(Dir(), "node_modules", filepath.FromSlash(Name(spec)))
|
|
}
|
|
|
|
// Add installs a plugin and adds it to the list, in place of one of the
|
|
// same package. A package is installed with its scripts left unrun. A
|
|
// community plugin's short or shown name (workbuddy, WorkBuddy) is its
|
|
// package (resolve); the entry gives the package installed.
|
|
func Add(ctx context.Context, spec string) (Entry, error) {
|
|
spec, err := resolve(ctx, strings.TrimSpace(spec))
|
|
if err != nil {
|
|
return Entry{}, err
|
|
}
|
|
return add(ctx, spec, "")
|
|
}
|
|
|
|
// AddExact is Add of a spec that is a package's own (another computer's
|
|
// plugins.json), installed as written, never taken for a name.
|
|
func AddExact(ctx context.Context, spec string) (Entry, error) { return add(ctx, spec, "") }
|
|
|
|
// add is Add, an npm package installed at the version given when there is
|
|
// one (installAt), its spec kept as given.
|
|
func add(ctx context.Context, spec, version string) (Entry, error) {
|
|
spec = strings.TrimSpace(spec)
|
|
if spec == "" {
|
|
return Entry{}, errors.New("no plugin given")
|
|
}
|
|
// the package each plugin is before the install, which may take a git
|
|
// one's name over (one repository in place of another, or of npm's)
|
|
was := map[string]string{}
|
|
for _, x := range Load().Plugins {
|
|
was[x.Spec] = Name(x.Spec)
|
|
}
|
|
if IsPath(spec) {
|
|
t := Target(spec)
|
|
if _, err := os.Stat(t); err != nil {
|
|
return Entry{}, err
|
|
}
|
|
spec = t
|
|
} else if IsGit(spec) {
|
|
if err := install(ctx, spec); err != nil {
|
|
return Entry{}, err
|
|
}
|
|
if Name(spec) == spec {
|
|
return Entry{}, fmt.Errorf("bun installed %s but plugins/package.json doesn't list it", spec)
|
|
}
|
|
} else {
|
|
if !pkgName.MatchString(Name(spec)) {
|
|
return Entry{}, fmt.Errorf("%q isn't an npm package name", spec)
|
|
}
|
|
if Name(spec) == spec {
|
|
spec += "@latest"
|
|
}
|
|
if version != "" {
|
|
if err := installAt(ctx, Name(spec), version); err != nil {
|
|
return Entry{}, err
|
|
}
|
|
} else if err := install(ctx, spec); err != nil {
|
|
return Entry{}, err
|
|
}
|
|
}
|
|
if err := notPlugin(Target(spec)); err != nil {
|
|
// installed just now for this, and nothing loads it: taken out again
|
|
if !IsPath(spec) && !slices.Contains(slices.Collect(maps.Values(was)), Name(spec)) {
|
|
if bun, berr := Bun(ctx); berr == nil {
|
|
_, _ = runBun(ctx, bun, "remove", "--ignore-scripts", Name(spec))
|
|
}
|
|
}
|
|
return Entry{}, err
|
|
}
|
|
if err := ensurePi(ctx, Target(spec)); err != nil {
|
|
return Entry{}, err
|
|
}
|
|
listMu.Lock()
|
|
defer listMu.Unlock()
|
|
l := Load()
|
|
e := Entry{Spec: spec}
|
|
name := Name(spec)
|
|
if i := slices.IndexFunc(l.Plugins, func(x Entry) bool {
|
|
n, ok := was[x.Spec]
|
|
if !ok {
|
|
n = Name(x.Spec)
|
|
}
|
|
return x.Spec == spec || n == name
|
|
}); i >= 0 {
|
|
e.Options = l.Plugins[i].Options
|
|
// whether they turned it off, which an add never changes
|
|
e.Off = l.Plugins[i].Off
|
|
// a provider id the user picked it for stays its (another version,
|
|
// or its package from a git repository in place of npm's)
|
|
for id, s := range l.Prefer {
|
|
if s == l.Plugins[i].Spec {
|
|
l.Prefer[id] = spec
|
|
}
|
|
}
|
|
l.Plugins[i] = e
|
|
} else {
|
|
l.Plugins = append(l.Plugins, e)
|
|
}
|
|
if err := save(l); err != nil {
|
|
return Entry{}, err
|
|
}
|
|
Restart()
|
|
return e, nil
|
|
}
|
|
|
|
// indexFiles are the files a package that names none loads, as the host
|
|
// looks for them (host.js's INDEX_FILES).
|
|
var indexFiles = []string{"index.ts", "index.tsx", "index.js", "index.mjs", "index.cjs"}
|
|
|
|
// notPlugin says why the plugin at target is nothing the host can load,
|
|
// or nil: a package that names no file to import (main, exports), has no
|
|
// index file, and is neither pi's nor a middleware — a command alone
|
|
// (bin), an MCP server like magpie-x-search, is installed fine and then
|
|
// never loads (#1327).
|
|
func notPlugin(target string) error {
|
|
st, err := os.Stat(target)
|
|
if err != nil || !st.IsDir() || IsPi(target) {
|
|
return nil
|
|
}
|
|
if Gateway(target) != "" {
|
|
return nil
|
|
}
|
|
if f, _ := Middleware(target); f != "" {
|
|
return nil
|
|
}
|
|
if f, _ := Agent(target); f != "" {
|
|
return nil
|
|
}
|
|
if w, _ := Window(target); w != nil {
|
|
return nil
|
|
}
|
|
var pkg struct {
|
|
Name string `json:"name"`
|
|
Main string `json:"main"`
|
|
Exports json.RawMessage `json:"exports"`
|
|
Bin json.RawMessage `json:"bin"`
|
|
}
|
|
b, err := os.ReadFile(filepath.Join(target, "package.json"))
|
|
if err != nil || json.Unmarshal(b, &pkg) != nil {
|
|
return nil
|
|
}
|
|
if strings.TrimSpace(pkg.Main) != "" || len(pkg.Exports) > 0 && string(pkg.Exports) != "null" {
|
|
return nil
|
|
}
|
|
for _, f := range indexFiles {
|
|
if _, err := os.Stat(filepath.Join(target, f)); err == nil {
|
|
return nil
|
|
}
|
|
}
|
|
name := pkg.Name
|
|
if name == "" {
|
|
name = filepath.Base(target)
|
|
}
|
|
why := fmt.Sprintf("%s isn't an OpenCode or magpie plugin: its package names no file to load (no main or exports in package.json, no index file)", name)
|
|
if len(pkg.Bin) > 0 && string(pkg.Bin) != "null" {
|
|
why += ", only a command (bin). If it is an MCP server, add it under Library → MCP servers instead"
|
|
}
|
|
return errors.New(why)
|
|
}
|
|
|
|
// Update installs npm's newest version (asked now, installAt) of each npm
|
|
// plugin, and fetches each git one again. A plugin pinned to a version
|
|
// that npm has a newer one of is updated too, and unpinned, as its row's
|
|
// Update does: the page counts it in Update all, and the CLI's update
|
|
// says it installs the newest of each (ARNO on Discord: a plugin updated
|
|
// with Update all still said an update was out, pinned where it was). A
|
|
// pinned one already at npm's newest stays as its spec says.
|
|
func Update(ctx context.Context) error {
|
|
var errs []error
|
|
for _, e := range Load().Plugins {
|
|
if !IsPath(e.Spec) {
|
|
v := ""
|
|
if !IsGit(e.Spec) {
|
|
v = newestOf(ctx, Name(e.Spec))
|
|
}
|
|
var err error
|
|
switch {
|
|
case v != "" && Pinned(e.Spec):
|
|
if update.Newer(v, Installed(e.Spec)) {
|
|
_, err = add(ctx, Name(e.Spec), v)
|
|
} else {
|
|
err = reinstall(ctx, e.Spec)
|
|
}
|
|
case v != "":
|
|
err = installAt(ctx, Name(e.Spec), v)
|
|
default:
|
|
// a git one, or npm not answering: as its spec says
|
|
err = reinstall(ctx, e.Spec)
|
|
}
|
|
if err != nil {
|
|
errs = append(errs, fmt.Errorf("%s: %w", e.Spec, err))
|
|
continue
|
|
}
|
|
}
|
|
if err := ensurePi(ctx, Target(e.Spec)); err != nil {
|
|
errs = append(errs, fmt.Errorf("%s: %w", e.Spec, err))
|
|
}
|
|
}
|
|
Restart()
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
// indexOf is the plugin name means in l: the one added as name exactly,
|
|
// else the one of that package. A plugin of the user's own (a folder)
|
|
// beside a third party's is each told apart by its own spec, so taking
|
|
// one away or switching it off never reaches the other.
|
|
func indexOf(l List, name string) int {
|
|
if i := slices.IndexFunc(l.Plugins, func(x Entry) bool { return x.Spec == name }); i >= 0 {
|
|
return i
|
|
}
|
|
return slices.IndexFunc(l.Plugins, func(x Entry) bool { return Name(x.Spec) == name })
|
|
}
|
|
|
|
// Remove takes a plugin off the list (and out of plugins/).
|
|
func Remove(ctx context.Context, name string) error {
|
|
listMu.Lock()
|
|
l := Load()
|
|
i := indexOf(l, name)
|
|
if i < 0 {
|
|
listMu.Unlock()
|
|
return fmt.Errorf("no plugin %q", name)
|
|
}
|
|
e := l.Plugins[i]
|
|
l.Plugins = slices.Delete(l.Plugins, i, i+1)
|
|
maps.DeleteFunc(l.Prefer, func(_, s string) bool { return s == e.Spec })
|
|
err := save(l)
|
|
listMu.Unlock()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !IsPath(e.Spec) {
|
|
if bun, err := Bun(ctx); err == nil {
|
|
_, _ = runBun(ctx, bun, "remove", "--ignore-scripts", Name(e.Spec))
|
|
}
|
|
}
|
|
Restart()
|
|
return nil
|
|
}
|
|
|
|
// SetOff turns a plugin off or back on.
|
|
func SetOff(name string, off bool) error {
|
|
listMu.Lock()
|
|
defer listMu.Unlock()
|
|
l := Load()
|
|
i := indexOf(l, name)
|
|
if i < 0 {
|
|
return fmt.Errorf("no plugin %q", name)
|
|
}
|
|
l.Plugins[i].Off = off
|
|
if err := save(l); err != nil {
|
|
return err
|
|
}
|
|
Restart()
|
|
return nil
|
|
}
|
|
|
|
// Prefer has the plugin named serve provider id, which another plugin
|
|
// signs in to as well (Clash): the host runs it for id from now on, the
|
|
// other one keeping its other providers. A plugin removed takes its picks
|
|
// with it; one switched off leaves the id to the others until it is on
|
|
// again.
|
|
func Prefer(name, id string) error {
|
|
listMu.Lock()
|
|
defer listMu.Unlock()
|
|
l := Load()
|
|
i := indexOf(l, name)
|
|
if i < 0 {
|
|
return fmt.Errorf("no plugin %q", name)
|
|
}
|
|
if id == "" {
|
|
return errors.New("no provider given")
|
|
}
|
|
if l.Prefer == nil {
|
|
l.Prefer = map[string]string{}
|
|
}
|
|
l.Prefer[id] = l.Plugins[i].Spec
|
|
if err := save(l); err != nil {
|
|
return err
|
|
}
|
|
Restart()
|
|
return nil
|
|
}
|
|
|
|
// SetOptions sets the options a plugin is handed (a middleware's
|
|
// ctx.options); nil takes them away.
|
|
func SetOptions(name string, opts map[string]any) error {
|
|
listMu.Lock()
|
|
defer listMu.Unlock()
|
|
l := Load()
|
|
i := indexOf(l, name)
|
|
if i < 0 {
|
|
// a short name, as the community's READMEs write it: param-override
|
|
// for @magpie-community/middleware-param-override
|
|
i = slices.IndexFunc(l.Plugins, func(x Entry) bool { return Called(Name(x.Spec), name) })
|
|
}
|
|
if i < 0 {
|
|
return fmt.Errorf("no plugin %q", name)
|
|
}
|
|
if len(opts) == 0 {
|
|
opts = nil
|
|
}
|
|
l.Plugins[i].Options = opts
|
|
if err := save(l); err != nil {
|
|
return err
|
|
}
|
|
Restart()
|
|
return nil
|
|
}
|
|
|
|
// ShortName is a package's name without its scope and the words every
|
|
// package of its kind has: model-map for @magpie-community/middleware-model-map,
|
|
// zed for @magpie-community/opencode-zed-auth, calc for
|
|
// @magpie-community/tools-calc (so its tools are calc_eval, as from its
|
|
// folder). A plugin added from a folder is named by its path, which on
|
|
// Windows ends after a backslash.
|
|
func ShortName(pkg string) string {
|
|
pkg = bareName(pkg)
|
|
for _, kind := range []string{"middleware-", "opencode-", "tools-", "page-", "agent-"} {
|
|
if strings.HasPrefix(pkg, kind) && len(pkg) > len(kind) {
|
|
pkg = pkg[len(kind):]
|
|
break
|
|
}
|
|
}
|
|
return strings.TrimSuffix(pkg, "-auth")
|
|
}
|
|
|
|
// bareName is a package's name without its scope, or a folder's last part.
|
|
func bareName(pkg string) string {
|
|
if i := strings.LastIndexAny(pkg, `/\`); i >= 0 {
|
|
pkg = pkg[i+1:]
|
|
}
|
|
return pkg
|
|
}
|
|
|
|
// Called says whether a user's name for a plugin names this package: its
|
|
// short name, or its name without the scope, which was its short name
|
|
// before tools-, page- and agent- were taken off (agent-aider).
|
|
func Called(pkg, name string) bool {
|
|
return name != "" && (ShortName(pkg) == name || bareName(pkg) == name)
|
|
}
|
|
|
|
// install puts an npm package into plugins/ with bun add, its scripts
|
|
// left unrun as OpenCode leaves them.
|
|
func install(ctx context.Context, spec string) error {
|
|
bun, err := Bun(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(Dir(), 0o755); err != nil {
|
|
return err
|
|
}
|
|
// the package.json made for a first plugin is made under the lock
|
|
// too: written while another bun add writes its own, it would take
|
|
// every plugin out of it
|
|
installMu.Lock()
|
|
pj := filepath.Join(Dir(), "package.json")
|
|
if _, err := os.Stat(pj); err != nil {
|
|
// through temp and rename, as every write of it: a write cut
|
|
// short would leave bun a package.json it can't read
|
|
if err := edit.WriteAtomic(pj, []byte("{\n \"name\": \"magpie-plugins\",\n \"private\": true\n}\n")); err != nil {
|
|
installMu.Unlock()
|
|
return err
|
|
}
|
|
}
|
|
defer installMu.Unlock()
|
|
mendPackageJSON()
|
|
defer mendPackageJSON()
|
|
mendDist(ctx, bun)
|
|
err = bunAdd(ctx, bun, spec)
|
|
// a @magpie-community package or version npm doesn't have (a new
|
|
// one waits for an owner to publish it there) comes from the
|
|
// community repo's dist branch; one Bun's minimumReleaseAge holds
|
|
// back stays held, as the user's bunfig asks, and a bun this machine
|
|
// can't run at all can't add it from there either
|
|
var ue *BunUnstartableError
|
|
if name, version, ok := distWanted(spec); ok && err != nil && !minAge.MatchString(err.Error()) && !errors.As(err, &ue) {
|
|
if derr := installDist(ctx, bun, name, version); derr != nil {
|
|
return fmt.Errorf("%w (and from the community repo's dist branch: %v)", err, derr)
|
|
}
|
|
err = nil
|
|
}
|
|
if err == nil {
|
|
pruneDist()
|
|
pruneInstallCache()
|
|
}
|
|
return err
|
|
}
|
|
|
|
// bunAdd adds spec to plugins/ with bun, its scripts left unrun.
|
|
// installMu held.
|
|
func bunAdd(ctx context.Context, bun, spec string) error {
|
|
out, err := bunCommand(ctx, bun, Dir(), "add", "--ignore-scripts", spec).CombinedOutput()
|
|
if err != nil {
|
|
if hint := missingLibs(string(out)); hint != "" {
|
|
return fmt.Errorf("bun add %s: %s: %w", spec, hint, err)
|
|
}
|
|
if ue := wontLoad(bun, err); ue != nil {
|
|
return fmt.Errorf("bun add %s: %w", spec, ue)
|
|
}
|
|
return fmt.Errorf("bun add %s: %v: %s", spec, cantStart(bun, err), lastLines(string(out), 6))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// installAt installs the version of the npm package given, not a tag, and
|
|
// checks it is the one installed. Asked for pkg@latest, bun resolves
|
|
// "latest" by its own config: a minimumReleaseAge in the user's bunfig
|
|
// holds back every version younger than it, and a registry of the user's
|
|
// own (an .npmrc) may not have the newest yet. Either way bun installs an
|
|
// older version and exits 0, so an update said it was done and nothing
|
|
// changed (sweanng424 on Discord: Factory stayed at 0.1.18 with 0.1.21 out,
|
|
// through restarts and the hourly updates). Asked for the version, bun
|
|
// says why it can't install it.
|
|
func installAt(ctx context.Context, pkg, version string) error {
|
|
if err := install(ctx, pkg+"@"+version); err != nil {
|
|
// what bun said comes first: the page's status line shows the
|
|
// start of a long message
|
|
if m := minAge.FindStringSubmatch(err.Error()); m != nil {
|
|
return fmt.Errorf("%s %s isn't installed yet: Bun's minimumReleaseAge (in a .bunfig.toml) holds back versions published less than %s ago, and magpie installs it once it is older (%w)", pkg, version, ageText(m[1]), err)
|
|
}
|
|
var said []string
|
|
for _, l := range strings.Split(err.Error(), "\n") {
|
|
if l = strings.TrimSpace(l); strings.HasPrefix(l, "error:") {
|
|
said = append(said, strings.TrimSpace(strings.TrimPrefix(l, "error:")))
|
|
}
|
|
}
|
|
if len(said) > 0 {
|
|
return fmt.Errorf("%s %s isn't installed: %s (%w)", pkg, version, strings.Join(said, "; "), err)
|
|
}
|
|
return err
|
|
}
|
|
if got := Installed(pkg); got == "" || update.Newer(version, got) {
|
|
if got == "" {
|
|
got = "nothing"
|
|
}
|
|
return fmt.Errorf("bun add %s@%s installed %s", pkg, version, got)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// minAge is bun's word for a version its minimumReleaseAge holds back.
|
|
var minAge = regexp.MustCompile(`minimum-release-age: (\d+) seconds`)
|
|
|
|
// ageText is a number of seconds as a reader says it: 2 days, 12 hours.
|
|
func ageText(secs string) string {
|
|
n, err := strconv.Atoi(secs)
|
|
switch {
|
|
case err != nil:
|
|
return secs + " seconds"
|
|
case n >= 86400 && n%86400 == 0:
|
|
return plural(n/86400, "day")
|
|
case n >= 3600 && n%3600 == 0:
|
|
return plural(n/3600, "hour")
|
|
case n >= 60 && n%60 == 0:
|
|
return plural(n/60, "minute")
|
|
}
|
|
return plural(n, "second")
|
|
}
|
|
|
|
func plural(n int, unit string) string {
|
|
if n == 1 {
|
|
return "1 " + unit
|
|
}
|
|
return strconv.Itoa(n) + " " + unit + "s"
|
|
}
|
|
|
|
// newestOf is npm's newest version of the package, asked now, "" when npm
|
|
// didn't say; what it says is kept, as Info keeps it (tests stub it).
|
|
var newestOf = func(ctx context.Context, name string) string {
|
|
c, cancel := context.WithTimeout(ctx, checkEach)
|
|
info, err := npmAsk(c, name)
|
|
cancel()
|
|
if err == nil || errors.Is(err, errNotFound) {
|
|
npmMu.Lock()
|
|
npmCached()[name] = npmEntry{info, time.Now()}
|
|
saveNPM()
|
|
npmMu.Unlock()
|
|
}
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return info.Version
|
|
}
|
|
|
|
// piAgent is the package pi's extensions import pi from; the host loads
|
|
// them with it (pi.js), whichever of its names they import.
|
|
const piAgent = "@earendil-works/pi-coding-agent"
|
|
|
|
// IsPi is whether the plugin at target is pi's (a pi package or
|
|
// extension) rather than OpenCode's, as host.js's pi.js tells them.
|
|
func IsPi(target string) bool {
|
|
st, err := os.Stat(target)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if !st.IsDir() {
|
|
b, err := os.ReadFile(target)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
s := string(b)
|
|
for _, n := range []string{piAgent, "@mariozechner/pi-coding-agent"} {
|
|
if strings.Contains(s, `"`+n+`"`) || strings.Contains(s, `'`+n+`'`) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
b, err := os.ReadFile(filepath.Join(target, "package.json"))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
var pkg struct {
|
|
Pi any `json:"pi"`
|
|
Keywords []string `json:"keywords"`
|
|
Deps map[string]string `json:"dependencies"`
|
|
Peers map[string]string `json:"peerDependencies"`
|
|
}
|
|
if json.Unmarshal(b, &pkg) != nil {
|
|
return false
|
|
}
|
|
if _, ok := pkg.Pi.(map[string]any); ok || slices.Contains(pkg.Keywords, "pi-package") {
|
|
return true
|
|
}
|
|
for _, d := range []map[string]string{pkg.Deps, pkg.Peers} {
|
|
if _, ok := d[piAgent]; ok {
|
|
return true
|
|
}
|
|
if _, ok := d["@mariozechner/pi-coding-agent"]; ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ensurePi installs pi for a pi plugin that came without it (one that
|
|
// doesn't name it among what it depends on, or one on disk), as pi itself
|
|
// is what loads it.
|
|
func ensurePi(ctx context.Context, target string) error {
|
|
if !IsPi(target) {
|
|
return nil
|
|
}
|
|
if _, err := os.Stat(filepath.Join(Dir(), "node_modules", piAgent, "package.json")); err == nil {
|
|
return nil
|
|
}
|
|
if _, err := os.Stat(filepath.Join(target, "node_modules", piAgent, "package.json")); err == nil {
|
|
return nil
|
|
}
|
|
return install(ctx, piAgent)
|
|
}
|
|
|
|
// reinstall installs spec again: an npm one with bun add, a git one with
|
|
// bun update, which fetches its branch's (or tag's) commit now where bun
|
|
// add keeps the one bun.lock has.
|
|
func reinstall(ctx context.Context, spec string) error {
|
|
name := Name(spec)
|
|
if !IsGit(spec) || name == spec {
|
|
return install(ctx, spec)
|
|
}
|
|
bun, err := Bun(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out, err := runBun(ctx, bun, "update", "--ignore-scripts", name)
|
|
if err != nil {
|
|
if hint := missingLibs(string(out)); hint != "" {
|
|
return fmt.Errorf("bun update %s: %s: %w", name, hint, err)
|
|
}
|
|
if ue := wontLoad(bun, err); ue != nil {
|
|
return fmt.Errorf("bun update %s: %w", name, ue)
|
|
}
|
|
return fmt.Errorf("bun update %s: %v: %s", name, cantStart(bun, err), lastLines(string(out), 6))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func lastLines(s string, n int) string {
|
|
ls := strings.Split(strings.TrimSpace(s), "\n")
|
|
if len(ls) > n {
|
|
ls = ls[len(ls)-n:]
|
|
}
|
|
return strings.Join(ls, "\n")
|
|
}
|
|
|
|
func command(ctx context.Context, name string, args ...string) *exec.Cmd {
|
|
return proc.CommandContext(ctx, name, args...)
|
|
}
|
|
|
|
// env is the plugins' environment: magpie's, with its proxy.
|
|
func env() []string { return netproxy.Env(os.Environ()) }
|
|
|
|
// Version is the version of the package spec installed (or checked out
|
|
// at its path), "" when there is none.
|
|
func Version(spec string) string {
|
|
dir := Target(spec)
|
|
if fi, err := os.Stat(dir); err == nil && !fi.IsDir() {
|
|
dir = filepath.Dir(dir)
|
|
}
|
|
var pj struct {
|
|
Version string `json:"version"`
|
|
}
|
|
if b, err := os.ReadFile(filepath.Join(dir, "package.json")); err == nil && json.Unmarshal(b, &pj) == nil {
|
|
return pj.Version
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// PackageName is the npm package spec is: its name, or, for a plugin added
|
|
// from a folder or file (a checkout of it), the name its package.json gives.
|
|
func PackageName(spec string) string {
|
|
if !IsPath(spec) {
|
|
return Name(spec)
|
|
}
|
|
dir := Target(spec)
|
|
if fi, err := os.Stat(dir); err == nil && !fi.IsDir() {
|
|
dir = filepath.Dir(dir)
|
|
}
|
|
var pj struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if b, err := os.ReadFile(filepath.Join(dir, "package.json")); err == nil && json.Unmarshal(b, &pj) == nil && pj.Name != "" {
|
|
return pj.Name
|
|
}
|
|
return spec
|
|
}
|