Files
cobble/src/pages/api/admin/bots/speak-message.ts
T
tbenandClaude Opus 5.5 163d9a434c Add dictation and streamed read-aloud to the chat
- A mic in the composer records, transcribes with the bot's
  transcription model (api/admin/bots/transcribe) and writes the text
  into the input at the cursor to edit before sending. Escape cancels;
  recordings aren't stored.
- Every bot message has a read-aloud button that plays
  GET api/admin/bots/speak-message, streamed from the provider as the
  audio is made, so it starts in a couple of seconds. One message plays
  at a time; only the user's own sessions can be read.
- Both buttons stay visible but disabled while the bot has no model for
  them (bot_voice page prop).
- Transcription and speech no longer pick free models automatically:
  they're not available until a model is picked, and Settings says so.
  Transcription lists speech-to-text models only, so the chat-model
  transcript path and its missing-audio check are gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 21:21:04 +01:00

70 lines
2.4 KiB
TypeScript

import userAuth from "@/src/functions/backend/auth/user-auth";
import grabSpeakableMessage from "@/src/functions/backend/db/bots/grab-speakable-message";
import speakText from "@/src/harness/audio/speak-text";
import HarnessError from "@/src/harness/harness-error";
import { SpeakBotMessageRequestSchema } from "@/src/schemas/bot-schema";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import safeErrorMessage from "@/src/utils/safe-error-message";
import speechText from "@/src/utils/speech-text";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
/**
* `GET ?message_id=`: reads one of the bot's messages in the user's own
* conversation aloud with the bot's speech model and voice, streamed as
* the provider makes it, so an `<audio>` pointed here starts playing
* within a couple of seconds. Closing the player stops the provider call.
* Nothing is stored; the browser may keep it for an hour.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({ req, query }) => {
try {
if (req.method != "GET") {
throw new AppError(`Method not allowed`);
}
const { user } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
const { message_id } = parseWithSchema({
schema: SpeakBotMessageRequestSchema,
data: query,
});
const { message, conversation } = await grabSpeakableMessage({
user_id: Number(user.id),
message_id,
});
const text = speechText({ text: message.text || "" });
if (!text) {
throw new AppError(`There's nothing to read in that message.`);
}
const audio = await speakText({
text,
bot_id: conversation.bot.id,
workspace_id: conversation.workspace_id,
signal: req.signal,
});
return new Response(audio.stream, {
headers: {
"Content-Type": audio.media_type,
"Cache-Control": "private, max-age=3600",
},
});
} catch (error: unknown) {
return {
success: false,
msg: error instanceof HarnessError ? error.message : safeErrorMessage({ error }),
};
}
};