loopable / web /src /assistant /turnClient.ts
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
8145bee verified
Raw History Blame
1.28 kB
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);
}