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]>
This commit is contained in:
2026-10-19 21:21:04 +01:00
co-authored by Claude Opus 5.5
parent 727cbf53c8
commit 163d9a434c
37 changed files with 936 additions and 197 deletions
+69
View File
@@ -0,0 +1,69 @@
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 }),
};
}
};
+80
View File
@@ -0,0 +1,80 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import grabUserConversation from "@/src/functions/backend/db/bot-sessions/grab-user-conversation";
import transcribeAudio from "@/src/harness/audio/transcribe-audio";
import { TranscribeVoiceRequestSchema } from "@/src/schemas/bot-schema";
import HarnessError from "@/src/harness/harness-error";
import AppError from "@/src/utils/app-error";
import bufferFromBase64 from "@/src/utils/buffer-from-base-64";
import parseWithSchema from "@/src/utils/parse-with-schema";
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
/**
* Recording types as the formats the harness takes.
*/
const AudioFormats: Record<string, string> = {
mp4: "m4a",
"x-m4a": "m4a",
mpeg: "mp3",
"x-wav": "wav",
wave: "wav",
};
/**
* Turns a composer dictation into text with the bot's transcription
* model, for the user to edit and send. The recording isn't kept.
*/
export const handler: BunextAPIRouteHandler<
APIResponseObject<{ text: string }>
> = async ({ req, body }) => {
try {
if (req.method != "POST") {
throw new AppError(`Method not allowed`);
}
const { user, user_types } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
if (isReadOnlyUser({ user_types: user_types || [] })) {
throw new AppError(`This user can only read records.`);
}
const { bot_id, workspace_id, data_url } = parseWithSchema({
schema: TranscribeVoiceRequestSchema,
data: body,
});
const conversation = await grabUserConversation({ user_id: user.id, bot_id, workspace_id });
const type = data_url.match(/^data:audio\/([a-z0-9.+-]+)/i)?.[1]?.toLowerCase() || "webm";
const { buffer } = await bufferFromBase64(data_url);
if (!buffer.length) {
throw new AppError(`Nothing was recorded.`);
}
const text = await transcribeAudio({
audio: { data: new Uint8Array(buffer), format: AudioFormats[type] || type },
bot_id: conversation.bot.id,
workspace_id: conversation.workspace_id,
});
if (!text) {
throw new AppError(`No speech was heard. Try again a little closer to the mic.`);
}
return { success: true, singleRes: { text } };
} catch (error: unknown) {
return {
success: false,
msg: error instanceof HarnessError ? error.message : safeErrorMessage({ error }),
};
}
};