cc-haha/desktop/src/api/diagnostics.ts
程序员阿江(Relakkes) f2437bf724 feat: Make diagnostics actionable for runtime failures
Diagnostics exports previously preserved only compact summaries, which made user-uploaded issue bundles hard to debug. This records richer sanitized details from server, CLI, SDK, browser, React, and desktop API failure paths while keeping request bodies and secrets out of client-side reports.

Constraint: Diagnostic bundles must be useful for GitHub issues without including chat contents, file contents, full environments, or API keys.
Rejected: Only expand the Settings UI summary | the exported bundle would still miss hidden runtime failures.
Confidence: high
Scope-risk: moderate
Directive: Keep diagnostics write paths best-effort and non-blocking; do not add request-body capture without a redaction review.
Tested: bun test src/server/__tests__/diagnostics-service.test.ts src/server/__tests__/conversation-service.test.ts
Tested: cd desktop && bun run test src/api/client.test.ts src/__tests__/diagnosticsSettings.test.tsx --run
Tested: bun run check:server
Tested: bun run check:desktop
Tested: agent-browser E2E on ports 37652/41752 captured client_unhandled_rejection and exported a redacted tar.gz bundle
2026-05-05 17:57:50 +08:00

51 lines
1.4 KiB
TypeScript

import { api } from './client'
export type DiagnosticSeverity = 'debug' | 'info' | 'warn' | 'error'
export type DiagnosticEvent = {
id: string
timestamp: string
type: string
severity: DiagnosticSeverity
summary: string
sessionId?: string
details?: unknown
}
export type DiagnosticEventInput = {
type: string
severity?: DiagnosticSeverity
summary: string
sessionId?: string
details?: unknown
}
export type DiagnosticsStatus = {
logDir: string
diagnosticsPath: string
cliDiagnosticsPath: string
runtimeErrorsPath: string
exportDir: string
retentionDays: number
maxBytes: number
totalBytes: number
eventCount: number
recentErrorCount: number
lastEventAt: string | null
}
export type DiagnosticsBundle = {
path: string
fileName: string
bytes: number
}
export const diagnosticsApi = {
getStatus: () => api.get<DiagnosticsStatus>('/api/diagnostics/status'),
getEvents: (limit = 100) => api.get<{ events: DiagnosticEvent[] }>(`/api/diagnostics/events?limit=${limit}`),
recordEvent: (event: DiagnosticEventInput) => api.post<{ ok: true }>('/api/diagnostics/events', event, { timeout: 5_000 }),
exportBundle: () => api.post<{ bundle: DiagnosticsBundle }>('/api/diagnostics/export', undefined, { timeout: 60_000 }),
openLogDir: () => api.post<{ ok: true }>('/api/diagnostics/open-log-dir'),
clear: () => api.delete<{ ok: true }>('/api/diagnostics'),
}