This commit is contained in:
2026-01-22 13:47:34 +08:00
parent 06a572c08a
commit edc25c159d
10 changed files with 2308 additions and 7 deletions

View File

@@ -7,6 +7,7 @@
#import "KBAiMainVC.h"
#import "ConversationOrchestrator.h"
#import "DeepgramStreamingManager.h"
#import "KBAICommentView.h"
#import "KBAiChatView.h"
#import "KBAiRecordButton.h"
@@ -15,13 +16,15 @@
#import "KBUserSessionManager.h"
@interface KBAiMainVC () <KBAiRecordButtonDelegate,
VoiceChatStreamingManagerDelegate>
VoiceChatStreamingManagerDelegate,
DeepgramStreamingManagerDelegate>
@property(nonatomic, weak) LSTPopView *popView;
// UI
@property(nonatomic, strong) KBAiChatView *chatView;
@property(nonatomic, strong) KBAiRecordButton *recordButton;
@property(nonatomic, strong) UILabel *statusLabel;
@property(nonatomic, strong) UILabel *transcriptLabel;
@property(nonatomic, strong) UIButton *commentButton;
@property(nonatomic, strong) KBAICommentView *commentView;
@property(nonatomic, strong) UIView *tabbarBackgroundView;
@@ -32,9 +35,11 @@
//
@property(nonatomic, strong) ConversationOrchestrator *orchestrator;
@property(nonatomic, strong) VoiceChatStreamingManager *streamingManager;
@property(nonatomic, strong) DeepgramStreamingManager *deepgramManager;
//
@property(nonatomic, strong) NSMutableString *assistantVisibleText;
@property(nonatomic, strong) NSMutableString *deepgramFullText;
//
@property(nonatomic, assign) NSTimeInterval lastRMSLogTime;
@@ -55,6 +60,7 @@
[self setupUI];
[self setupOrchestrator];
[self setupStreamingManager];
[self setupDeepgramManager];
}
- (void)viewWillAppear:(BOOL)animated {
@@ -68,6 +74,7 @@
//
[self.orchestrator stop];
[self.streamingManager disconnect];
[self.deepgramManager disconnect];
}
- (void)viewDidLayoutSubviews {
@@ -132,6 +139,16 @@
self.statusLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.statusLabel];
//
self.transcriptLabel = [[UILabel alloc] init];
self.transcriptLabel.text = @"";
self.transcriptLabel.font = [UIFont systemFontOfSize:16];
self.transcriptLabel.textColor = [UIColor labelColor];
self.transcriptLabel.numberOfLines = 0;
self.transcriptLabel.textAlignment = NSTextAlignmentLeft;
self.transcriptLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.transcriptLabel];
//
// self.chatView = [[KBAiChatView alloc] init];
// self.chatView.backgroundColor = [UIColor systemBackgroundColor];
@@ -177,6 +194,13 @@
make.right.equalTo(self.view).offset(-16);
}];
[self.transcriptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.statusLabel.mas_bottom).offset(8);
make.left.equalTo(self.view).offset(16);
make.right.equalTo(self.view).offset(-16);
make.bottom.lessThanOrEqualTo(self.recordButton.mas_top).offset(-16);
}];
[self.recordButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft).offset(20);
make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight).offset(-20);
@@ -304,6 +328,26 @@
self.lastRMSLogTime = 0;
}
#pragma mark - Deepgram Manager
- (void)setupDeepgramManager {
self.deepgramManager = [[DeepgramStreamingManager alloc] init];
self.deepgramManager.delegate = self;
self.deepgramManager.serverURL = @"wss://api.deepgram.com/v1/listen";
self.deepgramManager.apiKey = @"9c792eb63a65d644cbc95785155754cd1e84f8cf";
self.deepgramManager.language = @"en";
self.deepgramManager.model = @"nova-3";
self.deepgramManager.punctuate = YES;
self.deepgramManager.smartFormat = YES;
self.deepgramManager.interimResults = YES;
self.deepgramManager.encoding = @"linear16";
self.deepgramManager.sampleRate = 16000.0;
self.deepgramManager.channels = 1;
[self.deepgramManager prepareConnection];
self.deepgramFullText = [[NSMutableString alloc] init];
}
#pragma mark -
- (void)showComment {
CGFloat customViewHeight = KB_SCREEN_HEIGHT * (0.8);
@@ -402,17 +446,19 @@
self.statusLabel.text = @"正在连接...";
self.recordButton.state = KBAiRecordButtonStateRecording;
[self.streamingManager startWithToken:token language:@"en-US" voiceId:nil];
[self.deepgramFullText setString:@""];
self.transcriptLabel.text = @"";
[self.deepgramManager start];
}
- (void)recordButtonDidEndPress:(KBAiRecordButton *)button {
NSLog(@"[KBAiMainVC] Record button end press");
[self.streamingManager stopAndFinalize];
[self.deepgramManager stopAndFinalize];
}
- (void)recordButtonDidCancelPress:(KBAiRecordButton *)button {
NSLog(@"[KBAiMainVC] Record button cancel press");
[self.streamingManager cancel];
[self.deepgramManager cancel];
}
#pragma mark - VoiceChatStreamingManagerDelegate
@@ -501,4 +547,55 @@
[self showError:error];
}
#pragma mark - DeepgramStreamingManagerDelegate
- (void)deepgramStreamingManagerDidConnect {
self.statusLabel.text = @"已连接,准备中...";
}
- (void)deepgramStreamingManagerDidDisconnect:(NSError *_Nullable)error {
self.recordButton.state = KBAiRecordButtonStateNormal;
if (error) {
[self showError:error];
}
}
- (void)deepgramStreamingManagerDidUpdateRMS:(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)deepgramStreamingManagerDidReceiveInterimTranscript:(NSString *)text {
self.statusLabel.text = @"正在识别...";
NSString *displayText = text ?: @"";
if (self.deepgramFullText.length > 0 && displayText.length > 0) {
displayText =
[NSString stringWithFormat:@"%@ %@", self.deepgramFullText, displayText];
} else if (self.deepgramFullText.length > 0) {
displayText = [self.deepgramFullText copy];
}
self.transcriptLabel.text = displayText;
}
- (void)deepgramStreamingManagerDidReceiveFinalTranscript:(NSString *)text {
if (text.length > 0) {
if (self.deepgramFullText.length > 0) {
[self.deepgramFullText appendString:@" "];
}
[self.deepgramFullText appendString:text];
}
self.transcriptLabel.text = self.deepgramFullText;
self.statusLabel.text = @"识别完成";
self.recordButton.state = KBAiRecordButtonStateNormal;
}
- (void)deepgramStreamingManagerDidFail:(NSError *)error {
self.recordButton.state = KBAiRecordButtonStateNormal;
[self showError:error];
}
@end