Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(aui): handle missing thread goal in useThreadGoal

Steven Enamakel2558da1c1c88
diff --git a/app/src/features/conversations/aui/useThreadGoal.ts b/app/src/features/conversations/aui/useThreadGoal.ts
new file mode 100644
index 0000000000..848a8bcaac
--- /dev/null
+++ b/app/src/features/conversations/aui/useThreadGoal.ts
@@ -0,0 +1,46 @@
+/**
+ * The thread's current goal, read from `threadGoalSlice` (populated by the
+ * `thread_goal_updated` / `thread_goal_cleared` socket events via
+ * `ChatRuntimeProvider`, and primed on thread open by
+ * {@link useLoadThreadGoal}).
+ */
+import { useEffect, useRef } from 'react';
+
+import { threadApi } from '../../../services/api/threadApi';
+import { useAppDispatch, useAppSelector } from '../../../store/hooks';
+import { setThreadGoal, type ThreadGoalView } from '../../../store/threadGoalSlice';
+
+/** `null` when the thread has no goal (or none loaded yet). */
+export function useThreadGoal(threadId: string | null): ThreadGoalView | null {
+ return useAppSelector(state =>
+ threadId ? state.threadGoal.byThread[threadId] ?? null : null
+ );
+}
+
+/**
+ * Loads the thread's current goal once per `threadId` via
+ * `openhuman.threads_goal_get`, mirroring {@link useLoadThreadTodos}.
+ */
+export function useLoadThreadGoal(threadId: string | null): void {
+ const dispatch = useAppDispatch();
+ const requestedFor = useRef<string | null>(null);
+
+ useEffect(() => {
+ if (!threadId || requestedFor.current === threadId) return;
+ requestedFor.current = threadId;
+ let cancelled = false;
+ void (async () => {
+ try {
+ const goal = await threadApi.getGoal(threadId);
+ if (cancelled) return;
+ dispatch(setThreadGoal({ threadId, goal }));
+ } catch {
+ // Older core without the RPC, or a transient failure — the live
+ // socket event (or the next thread open) will still populate this.
+ }
+ })();
+ return () => {
+ cancelled = true;
+ };
+ }, [threadId, dispatch]);
+}