1
This commit is contained in:
@@ -11,8 +11,11 @@
|
||||
#import "KBAiChatView.h"
|
||||
#import "KBAiRecordButton.h"
|
||||
#import "LSTPopView.h"
|
||||
#import "VoiceChatStreamingManager.h"
|
||||
#import "KBUserSessionManager.h"
|
||||
|
||||
@interface KBAiMainVC () <KBAiRecordButtonDelegate>
|
||||
@interface KBAiMainVC () <KBAiRecordButtonDelegate,
|
||||
VoiceChatStreamingManagerDelegate>
|
||||
@property(nonatomic, weak) LSTPopView *popView;
|
||||
|
||||
// UI
|
||||
@@ -28,6 +31,13 @@
|
||||
|
||||
// 核心模块
|
||||
@property(nonatomic, strong) ConversationOrchestrator *orchestrator;
|
||||
@property(nonatomic, strong) VoiceChatStreamingManager *streamingManager;
|
||||
|
||||
// 文本跟踪
|
||||
@property(nonatomic, strong) NSMutableString *assistantVisibleText;
|
||||
|
||||
// 日志节流
|
||||
@property(nonatomic, assign) NSTimeInterval lastRMSLogTime;
|
||||
|
||||
@end
|
||||
|
||||
@@ -44,6 +54,7 @@
|
||||
|
||||
[self setupUI];
|
||||
[self setupOrchestrator];
|
||||
[self setupStreamingManager];
|
||||
}
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated {
|
||||
@@ -56,6 +67,7 @@
|
||||
|
||||
// 页面消失时停止对话
|
||||
[self.orchestrator stop];
|
||||
[self.streamingManager disconnect];
|
||||
}
|
||||
|
||||
- (void)viewDidLayoutSubviews {
|
||||
@@ -184,11 +196,15 @@
|
||||
- (void)setupOrchestrator {
|
||||
self.orchestrator = [[ConversationOrchestrator alloc] init];
|
||||
|
||||
// 配置服务器地址(TODO: 替换为实际地址)
|
||||
// self.orchestrator.asrServerURL = @"wss://your-asr-server.com/ws/asr";
|
||||
// self.orchestrator.llmServerURL =
|
||||
// @"https://your-llm-server.com/api/chat/stream";
|
||||
// self.orchestrator.ttsServerURL = @"https://your-tts-server.com/api/tts";
|
||||
// 配置服务器地址
|
||||
// 1. ASR 语音识别服务(WebSocket)
|
||||
self.orchestrator.asrServerURL = @"ws://192.168.2.21:7529/ws/asr";
|
||||
|
||||
// 2. LLM 大语言模型服务(HTTP Stream)
|
||||
self.orchestrator.llmServerURL = @"http://192.168.2.21:7529/api/chat/stream";
|
||||
|
||||
// 3. TTS 语音合成服务(HTTP)
|
||||
self.orchestrator.ttsServerURL = @"http://192.168.2.21:7529/api/tts/stream";
|
||||
|
||||
__weak typeof(self) weakSelf = self;
|
||||
|
||||
@@ -278,6 +294,16 @@
|
||||
};
|
||||
}
|
||||
|
||||
#pragma mark - Streaming Manager
|
||||
|
||||
- (void)setupStreamingManager {
|
||||
self.streamingManager = [[VoiceChatStreamingManager alloc] init];
|
||||
self.streamingManager.delegate = self;
|
||||
self.streamingManager.serverURL = @"ws://192.168.2.21:7529/api/ws/chat";
|
||||
self.assistantVisibleText = [[NSMutableString alloc] init];
|
||||
self.lastRMSLogTime = 0;
|
||||
}
|
||||
|
||||
#pragma mark - 事件
|
||||
- (void)showComment {
|
||||
CGFloat customViewHeight = KB_SCREEN_HEIGHT * (0.8);
|
||||
@@ -367,16 +393,112 @@
|
||||
#pragma mark - KBAiRecordButtonDelegate
|
||||
|
||||
- (void)recordButtonDidBeginPress:(KBAiRecordButton *)button {
|
||||
[self.orchestrator userDidPressRecord];
|
||||
NSLog(@"[KBAiMainVC] Record button began press");
|
||||
NSString *token = [[KBUserSessionManager shared] accessToken] ?: @"";
|
||||
if (token.length == 0) {
|
||||
[[KBUserSessionManager shared] goLoginVC];
|
||||
return;
|
||||
}
|
||||
|
||||
self.statusLabel.text = @"正在连接...";
|
||||
self.recordButton.state = KBAiRecordButtonStateRecording;
|
||||
[self.streamingManager startWithToken:token language:@"en" voiceId:nil];
|
||||
}
|
||||
|
||||
- (void)recordButtonDidEndPress:(KBAiRecordButton *)button {
|
||||
[self.orchestrator userDidReleaseRecord];
|
||||
NSLog(@"[KBAiMainVC] Record button end press");
|
||||
[self.streamingManager stopAndFinalize];
|
||||
}
|
||||
|
||||
- (void)recordButtonDidCancelPress:(KBAiRecordButton *)button {
|
||||
// 取消录音(同样调用 release,ASR 会返回空或部分结果)
|
||||
[self.orchestrator userDidReleaseRecord];
|
||||
NSLog(@"[KBAiMainVC] Record button cancel press");
|
||||
[self.streamingManager cancel];
|
||||
}
|
||||
|
||||
#pragma mark - VoiceChatStreamingManagerDelegate
|
||||
|
||||
- (void)voiceChatStreamingManagerDidConnect {
|
||||
self.statusLabel.text = @"已连接,准备中...";
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidDisconnect:(NSError *_Nullable)error {
|
||||
self.recordButton.state = KBAiRecordButtonStateNormal;
|
||||
if (error) {
|
||||
[self showError:error];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidStartSession:(NSString *)sessionId {
|
||||
self.statusLabel.text = @"正在聆听...";
|
||||
self.recordButton.state = KBAiRecordButtonStateRecording;
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidStartTurn:(NSInteger)turnIndex {
|
||||
self.statusLabel.text = @"正在聆听...";
|
||||
self.recordButton.state = KBAiRecordButtonStateRecording;
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidReceiveEagerEndOfTurnWithTranscript:(NSString *)text
|
||||
confidence:(double)confidence {
|
||||
self.statusLabel.text = @"准备响应...";
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidResumeTurn {
|
||||
self.statusLabel.text = @"正在聆听...";
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidUpdateRMS:(float)rms {
|
||||
[self.recordButton updateVolumeRMS:rms];
|
||||
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
|
||||
if (now - self.lastRMSLogTime >= 1.0) {
|
||||
self.lastRMSLogTime = now;
|
||||
NSLog(@"[KBAiMainVC] RMS: %.3f", rms);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidReceiveInterimTranscript:(NSString *)text {
|
||||
self.statusLabel.text = text.length > 0 ? text : @"正在识别...";
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidReceiveFinalTranscript:(NSString *)text {
|
||||
if (text.length > 0) {
|
||||
[self.chatView addUserMessage:text];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidReceiveLLMStart {
|
||||
self.statusLabel.text = @"AI 正在思考...";
|
||||
[self.assistantVisibleText setString:@""];
|
||||
[self.chatView addAssistantMessage:@""];
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidReceiveLLMToken:(NSString *)token {
|
||||
if (token.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self.assistantVisibleText appendString:token];
|
||||
[self.chatView updateLastAssistantMessage:self.assistantVisibleText];
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidReceiveAudioChunk:(NSData *)audioData {
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidCompleteWithTranscript:(NSString *)transcript
|
||||
aiResponse:(NSString *)aiResponse {
|
||||
NSString *finalText = aiResponse.length > 0 ? aiResponse
|
||||
: self.assistantVisibleText;
|
||||
if (finalText.length > 0) {
|
||||
[self.chatView updateLastAssistantMessage:finalText];
|
||||
[self.chatView markLastAssistantMessageComplete];
|
||||
}
|
||||
self.recordButton.state = KBAiRecordButtonStateNormal;
|
||||
self.statusLabel.text = @"完成";
|
||||
}
|
||||
|
||||
- (void)voiceChatStreamingManagerDidFail:(NSError *)error {
|
||||
self.recordButton.state = KBAiRecordButtonStateNormal;
|
||||
[self showError:error];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user