Spaces:
Building
Building
Update flare-ui/src/app/services/conversation-manager.service.ts
Browse files
flare-ui/src/app/services/conversation-manager.service.ts
CHANGED
@@ -83,59 +83,58 @@ export class ConversationManagerService implements OnDestroy {
|
|
83 |
}
|
84 |
|
85 |
async startConversation(sessionId: string, config?: ConversationConfig): Promise<void> {
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
}
|
95 |
-
|
96 |
-
this.sessionId = sessionId;
|
97 |
-
|
98 |
-
// Reset state but don't clear messages
|
99 |
-
// this.clearMessages(); // BU SATIRI KALDIR veya YORUM YAP
|
100 |
-
this.currentStateSubject.next('idle');
|
101 |
-
|
102 |
-
// Connect WebSocket first
|
103 |
-
await this.wsService.connect(sessionId).catch(error => {
|
104 |
-
throw new Error(`WebSocket connection failed: ${error.message}`);
|
105 |
-
});
|
106 |
-
|
107 |
-
// Start audio recording
|
108 |
-
await this.audioService.startRecording().catch(error => {
|
109 |
-
// Disconnect WebSocket if audio fails
|
110 |
-
this.wsService.disconnect();
|
111 |
-
throw new Error(`Audio recording failed: ${error.message}`);
|
112 |
-
});
|
113 |
-
|
114 |
-
// Set up subscriptions
|
115 |
-
this.setupSubscriptions();
|
116 |
-
|
117 |
-
// Send start signal with configuration
|
118 |
-
this.wsService.sendControl('start_session', this.conversationConfig);
|
119 |
-
|
120 |
-
console.log('✅ Conversation started with config:', this.conversationConfig);
|
121 |
-
|
122 |
-
} catch (error: any) {
|
123 |
-
console.error('Failed to start conversation:', error);
|
124 |
-
|
125 |
-
const conversationError: ConversationError = {
|
126 |
-
type: this.determineErrorType(error),
|
127 |
-
message: error.message || 'Failed to start conversation',
|
128 |
-
details: error,
|
129 |
-
timestamp: new Date()
|
130 |
-
};
|
131 |
-
|
132 |
-
this.errorSubject.next(conversationError);
|
133 |
-
this.currentStateSubject.next('error');
|
134 |
-
this.cleanup();
|
135 |
-
|
136 |
-
throw error;
|
137 |
}
|
138 |
-
}
|
139 |
|
140 |
stopConversation(): void {
|
141 |
try {
|
|
|
83 |
}
|
84 |
|
85 |
async startConversation(sessionId: string, config?: ConversationConfig): Promise<void> {
|
86 |
+
try {
|
87 |
+
if (!sessionId) {
|
88 |
+
throw new Error('Session ID is required');
|
89 |
+
}
|
90 |
+
|
91 |
+
// Update configuration
|
92 |
+
if (config) {
|
93 |
+
this.conversationConfig = { ...this.conversationConfig, ...config };
|
94 |
+
}
|
95 |
+
|
96 |
+
this.sessionId = sessionId;
|
97 |
+
|
98 |
+
// Reset state
|
99 |
+
this.currentStateSubject.next('idle');
|
100 |
+
|
101 |
+
// Connect WebSocket first
|
102 |
+
await this.wsService.connect(sessionId).catch(error => {
|
103 |
+
throw new Error(`WebSocket connection failed: ${error.message}`);
|
104 |
+
});
|
105 |
+
|
106 |
+
// Set up subscriptions BEFORE sending any messages
|
107 |
+
this.setupSubscriptions();
|
108 |
+
|
109 |
+
// Send start signal with configuration
|
110 |
+
this.wsService.sendControl('start_session', this.conversationConfig);
|
111 |
+
|
112 |
+
// Start audio recording AFTER WebSocket is ready
|
113 |
+
await this.audioService.startRecording().catch(error => {
|
114 |
+
// Disconnect WebSocket if audio fails
|
115 |
+
this.wsService.disconnect();
|
116 |
+
throw new Error(`Audio recording failed: ${error.message}`);
|
117 |
+
});
|
118 |
+
|
119 |
+
console.log('✅ Conversation started with config:', this.conversationConfig);
|
120 |
+
|
121 |
+
} catch (error: any) {
|
122 |
+
console.error('Failed to start conversation:', error);
|
123 |
+
|
124 |
+
const conversationError: ConversationError = {
|
125 |
+
type: this.determineErrorType(error),
|
126 |
+
message: error.message || 'Failed to start conversation',
|
127 |
+
details: error,
|
128 |
+
timestamp: new Date()
|
129 |
+
};
|
130 |
+
|
131 |
+
this.errorSubject.next(conversationError);
|
132 |
+
this.currentStateSubject.next('error');
|
133 |
+
this.cleanup();
|
134 |
+
|
135 |
+
throw error;
|
136 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
}
|
|
|
138 |
|
139 |
stopConversation(): void {
|
140 |
try {
|