Озвучивание ответов (Google Chirp 3 HD): режим и голос на чат, миграция 007, загрузка аудио в Slack; docs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,12 +6,17 @@ import { loadPrompt } from "../agents/prompts.js";
|
||||
import { saveProblems } from "../agents/step2.js";
|
||||
import type { DriveSync } from "./drive.js";
|
||||
import type { SlackApi } from "./api.js";
|
||||
import { normalizeVoice, speechText, type ReplyMode, type Tts } from "./tts.js";
|
||||
|
||||
export interface ManagerInfo {
|
||||
personId: string;
|
||||
name: string;
|
||||
slackUserId: string | null;
|
||||
slackChannelId: string | null;
|
||||
/** Режим ответов в чате: text (по умолчанию) / both / audio. */
|
||||
replyMode?: ReplyMode;
|
||||
/** Голос Chirp 3 HD для чата; null - по умолчанию. */
|
||||
ttsVoice?: string | null;
|
||||
}
|
||||
|
||||
export interface IncomingMessage {
|
||||
@@ -25,7 +30,10 @@ export interface IncomingMessage {
|
||||
|
||||
export interface ChatDeps {
|
||||
ctx: AgentContext;
|
||||
slack: Pick<SlackApi, "postMessage" | "fileInfo" | "download">;
|
||||
slack: Pick<SlackApi, "postMessage" | "fileInfo" | "download" | "uploadFile">;
|
||||
/** Синтез речи; без него ответы всегда текстовые. */
|
||||
tts?: Tts;
|
||||
defaultVoice?: string;
|
||||
drive?: DriveSync;
|
||||
timezone: string;
|
||||
now?: () => Date;
|
||||
@@ -36,6 +44,8 @@ export interface ChatDeps {
|
||||
export interface ChatResult {
|
||||
reply: string | null;
|
||||
published: boolean;
|
||||
/** Аудиоответ отправлен. */
|
||||
audioSent?: boolean;
|
||||
attachments: { name: string; path: string; mimetype: string; size: number; link?: string }[];
|
||||
}
|
||||
|
||||
@@ -164,7 +174,8 @@ export async function handleManagerMessages(
|
||||
const replyRaw = out.sections.get("ответ менеджеру")!;
|
||||
const reply = /^\(без ответа\)\.?$/i.test(replyRaw) ? null : replyRaw;
|
||||
let published = false;
|
||||
if (reply && channel && !deps.dryPublish) published = await deps.slack.postMessage(channel, reply);
|
||||
let audioSent = false;
|
||||
if (reply && channel && !deps.dryPublish) ({ published, audioSent } = await publishReply(deps, manager, channel, reply));
|
||||
if (reply && opts.systemEvent) {
|
||||
// напоминание ассистента по событию - в историю напоминаний (вход 7)
|
||||
await ctx.pool.query(
|
||||
@@ -176,5 +187,25 @@ export async function handleManagerMessages(
|
||||
|
||||
// 6. транскрипты и итоговые файлы -> Drive
|
||||
if (deps.drive && attachments.length) await deps.drive.upload(mediaDir, sub).catch((e) => ctx.log.warn({ err: String(e) }, "повторная выгрузка в Drive не удалась"));
|
||||
return { reply, published, attachments };
|
||||
return { reply, published, audioSent, attachments };
|
||||
}
|
||||
|
||||
/** Публикует ответ по настройке чата: текст / текст и аудио / только аудио. Любой сбой аудио - ответ уходит текстом, ИТ видит проблему в логе. */
|
||||
export async function publishReply(deps: ChatDeps, manager: ManagerInfo, channel: string, reply: string): Promise<{ published: boolean; audioSent: boolean }> {
|
||||
const mode = manager.replyMode ?? "text";
|
||||
const voice = normalizeVoice(manager.ttsVoice) ?? normalizeVoice(deps.defaultVoice) ?? "Aoede";
|
||||
let audioSent = false;
|
||||
if (mode !== "text" && deps.tts && deps.slack.uploadFile && speechText(reply)) {
|
||||
try {
|
||||
const mp3 = await deps.tts.synthesize(reply, voice);
|
||||
await deps.slack.uploadFile(channel, "assistant-reply.mp3", mp3, "Voice reply");
|
||||
audioSent = true;
|
||||
} catch (e) {
|
||||
deps.ctx.log.warn({ manager: manager.personId, err: String(e) }, "аудиоответ не отправлен, отправляю текстом");
|
||||
await deps.ctx.pool.query("insert into agent_problem_log (manager_person_id, step, severity, body) values ($1,'step4','log',$2)", [manager.personId, `Аудиоответ (Chirp 3 HD, ${voice}) не отправлен: ${String(e).slice(0, 400)}`]);
|
||||
}
|
||||
}
|
||||
const needText = mode === "text" || mode === "both" || !audioSent;
|
||||
const published = needText ? await deps.slack.postMessage(channel, reply) : false;
|
||||
return { published: published || audioSent, audioSent };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user