Update code

This commit is contained in:
User
2026-03-12 12:47:56 +08:00
parent 92e7fc5bda
commit 9dab61345c
9383 changed files with 1463454 additions and 1 deletions

View File

@@ -0,0 +1,98 @@
import axios from 'axios';
const api = axios.create({
baseURL: '/api/chat',
timeout: 30000,
});
export async function startChatSession(sessionId, voiceSubtitles = [], systemPrompt = '') {
const { data } = await api.post('/start', { sessionId, voiceSubtitles, systemPrompt });
return data;
}
export async function sendMessage(sessionId, message) {
const { data } = await api.post('/send', { sessionId, message });
return data;
}
/**
* SSE 流式发送消息,逐块回调
* @param {string} sessionId
* @param {string} message
* @param {object} callbacks - { onChunk, onToolCall, onDone, onError }
* @returns {function} abort - 调用可取消请求
*/
export function sendMessageStream(sessionId, message, { onChunk, onToolCall, onDone, onError }) {
const controller = new AbortController();
(async () => {
try {
const response = await fetch('/api/chat/send-stream', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sessionId, message }),
signal: controller.signal,
});
if (!response.ok) {
const err = await response.json().catch(() => ({ error: response.statusText }));
onError?.(err.error || 'Request failed');
return;
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n\n');
buffer = lines.pop() || '';
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed.startsWith('data: ')) continue;
try {
const data = JSON.parse(trimmed.slice(6));
switch (data.type) {
case 'chunk':
onChunk?.(data.content);
break;
case 'tool_call':
onToolCall?.(data.tools);
break;
case 'done':
onDone?.(data.content);
break;
case 'error':
onError?.(data.error);
break;
}
} catch (e) {
// skip malformed SSE
}
}
}
} catch (err) {
if (err.name !== 'AbortError') {
onError?.(err.message || 'Stream failed');
}
}
})();
return () => controller.abort();
}
export async function getChatHistory(sessionId) {
const { data } = await api.get(`/history/${sessionId}`);
return data.data;
}
export async function deleteChatSession(sessionId) {
const { data } = await api.delete(`/${sessionId}`);
return data;
}

View File

