File size: 1,054 Bytes
f1c3ff2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/** Help chatbot client — POST /api/help/chat with graceful client-side fallback. */

export interface HelpChatRequest {
  question: string;
  page_id?: string | null;
}

export interface HelpChatSource {
  id: string;
  title: string;
  kind: string;
  score?: number;
  page_id?: string | null;
}

export interface HelpChatResponse {
  answer: string;
  sources: HelpChatSource[];
  provider?: string;
  page_hint?: string | null;
}

async function handle<T>(res: Response): Promise<T> {
  if (!res.ok) {
    const body = await res.text().catch(() => '');
    throw new Error(`API ${res.status}: ${body || res.statusText}`);
  }
  return res.json() as Promise<T>;
}

export async function askHelp(req: HelpChatRequest, signal?: AbortSignal): Promise<HelpChatResponse> {
  const res = await fetch('/api/help/chat', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      question: req.question,
      page_id: req.page_id ?? null,
    }),
    signal,
  });
  return handle<HelpChatResponse>(res);
}