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> { const result = await call("/query/chat", { method: "POST", signal, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }, (body: unknown) => body as Partial | 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> { return call("/assistant/stop", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({}), }, (body: unknown) => body); }