Files
mars-ui/src/services/api.js

161 lines
4.6 KiB
JavaScript

import axios from 'axios';
class ApiService {
constructor() {
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',
'Authorization': this.authToken
},
withCredentials: true
});
}
/**
* Set the authentication token
* @param {string} token - The authentication token
*/
setAuthToken(token) {
this.authToken = token;
this.client.defaults.headers.common['Authorization'] = token;
}
/**
* Join a project to start a conversation
* @param {string} channelName - The channel name
* @param {string} agentRtcUid - The agent RTC UID
* @returns {Promise} - The response from the API
*/
async joinProject(channelName = 'convaiconsole_122624', agentRtcUid = '29501') {
try {
const response = await this.client.post(
`${this.baseUrl}/projects/${this.projectId}/join/`,
{
"name": "convaiconsole_122624",
"properties": {
"channel": "convaiconsole_122624",
"agent_rtc_uid": "29501",
"remote_rtc_uids": [
"*"
],
"enable_string_uid": true,
"idle_timeout": 120,
"llm": {
"url": "https://vip.apiyi.com/v1/chat/completions",
"api_key": "sk-xVIc9b7EfY7LlPagF31d90F4736f4aE18cB91b5957A40506",
"max_history": 10,
"system_messages": [
{
"role": "system",
"content": ""
}
],
"params": {
"model": "deepseek-r1",
"max_token": 1024
},
"greeting_message": "你好呀,有什么可以帮您?",
"failure_message": "我出错了,请稍等!"
},
"asr": {
"language": "zh-CN"
},
"vad": {
"interrupt_duration_ms": 160,
"prefix_padding_ms": 300,
"silence_duration_ms": 480,
"threshold": 0.5
},
"tts": {
"vendor": "bytedance",
"params": {
"token": "wN-fMujjNdcwJ2M3-MbhMHSF6-j_3dT3",
"app_id": "4417529362",
"cluster": "volcano_tts",
"voice_type": "BV700_streaming",
"speed_ratio": 1,
"volume_ratio": 1,
"pitch_ratio": 1,
"emotion": ""
}
},
"parameters": {
"transcript": {
"enable": true,
"protocol_version": "v2",
"enable_words": false,
"redundant": false
},
"enable_metrics": true,
"audio_scenario": "default"
},
"token": "007eJxTYEibc7f9w4Ebac5HtT9ej/CL7KzPrGb+GZmn6/G+kLVp8XsFBgPDRMOU1KRkSzOTRJNks0SLlDQg3yjRLDnVPDHRJGV67630hkBGhhvth1kZGSAQxBdhSM7PK0vMBJLF+Tmp8YZGRmZGJgwMAIF3KEg=",
"advanced_features": {
"enable_aivad": false
}
}
}
);
this.sessionId = response.data.session_id;
return response.data;
} catch (error) {
console.error('Error joining project:', error);
throw error;
}
}
/**
* Send a message to the AI agent
* @param {string} message - The message to send
* @returns {Promise} - The response from the API
*/
async sendMessage(message) {
if (!this.sessionId) {
throw new Error('No active session. Please join a project first.');
}
try {
const response = await this.client.post(
`${this.baseUrl}/sessions/${this.sessionId}/messages/`,
{
type: 'text',
content: message
}
);
return response.data;
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
}
/**
* End the current session
* @returns {Promise} - The response from the API
*/
async endSession() {
if (!this.sessionId) {
return null;
}
try {
const response = await this.client.delete(
`${this.baseUrl}/sessions/${this.sessionId}/`
);
this.sessionId = null;
return response.data;
} catch (error) {
console.error('Error ending session:', error);
throw error;
}
}
}
export default new ApiService();