@@ -0,0 +1,323 @@
/**
* 火山引擎 RTC SDK 封装
* 负责 WebRTC 音频流的建立和管理
*/
class RTCService {
constructor() {
this.engine = null;
this.joined = false;
this.callbacks = {
onSubtitle: null,
onAudioStatus: null,
onConnectionStateChange: null,
onError: null,
onUserJoined: null,
onUserLeft: null,
onToolCall: null,
onRoomMessage: null,
};
}
async init(appId) {
if (this.engine) {
this.destroy();
}
try {
const VERTC = await import('@volcengine/rtc');
const createEngine = VERTC.default?.createEngine || VERTC.createEngine;
const events = VERTC.default?.events || VERTC.events;
if (!createEngine) {
throw new Error('Failed to load RTC SDK: createEngine not found');
}
this.engine = createEngine(appId);
this.events = events;
this.engine.on(events.onConnectionStateChanged, (state) => {
console.log('[RTC] Connection state:', state);
this.callbacks.onConnectionStateChange?.(state);
});
if (events.onSubtitleStateChanged) {
this.engine.on(events.onSubtitleStateChanged, (state) => {
console.log('[RTC] Subtitle state changed:', state);
});
}
if (events.onSubtitleMessageReceived) {
this.engine.on(events.onSubtitleMessageReceived, (subtitles) => {
console.log('[RTC] Subtitle received:', subtitles.length, 'items');
subtitles.forEach((sub) => {
// bot 的 userId 以 'bot_' 开头,无 userId 或 bot_ 开头都是 assistant
const isBot = !sub.userId || sub.userId.startsWith('bot_');
this.callbacks.onSubtitle?.({
text: sub.text,
role: isBot ? 'assistant' : 'user',
isFinal: sub.definite,
sequence: sub.sequence,
});
});
});
}
this.engine.on(events.onUserJoined, (info) => {
console.log('[RTC] User joined:', info.userInfo?.userId);
this.callbacks.onUserJoined?.(info);
});
this.engine.on(events.onUserLeave, (info) => {
console.log('[RTC] User left:', info.userInfo?.userId);
this.callbacks.onUserLeft?.(info);
});
this.engine.on(events.onError, (error) => {
console.error('[RTC] Error:', error);
this.callbacks.onError?.(error);
});
// === Function Calling: 监听房间消息SDK 回调参数是单个 event 对象) ===
if (events.onRoomBinaryMessageReceived) {
this.engine.on(events.onRoomBinaryMessageReceived, (event) => {
try {
const uid = event.uid || event.userId || 'unknown';
const raw = event.message;
const text = typeof raw === 'string' ? raw : new TextDecoder().decode(raw);
console.log('[RTC][FC] Room binary from', uid, ':', text.substring(0, 500));
this.callbacks.onRoomMessage?.({ uid, text });
const parsed = JSON.parse(text);
this._handleRoomMessage(uid, parsed);
} catch (e) {
console.log('[RTC][FC] Room binary (non-JSON):', e.message);
}
});
}
if (events.onRoomMessageReceived) {
this.engine.on(events.onRoomMessageReceived, (event) => {
const uid = event.uid || event.userId || 'unknown';
const msg = event.message || '';
console.log('[RTC][FC] Room text from', uid, ':', String(msg).substring(0, 500));
this.callbacks.onRoomMessage?.({ uid, text: String(msg) });
try {
const parsed = JSON.parse(msg);
this._handleRoomMessage(uid, parsed);
} catch (e) {
console.log('[RTC][FC] Room text (non-JSON):', e.message);
}
});
}
if (events.onUserBinaryMessageReceived) {
this.engine.on(events.onUserBinaryMessageReceived, (event) => {
try {
const uid = event.uid || event.userId || 'unknown';
const raw = event.message;
const text = typeof raw === 'string' ? raw : new TextDecoder().decode(raw);
console.log('[RTC][FC] User binary from', uid, ':', text.substring(0, 500));
this.callbacks.onRoomMessage?.({ uid, text });
const parsed = JSON.parse(text);
this._handleRoomMessage(uid, parsed);
} catch (e) {
console.log('[RTC][FC] User binary (non-JSON):', e.message);
}
});
}
if (events.onUserMessageReceived) {
this.engine.on(events.onUserMessageReceived, (event) => {
const uid = event.uid || event.userId || 'unknown';
const msg = event.message || '';
console.log('[RTC][FC] User text from', uid, ':', String(msg).substring(0, 500));
this.callbacks.onRoomMessage?.({ uid, text: String(msg) });
try {
const parsed = JSON.parse(msg);
this._handleRoomMessage(uid, parsed);
} catch (e) {
console.log('[RTC][FC] User text (non-JSON):', e.message);
}
});
}
// === 诊断事件 ===
if (events.onUserPublishStream) {
this.engine.on(events.onUserPublishStream, (info) => {
console.log('[RTC][DIAG] Remote user published stream:', info.userId, 'mediaType:', info.mediaType);
});
}
if (events.onUserUnpublishStream) {
this.engine.on(events.onUserUnpublishStream, (info) => {
console.log('[RTC][DIAG] Remote user unpublished stream:', info.userId, 'mediaType:', info.mediaType);
});
}
if (events.onAutoplayFailed) {
this.engine.on(events.onAutoplayFailed, (info) => {
console.error('[RTC][DIAG] ❌ Autoplay FAILED! Audio blocked by browser:', info);
});
}
if (events.onPlayerEvent) {
this.engine.on(events.onPlayerEvent, (info) => {
console.log('[RTC][DIAG] Player event:', info);
});
}
if (events.onRemoteStreamStats) {
this.engine.on(events.onRemoteStreamStats, (stats) => {
if (stats.audioRecvBytes > 0) {
console.log('[RTC][DIAG] Receiving audio from:', stats.uid, 'bytes:', stats.audioRecvBytes);
}
});
}
// 启用音频属性报告,检测是否有远端音频
try {
this.engine.enableAudioPropertiesReport?.({ interval: 3000 });
if (events.onRemoteAudioPropertiesReport) {
this.engine.on(events.onRemoteAudioPropertiesReport, (infos) => {
infos?.forEach((info) => {
if (info.audioPropertiesInfo?.linearVolume > 0) {
console.log('[RTC][DIAG] 🔊 Remote audio detected! user:', info.streamKey?.userId, 'volume:', info.audioPropertiesInfo.linearVolume);
}
});
});
}
if (events.onLocalAudioPropertiesReport) {
this.engine.on(events.onLocalAudioPropertiesReport, (infos) => {
infos?.forEach((info) => {
if (info.audioPropertiesInfo?.linearVolume > 0) {
console.log('[RTC][DIAG] 🎤 Local mic active, volume:', info.audioPropertiesInfo.linearVolume);
}
});
});
}
} catch (e) {
console.warn('[RTC][DIAG] enableAudioPropertiesReport not available:', e.message);
}
console.log('[RTC] Engine initialized with diagnostic listeners');
console.log('[RTC] Available events:', Object.keys(events).filter(k => k.startsWith('on')).join(', '));
return true;
} catch (error) {
console.error('[RTC] Init failed:', error);
throw error;
}
}
async joinRoom(roomId, userId, token) {
if (!this.engine) throw new Error('Engine not initialized');
await this.engine.joinRoom(
token,
roomId,
{ userId },
{
isAutoPublish: true,
isAutoSubscribeAudio: true,
isAutoSubscribeVideo: false,
}
);
await this.engine.startAudioCapture();
// 激活字幕接收(必须在 joinRoom 之后调用)
try {
await this.engine.startSubtitle({});
console.log('[RTC] Subtitle enabled');
} catch (e) {
console.warn('[RTC] startSubtitle failed:', e.message || e);
}
this.joined = true;
console.log(`[RTC] Joined room ${roomId} as ${userId}`);
}
async leaveRoom() {
if (!this.engine || !this.joined) return;
try {
await this.engine.stopAudioCapture();
await this.engine.leaveRoom();
this.joined = false;
console.log('[RTC] Left room');
} catch (e) {
console.warn('[RTC] Leave room error:', e);
}
}
async setMuted(muted) {
if (!this.engine) return;
if (muted) {
await this.engine.stopAudioCapture();
} else {
await this.engine.startAudioCapture();
}
}
_handleRoomMessage(uid, parsed) {
console.log('[RTC][FC] Parsed message type:', parsed.type || parsed.event || 'unknown', 'from:', uid);
// 尝试多种可能的 tool call 消息格式
let toolCalls = null;
// 格式1: { type: "function_call", data: { tool_calls: [...] } }
if (parsed.type === 'function_call' && parsed.data?.tool_calls) {
toolCalls = parsed.data.tool_calls;
}
// 格式2: { event: "function_call", tool_calls: [...] }
else if (parsed.event === 'function_call' && parsed.tool_calls) {
toolCalls = parsed.tool_calls;
}
// 格式3: { type: "conversation", data: { event: "function_call", ... } }
else if (parsed.type === 'conversation' && parsed.data?.event === 'function_call') {
toolCalls = parsed.data.tool_calls || [parsed.data];
}
// 格式4: 直接是 tool_calls 数组
else if (parsed.tool_calls) {
toolCalls = parsed.tool_calls;
}
// 格式5: 单个 function_call 对象
else if (parsed.function?.name || parsed.function_name) {
toolCalls = [parsed];
}
if (toolCalls && toolCalls.length > 0) {
console.log('[RTC][FC] ✅ Tool calls detected:', toolCalls.length);
toolCalls.forEach((tc) => {
const callId = tc.id || tc.tool_call_id || `tc_${Date.now()}`;
const funcName = tc.function?.name || tc.function_name || 'unknown';
const args = tc.function?.arguments || tc.arguments || '{}';
console.log(`[RTC][FC] Tool call: ${funcName}(${args}), id=${callId}`);
this.callbacks.onToolCall?.({ tool_call_id: callId, function_name: funcName, arguments: args });
});
} else {
console.log('[RTC][FC] Message is not a tool call, full payload:', JSON.stringify(parsed).substring(0, 300));
}
}
on(event, callback) {
if (event in this.callbacks) {
this.callbacks[event] = callback;
}
}
off(event) {
if (event in this.callbacks) {
this.callbacks[event] = null;
}
}
destroy() {
if (this.engine) {
try {
if (this.joined) {
this.engine.stopAudioCapture().catch(() => {});
this.engine.leaveRoom().catch(() => {});
}
this.engine.destroyEngine?.();
} catch (e) {
console.warn('[RTC] Destroy error:', e);
}
this.engine = null;
this.joined = false;
}
}
}
const rtcService = new RTCService();
export default rtcService;

