* feat: show activity indicators for terminals and their workspaces A terminal surfaces whether the agent running inside it is working, idle, or waiting for input as a tab dot, and that state rolls up into the owning workspace status — so an agent run inside a terminal reads the same as a native agent. Finishing a turn raises a "Terminal finished" notification that routes back to the workspace terminal tab. Activity is reported by the agents, not parsed from terminal output: Claude and Codex command hooks and an OpenCode plugin post coarse state to a local daemon endpoint, and a source-agnostic per-terminal tracker maps it to working/idle/attention. Hook installation is opt-in and off by default because it edits your real agent config files; enable it under a host's Terminal settings and Paseo installs the hooks globally, runs them via `paseo hooks <agent> <event>`, and removes its own hooks when you opt back out. Installation and reporting are idempotent and fail-open, so a missing or failing hook never breaks the user's shell. * Fix terminal activity routing and CI test Test drift: anchor the provider-leak test to the repo root instead of process.cwd() so it passes under the server workspace CI runner. Also restore auth-first JSON parsing outside the terminal-activity route and route subdirectory terminal activity to the deepest active workspace. * Fix workspace resolver regression Code drift: terminal activity routing needs parent-workspace prefix matching, but agent directory and historical fetches keep exact registered workspace semantics, including archived exact matches. * Narrow terminal/draft workspace subscriptions to the fields they use terminal-panel and the draft agent tab subscribed to the whole workspace descriptor via useWorkspace but read only workspaceDirectory (plus projectKind in TerminalPanel, plus id in the draft tab), so they re-rendered on any field change of that workspace (status, diffStat, scripts). Switch them to the existing useWorkspaceDirectory / useWorkspaceFields hooks so they re-render only when the fields they actually read change. No behavior change. * Make TerminalActivity.state forward-compatible A strict z.enum rejects the whole payload when a newer daemon reports a terminal activity state this client predates, breaking the protocol's old-client-parses-new-daemon contract (the activity field has no .catch at its embedding in messages.ts, so the failure bubbles up). Degrade unknown states to "idle" (no indicator, no notification) via .catch, matching the enum-with-catch pattern used elsewhere in the protocol. Add a forward-compat regression test.
156 lines
4.9 KiB
TypeScript
156 lines
4.9 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
import { AppState } from "react-native";
|
|
import type { DaemonClient } from "@getpaseo/client/internal/daemon-client";
|
|
import { getIsElectron, isWeb, isNative } from "@/constants/platform";
|
|
import { readDesktopSystemIdleTimeMs } from "@/desktop/electron/idle";
|
|
import { invokeDesktopCommand } from "@/desktop/electron/invoke";
|
|
import {
|
|
type ClientActivityTracker,
|
|
createClientActivityTracker,
|
|
DESKTOP_IDLE_POLL_INTERVAL_MS,
|
|
HEARTBEAT_INTERVAL_MS,
|
|
} from "./client-activity-tracker";
|
|
|
|
interface ClientActivityOptions {
|
|
client: DaemonClient;
|
|
focusedAgentId: string | null;
|
|
focusedTerminalId: string | null;
|
|
onAppResumed?: (awayMs: number) => void;
|
|
}
|
|
|
|
/**
|
|
* Handles client activity reporting:
|
|
* - Heartbeat sending every 15 seconds
|
|
* - App visibility tracking
|
|
* - Records lastActivityAt only on real user activity (not on heartbeat)
|
|
*/
|
|
export function useClientActivity({
|
|
client,
|
|
focusedAgentId,
|
|
focusedTerminalId,
|
|
onAppResumed,
|
|
}: ClientActivityOptions): void {
|
|
const onAppResumedRef = useRef(onAppResumed);
|
|
onAppResumedRef.current = onAppResumed;
|
|
|
|
const trackerRef = useRef<ClientActivityTracker | null>(null);
|
|
if (!trackerRef.current) {
|
|
trackerRef.current = createClientActivityTracker({
|
|
client,
|
|
deviceType: isWeb ? "web" : "mobile",
|
|
initialFocusedAgentId: focusedAgentId,
|
|
initialFocusedTerminalId: focusedTerminalId,
|
|
initialAppVisible: AppState.currentState === "active",
|
|
now: () => Date.now(),
|
|
onAppResumed: (awayMs) => onAppResumedRef.current?.(awayMs),
|
|
});
|
|
}
|
|
const tracker = trackerRef.current;
|
|
|
|
// Track app visibility via AppState (native).
|
|
useEffect(() => {
|
|
const subscription = AppState.addEventListener("change", (nextState) => {
|
|
tracker.notifyAppVisibility(nextState === "active");
|
|
tracker.sendHeartbeat();
|
|
});
|
|
return () => subscription.remove();
|
|
}, [tracker]);
|
|
|
|
// Track user activity and visibility on web.
|
|
useEffect(() => {
|
|
if (isNative) return;
|
|
if (typeof document === "undefined") return;
|
|
|
|
const handleUserActivity = () => {
|
|
tracker.recordUserActivity();
|
|
tracker.maybeSendImmediateHeartbeat();
|
|
};
|
|
|
|
const handleVisibilityChange = () => {
|
|
const visible = document.visibilityState === "visible";
|
|
const { changed } = tracker.notifyAppVisibility(visible);
|
|
if (changed && visible) {
|
|
tracker.maybeSendImmediateHeartbeat();
|
|
}
|
|
};
|
|
|
|
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
window.addEventListener("focus", handleUserActivity);
|
|
window.addEventListener("pointerdown", handleUserActivity, { passive: true });
|
|
window.addEventListener("keydown", handleUserActivity);
|
|
window.addEventListener("wheel", handleUserActivity, { passive: true });
|
|
window.addEventListener("touchstart", handleUserActivity, { passive: true });
|
|
|
|
return () => {
|
|
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
window.removeEventListener("focus", handleUserActivity);
|
|
window.removeEventListener("pointerdown", handleUserActivity);
|
|
window.removeEventListener("keydown", handleUserActivity);
|
|
window.removeEventListener("wheel", handleUserActivity);
|
|
window.removeEventListener("touchstart", handleUserActivity);
|
|
};
|
|
}, [tracker]);
|
|
|
|
// Track OS-wide activity in Electron so backgrounded desktop windows still report presence.
|
|
useEffect(() => {
|
|
if (!getIsElectron()) return;
|
|
|
|
let disposed = false;
|
|
const pollSystemIdleTime = async () => {
|
|
const systemIdleMs = await readDesktopSystemIdleTimeMs(invokeDesktopCommand);
|
|
if (disposed) return;
|
|
tracker.notifySystemIdleMs(systemIdleMs);
|
|
};
|
|
|
|
const interval = setInterval(() => {
|
|
void pollSystemIdleTime();
|
|
}, DESKTOP_IDLE_POLL_INTERVAL_MS);
|
|
|
|
return () => {
|
|
disposed = true;
|
|
clearInterval(interval);
|
|
};
|
|
}, [tracker]);
|
|
|
|
// Send heartbeat on focused agent change.
|
|
useEffect(() => {
|
|
tracker.setFocusedAgentId(focusedAgentId);
|
|
}, [focusedAgentId, tracker]);
|
|
|
|
// Send heartbeat on focused terminal change.
|
|
useEffect(() => {
|
|
tracker.setFocusedTerminalId(focusedTerminalId);
|
|
}, [focusedTerminalId, tracker]);
|
|
|
|
// Periodic heartbeat gated by connection status.
|
|
useEffect(() => {
|
|
let intervalId: ReturnType<typeof setInterval> | null = null;
|
|
|
|
const start = () => {
|
|
if (intervalId) clearInterval(intervalId);
|
|
tracker.sendHeartbeat();
|
|
intervalId = setInterval(() => tracker.sendHeartbeat(), HEARTBEAT_INTERVAL_MS);
|
|
};
|
|
|
|
const stop = () => {
|
|
if (intervalId) {
|
|
clearInterval(intervalId);
|
|
intervalId = null;
|
|
}
|
|
};
|
|
|
|
const unsubscribe = client.subscribeConnectionStatus((state) => {
|
|
if (state.status === "connected") {
|
|
start();
|
|
} else {
|
|
stop();
|
|
}
|
|
});
|
|
|
|
return () => {
|
|
unsubscribe();
|
|
stop();
|
|
};
|
|
}, [client, tracker]);
|
|
}
|