davsync

package
v0.1.4921 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 52 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package davsync keeps magpie's setup the same on every computer through a WebDAV folder, an S3-compatible bucket or a GitHub repository. What goes there is a backup (see internal/backup), sealed with a passphrase before it leaves the computer, so the server only ever holds a file it can't read.

The magpie serving the gateway syncs every few minutes. The setup is taken in five parts: providers (with their pictures and groups), settings, profiles, the agents' models and the library (instructions, MCP servers and skills). A part changed only here is pushed; one changed only on the server is brought in; one changed on both since the last sync keeps the newer, and the one it replaced is saved in the sync folder beside magpie's files and named in a notice. What the server holds is mirrored: a provider removed on one computer goes from the others. A computer joining a folder keeps the gateway keys the folder's store has never seen; after the join, a key removed on the folder goes from every computer.

Index

Constants

View Source
const Every = 3 * time.Minute

Every is how often the gateway's magpie syncs.

View Source
const Manual = -1

Manual is AutoEvery's never: sync runs only when asked.

View Source
const SetupPrefix = "magpie-sync:"

SetupPrefix begins a sync setup copied as text (WJaunita45604 on X: one copy, pasted on another computer, sets sync up there the same).

Variables

View Source
var AutoChoices = []int{Manual, 3, 15, 30, 60}

AutoChoices are the minutes SetAuto takes, Manual first.

View Source
var ErrBusy = errors.New("another magpie is syncing: try again in a moment")

ErrBusy is a wait for another magpie's sync that ran out of time.

View Source
var ErrNoObject = errors.New("not in the bucket")

ErrNoObject is a name with nothing at it.

View Source
var ErrNotSetup = errors.New("this isn't a sync setup copied from magpie")

ErrNotSetup is OpenSetupText given text that isn't a copied setup.

View Source
var ErrOff = errors.New("sync is off")

ErrOff is a Restore, Undo or SetAuto asked for with sync off.

View Source
var ErrPlanChanged = errors.New("something changed since the preview: look at it again before syncing")

ErrPlanChanged is SyncPreviewed's when the sync would now do something other than the preview said: something changed here or on the server.

View Source
var ErrSetupOff = errors.New("sync isn't set up on this computer")

ErrSetupOff is SetupText with sync off: there is nothing to copy.

View Source
var ErrSetupPassphrase = errors.New("the passphrase doesn't open this setup: type the one sync uses on the other computer")

ErrSetupPassphrase is OpenSetupText with a passphrase that doesn't open it.

View Source
var Parts = []string{"providers", "settings", "profiles", "agents", "library"}

Parts, in the order they are told.

Functions

func Check added in v0.1.484

func Check(c Config) error

Check is Configure's look at the address (and an S3 endpoint), for a caller to make before asking for the password and passphrase.

func Configure

func Configure(c Config) error

Configure turns sync on, or changes it. A password or passphrase left empty keeps the one set before — the password only for the same server and user: it is never sent to another, and is asked for again there.

func Dismiss

func Dismiss() error

Dismiss clears the notice, and with a restore's, its undo.

func Follow added in v0.1.2372

func Follow(ctx context.Context) error

Follow gives this computer's own agent fields back to sync, and syncs: the server's values come in, and the others' picks follow again.

func Now

func Now(ctx context.Context) error

Now syncs once; nothing when sync is off. Usage is shared at most every usageEvery.

func Off

func Off() error

Off turns sync off. The file on the server stays, and so does what this computer last synced with it: turned on again to the same server, it is not a new computer, so what was changed here while it was off goes up rather than the server's older setup coming down over it (#939: every agent back to how it was when sync was turned off). How the last sync went, its notice and a restore's undo go.

func Restore added in v0.1.942

func Restore(ctx context.Context) ([]string, error)

Restore makes this computer's setup the server's, whatever changed here: each part synced that differs is brought in, as a sync brings in a part changed only there. What was here first is sealed into the sync folder, with the API keys, and Undo brings it back. It is the parts brought in.