View File

@@ -0,0 +1,82 @@
import axios from 'axios';
const api = axios.create({
baseURL: '/api/voice',
timeout: 10000,
});
export async function getVoiceConfig() {
const { data } = await api.get('/config');
return data.data;
}
export async function prepareVoiceChat(params) {
const { data } = await api.post('/prepare', params);
return data;
}
export async function startVoiceChat(params) {
const { data } = await api.post('/start', params);
return data;
}
export async function stopVoiceChat(sessionId) {
const { data } = await api.post('/stop', { sessionId });
return data;
}
export async function sendSubtitle(params) {
const { data } = await api.post('/subtitle', params);
return data;
}
export async function getSubtitles(sessionId) {
const { data } = await api.get(`/subtitles/${sessionId}`);
return data.data;
}
export async function getActiveSessions() {
const { data } = await api.get('/sessions');
return data.data;
}
export async function forwardRoomMessage({ roomId, uid, text }) {
const { data } = await api.post('/room_message', { roomId, uid, text });
return data;
}
export async function executeToolCall({ sessionId, toolCallId, functionName, arguments: args }) {
const { data } = await api.post('/tool-callback', {
sessionId,
tool_call_id: toolCallId,
function_name: functionName,
arguments: args,
});
return data;
}
export async function executeFcCallback({ roomId, taskId, type, message }) {
const { data } = await api.post('/fc_callback', {
RoomID: roomId,
TaskID: taskId,
Type: type,
Message: message,
});
return data;
}
// ========== 会话历史 API ==========
const sessionApi = axios.create({
baseURL: '/api/session',
timeout: 10000,
});
export async function getSessionHistory(sessionId, limit = 20) {
const { data } = await sessionApi.get(`/${sessionId}/history`, { params: { limit, format: 'llm' } });
return data.data;
}
export async function switchSessionMode(sessionId, targetMode) {
const { data } = await sessionApi.post(`/${sessionId}/switch`, { targetMode });
return data.data;
}