Files
paseo/packages/app/src/hooks/use-agent-attention-clear.ts
T
Mohamed BoudraandGitHub 53c14d9855 Extract client SDK package (#1052)
* Extract client SDK package

* Polish SDK client identity defaults

* Build client before dependent CI jobs

* Restore daemon client server export

* Extract protocol and client SDK packages

* Fix provider override schema validation

* Fix app test daemon client imports

* Simplify workspace build targets

* Fix CLI test server build bootstrap

* Run SDK package tests in CI

* Fix rebase package split drift

* Restore lockfile registry metadata

* Update SDK config test for prompt default

* Move terminal stream router test to client package

* Fix rebase drift for protocol imports

* Fix SDK agent capability fixture

* Restore legacy server client exports

* Fix server export compatibility test

* Advertise custom mode icon client capability

* Remove server daemon-client exports

* Format rebased mode control import

* Fix rebase drift for protocol imports

Files added by upstream PRs (#893, #1147, #1154) referenced the pre-split
shared/ paths that this branch moves into @getpaseo/protocol. Redirect
those imports to the protocol package so typecheck stays green after the
rebase.
2026-05-28 01:58:18 +08:00

136 lines
4.3 KiB
TypeScript

import { useCallback, useEffect, useRef, useState } from "react";
import { AppState } from "react-native";
import type { DaemonClient } from "@getpaseo/client/internal/daemon-client";
import {
shouldClearAgentAttention,
type AgentAttentionClearTrigger,
} from "@/utils/agent-attention";
import { getIsAppActivelyVisible } from "@/utils/app-visibility";
import { isWeb } from "@/constants/platform";
type AttentionReason = "finished" | "error" | "permission" | null | undefined;
interface UseAgentAttentionClearParams {
agentId: string | null | undefined;
client: DaemonClient | null;
isConnected: boolean;
requiresAttention: boolean | null | undefined;
attentionReason: AttentionReason;
isScreenFocused: boolean;
}
interface AgentAttentionClearController {
clearOnInputFocus: () => void;
clearOnPromptSend: () => void;
clearOnAgentBlur: () => void;
}
export function useAgentAttentionClear({
agentId,
client,
isConnected,
requiresAttention,
attentionReason,
isScreenFocused,
}: UseAgentAttentionClearParams): AgentAttentionClearController {
const [isAppVisible, setIsAppVisible] = useState<boolean>(() => getIsAppActivelyVisible());
const deferredFocusEntryClearRef = useRef(false);
const prevRequiresAttentionRef = useRef(Boolean(requiresAttention));
const prevActivelyViewedRef = useRef(isScreenFocused && getIsAppActivelyVisible());
const prevScreenFocusedRef = useRef(false);
const prevAppVisibleRef = useRef(getIsAppActivelyVisible());
const clearAttention = useCallback(
(trigger: AgentAttentionClearTrigger) => {
const resolvedAgentId = agentId?.trim();
if (!client || !resolvedAgentId) {
return;
}
if (
!shouldClearAgentAttention({
agentId: resolvedAgentId,
isConnected,
requiresAttention,
attentionReason,
trigger,
hasDeferredFocusEntryClear: deferredFocusEntryClearRef.current,
})
) {
return;
}
deferredFocusEntryClearRef.current = false;
client.clearAgentAttention(resolvedAgentId).catch(() => {});
},
[agentId, attentionReason, client, isConnected, requiresAttention],
);
useEffect(() => {
const updateVisibility = () => {
setIsAppVisible(getIsAppActivelyVisible());
};
const appStateSubscription = AppState.addEventListener("change", updateVisibility);
if (isWeb && typeof document !== "undefined") {
document.addEventListener("visibilitychange", updateVisibility);
window.addEventListener("focus", updateVisibility);
window.addEventListener("blur", updateVisibility);
return () => {
appStateSubscription.remove();
document.removeEventListener("visibilitychange", updateVisibility);
window.removeEventListener("focus", updateVisibility);
window.removeEventListener("blur", updateVisibility);
};
}
return () => {
appStateSubscription.remove();
};
}, []);
useEffect(() => {
if (!requiresAttention) {
deferredFocusEntryClearRef.current = false;
}
}, [requiresAttention]);
useEffect(() => {
const isActivelyViewed = isScreenFocused && isAppVisible;
if (
!prevRequiresAttentionRef.current &&
Boolean(requiresAttention) &&
prevActivelyViewedRef.current &&
isActivelyViewed
) {
deferredFocusEntryClearRef.current = true;
}
prevRequiresAttentionRef.current = Boolean(requiresAttention);
prevActivelyViewedRef.current = isActivelyViewed;
}, [isAppVisible, isScreenFocused, requiresAttention]);
useEffect(() => {
const enteredScreenFocus = !prevScreenFocusedRef.current && isScreenFocused && isAppVisible;
const resumedIntoFocusedAgent = !prevAppVisibleRef.current && isAppVisible && isScreenFocused;
if (enteredScreenFocus || resumedIntoFocusedAgent) {
clearAttention("focus-entry");
}
prevScreenFocusedRef.current = isScreenFocused;
prevAppVisibleRef.current = isAppVisible;
}, [clearAttention, isAppVisible, isScreenFocused]);
return {
clearOnInputFocus: useCallback(() => {
clearAttention("input-focus");
}, [clearAttention]),
clearOnPromptSend: useCallback(() => {
clearAttention("prompt-send");
}, [clearAttention]),
clearOnAgentBlur: useCallback(() => {
clearAttention("agent-blur");
}, [clearAttention]),
};
}