Documentation
¶
Overview ¶
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).
Index ¶
- Constants
- Variables
- func APIKey(ctx context.Context, provider string, method int, inputs map[string]string, ...) (string, error)
- func AuthPath() string
- func Bun(ctx context.Context) (string, error)
- func Call(ctx context.Context, method string, params, out any) error
- func Dir() string
- func Fetch(ctx context.Context, r FetchRequest) (*http.Response, error)
- func Finish(ctx context.Context, session, code string) (string, error)
- func HasBun() bool
- func IsPath(spec string) bool
- func Name(spec string) string
- func OnChange(f func())
- func Remove(ctx context.Context, name string) error
- func Restart()
- func Running() bool
- func SetConfig(cfg map[string]any) error
- func SetOff(name string, off bool) error
- func Settle()
- func SignOut(ctx context.Context, provider string) error
- func SignedIn(provider string) bool
- func Target(spec string) string
- func Update(ctx context.Context) error
- func Validate(ctx context.Context, provider string, method int, key, value string) (string, error)
- type Authorization
- type Entry
- type FetchRequest
- type List
- type Loaded
- type Method
- type Model
- type Options
- type Prompt
- type Provider
Constants ¶
const BunVersion = "1.3.14"
BunVersion is the Bun magpie downloads to run plugins with, the first time one is needed.
Variables ¶
var ErrFailed = errors.New("the sign-in failed")
ErrFailed is a sign-in the plugin says failed.
Functions ¶
func APIKey ¶
func APIKey(ctx context.Context, provider string, method int, inputs map[string]string, key string) (string, error)
APIKey signs in with a key, as an "api" method does.
func AuthPath ¶
func AuthPath() string
AuthPath is the plugins' sign-ins, OpenCode's auth.json in shape.
func Bun ¶
Bun is the bun to run plugins with: $MAGPIE_BUN when set, else the one magpie downloaded, downloading it now when there is none yet.
func Fetch ¶
Fetch sends r through the provider's plugin — its loader's fetch, or Bun's own when it gives none — and streams back the reply.
func Finish ¶
Finish waits for an OAuth sign-in to finish: an "auto" one on its own, a "code" one with the code pasted back. It gives the provider the sign-in was saved for.
func IsPath ¶
IsPath is whether spec names a file or folder rather than a package, as OpenCode tells them apart.
func Name ¶
Name is the package spec names, without its version: "@scope/x" of "@scope/x@1.2", or the path.
func OnChange ¶
func OnChange(f func())
OnChange registers f to be told when a plugin sign-in or the plugins change (a sign-in saved or refreshed, a plugin added).
func Restart ¶
func Restart()
Restart stops the host, so the next call starts one with the plugins as they are now.
func Settle ¶
func Settle()
Settle waits for Cached's refreshes to end, then stops the host: for tests, whose folders the host runs in go when they end.
func Target ¶
Target is where the plugin is to load from: the installed package's folder, or the path.
Types ¶
type Authorization ¶
type Authorization struct {
Session string `json:"session"`
URL string `json:"url"`
Instructions string `json:"instructions"`
Method string `json:"method"`
}
Authorization is an OAuth sign-in begun: the page to open, and whether the plugin waits for it itself ("auto") or needs the code the page shows pasted back ("code").
type Entry ¶
type Entry struct {
// Spec is how it was added: an npm package (name, name@version) 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"`
}
Entry is one plugin the user added.
type FetchRequest ¶
type FetchRequest struct {
Provider string `json:"provider"`
Model string `json:"model"`
NPM string `json:"npm"`
URL string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
Body []byte `json:"-"`
Session string `json:"session,omitempty"`
}
FetchRequest is a request a provider's plugin makes.
type List ¶
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"`
}
List is plugins.json.
type Method ¶
Method is a way a plugin signs in: "oauth" (a browser, then a code pasted back or not) or "api" (a key).
type Model ¶
type Model struct {
ID string `json:"id"`
Name string `json:"name"`
NPM string `json:"npm"` // the AI SDK package it is spoken to with
URL string `json:"url"`
APIID string `json:"apiId"`
Context int `json:"context"`
Input int `json:"input"`
Output int `json:"output"`
Reasoning bool `json:"reasoning"`
Image bool `json:"image"`
Released string `json:"released"`
Variants []string `json:"variants"`
Cost *struct {
Input float64 `json:"input"`
Output float64 `json:"output"`
} `json:"cost"`
}
Model is a model a plugin's provider serves, as OpenCode lists it.
type Options ¶
type Options struct {
BaseURL string `json:"baseURL"`
APIKey string `json:"apiKey"`
Headers map[string]string `json:"headers"`
Fetch bool `json:"fetch"`
}
Options is what the provider's auth loader gave: where its requests go, the key the AI SDK would send, and whether it carries them itself.
type Prompt ¶
type Prompt struct {
Type string `json:"type"` // text, select
Key string `json:"key"`
Message string `json:"message"`
Placeholder string `json:"placeholder,omitempty"`
Options []struct {
Label string `json:"label"`
Value string `json:"value"`
Hint string `json:"hint,omitempty"`
} `json:"options,omitempty"`
}
Prompt is a question a sign-in method asks before it starts.
type Provider ¶
type Provider struct {
ID string `json:"id"` // OpenCode's: google, github-copilot
Spec string `json:"spec"` // the plugin
Name string `json:"name"`
NPM string `json:"npm"`
API string `json:"api"`
Methods []Method `json:"methods"`
SignedIn bool `json:"signedIn"`
AuthType string `json:"authType"`
AccountID string `json:"accountId"`
Models []Model `json:"models"`
}
Provider is a provider a plugin signs in to.