File size: 669 Bytes
a40a50f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export interface ChatRequest {
  user: string;
  session: string;
  prompt: string;
}

export async function* streamChat(
  req: ChatRequest
): AsyncGenerator<string> {
  const url = `${process.env.NEXT_PUBLIC_API_URL}/chat/stream`;
  const res = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(req),
  });

  if (!res.ok || !res.body) {
    throw new Error('API request failed');
  }

  const reader = res.body.getReader();
  const decoder = new TextDecoder();

  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    yield decoder.decode(value);
  }
}