Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions web/src/components/research/ApprovalQueuePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCallback, useEffect, useState } from "react"
import { Check, Loader2, RefreshCw, X } from "lucide-react"

import { Button } from "@/components/ui/button"
import { getErrorMessage } from "@/lib/fetch"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"

type ApprovalItem = {
Expand Down Expand Up @@ -35,7 +36,7 @@ export function ApprovalQueuePanel() {
const payload = (await res.json()) as ApprovalQueueResponse
setItems(payload.items || [])
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
setItems([])
} finally {
setLoading(false)
Expand All @@ -62,7 +63,7 @@ export function ApprovalQueuePanel() {
}
await load()
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
} finally {
setActingId(null)
}
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/research/FeedTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect, useMemo, useState } from "react"
import { Loader2, RefreshCw } from "lucide-react"

import { Button } from "@/components/ui/button"
import { getErrorMessage } from "@/lib/fetch"
import { Card, CardContent } from "@/components/ui/card"

import { PaperCard, type Paper } from "./PaperCard"
Expand Down Expand Up @@ -81,7 +82,7 @@ export function FeedTab({ userId, trackId, onLike, onSave, onDislike }: FeedTabP
const payload = (await res.json()) as FeedResponse
setItems(payload.items || [])
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
setItems([])
} finally {
setLoading(false)
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/research/MemoryTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect, useState } from "react"
import { Loader2, RefreshCw } from "lucide-react"

import { Button } from "@/components/ui/button"
import { getErrorMessage } from "@/lib/fetch"
import { Card, CardContent } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"

Expand Down Expand Up @@ -50,7 +51,7 @@ export function MemoryTab({ userId, trackId }: MemoryTabProps) {
const payload = (await res.json()) as MemoryResponse
setItems(payload.items || [])
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
setItems([])
} finally {
setLoading(false)
Expand Down
67 changes: 11 additions & 56 deletions web/src/components/research/ResearchDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useEffect, useMemo, useState } from "react"

import { fetchJson, getErrorMessage } from "@/lib/fetch"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
Expand Down Expand Up @@ -90,52 +91,6 @@ type ConfirmAction =
| { type: "bulk_move"; itemIds: number[]; targetTrackId: number }
| { type: "clear_track_memory"; trackId: number }

type UpstreamErrorBody = {
detail?: string
error?: string
}

function toFriendlyErrorMessage(status: number, rawText: string): string | null {
if (!rawText) return null

let parsed: UpstreamErrorBody | null = null
try {
parsed = JSON.parse(rawText) as UpstreamErrorBody
} catch {
parsed = null
}

const detail = parsed && typeof parsed.detail === "string" ? parsed.detail : undefined

if (detail && (detail.includes("Upstream API unreachable") || detail.includes("Upstream API timed out"))) {
if (detail.includes("timed out")) {
return "Unable to connect to service (request timed out). Please ensure the backend is running. Please try again."
}
return "Unable to connect to service. Please ensure the backend is running."
}

// Fallback: surface backend-provided detail when available
if (detail) return detail

return null
}

async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
const res = await fetch(url, init)
if (!res.ok) {
const text = await res.text().catch(() => "")
const friendly = toFriendlyErrorMessage(res.status, text)
if (friendly) {
throw new Error(friendly)
}
const statusLabel = `${res.status} ${res.statusText}`.trim()
const base = statusLabel || "Request failed"
const message = text ? `${base} ${text}`.trim() : base
throw new Error(message)
}
return res.json() as Promise<T>
}

