添加结束对话的功能

This commit is contained in:
fsy
2025-03-22 20:47:27 +08:00
parent ba8c73aac3
commit 91eb775713
2 changed files with 11 additions and 101 deletions

View File

@@ -5,7 +5,6 @@ class ApiService {
this.baseUrl = '/create-api'; // Using Vite proxy instead of direct URL
this.projectId = '01a1debc964a4c6a8df1de2a6ce7aa4d';
this.authToken = 'Basic OGRkM2EzOGUxNTJjNGU1NDlmNWMwOTg0YmRhYzc1ZTE6ZWY1MTI2ZTRmMWFlNGE5MWE0MzVhN2Q0ZDc0YzNlYjg='; // Set the auth token
this.sessionId = null;
this.client = axios.create({
headers: {
'Content-Type': 'application/json',
@@ -14,7 +13,10 @@ class ApiService {
withCredentials: true
});
}
setAgentId(agentId) {
this.agentId = agentId;
}
/**
* Set the authentication token
* @param {string} token - The authentication token
@@ -100,8 +102,6 @@ class ApiService {
}
}
);
this.sessionId = response.data.session_id;
return response.data;
} catch (error) {
console.error('Error joining project:', error);
@@ -114,16 +114,13 @@ class ApiService {
* @returns {Promise} - The response from the API
*/
async endSession() {
if (!this.sessionId) {
return null;
}
console.log("this.agentId:",this.agentId);
try {
const response = await this.client.delete(
`${this.baseUrl}/sessions/${this.sessionId}/`
const response = await this.client.post(
`${this.baseUrl}/projects/${this.projectId}/agents/${this.agentId}/leave`
);
this.sessionId = null;
return response.data;
} catch (error) {
console.error('Error ending session:', error);

View File

@@ -40,12 +40,11 @@ export const useChatStore = defineStore('chat', {
console.log('res:',response);
// Join the Agora channel
// const { token, channel } = response.properties;
// const agentRtcUid = response.properties.agent_rtc_uid;
const agent_id = response.agent_id;
const create_ts =response.create_ts;
const status =response.status;
apiService.setAgentId(agent_id);
await agoraService.join(agent_id, create_ts, status)
this.isConnected = true;
@@ -92,7 +91,7 @@ export const useChatStore = defineStore('chat', {
}
// 监听语音事件
this.setupSpeechEventListeners();
// this.setupSpeechEventListeners();
return true;
},
@@ -142,7 +141,7 @@ export const useChatStore = defineStore('chat', {
this.isListening = false;
// 移除语音事件监听
this.removeSpeechEventListeners();
// this.removeSpeechEventListeners();
// 结束Agora对话状态
agoraService.endConversation();
@@ -150,38 +149,6 @@ export const useChatStore = defineStore('chat', {
return true;
},
/**
* 设置语音事件监听
*/
setupSpeechEventListeners() {
window.addEventListener('speech-start', this.handleSpeechStart.bind(this));
window.addEventListener('speech-end', this.handleSpeechEnd.bind(this));
window.addEventListener('speech-update', this.handleSpeechUpdate.bind(this));
},
/**
* 移除语音事件监听
*/
removeSpeechEventListeners() {
window.removeEventListener('speech-start', this.handleSpeechStart.bind(this));
window.removeEventListener('speech-end', this.handleSpeechEnd.bind(this));
window.removeEventListener('speech-update', this.handleSpeechUpdate.bind(this));
},
/**
* 处理语音开始事件
*/
handleSpeechStart() {
this.isSpeaking = true;
},
/**
* 处理语音结束事件
*/
handleSpeechEnd() {
this.isSpeaking = false;
},
/**
* 处理语音更新事件
* @param {CustomEvent} event - 包含语音文本的事件
@@ -264,52 +231,8 @@ export const useChatStore = defineStore('chat', {
this.currentTranscript = transcript;
},
/**
* Send a message to the AI
* @param {string} content - The message content
*/
// async sendMessage(content) {
// try {
// this.isProcessing = true;
// console.log("user send content:",content);
// // Add user message to the list
// this.addMessage({
// id: Date.now(),
// content: content,
// sender: 'user',
// timestamp: new Date().toISOString(),
// });
// // Send message to API
// const response = await apiService.sendMessage(content);
// // 检查response返回的内容
// console.log("AI response:",response);
// // Add AI response to the list
// if (response && response.content) {
// this.addMessage({
// id: Date.now(),
// content: response.content,
// sender: 'ai',
// timestamp: new Date().toISOString(),
// });
// }
// this.isProcessing = false;
// return true;
// } catch (error) {
// this.error = error.message || 'Failed to send message';
// this.isProcessing = false;
// console.error('Error sending message:', error);
// return false;
// }
// },
/**
* Add a message to the list
* @param {Object} message - The message object
*/
addMessage(message) {
this.messages.push(message);
@@ -324,20 +247,11 @@ export const useChatStore = defineStore('chat', {
/**
* Update the audio level
* @param {number} level - The audio level (0-100)
*/
updateAudioLevel(level) {
this.audioLevel = level;
},
/**
* Set the speaking state
* @param {boolean} isSpeaking - Whether the AI is speaking
*/
setSpeaking(isSpeaking) {
this.isSpeaking = isSpeaking;
},
/**
* End the chat session
*/
@@ -356,7 +270,6 @@ export const useChatStore = defineStore('chat', {
this.isConnected = false;
this.isListening = false;
this.isSpeaking = false;
this.isProcessing = false;
this.currentTranscript = '';
this.inConversation = false;