mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-06 02:47:54 +00:00
- Confirm-domain step, then live research transcript + action rail - Poll research status; client-side force-start fallback --- **Session Details** - Session: [View Session](https://supermemory.us1.vorflux.com/agent-sessions/4f4c1321-9b65-4dac-9906-8b78c4c32926) - Requested by: Unknown - Address comments on this PR. Add `(aside)` to your comment to have me ignore it.
64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
"use client"
|
|
|
|
import { useQuery } from "@tanstack/react-query"
|
|
import { useAuth } from "@lib/auth-context"
|
|
|
|
const BACKEND =
|
|
process.env.NEXT_PUBLIC_BACKEND_URL ?? "https://api.supermemory.ai"
|
|
|
|
const POLL_INTERVAL_MS = 2_000
|
|
const MAX_POLLS = 150
|
|
|
|
export type ResearchStat = { label: string; value: string }
|
|
|
|
export type ResearchEvent = {
|
|
aspect: string
|
|
label: string
|
|
status: "in_progress" | "complete" | "error" | string
|
|
detail: string | null
|
|
stats: ResearchStat[]
|
|
highlights: string[]
|
|
sources: string[]
|
|
createdAt: number
|
|
}
|
|
|
|
export type ResearchState = {
|
|
status: "queued" | "running" | "done" | null
|
|
domain: string | null
|
|
findings: number
|
|
events: ResearchEvent[]
|
|
}
|
|
|
|
const EMPTY: ResearchState = {
|
|
status: null,
|
|
domain: null,
|
|
findings: 0,
|
|
events: [],
|
|
}
|
|
|
|
export function useResearchStatus(enabled = true) {
|
|
const { org } = useAuth()
|
|
const orgId = org?.id
|
|
|
|
const { data } = useQuery<ResearchState>({
|
|
queryKey: ["brain-research-status", orgId],
|
|
queryFn: async () => {
|
|
const res = await fetch(`${BACKEND}/brain/research/status`, {
|
|
credentials: "include",
|
|
headers: { "X-App-Source": "nova" },
|
|
})
|
|
if (!res.ok) return EMPTY
|
|
return (await res.json()) as ResearchState
|
|
},
|
|
enabled: Boolean(enabled && orgId),
|
|
refetchInterval: (query) => {
|
|
const status = query.state.data?.status
|
|
const polls = query.state.dataUpdateCount
|
|
if (status === "done" || polls >= MAX_POLLS) return false
|
|
return POLL_INTERVAL_MS
|
|
},
|
|
staleTime: 0,
|
|
})
|
|
|
|
return data ?? EMPTY
|
|
}
|