A plugin with auth.refresh had a sign-in renewed only when something used the account (a request, its models, usage or check-in) within its refreshLead of expiry. An account left idle past its token's life lapsed. OrcaTerm's WeChat token lives two hours, and after that its refresh answers SESSION_EXPIRED for good, so the user had to sign in again (orinsid, magpie-community/plugins#675). The gateway now renews each such sign-in as it comes due (provider.KeepPluginsSignedIn, started beside KeepPluginsListed): - Due times come from plugin-auth.json through filememo (plugin.RenewAt: expires less the lead the host reckons, now listed as Provider.Renews/RenewLead). Nothing is asked, and no host started, until an account is due. The loop sleeps until the next one, at most 5 minutes, and renews them one after another. - A due sign-in goes through the host's own renewal (host.js keepSignedIn, which shares renewal() with fresh()). It is one at a time per account whoever asks: a request arriving meanwhile waits for the same promise, under the cross-process lock file, through the account's proxy. - Kept: accounts switched on and not marked, of a plugin provider not switched off, hidden (leftAlone) or being moved. - A failed renewal marks nothing. It is held back for 1m, doubling to at most 30m, for that sign-in only. Only the plugin's signIn:"expired" marks it, as at a request. - A plugin can opt out with auth.keepAlive: false. None in the plugins repo needs to: no refresh costs quota, the ones rotating single-use refresh tokens (Cline, Factory, Nous, SenseNova, CodeArts, Qoder, MiniMax) rotate at use already, and kimi-work's reference accounts and Raccoon's desktop reuse never refresh another app's token. Docs: provider-plugins.md "A sign-in nothing uses"; leftAlone list in providers-accounts.md. Run at the pushed head, on mote (mote-run), -tags nogui: - go test -count=1 -timeout 45m ./internal/plugin ./internal/provider ./internal/gateway: ok (109s, 119s, 637s), rebased on origin/main6cfe78635. Rebased again onb088fd0f0(gateway.go, prompt.go changed): go build ./..., go vet of the three packages and the new tests plus TestLivePrompt*: ok. - the new tests on Windows (win-run) and Linux (wsl-run): ok. - GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go vet ./...: ok. gofmt -l: clean. Broken by hand, each red, then restored: - keepSignedIn refreshing by itself, not through renewal()/the lock: TestKeepSignedInRenewsIdleSignIns red (r-ok-busy renewed twice beside concurrent requests). - keepAlive:false ignored: red (stayco listed as renewed). - a network failure reported as gone: red (down: Gone true). - Go backoff removed: TestKeeperBacksOffAFailedRenewal red. - the On, Lapsed and leftAlone filters each dropped: TestKeptSignInsAreTheOnesSwitchedOn red. The new tests don't compile on the old code (no KeepSignedIn). Not tried: a real OrcaTerm account idling past its 2h token with the gateway running; Windows/Linux runs of the new tests (vet only); a magpie with many plugin accounts beyond the unit test's look.
27 lines
919 B
JavaScript
27 lines
919 B
JavaScript
// A plugin that renews its sign-ins through magpie (auth.refresh) but
|
|
// says keepAlive: false: an account nothing uses is left to lapse. Each
|
|
// renewal is written to $RENEW_LOG as "stay:" and the refresh token.
|
|
import fs from "node:fs"
|
|
|
|
export const StayPlugin = async () => ({
|
|
config: async (cfg) => {
|
|
cfg.provider = cfg.provider ?? {}
|
|
cfg.provider.stayco = {
|
|
name: "StayCo",
|
|
npm: "@ai-sdk/openai-compatible",
|
|
api: "https://stay.invalid/v1",
|
|
models: { "stay-1": { name: "Stay One", limit: { context: 1000, output: 100 } } },
|
|
}
|
|
},
|
|
auth: {
|
|
provider: "stayco",
|
|
methods: [{ type: "api", label: "API key" }],
|
|
keepAlive: false,
|
|
refreshLead: 60_000,
|
|
async refresh(auth) {
|
|
fs.appendFileSync(process.env.RENEW_LOG, "stay:" + auth.refresh + "\n")
|
|
return { access: "stay-new", refresh: auth.refresh + "+", expires: Date.now() + 3600e3 }
|
|
},
|
|
},
|
|
})
|