Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): xAI 未登录时模型发现干净跳过,不再抛 FATAL unhandledRejection (#3039)
Battleplusf17f911a5d3d
diff --git a/apps/desktop/src/main/maker-host/__tests__/xaiModelDiscovery.test.ts b/apps/desktop/src/main/maker-host/__tests__/xaiModelDiscovery.test.tsindex e6284bf744..53fcfd9bc5 100644--- a/apps/desktop/src/main/maker-host/__tests__/xaiModelDiscovery.test.ts+++ b/apps/desktop/src/main/maker-host/__tests__/xaiModelDiscovery.test.ts@@ -123,6 +123,33 @@ describe('xAI account model discovery', () => {expect(applySnapshot).toHaveBeenCalledWith([]);});+ it('skips discovery cleanly when no access token is available (not logged in)', async () => {+ const dir = await fsp.mkdtemp(path.join(os.tmpdir(), 'cindy-xai-models-'));+ tempDirs.push(dir);+ const cacheFile = path.join(dir, 'xai-models.json');+ const applySnapshot = vi.fn();+ const fetchImpl = vi.fn();+ const deps = {+ fetchImpl: fetchImpl as typeof fetch,+ getAccessToken: async () => {+ throw new Error('xAI 未登录:请先在「设置 → 模型供应商」登录 xAI(SuperGrok)');+ },+ peekAccessToken: () => null,+ hasLogin: () => false,+ getConnectionSource: () => 'explicit-provider-oauth' as const,+ getScopeKey: () => 'owner-a:1',+ cacheFilePath: () => cacheFile,+ applySnapshot,+ invalidateAuth: vi.fn(),+ log: { info: vi.fn(), warn: vi.fn() },+ };++ await expect(refreshXaiModelsFromHttp(deps)).resolves.toBe(false);+ expect(fetchImpl).not.toHaveBeenCalled();+ expect(applySnapshot).not.toHaveBeenCalled();+ expect(deps.log.warn).toHaveBeenCalledTimes(1);+ });+it('keeps the current snapshot on temporary failure and never applies late account results', async () => {const applySnapshot = vi.fn();await expect(@@ -373,4 +400,48 @@ describe('xAI account model discovery', () => {expect(fetchImpl).not.toHaveBeenCalled();expect(applySnapshot).not.toHaveBeenCalled();});++ it('warns and returns false when token refresh after invalidateAuth fails', async () => {+ let token = 'token-old';+ let getAccessTokenCalls = 0;+ const applySnapshot = vi.fn();+ const invalidateAuth = vi.fn(async () => 'refreshed' as const);+ const fetchImpl = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {+ const auth = new Headers(init?.headers).get('authorization');+ if (auth === 'Bearer token-old') {+ return jsonResponse(+ { code: 'unauthenticated:bad-credentials', error: 'bad creds' },+ 403,+ );+ }+ // Should never reach here because second getAccessToken fails+ throw new Error('unexpected fetch after token failure');+ });+ const warn = vi.fn();+ await expect(+ refreshXaiModelsFromHttp({+ fetchImpl: fetchImpl as typeof fetch,+ getAccessToken: async () => {+ getAccessTokenCalls += 1;+ if (getAccessTokenCalls === 1) return token;+ throw new Error('token refresh failed after invalidate');+ },+ peekAccessToken: () => token,+ hasLogin: () => true,+ getConnectionSource: () => 'explicit-provider-oauth' as const,+ getScopeKey: () => 'owner-a:1',+ cacheFilePath: () => '/unused',+ applySnapshot,+ invalidateAuth,+ log: { info: vi.fn(), warn },+ }),+ ).resolves.toBe(false);+ expect(invalidateAuth).toHaveBeenCalledTimes(1);+ expect(warn).toHaveBeenCalledWith(+ 'xAI account model discovery: token refresh after invalidate failed',+ expect.objectContaining({ error: 'token refresh failed after invalidate' }),+ );+ expect(applySnapshot).not.toHaveBeenCalled();+ });+});diff --git a/apps/desktop/src/main/maker-host/model-discovery/xai.ts b/apps/desktop/src/main/maker-host/model-discovery/xai.tsindex 34f5ab9586..d032461485 100644--- a/apps/desktop/src/main/maker-host/model-discovery/xai.ts+++ b/apps/desktop/src/main/maker-host/model-discovery/xai.ts@@ -362,7 +362,19 @@ async function runRefresh(const connectionSource = deps.getConnectionSource();if (connectionSource !== 'explicit-provider-oauth') return false;const scopeKey = deps.getScopeKey();- let token = await deps.getAccessToken();+ let token: string;+ try {+ token = await deps.getAccessToken();+ } catch (error) {+ // getGrokAccessToken throws when xAI is not logged in or its token cannot+ // be refreshed — the bridge turns that into a 502 per request. Discovery+ // runs from startup/readiness paths that have no catch site, so degrade to+ // a clean skip instead of surfacing as an unhandled rejection.+ deps.log.warn('xAI account model discovery skipped: no usable access token', {+ error: error instanceof Error ? error.message : String(error),+ });+ return false;+ }onAccessTokenResolved?.(token);if (!isCurrent(deps, scopeKey, token)) return false;for (let attempt = 0; attempt < 2; attempt += 1) {@@ -393,7 +405,14 @@ async function runRefresh(});if (outcome !== 'refreshed' && outcome !== 'superseded') return false;if (deps.getScopeKey() !== scopeKey || !deps.hasLogin()) return false;- token = await deps.getAccessToken();+ try {+ token = await deps.getAccessToken();+ } catch (tokenError) {+ deps.log.warn('xAI account model discovery: token refresh after invalidate failed', {+ error: tokenError instanceof Error ? tokenError.message : String(tokenError),+ });+ return false;+ }if (!isCurrent(deps, scopeKey, token)) return false;}}