Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): keep providers available when optional Pi is unavailable (#1468)
陈柏宇820567605fa8
diff --git a/apps/desktop/src/renderer/__tests__/agentCapabilitiesDeviceCache.test.ts b/apps/desktop/src/renderer/__tests__/agentCapabilitiesDeviceCache.test.tsindex 1ba141fb12..ebafd8bfe1 100644--- a/apps/desktop/src/renderer/__tests__/agentCapabilitiesDeviceCache.test.ts+++ b/apps/desktop/src/renderer/__tests__/agentCapabilitiesDeviceCache.test.ts@@ -41,6 +41,33 @@ function stubElectron() {return { getCapabilities, invoke };}+function stubLocalCatalog() {+ let providerId = 'provider-old';+ let capabilityRevision = 'old';+ let unavailableAgent: string | null = null;+ const getCapabilities = vi.fn(async (agent: string) => {+ if (agent === unavailableAgent) throw new Error(`Agent '${agent}' is not registered`);+ return caps(`${capabilityRevision}:${agent}`);+ });+ const listProviders = vi.fn(async () => ({+ dataOwnerId: null,+ ownerGeneration: 0,+ providers: [{ id: providerId }],+ providerOrder: [providerId],+ }));+ vi.stubGlobal('window', { electronAPI: { maker: { getCapabilities, listProviders } } });+ return {+ getCapabilities,+ setSnapshot(nextProviderId: string, nextCapabilityRevision: string): void {+ providerId = nextProviderId;+ capabilityRevision = nextCapabilityRevision;+ },+ setUnavailableAgent(agent: string | null): void {+ unavailableAgent = agent;+ },+ };+}+describe('useAgentCapabilities deviceId-aware cache', () => {it('本机路径:preload 命中 maker.getCapabilities,不碰 deviceLink', async () => {const { getCapabilities, invoke } = stubElectron();@@ -55,7 +82,7 @@ describe('useAgentCapabilities deviceId-aware cache', () => {);});- it('本机目录热刷新会原子替换两个 agent 的缓存快照', async () => {+ it('本机目录热刷新会原子替换核心 agent 的缓存快照', async () => {const { getCapabilities } = stubElectron();const mod = await import('@/hooks/useAgentCapabilities');await mod.preloadAllCapabilities();@@ -71,6 +98,94 @@ describe('useAgentCapabilities deviceId-aware cache', () => {);});+ it('本机目录快照在可选 Pi 不可用时仍返回 Claude Code 与 Codex 能力', async () => {+ const { getCapabilities } = stubElectron();+ getCapabilities.mockImplementation(async (agent: string) => {+ if (agent === 'pi') throw new Error("Agent 'pi' is not registered");+ return caps(`local:${agent}`);+ });+ const mod = await import('@/hooks/useAgentCapabilities');++ await expect(mod.loadLocalCapabilitiesSnapshot()).resolves.toEqual([+ ['claude-code', caps('local:claude-code')],+ ['codex', caps('local:codex')],+ ]);+ expect(getCapabilities).toHaveBeenCalledWith('pi');+ });++ it('本机目录快照在核心 agent 不可用时仍拒绝提交部分能力', async () => {+ const { getCapabilities } = stubElectron();+ getCapabilities.mockImplementation(async (agent: string) => {+ if (agent === 'codex') throw new Error("Agent 'codex' is not registered");+ return caps(`local:${agent}`);+ });+ const mod = await import('@/hooks/useAgentCapabilities');++ await expect(mod.loadLocalCapabilitiesSnapshot()).rejects.toThrow(+ "Agent 'codex' is not registered",+ );+ });++ it('Pi 不可用且没有旧快照时仍联合提交 provider 与核心能力', async () => {+ const harness = stubLocalCatalog();+ harness.setSnapshot('provider-initial', 'initial');+ harness.setUnavailableAgent('pi');+ const catalog = await import('@/lib/localCatalogSnapshot');+ const providers = await import('@/lib/providersSnapshotStore');+ const capabilities = await import('@/hooks/useAgentCapabilities');++ await expect(catalog.refreshLocalCatalogSnapshot()).resolves.toBe(true);++ expect(providers.getCachedProvidersSnapshot()?.providers).toEqual([{ id: 'provider-initial' }]);+ expect(capabilities.getCachedCapabilities('claude-code')?.availableModels[0].displayName).toBe(+ 'initial:claude-code',+ );+ expect(capabilities.getCachedCapabilities('codex')?.availableModels[0].displayName).toBe(+ 'initial:codex',+ );+ expect(capabilities.getCachedCapabilities('pi')).toBeNull();+ });++ it('Pi 不可用时用新 provider 快照替换 last-valid snapshot', async () => {+ const harness = stubLocalCatalog();+ const catalog = await import('@/lib/localCatalogSnapshot');+ const providers = await import('@/lib/providersSnapshotStore');+ const capabilities = await import('@/hooks/useAgentCapabilities');+ await expect(catalog.refreshLocalCatalogSnapshot()).resolves.toBe(true);++ harness.setSnapshot('provider-new', 'new');+ harness.setUnavailableAgent('pi');+ await expect(catalog.refreshLocalCatalogSnapshot()).resolves.toBe(true);++ expect(providers.getCachedProvidersSnapshot()?.providers).toEqual([{ id: 'provider-new' }]);+ expect(capabilities.getCachedCapabilities('claude-code')?.availableModels[0].displayName).toBe(+ 'new:claude-code',+ );+ expect(capabilities.getCachedCapabilities('codex')?.availableModels[0].displayName).toBe(+ 'new:codex',+ );+ });++ it('核心 agent 失败时联合刷新保留 last-valid provider 与能力快照', async () => {+ const harness = stubLocalCatalog();+ const catalog = await import('@/lib/localCatalogSnapshot');+ const providers = await import('@/lib/providersSnapshotStore');+ const capabilities = await import('@/hooks/useAgentCapabilities');+ await expect(catalog.refreshLocalCatalogSnapshot()).resolves.toBe(true);++ harness.setSnapshot('provider-rejected', 'rejected');+ harness.setUnavailableAgent('codex');+ await expect(catalog.refreshLocalCatalogSnapshot()).resolves.toBe(false);++ expect(providers.getCachedProvidersSnapshot()?.providers).toEqual([{ id: 'provider-old' }]);+ expect(capabilities.getCachedCapabilities('claude-code')?.availableModels[0].displayName).toBe(+ 'old:claude-code',+ );+ expect(capabilities.getCachedCapabilities('codex')?.availableModels[0].displayName).toBe(+ 'old:codex',+ );+ });+it('远程路径:prefetch 命中 deviceLink.invoke(maker:get-capabilities),不碰本地 maker', async () => {const { getCapabilities, invoke } = stubElectron();const mod = await import('@/hooks/useAgentCapabilities');diff --git a/apps/desktop/src/renderer/hooks/useAgentCapabilities.ts b/apps/desktop/src/renderer/hooks/useAgentCapabilities.tsindex 4dc60865d2..7b46b79047 100644--- a/apps/desktop/src/renderer/hooks/useAgentCapabilities.ts+++ b/apps/desktop/src/renderer/hooks/useAgentCapabilities.ts@@ -493,12 +493,25 @@ export function beginLocalCapabilitiesRefresh(): number {return localGen;}-/** 读取两种 agent 的完整能力快照;失败向上抛,不触碰现有缓存。 */+/**+ * 读取本地 agent 能力快照;核心 agent 失败向上抛,可选 Pi 的能力读取失败不阻断 provider 目录。+ */export async function loadLocalCapabilitiesSnapshot(): Promise<LocalCapabilitiesSnapshot> {const api = getMakerApi();if (!api) throw new Error('maker IPC not available');- return Promise.all(- ALL_AGENT_KINDS.map(async (agent) => [agent, await api.getCapabilities(agent)] as const),+ const entries = await Promise.all(+ ALL_AGENT_KINDS.map(async (agent): Promise<readonly [AgentKind, AgentCapabilities] | null> => {+ try {+ return [agent, await api.getCapabilities(agent)] as const;+ } catch (error) {+ if (agent !== 'pi') throw error;+ log.warn('optional Pi capabilities unavailable; continuing with core agents:', error);+ return null;+ }+ }),+ );+ return entries.filter(+ (entry): entry is readonly [AgentKind, AgentCapabilities] => entry !== null,);}@@ -506,7 +519,7 @@ export function isLocalCapabilitiesRefreshCurrent(generation: number): boolean {return localGen === generation;}-/** 仅提交当前代际的完整能力快照,并在提交后统一通知 mounted hooks。 */+/** 仅提交当前代际的可用能力快照,并在提交后统一通知 mounted hooks。 */export function commitLocalCapabilitiesSnapshot(generation: number,entries: LocalCapabilitiesSnapshot,@@ -520,8 +533,8 @@ export function commitLocalCapabilitiesSnapshot(}/**- * Codex auth/discovery 广播后的本地热刷新。旧 cache 在两个 IPC 都成功前继续可读;完成后同时- * 替换 cache 并通知 mounted hooks,避免 provider:list 已更新而 scheduler/selector 仍用旧能力。+ * Codex auth/discovery 广播后的本地热刷新。旧 cache 在核心 IPC 都成功前继续可读;完成后+ * 同时替换 cache 并通知 mounted hooks,避免 provider:list 已更新而 scheduler/selector 仍用旧能力。*/export async function refreshLocalCapabilities(): Promise<void> {const generation = beginLocalCapabilitiesRefresh();diff --git a/apps/desktop/src/renderer/lib/localCatalogSnapshot.ts b/apps/desktop/src/renderer/lib/localCatalogSnapshot.tsindex 18b4af584a..70ac66511d 100644--- a/apps/desktop/src/renderer/lib/localCatalogSnapshot.ts+++ b/apps/desktop/src/renderer/lib/localCatalogSnapshot.ts@@ -1,8 +1,9 @@/*** 本地 provider catalog 的 renderer 原子刷新协调器。*- * providers 与两份 agent capabilities 必须来自同一轮 main 快照:三次 IPC 全部成功、- * 且期间没有更新一代目录时才同步提交。失败或乱序结果一律保留上一份有效快照。+ * providers 与核心 agent capabilities 必须来自同一轮 main 快照:核心 IPC 全部成功、+ * 且期间没有更新一代目录时才同步提交。可选 Pi 的能力读取失败时提交核心能力;其它失败+ * 或乱序结果一律保留上一份有效快照。* device-link 的远端 capabilities 不经过这里,继续按 deviceId 独立缓存。*/import { createLogger } from '@/lib/logger';@@ -22,7 +23,7 @@ import {const log = createLogger('localCatalogSnapshot');let refreshGeneration = 0;-/** 联合刷新 providers + Claude/Codex capabilities,并只提交最新的完整结果。 */+/** 联合刷新 providers + 可用 agent capabilities,并只提交最新的完整结果。 */export async function refreshLocalCatalogSnapshot(): Promise<boolean> {const generation = ++refreshGeneration;const providersGeneration = beginProvidersRefresh();@@ -34,9 +35,9 @@ export async function refreshLocalCatalogSnapshot(): Promise<boolean> {loadLocalCapabilitiesSnapshot(),]);if (- refreshGeneration !== generation- || !isProvidersRefreshCurrent(providersGeneration, providers)- || !isLocalCapabilitiesRefreshCurrent(capabilitiesGeneration)+ refreshGeneration !== generation ||+ !isProvidersRefreshCurrent(providersGeneration, providers) ||+ !isLocalCapabilitiesRefreshCurrent(capabilitiesGeneration)) {return false;}@@ -53,7 +54,7 @@ export async function refreshLocalCatalogSnapshot(): Promise<boolean> {}}-/** 启动预热保留原有三次瞬时 IPC 重试语义;每次仍按完整快照原子提交。 */+/** 启动预热保留原有三次瞬时 IPC 重试语义;每次仍按可用快照原子提交。 */export async function preloadLocalCatalogSnapshot(): Promise<void> {for (let attempt = 0; attempt < 3; attempt += 1) {if (await refreshLocalCatalogSnapshot()) return;