File size: 1,886 Bytes
98277cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { ApiError } from './api'
import { apiGetWrapped } from './api'

export type RawNoteRecord = {
  id: number
  source_platform: string | null
  content: string | null
  author: string | null
  url: string | null
  created_at: string | null
}

export type RawNoteListResponse = {
  notes: RawNoteRecord[]
  total: number
  limit: number
  offset: number
  query: string | null
}

export type CleanedNoteRecord = {
  id: number
  raw_note_id: number | null
  cleaned_content: string | null
  created_at: string | null
  raw_author: string | null
  raw_url: string | null
}

export type CleanedNoteListResponse = {
  notes: CleanedNoteRecord[]
  total: number
  limit: number
  offset: number
  query: string | null
}

export type ListContentParams = {
  limit: number
  offset: number
  query?: string
}

function buildSearch(params: ListContentParams) {
  const search = new URLSearchParams()
  search.set('limit', String(params.limit))
  search.set('offset', String(params.offset))
  const q = String(params.query || '').trim()
  if (q) search.set('query', q)
  return search.toString()
}

export async function listRawNotes(params: ListContentParams) {
  return apiGetWrapped<RawNoteListResponse>(`content/raw-notes?${buildSearch(params)}`)
}

export async function listCleanedNotes(params: ListContentParams) {
  return apiGetWrapped<CleanedNoteListResponse>(`content/cleaned-notes?${buildSearch(params)}`)
}

export function isOrchestratorDbUnavailable(error: ApiError | null | undefined) {
  if (!error) return false
  if (error.status !== 503) return false
  if (typeof error.message === 'string' && error.message.toLowerCase().includes('orchestrator db')) return true
  const data = error.data
  if (!data || typeof data !== 'object') return false
  const obj = data as Record<string, unknown>
  return obj.code === 10010 || obj.msg === 'orchestrator db unavailable'
}