fix(desktop): enhance doubao ai account binding and upsert error logging

This commit is contained in:
2026-05-08 17:16:46 +08:00
parent f38930b0ac
commit 846a509c7d
3 changed files with 288 additions and 8 deletions
@@ -1,3 +1,7 @@
import { createHash } from 'node:crypto'
import { appendFileSync, mkdirSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { ApiClientError, createApiClient, type ApiClient } from '@geo/http-client'
import type {
ApiEnvelope,
@@ -29,6 +33,7 @@ import type {
ReportDesktopAccountHealthResponse,
UpsertDesktopAccountRequest,
} from '@geo/shared-types'
import { app } from 'electron/main'
import {
failObservedRequest,
@@ -70,6 +75,7 @@ let transportSession: DesktopTransportSession = {
clientToken: null,
}
const ACCOUNT_UPSERT_CACHE_TTL_MS = 5 * 60_000
const ACCOUNT_UPSERT_DIAGNOSTIC_FILE = 'desktop-account-upsert-errors.jsonl'
interface AccountUpsertCacheEntry {
signature: string
@@ -185,6 +191,76 @@ function clearAccountUpsertCache(): void {
accountUpsertCache.clear()
}
function diagnosticHash(value: string | null | undefined): string | null {
const normalized = value?.trim()
if (!normalized) {
return null
}
return createHash('sha256').update(normalized).digest('hex').slice(0, 16)
}
function normalizeErrorForDiagnostics(error: unknown): Record<string, unknown> {
if (error instanceof ApiClientError) {
return {
name: error.name,
message: error.message,
code: error.code,
status: error.status ?? null,
detail: error.detail ?? null,
requestId: error.requestId ?? null,
handled: error.handled === true,
}
}
if (error instanceof Error) {
return {
name: error.name,
message: error.message,
}
}
return {
name: typeof error,
message: String(error),
}
}
function appendAccountUpsertDiagnostic(
payload: UpsertDesktopAccountRequest,
error: unknown,
): void {
const target = join(app.getPath('userData'), ACCOUNT_UPSERT_DIAGNOSTIC_FILE)
const event = {
at: new Date().toISOString(),
action: 'desktop_account_upsert_failed',
transport: {
baseURL: transportState.baseURL,
auth: transportState.auth,
requestDebugMode: transportState.requestDebugMode,
hasClient: desktopApiClient !== null,
hasClientToken: Boolean(transportSession.clientToken),
},
request: {
platform: payload.platform,
platformUidHash: diagnosticHash(payload.platform_uid),
accountFingerprintHash: diagnosticHash(payload.account_fingerprint ?? null),
displayName: payload.display_name,
health: payload.health,
verifiedAt: payload.verified_at ?? null,
tagCount: payload.tags?.length ?? 0,
hasIfSyncVersion: typeof payload.if_sync_version === 'number',
},
error: normalizeErrorForDiagnostics(error),
}
try {
mkdirSync(dirname(target), { recursive: true })
appendFileSync(target, `${JSON.stringify(event)}\n`, 'utf8')
} catch {
// Best-effort diagnostics. Never block account binding on local log writes.
}
}
function currentRequestDebugMode(): 'main-process' | 'renderer-devtools' {
return canUseRendererDevtoolsProxy() ? 'renderer-devtools' : 'main-process'
}
@@ -521,6 +597,7 @@ export async function upsertDesktopAccount(
if (accountUpsertCache.get(cacheKey)?.inFlight === request) {
accountUpsertCache.delete(cacheKey)
}
appendAccountUpsertDiagnostic(payload, error)
throw error
}
}