function clampNumber(value: number, min: number, max: number, fallback: number) {
if (!Number.isFinite(value)) return fallback
return Math.min(max, Math.max(min, value))
Expand Down Expand Up @@ -256,7 +211,7 @@ export default function ResearchDashboard() {

useEffect(() => {
setError(null)
refreshTracks().catch((e) => setError(e instanceof Error ? e.message : String(e)))
refreshTracks().catch((e) => setError(getErrorMessage(e)))
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

Expand Down Expand Up @@ -309,7 +264,7 @@ export default function ResearchDashboard() {
await refreshTracks()
}
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
} finally {
setLoading(false)
}
Expand All @@ -334,7 +289,7 @@ export default function ResearchDashboard() {
setSuggestText("")
await refreshInbox(activeTrackId)
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
} finally {
setLoading(false)
}
Expand All @@ -357,7 +312,7 @@ export default function ResearchDashboard() {
})
await refreshInbox(activeTrackId)
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
} finally {
setLoading(false)
}
Expand All @@ -381,7 +336,7 @@ export default function ResearchDashboard() {
})
await refreshInbox(activeTrackId)
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
} finally {
setLoading(false)
}
Expand Down Expand Up @@ -420,7 +375,7 @@ export default function ResearchDashboard() {
const activeId = await refreshTracks()
await refreshInbox(activeId)
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
} finally {
setLoading(false)
}
Expand Down Expand Up @@ -458,7 +413,7 @@ export default function ResearchDashboard() {
})
await buildContext(false)
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
} finally {
setLoading(false)
}
Expand All @@ -474,7 +429,7 @@ export default function ResearchDashboard() {
)
await refreshInbox(trackId)
} catch (e) {
setError(e instanceof Error ? e.message : String(e))
setError(getErrorMessage(e))
} finally {
setLoading(false)
}
Expand Down Expand Up @@ -973,7 +928,7 @@ export default function ResearchDashboard() {
/>
<Button
variant="outline"
onClick={() => refreshEval().catch((e) => setError(e instanceof Error ? e.message : String(e)))}
onClick={() => refreshEval().catch((e) => setError(getErrorMessage(e)))}
disabled={loading}
>
Refresh
Expand All @@ -989,7 +944,7 @@ export default function ResearchDashboard() {
</div>

{!evalSummary ? (
<Button variant="secondary" onClick={() => refreshEval().catch((e) => setError(e instanceof Error ? e.message : String(e)))} disabled={loading}>
<Button variant="secondary" onClick={() => refreshEval().catch((e) => setError(getErrorMessage(e)))} disabled={loading}>
Load Summary
</Button>
) : (
Expand Down
51 changes: 3 additions & 48 deletions web/src/components/research/ResearchDiscoveryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useEffect, useMemo, useState } from "react"
import { useSearchParams } from "next/navigation"

import { ArrowLeft, Compass } from "lucide-react"
import { fetchJson, getErrorMessage } from "@/lib/fetch"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
Expand All @@ -14,52 +15,6 @@ import type { Track } from "./TrackSelector"

type SeedType = "doi" | "arxiv" | "openalex" | "semantic_scholar" | "author"

type UpstreamErrorBody = {
detail?: string
error?: string
}

function toFriendlyErrorMessage(status: number, rawText: string): string | null {
if (!rawText) return null

let parsed: UpstreamErrorBody | null = null
try {
parsed = JSON.parse(rawText) as UpstreamErrorBody
} catch {
parsed = null
}

const detail = parsed && typeof parsed.detail === "string" ? parsed.detail : undefined

if (detail && (detail.includes("Upstream API unreachable") || detail.includes("Upstream API timed out"))) {
if (detail.includes("timed out")) {
return "Unable to connect to service (request timed out). Please ensure the backend is running. Please try again."
}
return "Unable to connect to service. Please ensure the backend is running."
}

// Fallback: surface backend-provided detail when available
if (detail) return detail

return null
}

async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
const res = await fetch(url, init)
if (!res.ok) {
const text = await res.text().catch(() => "")
const friendly = toFriendlyErrorMessage(res.status, text)
if (friendly) {
throw new Error(friendly)
}
const statusLabel = `${res.status} ${res.statusText}`.trim()
const base = statusLabel || "Request failed"
const message = text ? `${base} ${text}`.trim() : base
throw new Error(message)
}
return res.json() as Promise<T>
}

export default function ResearchDiscoveryPage() {
const searchParams = useSearchParams()
const [userId] = useState("default")
Expand Down Expand Up @@ -91,7 +46,7 @@ export default function ResearchDiscoveryPage() {
)

useEffect(() => {
refreshTracks().catch((err) => setError(err instanceof Error ? err.message : String(err)))
refreshTracks().catch((err) => setError(getErrorMessage(err)))
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

Expand Down Expand Up @@ -126,7 +81,7 @@ export default function ResearchDiscoveryPage() {
)
await refreshTracks()
} catch (err) {
setError(err instanceof Error ? err.message : String(err))
setError(getErrorMessage(err))
} finally {
setLoading(false)
}
Expand Down
Loading
Loading