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。

fix(voice-input): 火山引擎实时转写握手 401 上报 Cindy AI Key 失效(处理 #6148 Codex review)

fmfsaisai0644251b3d9a
diff --git a/apps/desktop/src/main/voice-input/VolcengineSaucAsrProvider.ts b/apps/desktop/src/main/voice-input/VolcengineSaucAsrProvider.ts
index ad8c1eb328..b006e398da 100644
--- a/apps/desktop/src/main/voice-input/VolcengineSaucAsrProvider.ts
+++ b/apps/desktop/src/main/voice-input/VolcengineSaucAsrProvider.ts
@@ -21,6 +21,11 @@ type VolcengineSaucAsrProviderOptions = {
connectTimeoutMs?: number;
missingCredentialMessage?: string;
errorFallbackMessage?: string;
+ /**
+ * Called with proxyApiKey when the WebSocket handshake is rejected with HTTP 401 on the
+ * gateway proxy path. The connectionProvider path (Cindy voice tokens) never calls it.
+ */
+ onAuthorizationRejected?: (token: string) => void;
};
const log = createLogger('voice-input:volcengine-sauc-asr');
@@ -86,6 +91,7 @@ export class VolcengineSaucAsrProvider implements AsrProvider {
private readonly connectTimeoutMs: number;
private readonly missingCredentialMessage: string;
private readonly errorFallbackMessage: string;
+ private readonly onAuthorizationRejected?: (token: string) => void;
private socket?: WebSocket;
private callback: (event: AsrEvent) => void = () => {};
private connected = false;
@@ -122,6 +128,7 @@ export class VolcengineSaucAsrProvider implements AsrProvider {
this.connectTimeoutMs = options.connectTimeoutMs ?? CONNECT_TIMEOUT_MS;
this.missingCredentialMessage = options.missingCredentialMessage ?? 'API key is required for Volcengine SAUC ASR.';
this.errorFallbackMessage = options.errorFallbackMessage ?? 'Volcengine SAUC transcription failed.';
+ this.onAuthorizationRejected = options.onAuthorizationRejected;
}
onEvent(callback: (event: AsrEvent) => void): void {
@@ -219,6 +226,9 @@ export class VolcengineSaucAsrProvider implements AsrProvider {
const onUnexpectedResponse = (_request: ClientRequest, response: IncomingMessage): void => {
response.resume();
const statusCode = response.statusCode ?? 'unknown';
+ if (statusCode === 401 && !this.connectionProvider && this.proxyApiKey) {
+ this.onAuthorizationRejected?.(this.proxyApiKey);
+ }
const statusMessage = response.statusMessage ? ` ${response.statusMessage}` : '';
// Include the dialed host/path + gateway trace id: a handshake 404
// against a gateway missing the ASR passthrough route is otherwise
diff --git a/apps/desktop/src/main/voice-input/__tests__/VolcengineSaucAsrProvider.test.ts b/apps/desktop/src/main/voice-input/__tests__/VolcengineSaucAsrProvider.test.ts
index 62d417a06d..e8f1ff4e21 100644
--- a/apps/desktop/src/main/voice-input/__tests__/VolcengineSaucAsrProvider.test.ts
+++ b/apps/desktop/src/main/voice-input/__tests__/VolcengineSaucAsrProvider.test.ts
@@ -586,6 +586,60 @@ describe('VolcengineSaucAsrProvider protocol helpers', () => {
}
});
+ it.each([
+ ['proxy key, HTTP 401', 401, false, 1],
+ ['proxy key, HTTP 403', 403, false, 0],
+ ['managed connection ticket, HTTP 401', 401, true, 0],
+ ] as const)('reports the Cindy AI key on a rejected handshake only for the proxy key path (%s)', async (
+ _label,
+ status,
+ managed,
+ reports,
+ ) => {
+ const server = new WebSocketServer({
+ host: '127.0.0.1',
+ port: 0,
+ verifyClient(_info, done) {
+ done(false, status, 'Rejected');
+ },
+ });
+ let provider: VolcengineSaucAsrProvider | undefined;
+ const onAuthorizationRejected = vi.fn();
+
+ try {
+ await waitFor(() => server.address() !== null);
+ const address = server.address();
+ if (!address || typeof address === 'string') throw new Error('Expected local test server address.');
+ const path = '/volcengine/api/v3/sauc/bigmodel_async';
+
+ provider = new VolcengineSaucAsrProvider({
+ ...(managed
+ ? {
+ connectionProvider: async () => ({
+ websocketUrl: `ws://127.0.0.1:${address.port}${path}`,
+ authorizationToken: 'voice-ticket',
+ }),
+ }
+ : {
+ proxyApiKey: 'gateway-key-sent',
+ baseUrl: `http://127.0.0.1:${address.port}`,
+ endpointPath: path,
+ }),
+ resourceId: 'volc.test',
+ connectTimeoutMs: 2_000,
+ onAuthorizationRejected,
+ });
+ provider.onEvent(() => {});
+
+ await expect(provider.start()).rejects.toThrow(`HTTP ${status}`);
+ expect(onAuthorizationRejected).toHaveBeenCalledTimes(reports);
+ if (reports > 0) expect(onAuthorizationRejected).toHaveBeenCalledWith('gateway-key-sent');
+ } finally {
+ await provider?.stop();
+ await new Promise<void>((resolve) => server.close(() => resolve()));
+ }
+ });
+
it('reports provider-native handshake failures without waiting for the connect timeout', async () => {
const server = new WebSocketServer({
host: '127.0.0.1',
diff --git a/apps/desktop/src/main/voice-input/index.ts b/apps/desktop/src/main/voice-input/index.ts
index 55e15e70d9..76522c0c5f 100644
--- a/apps/desktop/src/main/voice-input/index.ts
+++ b/apps/desktop/src/main/voice-input/index.ts
@@ -1803,6 +1803,8 @@ async function createVoiceInputProvider(
sourceLanguage,
missingCredentialMessage: profile.missingCredentialMessage,
errorFallbackMessage: profile.errorFallbackMessage,
+ // 这条直连网关的 WebSocket 携带 Cindy AI Key;握手 401 时上报 Key 失效。
+ onAuthorizationRejected: reportCindyGatewayKeyRejected,
});
}
throw new Error(`Unsupported native ASR protocol ${nativeConfig.protocolProfile}.`);