初始提交
This commit is contained in:
161
src/services/api.js
Normal file
161
src/services/api.js
Normal file
@@ -0,0 +1,161 @@
|
||||
import axios from 'axios';
|
||||
|
||||
class ApiService {
|
||||
constructor() {
|
||||
this.baseUrl = '/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_130103', agentRtcUid = '59560') {
|
||||
try {
|
||||
const response = await this.client.post(
|
||||
`${this.baseUrl}/projects/${this.projectId}/join/`,
|
||||
{
|
||||
name: channelName,
|
||||
properties: {
|
||||
channel: channelName,
|
||||
agent_rtc_uid: agentRtcUid,
|
||||
remote_rtc_uids: ["*"],
|
||||
enable_string_uid: true,
|
||||
idle_timeout: 120,
|
||||
llm: {
|
||||
url: "/ai-api",
|
||||
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: "minimax",
|
||||
params: {
|
||||
group_id: "wN-fMujjNdcwJ2M3-MbhMHSF6-j_3dT3",
|
||||
key: "4417529362",
|
||||
model: "speech-01-turbo-240228",
|
||||
voice_settings: {
|
||||
voice_id: "female-shaonv",
|
||||
speed: 1,
|
||||
vol: 1,
|
||||
pitch: 0,
|
||||
emotion: "neutral"
|
||||
}
|
||||
}
|
||||
},
|
||||
parameters: {
|
||||
transcript: {
|
||||
enable: true,
|
||||
protocol_version: "v2",
|
||||
enable_words: false,
|
||||
redundant: false
|
||||
},
|
||||
enable_metrics: true,
|
||||
audio_scenario: "default"
|
||||
},
|
||||
token: "007eJxTYDB8wl8ofHzCsqYtWZqLK64uTOnlWjOtuUS7m6Od7Q7P7+cKDAaGiYYpqUnJlmYmiSbJZokWKWlAvlGiWXKqeWKiScrFJdfTGwIZGV5YrmVhZIBAEF+EITk/rywxE0gW5+ekxhsaGxgaGDMwAADNWiaV",
|
||||
advanced_features: {
|
||||
enable_aivad: true
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
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();
|
||||
Reference in New Issue
Block a user