diff --git a/packages/agnes-code/opencode2.mjs b/packages/agnes-code/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/agnes-code/opencode2.mjs +++ b/packages/agnes-code/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/agnes/opencode2.mjs b/packages/agnes/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/agnes/opencode2.mjs +++ b/packages/agnes/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index 37de1bd..3a95e43 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -222,8 +222,13 @@ async function listAgyModels(agyPath) { const { err, stdout, code, timedOut } = await execOnce(agyPath, ["models"], { timeoutMs: 15_000, unrefTimer: true }) // null, not FALLBACK_MODELS: a failed listing must not overwrite a dynamic catalog. if (err || timedOut || code !== 0) return null + return parseAgyModels(stdout) +} + +// parseAgyModels reads `agy models` output: one " " per line. +function parseAgyModels(stdout) { const out = [] - for (const raw of stdout.split("\n")) { + for (const raw of String(stdout ?? "").split("\n")) { const line = raw.trim() if (!line) continue const match = /^(\S+)\s+(.+)$/.exec(line) @@ -238,12 +243,25 @@ async function listAgyModels(agyPath) { // probeSignedIn is whether agy lists the signed-in account's models: an // exit code alone can't say so, so a listing without one model in it is -// not signed in either. +// not signed in either. The listing it read is the catalog's, and what agy +// acp offers is asked at once (#1064): `agy models` and the session each +// take seconds on a real agy, and the host lists models right after. +const OFFERED_SIGNIN_WAIT_MS = 3_000 + async function probeSignedIn(agy) { const probe = await execOnce(agy, ["models"], { timeoutMs: 15_000 }) const listed = !probe.err && !probe.timedOut && probe.code === 0 && probe.stdout.split("\n").some(line => /^\S+\s+\S/.test(line.trim())) - if (listed) return { ok: true } + if (listed) { + const list = parseAgyModels(probe.stdout) + if (list) { + useAgyPath(agy) + storeListedCatalog(agy, list, Date.now()) + wantOfferedProbe = true + await briefly(probeOfferedOnce(agy), OFFERED_SIGNIN_WAIT_MS) + } + return { ok: true } + } const why = probe.timedOut ? "`agy models` timed out after 15s" : `\`agy models\` says: ${(probe.stderr || probe.stdout || "").trim().split(/\r?\n/).filter(Boolean).at(-1) || probe.err?.message || `exit ${probe.code}`}` @@ -256,6 +274,9 @@ const MODEL_CATALOG_FAST_WAIT_MS = 500 const MODEL_CATALOG_RETRY_MS = 5_000 let modelCatalogRefreshPromise = null let cachedModelCatalog = fallbackModelCatalog() +// A caller lists models (provider.models), so a listing that lands asks agy +// acp what it offers right away, not on the next models() call (#1064). +let wantOfferedProbe = false // `agy models` lists every model of the account, while agy's ACP server offers // a client only some of them. A session's model option (and its @@ -306,6 +327,29 @@ const OFFERED_PROBE_TIMEOUT_MS = 30_000 const OFFERED_PROBE_RETRY_MS = 60_000 let offeredProbePromise = null let offeredProbeAt = null +// Bumped when what was learnt about an agy no longer holds (another +// executable, a new sign-in): a probe still running for the old one is +// not believed. +let offeredGen = 0 + +function forgetOffered() { + acpOfferedModels = null + acpOfferedKey = "" + offeredProbePromise = null + offeredProbeAt = null + offeredGen++ +} + +// briefly waits for a promise at most ms, and never throws. +async function briefly(promise, ms) { + if (!promise) return + let timer + try { + await Promise.race([promise.catch(() => {}), new Promise((resolve) => { timer = setTimeout(resolve, ms) })]) + } finally { + clearTimeout(timer) + } +} function killQuietly(child) { try { child.stdin?.end() } catch {} @@ -318,6 +362,7 @@ function killQuietly(child) { } async function probeOfferedModels(agyPath) { + const gen = offeredGen let workspace = null let child = null let conn = null @@ -346,7 +391,7 @@ async function probeOfferedModels(agyPath) { }) if (agyTooOld(init?.agentInfo?.version ?? null)) return const session = await conn.request("session/new", { cwd: workspace, mcpServers: [] }) - noteOfferedModels(session?.configOptions, session?.models) + if (gen === offeredGen) noteOfferedModels(session?.configOptions, session?.models) const caps = init?.agentCapabilities ?? {} if (session?.sessionId && (caps.sessionCapabilities?.close ?? caps.session_capabilities?.close)) { await conn.request("session/close", { sessionId: session.sessionId }).catch(() => {}) @@ -367,8 +412,11 @@ function probeOfferedOnce(agyPath) { const now = Date.now() if (offeredProbeAt !== null && now - offeredProbeAt < OFFERED_PROBE_RETRY_MS) return null offeredProbeAt = now - offeredProbePromise = probeOfferedModels(agyPath).finally(() => { offeredProbePromise = null }) - return offeredProbePromise + const probe = probeOfferedModels(agyPath).finally(() => { + if (offeredProbePromise === probe) offeredProbePromise = null + }) + offeredProbePromise = probe + return probe } function catalogOf(entry) { @@ -445,17 +493,49 @@ function fallbackModelCatalog(agyPath = null) { return { agyPath, list: FALLBACK_MODELS, catalog: null, offeredKey: null, isFallback: true, fetchedAt: 0, lastAttemptAt: null } } +// useAgyPath starts over when the executable changes: an old executable's +// dynamic additions and offer must not route to, or list for, a new one. +function useAgyPath(agyPath) { + if (cachedModelCatalog.agyPath === agyPath) return + // The first agy this process finds is not a change of one. + if (cachedModelCatalog.agyPath !== null) forgetOffered() + cachedModelCatalog = fallbackModelCatalog(agyPath) + modelCatalogRefreshPromise = null +} + +function storeListedCatalog(agyPath, list, lastAttemptAt) { + cachedModelCatalog = { + agyPath, + list, + catalog: null, + offeredKey: null, + isFallback: false, + fetchedAt: Date.now(), + lastAttemptAt, + } +} + +// catalogWarming is the background work that can still change what models() +// lists (the listing, then the session probe it starts), or null when there +// is none. Hosts that read the list once (OpenCode 2.x's adapter) read it +// again when this ends. +function catalogWarming() { + if (!modelCatalogRefreshPromise && !offeredProbePromise) return null + return (async () => { + if (modelCatalogRefreshPromise) await modelCatalogRefreshPromise.catch(() => {}) + if (offeredProbePromise) await offeredProbePromise.catch(() => {}) + return true + })() +} + // Magpie gives models() about two seconds, while `agy models` can take longer. // Serve the last catalog (static until the first success), refresh it in the // background at most once at a time, and wait for that refresh only briefly. // probeOffered: the caller is listing models (provider.models), not routing a // request that is about to open a session of its own. async function loadModelCatalog(agyPath, { probeOffered = false } = {}) { - if (cachedModelCatalog.agyPath !== agyPath) { - // An old executable's dynamic additions must not route to a new one. - cachedModelCatalog = fallbackModelCatalog(agyPath) - modelCatalogRefreshPromise = null - } + useAgyPath(agyPath) + if (probeOffered) wantOfferedProbe = true const current = cachedModelCatalog if (!agyPath) return catalogOf(current) const now = Date.now() @@ -469,15 +549,10 @@ async function loadModelCatalog(agyPath, { probeOffered = false } = {}) { current.lastAttemptAt = now const refresh = listAgyModels(agyPath).then((list) => { if (list?.length && cachedModelCatalog === current) { - cachedModelCatalog = { - agyPath, - list, - catalog: null, - offeredKey: null, - isFallback: false, - fetchedAt: Date.now(), - lastAttemptAt: current.lastAttemptAt, - } + storeListedCatalog(agyPath, list, current.lastAttemptAt) + // `agy models` answered, so agy is signed in: ask what agy acp + // offers now, while the host still waits for its first list. + if (wantOfferedProbe) probeOfferedOnce(agyPath) } }).catch(() => { // Keep the last successful catalog, including dynamic-only models. @@ -500,14 +575,7 @@ async function loadModelCatalog(agyPath, { probeOffered = false } = {}) { // `agy models` answered, so agy is signed in: learn what its ACP server // offers this client, waiting for it as briefly as for the listing. const probe = probeOffered && !cachedModelCatalog.isFallback ? probeOfferedOnce(agyPath) : null - if (probe) { - let timer - try { - await Promise.race([probe, new Promise((resolve) => { timer = setTimeout(resolve, MODEL_CATALOG_FAST_WAIT_MS) })]) - } finally { - clearTimeout(timer) - } - } + if (probe) await briefly(probe, MODEL_CATALOG_FAST_WAIT_MS) return catalogOf(cachedModelCatalog) } @@ -5257,6 +5325,9 @@ export const AntigravityBridgePlugin = async () => { callback: async (code, opts = {}) => { const done = await login.finish(code, { signal: opts.signal }) if (done.error) return { type: "failed", error: done.error } + // A new account: what agy offered the last one, or refused + // before the sign-in, is asked again. + forgetOffered() const probe = await probeSignedIn(agy) if (!probe.ok) return { type: "failed", error: `agy signed in, but ${probe.why}` } return { type: "success", key: "agy" } @@ -5480,6 +5551,9 @@ export const AntigravityBridgePlugin = async () => { provider: { id: PROVIDER_ID, npm: NPM_ANTHROPIC, + // Not an OpenCode hook: the adapter in opencode2.mjs reads the list + // once more when this background work ends. + warming: () => catalogWarming(), models: async () => { const agy = findAgy() // Never force a refresh here: Magpie times models() out after about two seconds. diff --git a/packages/agy-cli/opencode2.mjs b/packages/agy-cli/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/agy-cli/opencode2.mjs +++ b/packages/agy-cli/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index f7fa4f6..fee9046 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-agy-cli-auth", - "version": "0.1.31", + "version": "0.1.32", "description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI", "type": "module", "main": "./opencode.mjs", diff --git a/packages/agy-cli/test/catalog.mjs b/packages/agy-cli/test/catalog.mjs index 29ebeaa..6a0f3fd 100644 --- a/packages/agy-cli/test/catalog.mjs +++ b/packages/agy-cli/test/catalog.mjs @@ -72,6 +72,11 @@ test("首次 agy models 卡死时 750ms 内返回非空静态目录", async () = assert.equal(kit.timers.refed().length, 0, "agy models 卡死期间不应有挂住 Node 退出的模块定时器") await kit.finishChildren() assert.equal(kit.timers.refed().length, 0, "子进程结束后不应残留挂住退出的定时器") + // 探测一起就在 warming() 里:等它跑完再数,不靠固定时长。 + const warming = kit.plugin.provider.warming?.() + if (warming) await warming + await settle(kit) + assert.equal(kit.acpSpawns.length, 0, "agy models 没答出来时不应起探测会话") }) }) @@ -154,6 +159,90 @@ test("会话后来开放的型号变了,目录随之更新", async () => { }) }) +// #1064(hiee3):真实登录的 agy 上 `agy models` 约 4.4s、agy acp 开会话约 3s,都远过 500ms 的快速等待。 +// 这里各模拟 1.5s:第一次 models() 只能给静态目录,但列表一到就该起探测, +// 后台跑完后的第一次 models() 就要有 Claude 和 GPT-OSS,不该要第三次。 +const SLOW_MS = 1_500 +const OFFERED_CLAUDE = [ + ["gemini-4-argon-high", "Gemini 4 Argon (High)"], ["gemini-4-argon-low", "Gemini 4 Argon (Low)"], + ["claude-opus-5-5-high", "Claude Opus 5.5 (High)"], ["claude-opus-5-5-low", "Claude Opus 5.5 (Low)"], + ["claude-sonnet-5-5-high", "Claude Sonnet 5.5 (High)"], ["claude-sonnet-5-5-low", "Claude Sonnet 5.5 (Low)"], + ["gpt-oss-120b-medium", "GPT-OSS 120B (Medium)"], +] +const SLOW_AGY = { modelsBehavior: { stdout: modelsOutput(BASE_MODEL_ROWS), delayMs: SLOW_MS }, sessionNewDelayMs: SLOW_MS, offeredModels: OFFERED_CLAUDE } + +// 等后台的列表与探测都结束:等状态(没有在跑的假子进程,且插件说没有在热身),不等固定时长。 +async function warmed(kit) { + const warming = kit.plugin.provider.warming?.() + if (warming) await warming + for (let i = 0; i < 1000 && kit.children.size; i++) await sleep(10) + await settle(kit) +} + +function assertClaudeListed(models, label) { + for (const id of ["claude-opus-5-5", "claude-sonnet-5-5", "gpt-oss-120b"]) assert.ok(models[id], `${label}:没有 ${id},只有 ${ids(models).join(", ")}`) +} + +test("#1064 慢 agy:第一次 models() 给静态目录,后台热身后的第一次就列出 Claude 和 GPT-OSS", async () => { + await withKit(SLOW_AGY, async (kit) => { + const { value: first, ms } = await timed(() => kit.models()) + assert.ok(ms < FAST_BUDGET_MS, `首次目录耗时 ${Math.round(ms)}ms`) + assertFallbackPresent(first) + await warmed(kit) + assert.equal(kit.acpSpawns.length, 1, "列表一到就该起一次探测会话") + const { value: after, ms: warmMs } = await timed(() => kit.models()) + assert.ok(warmMs < 100, `热身后的目录耗时 ${Math.round(warmMs)}ms`) + assertClaudeListed(after, "热身后的第一次 models()") + assert.equal(kit.modelsSpawns.length, 1) + assert.equal(kit.acpSpawns.length, 1) + assert.equal(kit.plugin.provider.warming(), null, "热身完了不该还说在热身") + }) +}) + +test("#1064 慢 agy:已登录时 authorize 读到的列表和探测就是目录,紧接着的 models() 不再是静态目录", async () => { + await withKit(SLOW_AGY, async (kit) => { + const { value: started, ms } = await timed(() => kit.plugin.auth.methods[0].authorize()) + assert.equal(started.method, "auto") + assert.ok(ms < SLOW_MS * 2 + 3_000, `登录检查耗时 ${Math.round(ms)}ms`) + assert.equal((await started.callback()).type, "success") + const { value: listed, ms: listMs } = await timed(() => kit.models()) + assert.ok(listMs < 100, `登录后的目录耗时 ${Math.round(listMs)}ms`) + assertClaudeListed(listed, "登录后的第一次 models()") + assert.equal(kit.modelsSpawns.length, 1, "登录检查读过的列表不该再读一遍") + assert.equal(kit.acpSpawns.length, 1) + }) +}) + +test("#1064 登录检查不为探测等太久:会话一直不答时最多多等几秒", async () => { + await withKit({ ...SLOW_AGY, sessionNewDelayMs: 6_000 }, async (kit) => { + const { value: started, ms } = await timed(() => kit.plugin.auth.methods[0].authorize()) + assert.equal(started.method, "auto") + assert.ok(ms < SLOW_MS + 3_000 + 1_000, `登录检查等了 ${Math.round(ms)}ms`) + assert.ok(ms >= SLOW_MS + 2_500, `登录检查没等探测(${Math.round(ms)}ms)`) + const models = await kit.models() + assert.ok(models["gemini-4-argon"]) + assert.equal(models["claude-opus-5-5"], undefined, "探测没答前不该列出 Claude") + }) +}) + +test("#1064 agyPath 变了,旧 agy 开放的型号不再列出,新 agy 重新探测", async () => { + await withKit({ modelsBehavior: DYNAMIC_OK, offeredModels: OFFERED_CLAUDE }, async (kit) => { + await kit.models() + await warmed(kit) + assertClaudeListed(await kit.models(), "agy-a") + kit.env.AGY_PATH = kit.makeFakeAgy("agy-b") + kit.offeredModels = [["gemini-4-argon-high", "Gemini 4 Argon (High)"], ["gemini-4-argon-low", "Gemini 4 Argon (Low)"]] + const switched = await kit.models() + assert.deepEqual(ids(switched).filter(id => /^(?:claude|gpt)-/.test(id)), [], "换了 agy 还在列旧 agy 开放的型号") + await warmed(kit) + assert.equal(kit.acpSpawns.length, 2, "新 agy 应重新探测") + assert.equal(kit.acpSpawns.at(-1).file, kit.env.AGY_PATH) + const after = await kit.models() + assert.equal(after["claude-opus-5-5"], undefined) + assert.ok(after["gemini-4-argon"]) + }) +}) + test("握手报出的 agy 版本低于 1.4.12 时拒绝会话并说明原因", async () => { await withKit({ agentVersion: "1.4.11" }, async (kit) => { kit.defaultPrompt = async (agent) => { diff --git a/packages/agy-cli/test/testkit.mjs b/packages/agy-cli/test/testkit.mjs index 74cc6bf..a197939 100644 --- a/packages/agy-cli/test/testkit.mjs +++ b/packages/agy-cli/test/testkit.mjs @@ -264,7 +264,9 @@ class FakeAgy { const models = { currentModelId: this.kit.initialAgyModel } // What agy offers this client in the session; unset, the session names none. if (this.kit.offeredModels) models.availableModels = this.kit.offeredModels.map(([modelId, name]) => ({ modelId, name })) - this.respond(id, { sessionId: this.sessionId, models }) + // A real signed-in agy takes ~3s to open a session (#1064). + if (this.kit.sessionNewDelayMs) setTimeout(() => this.respond(id, { sessionId: this.sessionId, models }), this.kit.sessionNewDelayMs) + else this.respond(id, { sessionId: this.sessionId, models }) } else if (method === "session/set_model") { this.kit.setModels.push(params.modelId) this.respond(id, {}) @@ -424,6 +426,7 @@ export async function createBridgeKit(options = {}) { initialAgyModel: options.initialAgyModel ?? "gemini-3.8-flash-low", offeredModels: options.offeredModels ?? null, sessionNewError: options.sessionNewError ?? null, + sessionNewDelayMs: options.sessionNewDelayMs ?? 0, sessionResume: options.sessionResume ?? false, agentVersion: options.agentVersion ?? "1.5.0", setModels: [], diff --git a/packages/antigravity/opencode2.mjs b/packages/antigravity/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/antigravity/opencode2.mjs +++ b/packages/antigravity/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/astudio/opencode2.mjs b/packages/astudio/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/astudio/opencode2.mjs +++ b/packages/astudio/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/atomcode/opencode2.mjs b/packages/atomcode/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/atomcode/opencode2.mjs +++ b/packages/atomcode/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/autoclaw/opencode2.mjs b/packages/autoclaw/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/autoclaw/opencode2.mjs +++ b/packages/autoclaw/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/catpaw/opencode2.mjs b/packages/catpaw/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/catpaw/opencode2.mjs +++ b/packages/catpaw/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/cline/opencode2.mjs b/packages/cline/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/cline/opencode2.mjs +++ b/packages/cline/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/cloudflare-workers-ai/opencode2.mjs b/packages/cloudflare-workers-ai/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/cloudflare-workers-ai/opencode2.mjs +++ b/packages/cloudflare-workers-ai/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/codearts/opencode2.mjs b/packages/codearts/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/codearts/opencode2.mjs +++ b/packages/codearts/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/commandcode/opencode2.mjs b/packages/commandcode/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/commandcode/opencode2.mjs +++ b/packages/commandcode/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/copilot/opencode2.mjs b/packages/copilot/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/copilot/opencode2.mjs +++ b/packages/copilot/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/cursor/opencode2.mjs b/packages/cursor/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/cursor/opencode2.mjs +++ b/packages/cursor/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/doubao-work/opencode2.mjs b/packages/doubao-work/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/doubao-work/opencode2.mjs +++ b/packages/doubao-work/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/factory/opencode2.mjs b/packages/factory/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/factory/opencode2.mjs +++ b/packages/factory/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/gcp-ai/opencode2.mjs b/packages/gcp-ai/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/gcp-ai/opencode2.mjs +++ b/packages/gcp-ai/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/grok/opencode2.mjs b/packages/grok/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/grok/opencode2.mjs +++ b/packages/grok/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/gumloop/opencode2.mjs b/packages/gumloop/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/gumloop/opencode2.mjs +++ b/packages/gumloop/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/inkstone/opencode2.mjs b/packages/inkstone/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/inkstone/opencode2.mjs +++ b/packages/inkstone/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/joycode/opencode2.mjs b/packages/joycode/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/joycode/opencode2.mjs +++ b/packages/joycode/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/kimi-work/opencode2.mjs b/packages/kimi-work/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/kimi-work/opencode2.mjs +++ b/packages/kimi-work/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/kiro/opencode2.mjs b/packages/kiro/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/kiro/opencode2.mjs +++ b/packages/kiro/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/loomy/opencode2.mjs b/packages/loomy/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/loomy/opencode2.mjs +++ b/packages/loomy/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/mclaw/opencode2.mjs b/packages/mclaw/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/mclaw/opencode2.mjs +++ b/packages/mclaw/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/mimo/opencode2.mjs b/packages/mimo/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/mimo/opencode2.mjs +++ b/packages/mimo/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/minimax/opencode2.mjs b/packages/minimax/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/minimax/opencode2.mjs +++ b/packages/minimax/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/nous/opencode2.mjs b/packages/nous/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/nous/opencode2.mjs +++ b/packages/nous/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/openai-decisions/opencode2.mjs b/packages/openai-decisions/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/openai-decisions/opencode2.mjs +++ b/packages/openai-decisions/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/opendesign/opencode2.mjs b/packages/opendesign/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/opendesign/opencode2.mjs +++ b/packages/opendesign/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/orcaterm/opencode2.mjs b/packages/orcaterm/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/orcaterm/opencode2.mjs +++ b/packages/orcaterm/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/qoder/opencode2.mjs b/packages/qoder/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/qoder/opencode2.mjs +++ b/packages/qoder/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/raccoon/opencode2.mjs b/packages/raccoon/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/raccoon/opencode2.mjs +++ b/packages/raccoon/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/river/opencode2.mjs b/packages/river/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/river/opencode2.mjs +++ b/packages/river/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/sap-aicore/opencode2.mjs b/packages/sap-aicore/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/sap-aicore/opencode2.mjs +++ b/packages/sap-aicore/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/sensenova/opencode2.mjs b/packages/sensenova/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/sensenova/opencode2.mjs +++ b/packages/sensenova/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/tabbit/opencode2.mjs b/packages/tabbit/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/tabbit/opencode2.mjs +++ b/packages/tabbit/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/trae/opencode2.mjs b/packages/trae/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/trae/opencode2.mjs +++ b/packages/trae/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/warp/opencode2.mjs b/packages/warp/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/warp/opencode2.mjs +++ b/packages/warp/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/workbuddy/opencode2.mjs b/packages/workbuddy/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/workbuddy/opencode2.mjs +++ b/packages/workbuddy/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/zcode/opencode2.mjs b/packages/zcode/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/zcode/opencode2.mjs +++ b/packages/zcode/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/zed/opencode2.mjs b/packages/zed/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/zed/opencode2.mjs +++ b/packages/zed/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/packages/zen-free/opencode2.mjs b/packages/zen-free/opencode2.mjs index 364ea8a..b939308 100644 --- a/packages/zen-free/opencode2.mjs +++ b/packages/zen-free/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/scripts/opencode2.mjs b/scripts/opencode2.mjs index 364ea8a..b939308 100644 --- a/scripts/opencode2.mjs +++ b/scripts/opencode2.mjs @@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) { } } catch {} })() - void refresh() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) } export async function setup(ctx, mod = v1) { diff --git a/scripts/opencode2.test.mjs b/scripts/opencode2.test.mjs index e8f37da..6e9b2f0 100644 --- a/scripts/opencode2.test.mjs +++ b/scripts/opencode2.test.mjs @@ -271,3 +271,63 @@ test("a 1.x code sign-in is a 2.x code sign-in", async () => { const again = await f.methods[0].authorize({}) await expect(again.callback("bad")).rejects.toThrow() }) + +// a plugin whose first list is its static one while it reads the real one +// in the background (agy-cli, plugins#1064): 2.x reads provider.models once +// at startup, so the adapter reads it once more when provider.warming() +// ends, and only once +function slow(state) { + return { + SlowPlugin: async () => ({ + auth: { provider: "slow", methods: [{ type: "api", label: "Slow key" }] }, + config: async (cfg) => { + cfg.provider.slow = { npm: "@ai-sdk/openai-compatible", name: "Slow", models: { m1: { name: "M1" } } } + }, + provider: { + id: "slow", + warming: () => state.warming(), + models: async () => { + state.reads++ + return state.ready ? { full: { name: "Full" } } : { stub: { name: "Stub" } } + }, + }, + }), + } +} + +test("a list still warming when 2.x first reads it is read once more when it ends (plugins#1064)", async () => { + const { setup } = await import(join(packages, "agy-cli", "opencode2.mjs")) + let release + const state = { reads: 0, ready: false, warmings: 0 } + state.warming = () => { + state.warmings++ + return state.ready ? null : new Promise((r) => (release = r)) + } + const f = fakeContext() + f.conns.set("slow", { connection: { type: "credential", id: "cred_1" }, value: { type: "key", key: "sk" } }) + const stop = await setup(f.ctx, slow(state)) + await until(() => f.reloads.model === 1 && !!release) + expect(f.providers()[0].models.map((m) => m.name)).toEqual(["Stub"]) + expect(state.reads).toBe(1) + state.ready = true + release(true) + await until(() => f.reloads.model === 2) + expect(f.providers()[0].models.map((m) => m.name)).toEqual(["Full"]) + // once: the second read doesn't ask again, so nothing loops + await new Promise((r) => setTimeout(r, 20)) + expect(state.reads).toBe(2) + expect(state.warmings).toBe(1) + stop() +}) + +test("a plugin with nothing warming is read once at startup", async () => { + const { setup } = await import(join(packages, "agy-cli", "opencode2.mjs")) + const state = { reads: 0, ready: true, warming: () => null } + const f = fakeContext() + f.conns.set("slow", { connection: { type: "credential", id: "cred_1" }, value: { type: "key", key: "sk" } }) + const stop = await setup(f.ctx, slow(state)) + await until(() => f.reloads.model === 1) + await new Promise((r) => setTimeout(r, 20)) + expect(state.reads).toBe(1) + stop() +})