func Run

func Run(ctx context.Context)

Run syncs a little after it starts and every AutoEvery after that, until ctx ends — less often while the server is limiting requests, and never while sync is set to run only when asked. A failure is logged once, not on every try.

func SavedPassword added in v0.1.250

func SavedPassword(c Config) (kept, needed bool)

SavedPassword is Configure's look at the saved password for c, with c's own left empty, for a caller to make before asking for one: whether it is kept, or a new one is needed.

func SetAuto added in v0.1.942

func SetAuto(minutes int) error

SetAuto sets how many minutes apart the gateway's magpie syncs by itself, one of AutoChoices; Manual: never, only when asked (#847).

func SetFollowServer

func SetFollowServer(on bool) error

SetFollowServer sets whether this computer follows the setup on the server (Config.FollowServer) or syncs both ways.

func SetupText

func SetupText() (string, error)

SetupText is this computer's sync setup as one line of text, sealed with its passphrase (AES-GCM under a PBKDF2 key, as the server's file is), so the text alone gives no one the server's password.

func SyncNow added in v0.1.653

func SyncNow(ctx context.Context) error

SyncNow is Now asked for: usage is shared whatever the time.

func SyncPreviewed

func SyncPreviewed(ctx context.Context, token string) error

SyncPreviewed is SyncNow for what Preview told, its Token: when the sync would now do anything else, nothing is done, and it is ErrPlanChanged.

func Undo added in v0.1.942

func Undo() ([]string, error)

Undo puts back the setup the last Restore replaced. The next sync takes it to the server, as any change made here.

func UndoSync

func UndoSync(force bool) ([]string, error)

