MCPcopy Create free account
hub / github.com/Alphanimble/htmlstream / streamChat

Function streamChat

demo/chat/App.tsx:45–73  ·  view source on GitHub ↗
(
  messages: { role: string; content: string }[],
  onPart: (part: StreamPart) => void,
  signal: AbortSignal,
)

Source from the content-addressed store, hash-verified

43}
44
45async function streamChat(
46 messages: { role: string; content: string }[],
47 onPart: (part: StreamPart) => void,
48 signal: AbortSignal,
49): Promise<void> {
50 const res = await fetch("/api/chat", {
51 method: "POST",
52 headers: { "Content-Type": "application/json" },
53 body: JSON.stringify({ messages }),
54 signal,
55 });
56
57 if (!res.ok) {
58 let detail = res.statusText;
59 try {
60 const json = (await res.json()) as { error?: string };
61 detail = json.error ?? detail;
62 } catch {
63 detail = (await res.text()) || detail;
64 }
65 throw new Error(detail);
66 }
67
68 if (!res.body) {
69 throw new Error("No response stream");
70 }
71
72 await readStreamParts(res.body, onPart, signal);
73}
74
75function newMessage(
76 role: ChatMessage["role"],

Callers 1

ChatAppFunction · 0.85

Calls 1

readStreamPartsFunction · 0.85

Tested by

no test coverage detected