perf: optimize app startup and background lifecycles
This commit is contained in:
@@ -28,6 +28,23 @@ export async function ensureHostApiToken(): Promise<string> {
|
||||
return token;
|
||||
}
|
||||
|
||||
export type DesktopActivityModule = 'programming' | 'painting' | 'learning' | 'robot' | 'other';
|
||||
|
||||
export function reportDesktopActivity(input: {
|
||||
visible: boolean;
|
||||
module: DesktopActivityModule | null;
|
||||
}): Promise<unknown> {
|
||||
return invokeIpc('lifecycle:activity', input);
|
||||
}
|
||||
|
||||
export function setDesktopBackgroundLease(input: {
|
||||
id: string;
|
||||
kind: string;
|
||||
active: boolean;
|
||||
}): Promise<unknown> {
|
||||
return invokeIpc('lifecycle:lease', input);
|
||||
}
|
||||
|
||||
async function getHostApiBaseUrl(): Promise<string> {
|
||||
if (hostApiBaseResolved) return cachedHostApiBase;
|
||||
try {
|
||||
@@ -63,6 +80,7 @@ type HostApiProxyData = {
|
||||
ok?: boolean;
|
||||
json?: unknown;
|
||||
text?: string;
|
||||
transport?: 'dispatcher' | 'loopback';
|
||||
};
|
||||
|
||||
function headersToRecord(headers?: HeadersInit): Record<string, string> {
|
||||
@@ -72,6 +90,20 @@ function headersToRecord(headers?: HeadersInit): Record<string, string> {
|
||||
return { ...headers };
|
||||
}
|
||||
|
||||
function telemetryRoute(path: string): string {
|
||||
const pathname = path.split('?', 1)[0] || '/';
|
||||
return pathname
|
||||
.split('/')
|
||||
.map((segment, index) => {
|
||||
if (!segment) return '';
|
||||
// Keep diagnostics aggregate-only: IDs, slugs and arbitrary path
|
||||
// segments must never become local telemetry or console output.
|
||||
if (index <= 2 && /^[a-z0-9._-]{1,32}$/i.test(segment)) return segment;
|
||||
return ':id';
|
||||
})
|
||||
.join('/') || '/';
|
||||
}
|
||||
|
||||
async function parseResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
let message = `${response.status} ${response.statusText}`;
|
||||
@@ -133,9 +165,9 @@ function parseUnifiedProxyResponse<T>(
|
||||
}
|
||||
|
||||
trackUiEvent('hostapi.fetch', {
|
||||
path,
|
||||
route: telemetryRoute(path),
|
||||
method,
|
||||
source: 'ipc-proxy',
|
||||
source: data.transport === 'dispatcher' ? 'ipc-dispatcher' : 'ipc-proxy',
|
||||
durationMs: Date.now() - startedAt,
|
||||
status: data.status ?? 200,
|
||||
});
|
||||
@@ -165,7 +197,7 @@ function parseLegacyProxyResponse<T>(
|
||||
}
|
||||
|
||||
trackUiEvent('hostapi.fetch', {
|
||||
path,
|
||||
route: telemetryRoute(path),
|
||||
method,
|
||||
source: 'ipc-proxy-legacy',
|
||||
durationMs: Date.now() - startedAt,
|
||||
@@ -215,7 +247,7 @@ export async function hostApiFetch<T>(path: string, init?: RequestInit): Promise
|
||||
const normalized = normalizeAppError(error, { source: 'ipc-proxy', path, method });
|
||||
const message = normalized.message;
|
||||
trackUiEvent('hostapi.fetch_error', {
|
||||
path,
|
||||
route: telemetryRoute(path),
|
||||
method,
|
||||
source: 'ipc-proxy',
|
||||
durationMs: Date.now() - startedAt,
|
||||
@@ -227,7 +259,7 @@ export async function hostApiFetch<T>(path: string, init?: RequestInit): Promise
|
||||
}
|
||||
if (!allowLocalhostFallback()) {
|
||||
trackUiEvent('hostapi.fetch_error', {
|
||||
path,
|
||||
route: telemetryRoute(path),
|
||||
method,
|
||||
source: 'ipc-proxy',
|
||||
durationMs: Date.now() - startedAt,
|
||||
@@ -252,7 +284,7 @@ export async function hostApiFetch<T>(path: string, init?: RequestInit): Promise
|
||||
},
|
||||
});
|
||||
trackUiEvent('hostapi.fetch', {
|
||||
path,
|
||||
route: telemetryRoute(path),
|
||||
method,
|
||||
source: 'browser-fallback',
|
||||
durationMs: Date.now() - startedAt,
|
||||
|
||||
Reference in New Issue
Block a user