File size: 1,282 Bytes
89af3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { call, type ChatResult, type Result } from "../query/queryApi";
import type { ChatStreamRequest } from "./streamClient";

type JoinedTurn = { status: "joined" };

/** Keep C4 acknowledgements intact when the C3 stream is unavailable. */
export async function submitChat(body: ChatStreamRequest, signal?: AbortSignal): Promise<Result<ChatResult | JoinedTurn>> {
  const result = await call("/query/chat", {
    method: "POST", signal, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body),
  }, (body: unknown) => body as Partial<ChatResult> | JoinedTurn | null);
  if (!result.ok) return result;
  const reply = result.value;
  if (reply && "status" in reply && reply.status === "joined") return { ok: true, value: reply };
  if (!reply || !("message" in reply) || !reply.message?.id || !reply.userMessage?.id
    || !Array.isArray(reply.citations)) return { ok: false, status: 502, message: "This answer could not be loaded." };
  return { ok: true, value: reply as ChatResult };
}

/** Stop the caller's running turn through C4. */
export function stopTurn(): Promise<Result<unknown>> {
  return call("/assistant/stop", {
    method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({}),
  }, (body: unknown) => body);
}