UndoSync puts back the parts the last sync brought in, as they were before it (#6428). It stays on this computer: the next sync doesn't take it to the server, and the server's copy comes in again only when it changes there. A part changed here since that sync is not written over unless force, and the error says which.

func Upload added in v0.1.1134

func Upload(ctx context.Context) error

Upload writes this computer's setup over the server's file when that file isn't a backup a sync can read — empty, cut short, another app's — so a sync stopped on it has a way on that the user chooses. A file that opens as a backup, one sealed with another passphrase, a newer magpie's, and a page the server answered with instead of a file are never written over. What the server held is kept in the sync folder first, and nothing is written when it can't be. The write is made over the version read, so a file another computer wrote meanwhile isn't.

Types

type Applied

type Applied struct {
	At    time.Time `json:"at"`
	Parts []string  `json:"parts"`
}

Applied is what Status tells of the last sync that brought parts in, while its copy is there to undo it.

type Bucket added in v0.1.577

type Bucket struct {
	// contains filtered or unexported fields
}

Bucket is the S3 sync's bucket for files other than the backup — the gateway's request archive — each at <prefix>/magpie/<name>, beside the backup, written as it is: no passphrase seals them, so they can be read in the bucket too.

func S3Bucket added in v0.1.577

func S3Bucket() (*Bucket, bool)

S3Bucket is the bucket sync keeps the backup in; false when sync is off or to a WebDAV folder.

func (*Bucket) Delete

func (b *Bucket) Delete(ctx context.Context, name string) error

Delete removes what is at name; nothing there is no error.

func (*Bucket) Each

func (b *Bucket) Each(ctx context.Context, prefix string, f func(name string, size int64)) error

Each calls f with every object under prefix, a name as Put takes it ("archive/"), its name as Put takes it and its size, a page of a thousand at a time.

func (*Bucket) Get added in v0.1.577

func (b *Bucket) Get(ctx context.Context, name string) ([]byte, error)

Get reads what is at name, its first 16 MiB; ErrNoObject when there is nothing.

func (*Bucket) Open added in v0.1.600

func (b *Bucket) Open(ctx context.Context, name string) (io.ReadCloser, int64, error)

Open is what is at name as it is read, however large, and its size (-1 when the server doesn't say); ErrNoObject when there is nothing. The caller closes it.

func (*Bucket) Put added in v0.1.577

func (b *Bucket) Put(ctx context.Context, name string, data []byte) error

Put writes data at name, over what is there.

func (*Bucket) Where added in v0.1.577

func (b *Bucket) Where() string

Where names the bucket, its prefix and its server, as messages do.

type ChangedSince

type ChangedSince struct{ Parts []string }

ChangedSince is UndoSync's when parts changed here after the sync.

func (*ChangedSince) Error

func (e *ChangedSince) Error() string

type Config

type Config struct {
	URL        string `json:"url"`
	User       string `json:"user,omitempty"`
	Password   string `json:"password,omitempty"`
	Passphrase string `json:"passphrase"`
	// S3 alone: the server (none is AWS), its region (none is us-east-1,
	// auto for R2), and the bucket in the path rather than the host name
	Endpoint  string `json:"endpoint,omitempty"`
	Region    string `json:"region,omitempty"`
	PathStyle bool   `json:"pathStyle,omitempty"`
	// GitHub alone: an empty branch uses the repository's default branch.
	Branch string `json:"branch,omitempty"`
	Keys   bool   `json:"keys"`   // providers carry their API keys
	Agents bool   `json:"agents"` // the agents' models go too
	// Providers, Settings and Profiles are whether each of those parts
	// goes; nil, as in a setup made before they could be left out, is yes.
	// A part left out stays as it is on each computer (jinpwng on X,
	// #6428: only the library and memory synced, each computer keeping its
	// own providers, settings and models).
	Providers *bool `json:"providers,omitempty"`
	Settings  *bool `json:"settings,omitempty"`
	Profiles  *bool `json:"profiles,omitempty"`
	// Library is whether the library goes too; nil, as in a setup made
	// before it could, is yes
	Library *bool `json:"library,omitempty"`
	// Memory is whether memory goes too (memory.Combine says how two
	// computers' are merged); nil, as in a setup made before it could,
	// is yes
	Memory *bool `json:"memory,omitempty"`
	// Usage is whether this computer shares its usage with the others
	// syncing to the server, and sees theirs (#542)
	Usage bool `json:"usage,omitempty"`
	// SignIns is whether the subscription accounts and the plugins'
	// sign-ins go too, with their credentials, inside the file sealed with
	// Passphrase: off unless the user turns it on
	// (provider.MergeSignIns says how two computers' are merged).
	SignIns bool `json:"signIns,omitempty"`
	// SignInsExcept are the subscriptions (provider ids) whose sign-ins
	// this computer leaves out of SignIns (Ethereal499 on X: Claude's, as
	// Claude is wary of where its accounts are used): theirs here never go
	// up, the server's are never put in here, and theirs here are kept as
	// they are, none taken away for the server lacking it. The server's
	// copy goes on as it was for the computers that sync them. None, as
	// in a setup made before the choice, leaves none out.
	SignInsExcept []string `json:"signInsExcept,omitempty"`
	// ProviderSettingsHere keeps each provider's settings on this
	// computer, so only the list and the keys sync (provider.WithoutSettings;
	// StudyingPython on X: "只希望同步供应商的key和列表,不希望同步供应商设置"):
	// a setting made here (models, routing, limits, headers, proxy, on or
	// off…) never goes up, and the server's never comes in for a provider
	// here, while providers added, removed, renamed or given a new key
	// sync as ever. A provider new here takes the settings it came with,
	// and one new on the server goes up with this computer's. Off, as in a
	// setup made before the choice, the settings go with their providers.
	// The choice is this computer's: the setup text doesn't carry it.
	ProviderSettingsHere bool `json:"providerSettingsHere,omitempty"`
	// Other is the most recently left server, retained for older clients.
	// Servers holds every inactive kind. Nothing syncs to them.
	Other *Server `json:"other,omitempty"`
	// Servers keeps every inactive kind, including Other for older clients.
	Servers map[string]Server `json:"servers,omitempty"`
	// AutoEvery is how many minutes apart the gateway's magpie syncs by
	// itself: 0 is Every's 3, Manual never — then only Sync now syncs
	// (#847). Set by SetAuto alone; Configure keeps it.
	AutoEvery int `json:"autoEvery,omitempty"`
	// FollowServer is this computer following the setup on the server
	// rather than syncing both ways (Ramon317317 on X: one computer the
	// others all take theirs from). Each sync brings in every part that
	// changed on either side since the last one, and nothing goes up: a
	// change made here is replaced, a copy of it kept in the sync folder
	// and named in a notice. The computer the others follow keeps syncing
	// both ways. Set by SetFollowServer alone; Configure keeps it.
	FollowServer bool `json:"followServer,omitempty"`
}

Config is the sync's setup, kept in sync.json beside magpie's other files, readable by the user alone, as provider keys are. An s3://bucket/prefix address is an S3-compatible bucket, User its access key ID and Password the secret, kept as a WebDAV password is. For github://owner/repo/prefix, Password is the repository's Contents token, independent of GitHubToken in settings (the library's).

func Load

func Load() (Config, bool)

Load reads the setup; false when sync is off. The file's bytes are read once until it changes (filememo), each caller given a Config of its own: every Providers page poll asked it, most often of a file that isn't there.

func (Config) GitHub added in v0.1.1225

func (c Config) GitHub() bool

GitHub is whether c keeps the file in a GitHub repository.

func (Config) Kept added in v0.1.1225

func (c Config) Kept(kind string) (Server, bool)

Kept is an inactive server, including setups saved before Servers existed.

func (Config) Kind added in v0.1.484

func (c Config) Kind() string

Kind is the server's kind, as the user reads it.

func (Config) S3 added in v0.1.484

func (c Config) S3() bool

S3 is whether c keeps the file in an S3 bucket rather than a WebDAV folder.

func (Config) Synced

func (c Config) Synced() []string

Synced is the parts c syncs, in Parts' order, memory last.

type Item

type Item struct {
	Kind   string `json:"kind"`
	Name   string `json:"name"`
	Change string `json:"change"` // added, removed, changed
}

Item is one thing in a part that the sync adds, removes or changes on the side it writes. Only names: never a value, which may be a key.

type MemoryPlan

type MemoryPlan struct {
	In int `json:"in"`
	Up int `json:"up"`
}

MemoryPlan counts the memories a sync would change on each side.

type Notice

type Notice struct {
	At    time.Time `json:"at"`
	Here  []string  `json:"here,omitempty"`  // this computer's parts, replaced by the server's
	There []string  `json:"there,omitempty"` // the server's parts, replaced by this computer's
	Saved string    `json:"saved,omitempty"` // the folder the replaced copies are kept in
	// Followed is that the parts in Here were changed here and replaced
	// because this computer follows the server (Config.FollowServer), not
	// because the server's were newer
	Followed bool `json:"followed,omitempty"`
	// Restored is a Restore's notice: Here are the parts it brought in,
	// what was here before kept in Saved, which Undo brings back
	Restored bool `json:"restored,omitempty"`
}

Notice says what a sync replaced when a part had changed on both sides, or when this computer first joined a folder with a setup in it.

type OtherView added in v0.1.587

type OtherView struct {
	Kind        string `json:"kind"` // "webdav", "s3" or "github"
	URL         string `json:"url"`
	User        string `json:"user,omitempty"`
	PasswordSet bool   `json:"passwordSet,omitempty"`
	Endpoint    string `json:"endpoint,omitempty"`
	Region      string `json:"region,omitempty"`
	PathStyle   bool   `json:"pathStyle,omitempty"`
	Branch      string `json:"branch,omitempty"`
}

OtherView is the other kind's server as the Settings page shows it.

type OwnField added in v0.1.2372

type OwnField struct {
	Agent string `json:"agent"` // the agent's name
	Field string `json:"field"` // the field's label: model, effort…
}

OwnField is an agent field kept on this computer, as Settings names it.

type PartPlan

type PartPlan struct {
	Part string `json:"part"`
	// Do is "in" (the server's comes in here), "up" (this computer's goes
	// to the server), "same", or "off" (not synced: stays as it is here).
	Do string `json:"do"`
	// Replaces: a change made here too is replaced, a copy kept first.
	Replaces bool   `json:"replaces,omitempty"`
	Items    []Item `json:"items,omitempty"`
}

PartPlan is what a sync would do with one part.

type Plan

type Plan struct {
	// Empty: the server has no file yet, and this computer's goes up.
	Empty bool `json:"empty,omitempty"`
	// Follow: this computer follows the server, never writing it.
	Follow bool       `json:"follow,omitempty"`
	Parts  []PartPlan `json:"parts"`
	// Memory is how many memories come in here and go up, by id.
	Memory *MemoryPlan `json:"memory,omitempty"`
	// SignIns is how many sign-ins change here, when they sync.
	SignIns int `json:"signIns,omitempty"`
	// PluginsGone are the plugins removed here because another computer
	// removed them.
	PluginsGone []string `json:"pluginsGone,omitempty"`
	// Token tells this plan from another: SyncPreviewed syncs only while
	// the sync would still do what it says.
	Token string `json:"token"`
}

Plan is what a sync would do now (Preview), nothing done.

func Preview

func Preview(ctx context.Context) (Plan, error)

Preview is what syncing now would do, with the setup saved; nothing is changed here or on the server.

func PreviewWith

func PreviewWith(ctx context.Context, c Config) (Plan, error)

PreviewWith is Preview for a setup not saved yet: Settings shows what saving it would do first. A password or passphrase left empty is the saved one's, as Configure takes it.

type Server added in v0.1.587

type Server struct {
	URL       string `json:"url"`
	User      string `json:"user,omitempty"`
	Password  string `json:"password,omitempty"`
	Endpoint  string `json:"endpoint,omitempty"`
	Region    string `json:"region,omitempty"`
	PathStyle bool   `json:"pathStyle,omitempty"`
	Branch    string `json:"branch,omitempty"`
}

Server is where a Config syncs to, without what it syncs or the passphrase: the other kind's, kept beside the one synced to.

type ServerFile added in v0.1.1134

type ServerFile struct {
	// What is "empty", "cut" (a magpie backup cut short), "page" (a web
	// page), "xml", "format" (another app's JSON file, named by its
	// "format"), "json" or "other"
	What   string `json:"what"`
	Size   int    `json:"size"`
	Title  string `json:"title,omitempty"`  // a page's <title>
	Format string `json:"format,omitempty"` // a JSON file's own "format"
	Type   string `json:"type,omitempty"`   // the kind of file, as its first bytes tell
	Moved  string `json:"moved,omitempty"`  // where a redirect took the read: host and path
	// Replace is whether the file is one Upload may write over: a page or
	// an XML answer is the server's (or a proxy's), not a file at that path
	Replace bool `json:"replace,omitempty"`
}

ServerFile is what the server held, or answered with, where the backup should be, when it isn't one: what the Settings page says of it, and whether Upload may write this computer's setup over it. It never carries the file's contents — only its size, its kind, and a page's title.

type Setup

type Setup struct {
	URL       string `json:"url"`
	User      string `json:"user,omitempty"`
	Password  string `json:"password,omitempty"`
	Endpoint  string `json:"endpoint,omitempty"`
	Region    string `json:"region,omitempty"`
	PathStyle bool   `json:"pathStyle,omitempty"`
	Branch    string `json:"branch,omitempty"`
	// Providers, Settings and Profiles are false when left out of sync;
	// nil, as from a magpie before they could be, is synced.
	Providers *bool `json:"providers,omitempty"`
	Settings  *bool `json:"settings,omitempty"`
	Profiles  *bool `json:"profiles,omitempty"`
	Keys      bool  `json:"keys"`
	Agents    bool  `json:"agents"`
	Library   bool  `json:"library"`
	Memory    bool  `json:"memory"`
	Usage     bool  `json:"usage,omitempty"`
	SignIns   bool  `json:"signIns,omitempty"`
}

Setup is what a setup copied as text holds: the server, its password or token, and what is synced. Never the passphrase: the text is sealed with it, so it is typed on the other computer to open the text, as it is typed there to open the server's file.

func OpenSetupText

func OpenSetupText(text, pass string) (Setup, error)

OpenSetupText opens what SetupText gave, with the passphrase it was sealed with. Text before the setup, and line breaks a chat put in it, are skipped.

type View

type View struct {
	On            bool   `json:"on"`
	URL           string `json:"url,omitempty"`
	User          string `json:"user,omitempty"`
	PasswordSet   bool   `json:"passwordSet,omitempty"`
	PassphraseSet bool   `json:"passphraseSet,omitempty"`
	Keys          bool   `json:"keys"`
	Agents        bool   `json:"agents"`
	Kind          string `json:"kind,omitempty"` // "webdav", "s3" or "github", when on
	Endpoint      string `json:"endpoint,omitempty"`
	Region        string `json:"region,omitempty"`
	PathStyle     bool   `json:"pathStyle,omitempty"`
	Branch        string `json:"branch,omitempty"`
	Library       bool   `json:"library"`
	Memory        bool   `json:"memory"`
	Providers     bool   `json:"providers"`
	Settings      bool   `json:"settings"`
	Profiles      bool   `json:"profiles"`
	Usage         bool   `json:"usage,omitempty"`
	SignIns       bool   `json:"signIns,omitempty"`
	// SignInsExcept are the subscriptions whose sign-ins stay out
	// (Config.SignInsExcept)
	SignInsExcept []string `json:"signInsExcept,omitempty"`
	// ProviderSettingsHere is whether the providers' settings stay on
	// this computer (Config.ProviderSettingsHere)
	ProviderSettingsHere bool `json:"providerSettingsHere,omitempty"`
	// UsageError is why usage couldn't be shared the last time it was tried
	UsageError string    `json:"usageError,omitempty"`
	Last       time.Time `json:"last,omitzero"`
	Error      string    `json:"error,omitempty"`
	Notice     *Notice   `json:"notice,omitempty"`
	// Other is the other kind's server, kept for moving back to: not
	// synced to
	Other   *OtherView           `json:"other,omitempty"`
	Servers map[string]OtherView `json:"servers,omitempty"`
	// Auto is how many minutes apart it syncs by itself; 0 never: only when
	// asked
	Auto int `json:"auto"`
	// FollowServer is whether this computer takes the server's setup and
	// sends none of its own (Config.FollowServer)
	FollowServer bool `json:"followServer,omitempty"`
	// Undo is whether the last Restore can be undone
	Undo bool `json:"undo,omitempty"`
	// Applied is the last sync that brought parts in here, which
	// UndoSync takes back on this computer
	Applied *Applied `json:"applied,omitempty"`
	// File is what the server holds instead of a backup, when Error is
	// that it isn't one; File.Replace offers Upload
	File *ServerFile `json:"serverFile,omitempty"`
	// SignIn is the subscriptions whose settings sync brought with no
	// account here: sync carries no sign-in, so each is to be signed in to
	// on this computer before it is listed
	SignIn []string `json:"signIn,omitempty"`
	// Own is the agent fields picked on this computer after a sync set
	// them, which sync leaves as they are here (own.go)
	Own []OwnField `json:"own,omitempty"`
	// LibraryLeft and LibraryProblems are the last sync's (state)
	LibraryLeft     []string `json:"libraryLeft,omitempty"`
	LibraryProblems []string `json:"libraryProblems,omitempty"`
}

View is sync as the Settings page shows it: never the secrets.

func Status

func Status() View

Status is sync's setup and how the last sync went.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL