Sessions, turns, streamed replies, thinking and tool cards, held-action approvals and resume now use modules/chat; /api/stream forwards the chat topic. Sub-features the binary can't back yet stay hidden. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
import { randomUUID } from "@/lib/uuid";
|
|
|
|
/**
|
|
* What a send puts on the wire: the text, and the source id the server keys
|
|
* the turn on so a retry of the same turn is recognisable as one.
|
|
*/
|
|
export interface ChatTurn {
|
|
text: string;
|
|
sourceID: string;
|
|
/** A recorded clip, base64, transcribed by the server. */
|
|
voice?: { data: string; mime_type: string };
|
|
}
|
|
|
|
function newChatTurn(text: string, voice?: ChatTurn["voice"]): ChatTurn {
|
|
return { text, sourceID: randomUUID(), voice };
|
|
}
|
|
|
|
export function retryChatTurn(turn: ChatTurn): ChatTurn {
|
|
return { ...turn };
|
|
}
|
|
|
|
/** A retry descriptor as the transcript holds it, or nothing for a fresh send. */
|
|
export interface RetryOptions {
|
|
textOverride?: string;
|
|
turn?: ChatTurn;
|
|
voice?: ChatTurn["voice"];
|
|
/** A turn the server already has: follow its stream instead of sending. */
|
|
follow?: string;
|
|
}
|
|
|
|
// resolveTurn decides what to send: a real retry descriptor's original turn,
|
|
// or a fresh turn built from the composer text. `retry` is untrusted here --
|
|
// a DOM event handler bound as `el.onclick = sendMessage` (rather than
|
|
// `() => sendMessage()`) hands the click's MouseEvent to this function as
|
|
// `retry`, and a MouseEvent is truthy but has no `.turn`. Only an object
|
|
// carrying a `turn.text` string counts as a genuine retry; anything else
|
|
// (including no argument) falls back to the composer text instead of
|
|
// throwing on a missing property.
|
|
export function resolveTurn(
|
|
retry: RetryOptions | null | undefined,
|
|
composerValue: string,
|
|
): { text: string; turn: ChatTurn; isRetry: boolean } {
|
|
const isRetry = !!(
|
|
retry &&
|
|
typeof retry === "object" &&
|
|
retry.turn &&
|
|
typeof retry.turn.text === "string"
|
|
);
|
|
const text = isRetry ? retry.turn!.text : composerValue.trim();
|
|
const turn = isRetry ? retryChatTurn(retry.turn!) : newChatTurn(text, retry?.voice);
|
|
return { text, turn, isRetry };
|
|
}
|