@magpie-community/opencode-kiro-auth 0.1.43 → 0.1.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/index.mjs +54 -6
- package/opencode2.mjs +16 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,6 +59,13 @@ them, are under "Other ways to sign in…".
|
|
|
59
59
|
browser sign-in here. Sign in with `kiro-cli login` or the Kiro IDE and
|
|
60
60
|
use the next method instead; its management calls carry
|
|
61
61
|
`TokenType: EXTERNAL_IDP`.
|
|
62
|
+
- magpie renews the sign-in ahead of its end. A renewal that fails (Kiro
|
|
63
|
+
unreachable, a 5xx) isn't tried again on every pass: the next waits a
|
|
64
|
+
minute, then twice as long each time, up to an hour, and the failure
|
|
65
|
+
reaches magpie's log once. A refresh token Kiro refused (400, 401, 403)
|
|
66
|
+
marks the account for a new sign-in and isn't tried again until it has
|
|
67
|
+
one. A renewal that works, or a request's own refresh, starts the count
|
|
68
|
+
again.
|
|
62
69
|
- **Kiro CLI's or Kiro IDE's sign-in**: uses the account kiro-cli is signed
|
|
63
70
|
in to, or else the Kiro IDE's. Nothing is copied. It is read each time
|
|
64
71
|
from where they keep it:
|
package/index.mjs
CHANGED
|
@@ -288,11 +288,16 @@ const EXPIRED_CLI = EXPIRED + " with `kiro-cli login` or the Kiro IDE"
|
|
|
288
288
|
|
|
289
289
|
// gone is an error saying the sign-in itself is gone — refused, or never
|
|
290
290
|
// there. The built-in marked no Kiro account lapsed, not even then, nor
|
|
291
|
-
// cleared one: every answer says the account is kept.
|
|
292
|
-
// expired
|
|
291
|
+
// cleared one: every answer says the account is kept. Here it is marked
|
|
292
|
+
// signIn "expired" by magpie's renewal (auth.refresh, once: plugins#1716)
|
|
293
|
+
// and by the usage read, which says renewed for a clean read (auth.usage).
|
|
293
294
|
const gone = (msg) => Object.assign(new Error(msg), { gone: true })
|
|
294
295
|
const EARLY_MS = 2 * 60 * 1000 // a token this close to its end is refreshed before a request
|
|
295
296
|
const LEAD_MS = 10 * 60 * 1000 // and this close, magpie renews it ahead of time (auth.refresh)
|
|
297
|
+
// a renewal auth.refresh asked for that failed waits base before the next,
|
|
298
|
+
// doubling with each failure in a row up to cap (plugins#1716); now is the
|
|
299
|
+
// clock it reads, held in tests
|
|
300
|
+
const backoff = { base: 60 * 1000, cap: 60 * 60 * 1000, now: () => Date.now() }
|
|
296
301
|
const fresh = (c, early = EARLY_MS) => c.method === "apikey" || !c.expires || c.expires - Date.now() > early
|
|
297
302
|
|
|
298
303
|
async function post(url, contentType, body, headers = {}) {
|
|
@@ -763,11 +768,38 @@ function account(client, magpie) {
|
|
|
763
768
|
for (let i = 0; i < 16 && after.has(c.access); i++) c = { ...after.get(c.access) }
|
|
764
769
|
return c
|
|
765
770
|
}
|
|
771
|
+
// failing is auth.refresh's renewals that failed, by the refresh token
|
|
772
|
+
// they were tried with (plugins#1716): one isn't tried again till its
|
|
773
|
+
// next, and one Kiro refused (gone) never, till the account holds another
|
|
774
|
+
// refresh token (signed in again). What failed reaches magpie once for
|
|
775
|
+
// each state (failing, ended), not on every pass.
|
|
776
|
+
const failing = new Map() // refresh token → { n, next, final, e, told }
|
|
766
777
|
const renewed = (c, n) => {
|
|
767
778
|
after.set(c.access, n)
|
|
768
779
|
if (after.size > 16) after.delete(after.keys().next().value)
|
|
780
|
+
// renewed, by a request's refresh or magpie's: the count starts again
|
|
781
|
+
failing.delete(c.refresh)
|
|
769
782
|
return n
|
|
770
783
|
}
|
|
784
|
+
const failed = (k, e) => {
|
|
785
|
+
const t = failing.get(k) ?? { n: 0, told: "" }
|
|
786
|
+
t.n++
|
|
787
|
+
t.e = e
|
|
788
|
+
t.final = !!e?.gone
|
|
789
|
+
t.next = backoff.now() + Math.min(backoff.base * 2 ** (t.n - 1), backoff.cap)
|
|
790
|
+
failing.set(k, t)
|
|
791
|
+
if (failing.size > 16) failing.delete(failing.keys().next().value)
|
|
792
|
+
return t
|
|
793
|
+
}
|
|
794
|
+
// told throws a state's failure the first time, and is nothing to renew
|
|
795
|
+
// after that. A refusal is marked signIn "expired", so magpie shows the
|
|
796
|
+
// account as one to sign in again and stops asking.
|
|
797
|
+
const told = (t) => {
|
|
798
|
+
const state = t.final ? "ended" : "failing"
|
|
799
|
+
if (t.told === state) return undefined
|
|
800
|
+
t.told = state
|
|
801
|
+
throw t.final ? Object.assign(new Error(t.e.message), { gone: true, signIn: "expired" }) : t.e
|
|
802
|
+
}
|
|
771
803
|
let lock = Promise.resolve()
|
|
772
804
|
const locked = (fn) => {
|
|
773
805
|
const run = lock.then(fn, fn)
|
|
@@ -902,7 +934,21 @@ function account(client, magpie) {
|
|
|
902
934
|
let c = newest(read)
|
|
903
935
|
// renewed here already (a request's refresh, or this hook's whose
|
|
904
936
|
// result magpie hasn't saved yet), the store not yet saying so
|
|
905
|
-
if (c.access === read.access || !fresh(c, LEAD_MS))
|
|
937
|
+
if (c.access === read.access || !fresh(c, LEAD_MS)) {
|
|
938
|
+
const k = c.refresh
|
|
939
|
+
let t = failing.get(k)
|
|
940
|
+
// failed lately: not tried again till its time, nor ever once Kiro
|
|
941
|
+
// refused it
|
|
942
|
+
if (!t || (!t.final && backoff.now() >= t.next)) {
|
|
943
|
+
try {
|
|
944
|
+
c = renewed(c, await refresh(c))
|
|
945
|
+
t = null
|
|
946
|
+
} catch (e) {
|
|
947
|
+
t = failed(k, e)
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
if (t) return told(t)
|
|
951
|
+
}
|
|
906
952
|
held = { id: "oauth:own", c }
|
|
907
953
|
const out = { access: c.access, expires: c.expires }
|
|
908
954
|
if (c.refresh !== auth.refresh) out.refresh = c.refresh
|
|
@@ -3441,8 +3487,10 @@ export async function KiroAuthPlugin({ client, magpie } = {}) {
|
|
|
3441
3487
|
// this. kiro-cli's and the IDE's sign-ins have no end magpie knows
|
|
3442
3488
|
// (expires 0) and are never asked for.
|
|
3443
3489
|
refreshLead: LEAD_MS,
|
|
3444
|
-
// A failure is thrown
|
|
3445
|
-
//
|
|
3490
|
+
// A failure is thrown once and then backs off, from a minute doubling
|
|
3491
|
+
// to an hour; a refusal (gone) is thrown once marked signIn
|
|
3492
|
+
// "expired", as auth.usage marks it, and isn't tried again until the
|
|
3493
|
+
// account is signed in again (plugins#1716).
|
|
3446
3494
|
async refresh(auth) {
|
|
3447
3495
|
if (auth?.type !== "oauth" || auth.source || !auth.access || !auth.refresh) return undefined
|
|
3448
3496
|
return creds.renew(auth)
|
|
@@ -3573,4 +3621,4 @@ export async function KiroAuthPlugin({ client, magpie } = {}) {
|
|
|
3573
3621
|
}
|
|
3574
3622
|
|
|
3575
3623
|
// for tests
|
|
3576
|
-
export const _internal = { generate, responsesBody, crMessages, crRefusal, features, FEATURES, featureKey, waitOf, timing, TRIES, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, rateOf, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames, sealReasoning, unsealReasoning, reasoningScope }
|
|
3624
|
+
export const _internal = { backoff, generate, responsesBody, crMessages, crRefusal, features, FEATURES, featureKey, waitOf, timing, TRIES, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, rateOf, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames, sealReasoning, unsealReasoning, reasoningScope }
|
package/opencode2.mjs
CHANGED
|
@@ -507,6 +507,14 @@ async function setupPlugin(ctx, fn, live) {
|
|
|
507
507
|
// the account the list was last read for: a credential.updated that
|
|
508
508
|
// leaves it as it was reads nothing again
|
|
509
509
|
let readFor = null
|
|
510
|
+
// the last list each account's own read gave, by the account it was read
|
|
511
|
+
// for: a read that fails (a workspace config answering 503) keeps the
|
|
512
|
+
// list that same account last read rather than the bundled one, which
|
|
513
|
+
// brought back models its workspace hides (plugins#1642). Another
|
|
514
|
+
// account's list is never used, a read that gave an empty list is kept
|
|
515
|
+
// as empty, and the bundled list stands in only for an account whose
|
|
516
|
+
// list was never read.
|
|
517
|
+
const lists = new Map()
|
|
510
518
|
const stateOf = async () => {
|
|
511
519
|
const c = await ctx.integration.connection.active(authID).catch(() => undefined)
|
|
512
520
|
const a = c ? await getAuth(authID) : undefined
|
|
@@ -523,7 +531,8 @@ async function setupPlugin(ctx, fn, live) {
|
|
|
523
531
|
const s = source.get(authID)
|
|
524
532
|
const connection = await ctx.integration.connection.active(authID).catch(() => undefined)
|
|
525
533
|
const a = connection ? await getAuth(authID) : undefined
|
|
526
|
-
|
|
534
|
+
const readingFor = connKey(connection) + "\0" + (a ? fingerprint(a) : "")
|
|
535
|
+
readFor = readingFor
|
|
527
536
|
if (!a || listID !== authID || typeof hooks.provider?.models !== "function") {
|
|
528
537
|
if (s.connection || s.models) {
|
|
529
538
|
s.connection = undefined
|
|
@@ -544,7 +553,12 @@ async function setupPlugin(ctx, fn, live) {
|
|
|
544
553
|
return
|
|
545
554
|
}
|
|
546
555
|
s.connection = connection
|
|
547
|
-
|
|
556
|
+
if (got && typeof got === "object") {
|
|
557
|
+
s.models = Object.entries(got).map(([k, m]) => modelOf(authID, s.prov, k, record(m)))
|
|
558
|
+
lists.delete(readingFor)
|
|
559
|
+
lists.set(readingFor, s.models)
|
|
560
|
+
if (lists.size > 32) lists.delete(lists.keys().next().value)
|
|
561
|
+
} else s.models = lists.get(readingFor) ?? null
|
|
548
562
|
await ctx.provider.reload()
|
|
549
563
|
await ctx.model?.reload?.()
|
|
550
564
|
} finally {
|