MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / useTaskPolling

Function useTaskPolling

frontend/src/hooks/query/use-task-polling.ts:22–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

20 * Note: This hook does NOT fetch conversation data. It only handles task polling and navigation.
21 */
22export const useTaskPolling = () => {
23 const { conversationId } = useConversationId();
24 const navigate = useNavigate();
25
26 // Check if this is a task ID (format: "task-{uuid}")
27 const isTask = conversationId.startsWith("task-");
28 const taskId = isTask ? conversationId.replace("task-", "") : null;
29
30 // Poll the task if this is a task ID
31 const taskQuery = useQuery({
32 queryKey: ["start-task", taskId],
33 queryFn: async () => {
34 if (!taskId) return null;
35 return V1ConversationService.getStartTask(taskId);
36 },
37 enabled: !!taskId,
38 refetchInterval: (query) => {
39 const task = query.state.data;
40 if (!task) return false;
41
42 // Stop polling if ready or error
43 if (task.status === "READY" || task.status === "ERROR") {
44 return false;
45 }
46
47 // Poll every 3 seconds while task is in progress
48 return 3000;
49 },
50 retry: false,
51 });
52
53 // Navigate to conversation ID when task is ready
54 useEffect(() => {
55 const task = taskQuery.data;
56 if (task?.status === "READY" && task.app_conversation_id) {
57 // Replace the URL with the actual conversation ID
58 navigate(`/conversations/${task.app_conversation_id}`, { replace: true });
59 }
60 }, [taskQuery.data, navigate]);
61
62 return {
63 isTask,
64 taskId,
65 conversationId: isTask ? null : conversationId,
66 task: taskQuery.data,
67 taskStatus: taskQuery.data?.status,
68 taskDetail: taskQuery.data?.detail,
69 taskError: taskQuery.error,
70 isLoadingTask: taskQuery.isLoading,
71 // Repository information from task request
72 repositoryInfo: {
73 selectedRepository: taskQuery.data?.request?.selected_repository,
74 selectedBranch: taskQuery.data?.request?.selected_branch,
75 gitProvider: taskQuery.data?.request?.git_provider,
76 },
77 };
78};

Callers 6

ChatInterfaceFunction · 0.90
GitControlBarFunction · 0.90
ServerStatusFunction · 0.90
AgentStatusFunction · 0.90
AppContentFunction · 0.90

Calls 2

useConversationIdFunction · 0.90
getStartTaskMethod · 0.80

Tested by

no test coverage detected