Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

refactor(model-access): Key 失效上报的默认依赖移入 gatewayKeyRejection,收窄 index.ts 改动

fmfsaisaic4478308a1bc
diff --git a/apps/desktop/src/main/model-access/__tests__/gatewayKeyRejection.test.ts b/apps/desktop/src/main/model-access/__tests__/gatewayKeyRejection.test.ts
index a0b38365aa..f0cde02d67 100644
--- a/apps/desktop/src/main/model-access/__tests__/gatewayKeyRejection.test.ts
+++ b/apps/desktop/src/main/model-access/__tests__/gatewayKeyRejection.test.ts
@@ -5,6 +5,29 @@
*/
import { afterEach, describe, expect, it, vi } from 'vitest';
+// 单例的默认依赖(仅 singleton 用例经 install 使用;工厂用例全部显式注入)。
+const defaults = vi.hoisted(() => ({
+ canUseCindyGateway: true,
+ boundaryPending: false,
+ source: 'server' as 'server' | 'manual' | null,
+ currentKey: null as string | null,
+}));
+vi.mock('../../appCapabilities.js', () => ({
+ getAppCapabilities: () => ({ canUseCindyGateway: defaults.canUseCindyGateway }),
+}));
+vi.mock('../../appSessionState.js', () => ({
+ isAppSessionBoundaryPending: () => defaults.boundaryPending,
+}));
+vi.mock('../../logger.js', () => ({
+ createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),
+}));
+vi.mock('../../secrets/providerSecretStore.js', () => ({
+ getProviderSecretStore: () => ({ get: (id: string) => (id === 'xd' ? defaults.currentKey : null) }),
+}));
+vi.mock('../credentialsStore.js', () => ({
+ getModelAccessCredentialsStore: () => ({ getSource: () => defaults.source }),
+}));
+
import {
bearerKeyFromHeaders,
createGatewayKeyRejectionReporter,
@@ -225,13 +248,40 @@ describe('reportCindyGatewayKeyRejected singleton', () => {
expect(() => reportCindyGatewayKeyRejected(KEY_A)).not.toThrow();
});
- it('delegates both entries to the installed reporter', () => {
- const reporter = { reportUsedKey: vi.fn(), reportCurrentKey: vi.fn() };
- installCindyGatewayKeyRejectionReporter(reporter);
+ it('wires both entries to the default local dependencies and the installed sync', () => {
+ Object.assign(defaults, { canUseCindyGateway: true, boundaryPending: false, source: 'server', currentKey: KEY_A });
+ const requestSync = vi.fn(async () => undefined);
+ installCindyGatewayKeyRejectionReporter(requestSync);
+
+ reportCindyGatewayKeyRejectedForCurrentKey();
+ expect(requestSync).toHaveBeenCalledTimes(1);
+ reportCindyGatewayKeyRejected(KEY_B); // 规则 A:不是当前 Key
+ expect(requestSync).toHaveBeenCalledTimes(1);
+ reportCindyGatewayKeyRejected(KEY_A);
+ reportCindyGatewayKeyRejected(KEY_A);
+ expect(requestSync).toHaveBeenCalledTimes(2);
+ });
+
+ it.each([
+ ['gateway capability is unavailable', { canUseCindyGateway: false }],
+ ['an account switch is pending', { boundaryPending: true }],
+ ['the key was entered manually', { source: 'manual' as const }],
+ ])('default dependencies skip when %s', (_label, overrides) => {
+ Object.assign(defaults, { canUseCindyGateway: true, boundaryPending: false, source: 'server', currentKey: KEY_A }, overrides);
+ const requestSync = vi.fn(async () => undefined);
+ installCindyGatewayKeyRejectionReporter(requestSync);
reportCindyGatewayKeyRejected(KEY_A);
reportCindyGatewayKeyRejectedForCurrentKey();
- expect(reporter.reportUsedKey).toHaveBeenCalledWith(KEY_A);
- expect(reporter.reportCurrentKey).toHaveBeenCalledTimes(1);
+ expect(requestSync).not.toHaveBeenCalled();
+ });
+
+ it('uninstalls with null', () => {
+ Object.assign(defaults, { canUseCindyGateway: true, boundaryPending: false, source: 'server', currentKey: KEY_A });
+ const requestSync = vi.fn(async () => undefined);
+ installCindyGatewayKeyRejectionReporter(requestSync);
+ installCindyGatewayKeyRejectionReporter(null);
+ reportCindyGatewayKeyRejected(KEY_A);
+ expect(requestSync).not.toHaveBeenCalled();
});
it('the current-key entry is a no-op before model-access installs a reporter', () => {
diff --git a/apps/desktop/src/main/model-access/gatewayKeyRejection.ts b/apps/desktop/src/main/model-access/gatewayKeyRejection.ts
index ea18c71b72..542986418b 100644
--- a/apps/desktop/src/main/model-access/gatewayKeyRejection.ts
+++ b/apps/desktop/src/main/model-access/gatewayKeyRejection.ts
@@ -1,6 +1,11 @@
import { createHash } from 'node:crypto';
import type { ModelAccessCredentialSource } from '../../shared/modelAccess.js';
+import { getAppCapabilities } from '../appCapabilities.js';
+import { isAppSessionBoundaryPending } from '../appSessionState.js';
+import { createLogger } from '../logger.js';
+import { getProviderSecretStore } from '../secrets/providerSecretStore.js';
+import { getModelAccessCredentialsStore } from './credentialsStore.js';
/**
* gatewayKeyRejection.ts — Cindy AI Key 被网关拒绝(HTTP 401)后的统一上报入口。
@@ -18,9 +23,11 @@ import type { ModelAccessCredentialSource } from '../../shared/modelAccess.js';
* 同步用 `sync()` 而不是 `retry()`:它尊重 unsupported / disabled 终态,自带 single-flight
* 与内部重试;失败沿用现有同步表现,这里不新增提示。
*
- * 依赖全部注入(工厂函数),进程内单例由 model-access/index.ts 在 initModelAccess 时组装
- * 并安装。调用方从本文件导入 `reportCindyGatewayKeyRejected`,不直接依赖 index.ts——
- * 后者会拉入 maker-host 等整张主进程依赖图,不适合被底层网络模块反向导入。
+ * 工厂函数的依赖全部注入,便于单测。进程内单例的默认依赖在本文件组装,只有凭据同步
+ * (`requestSync`)由 model-access/index.ts 在 initModelAccess 时经
+ * `installCindyGatewayKeyRejectionReporter` 提供。调用方从本文件导入上报入口,不直接依赖
+ * index.ts——后者会拉入 maker-host 等整张主进程依赖图,不适合被底层网络模块反向导入;
+ * 本文件的默认依赖也都不会反向导入 index.ts。
*/
export interface GatewayKeyRejectionDeps {
@@ -123,11 +130,29 @@ export function createGatewayKeyRejectionReporter(
let installedReporter: GatewayKeyRejectionReporter | null = null;
-/** model-access/index.ts 初始化时安装进程内单例;传 null 卸载(仅测试复位用)。 */
+/**
+ * model-access/index.ts 初始化时安装进程内单例,只需提供凭据同步;其余依赖用本机默认实现。
+ * 传 null 卸载(仅测试复位用)。
+ */
export function installCindyGatewayKeyRejectionReporter(
- reporter: GatewayKeyRejectionReporter | null,
+ requestSync: (() => Promise<unknown>) | null,
): void {
- installedReporter = reporter;
+ if (!requestSync) {
+ installedReporter = null;
+ return;
+ }
+ const log = createLogger('modelAccess');
+ installedReporter = createGatewayKeyRejectionReporter({
+ canUseCindyGateway: () => getAppCapabilities().canUseCindyGateway,
+ isSessionBoundaryPending: isAppSessionBoundaryPending,
+ getCredentialSource: () => getModelAccessCredentialsStore().getSource(),
+ readCurrentKey: () => getProviderSecretStore().get('xd'),
+ requestSync,
+ log: {
+ info: (msg) => log.info(msg),
+ warn: (msg) => log.warn(msg),
+ },
+ });
}
/**
diff --git a/apps/desktop/src/main/model-access/index.ts b/apps/desktop/src/main/model-access/index.ts
index d3a6f745a7..9d15e5567e 100644
--- a/apps/desktop/src/main/model-access/index.ts
+++ b/apps/desktop/src/main/model-access/index.ts
@@ -36,11 +36,7 @@ import {
} from '../../shared/modelAccess.js';
import { getModelAccessCredentialsStore } from './credentialsStore.js';
import { getAppCapabilities } from '../appCapabilities.js';
-import { isAppSessionBoundaryPending } from '../appSessionState.js';
-import {
- createGatewayKeyRejectionReporter,
- installCindyGatewayKeyRejectionReporter,
-} from './gatewayKeyRejection.js';
+import { installCindyGatewayKeyRejectionReporter } from './gatewayKeyRejection.js';
import {
createCredentialsSync,
type CredentialsPayload,
@@ -62,7 +58,6 @@ import {
resetExecutableMediaModelCache,
} from './mediaModels.js';
export { isModelAccessReady } from './readiness.js';
-export { reportCindyGatewayKeyRejected } from './gatewayKeyRejection.js';
const log = createLogger('modelAccess');
@@ -517,17 +512,7 @@ function mapServerError(err: unknown): never {
export function initModelAccess(): void {
const sync = getSync();
// 主进程各调用方遇到 Cindy 网关 401 时经此重新拉取凭据(服务端会探测并换发失效 Key)。
- installCindyGatewayKeyRejectionReporter(createGatewayKeyRejectionReporter({
- canUseCindyGateway: () => getAppCapabilities().canUseCindyGateway,
- isSessionBoundaryPending: isAppSessionBoundaryPending,
- getCredentialSource: () => getModelAccessCredentialsStore().getSource(),
- readCurrentKey: () => getProviderSecretStore().get('xd'),
- requestSync: () => getSync().sync(),
- log: {
- info: (msg) => log.info(msg),
- warn: (msg) => log.warn(msg),
- },
- }));
+ installCindyGatewayKeyRejectionReporter(() => getSync().sync());
const byok = createByokRuntime(
broadcastReferenceModelPricing,
createByokCache(byokSnapshotSecretIo),