Files
geo/apps/desktop-client/src/renderer/composables/useDesktopRuntime.ts
T
root 55e1c2e74b feat(desktop): add monitor scheduler, Playwright CDP layer, and account health
Move monitor-task scheduling authority onto the client with a durable
file-backed queue that survives restart, drops stale cross-day tasks,
enforces per-platform serialism, and adapts global concurrency from
Electron process metrics. Publish tasks keep their existing FIFO.

Add a hidden Playwright CDP manager that attaches to Electron Chromium
on account session partitions, lets adapters opt into `executionMode:
"playwright"`, and leaves the existing hidden WebContentsView path in
place for current adapters.

Introduce an account-health subsystem with silent probes, projected
health/auth states, and IPC invalidation events so the renderer can
show accurate auth/probe status and verification timestamps.

Server-side, derive and forward title/business_date/scheduler_group_key/
question_text metadata on desktop task events so the local scheduler
can defer same-question fan-out before leasing.
2026-04-20 17:16:15 +08:00

157 lines
3.7 KiB
TypeScript

import { computed, onMounted, onUnmounted, readonly, shallowRef } from "vue";
import type { DesktopRuntimeSnapshot } from "../types";
const RUNTIME_POLL_INTERVAL_MS = 15_000;
const snapshot = shallowRef<DesktopRuntimeSnapshot | null>(null);
const loading = shallowRef(false);
const error = shallowRef<string | null>(null);
let intervalHandle: ReturnType<typeof setInterval> | null = null;
let subscribers = 0;
let visibilityListenerBound = false;
let runtimeInvalidationUnsubscribe: (() => void) | null = null;
function isPageVisible(): boolean {
return typeof document === "undefined" || document.visibilityState === "visible";
}
async function refreshRuntimeSnapshot() {
loading.value = true;
error.value = null;
try {
if (!window.desktopBridge?.app?.runtimeSnapshot) {
snapshot.value = null;
error.value = "desktop runtime bridge unavailable";
return;
}
snapshot.value = await window.desktopBridge.app.runtimeSnapshot();
} catch (err) {
error.value = err instanceof Error ? err.message : "runtime snapshot unavailable";
} finally {
loading.value = false;
}
}
async function refreshAccounts() {
if (!window.desktopBridge?.app?.refreshRuntimeAccounts) {
await refreshRuntimeSnapshot();
return;
}
loading.value = true;
error.value = null;
try {
await window.desktopBridge.app.refreshRuntimeAccounts();
snapshot.value = await window.desktopBridge.app.runtimeSnapshot();
} catch (err) {
error.value = err instanceof Error ? err.message : "refresh accounts failed";
} finally {
loading.value = false;
}
}
function stopPolling() {
if (!intervalHandle) {
return;
}
clearInterval(intervalHandle);
intervalHandle = null;
}
function syncPollingState() {
if (subscribers === 0 || !isPageVisible()) {
stopPolling();
return;
}
if (intervalHandle) {
return;
}
intervalHandle = setInterval(() => {
void refreshRuntimeSnapshot();
}, RUNTIME_POLL_INTERVAL_MS);
}
function handleVisibilityChange() {
if (isPageVisible()) {
void refreshRuntimeSnapshot();
}
syncPollingState();
}
function bindVisibilityListener() {
if (visibilityListenerBound || typeof document === "undefined") {
return;
}
document.addEventListener("visibilitychange", handleVisibilityChange);
visibilityListenerBound = true;
}
function bindRuntimeInvalidationListener() {
if (runtimeInvalidationUnsubscribe || !window.desktopBridge?.app?.onRuntimeInvalidated) {
return;
}
runtimeInvalidationUnsubscribe = window.desktopBridge.app.onRuntimeInvalidated(() => {
if (isPageVisible()) {
void refreshRuntimeSnapshot();
}
});
}
function unbindVisibilityListener() {
if (!visibilityListenerBound || typeof document === "undefined") {
return;
}
document.removeEventListener("visibilitychange", handleVisibilityChange);
visibilityListenerBound = false;
}
function unbindRuntimeInvalidationListener() {
runtimeInvalidationUnsubscribe?.();
runtimeInvalidationUnsubscribe = null;
}
export function useDesktopRuntime() {
onMounted(() => {
subscribers += 1;
bindVisibilityListener();
bindRuntimeInvalidationListener();
if (!snapshot.value && !loading.value) {
void refreshRuntimeSnapshot();
}
syncPollingState();
});
onUnmounted(() => {
subscribers = Math.max(0, subscribers - 1);
if (subscribers === 0) {
stopPolling();
unbindVisibilityListener();
unbindRuntimeInvalidationListener();
return;
}
syncPollingState();
});
return {
snapshot: readonly(snapshot),
loading: readonly(loading),
error: readonly(error),
refresh: refreshRuntimeSnapshot,
refreshAccounts,
hasSnapshot: computed(() => snapshot.value !== null),
};
}