import { VoiceSDK, ChatSDK } from '@odin-ai-staging/sdk';
class VoiceCustomerSupport {
private voiceSDK: VoiceSDK;
private chatSDK: ChatSDK;
private activeSession?: string;
constructor() {
const config = {
baseUrl: process.env.API_BASE_URL,
projectId: process.env.PROJECT_ID,
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
};
this.voiceSDK = new VoiceSDK(config);
this.chatSDK = new ChatSDK(config);
}
async startSupportSession(customerId: string, issueType: string) {
try {
// Create a new chat for this support session
const chat = await this.chatSDK.createChat(
`Voice Support - ${issueType}`,
[] // Could add relevant document keys based on issue type
);
// Start voice conversation
this.activeSession = await this.voiceSDK.startVoiceConversation({
saveToChat: true,
existingChatId: chat.chat_id,
agentId: this.getAgentForIssueType(issueType),
userInfo: {
name: `Customer ${customerId}`,
id: customerId
},
callbacks: {
onConnect: () => {
console.log('Support session started');
this.logSupportEvent('session_started', { customerId, issueType });
},
onTranscription: (text, isFinal) => {
if (isFinal) {
this.logSupportEvent('customer_spoke', {
customerId,
text: text.substring(0, 100) // Log first 100 chars
});
}
},
onMessage: (message) => {
if (message.type === 'ai_speech') {
this.logSupportEvent('agent_responded', {
customerId,
responseLength: message.text.length
});
}
},
onConversationSaved: (chatId, messageId) => {
console.log(`Support conversation saved to chat ${chatId}`);
},
onDisconnect: (details) => {
this.logSupportEvent('session_ended', {
customerId,
reason: details?.reason,
duration: this.getSessionDuration()
});
}
}
});
return {
sessionId: this.activeSession,
chatId: chat.chat_id
};
} catch (error) {
console.error('Failed to start support session:', error);
throw error;
}
}
async endSupportSession() {
if (this.activeSession) {
await this.voiceSDK.endVoiceSession(this.activeSession);
this.activeSession = undefined;
}
}
private getAgentForIssueType(issueType: string): string {
const agentMap = {
'technical': 'agent_technical_support',
'billing': 'agent_billing_support',
'general': 'agent_general_support'
};
return agentMap[issueType] || agentMap['general'];
}
private logSupportEvent(event: string, data: any) {
console.log(`Support Event: ${event}`, data);
// Send to your analytics/logging system
}
private getSessionDuration(): number {
// Calculate session duration
return 0; // Placeholder
}
}