From 3a5a6395af27db0e6d2637190cda6d18d3bf881c Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Mon, 26 Jan 2026 18:51:37 +0800 Subject: [PATCH] 1 --- keyBoard/Class/AiTalk/VC/KBAIHomeVC.m | 35 ++++++++++++++++++++++++++- keyBoard/Class/AiTalk/VC/KBAiMainVC.m | 1 + keyBoard/Class/AiTalk/VM/AiVM.h | 1 + keyBoard/Class/AiTalk/VM/AiVM.m | 10 +++++--- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m b/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m index e9d56e3..0235fa2 100644 --- a/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m +++ b/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m @@ -248,6 +248,25 @@ [self.voiceToTextManager prepareConnection]; } +- (NSInteger)currentCompanionId { + if (self.personas.count == 0) { + return 0; + } + + NSInteger index = self.currentIndex; + if (index < 0 || index >= self.personas.count) { + NSIndexPath *indexPath = self.collectionView.indexPathsForVisibleItems.firstObject; + if (indexPath) { + index = indexPath.item; + } else { + index = 0; + } + } + + KBPersonaModel *persona = self.personas[index]; + return persona.personaId; +} + #pragma mark - Lazy Load - (UICollectionView *)collectionView { @@ -290,7 +309,21 @@ } NSLog(@"[KBAIHomeVC] 语音识别结果:%@", text); - // TODO: 使用识别文本(例如发起聊天请求) + NSInteger companionId = [self currentCompanionId]; + if (companionId <= 0) { + NSLog(@"[KBAIHomeVC] companionId 无效,取消请求"); + return; + } + + [self.aiVM requestChatMessageWithContent:text + companionId:companionId + completion:^(KBAiMessageResponse * _Nullable response, NSError * _Nullable error) { + if (error) { + NSLog(@"[KBAIHomeVC] 请求聊天失败:%@", error.localizedDescription); + return; + } + NSLog(@"[KBAIHomeVC] 聊天请求成功,code=%ld", (long)response.code); + }]; } - (void)voiceToTextManager:(KBVoiceToTextManager *)manager diff --git a/keyBoard/Class/AiTalk/VC/KBAiMainVC.m b/keyBoard/Class/AiTalk/VC/KBAiMainVC.m index 77d39b9..67839ab 100644 --- a/keyBoard/Class/AiTalk/VC/KBAiMainVC.m +++ b/keyBoard/Class/AiTalk/VC/KBAiMainVC.m @@ -669,6 +669,7 @@ // 请求 chat/message 接口 [self.aiVM requestChatMessageWithContent:finalText + companionId:0 completion:^(KBAiMessageResponse *_Nullable response, NSError *_Nullable error) { __strong typeof(weakSelf) strongSelf = weakSelf; diff --git a/keyBoard/Class/AiTalk/VM/AiVM.h b/keyBoard/Class/AiTalk/VM/AiVM.h index 97d99eb..bfbc15a 100644 --- a/keyBoard/Class/AiTalk/VM/AiVM.h +++ b/keyBoard/Class/AiTalk/VM/AiVM.h @@ -51,6 +51,7 @@ typedef void (^AiVMAudioURLCompletion)(NSString *_Nullable audioURL, completion:(AiVMSyncCompletion)completion; - (void)requestChatMessageWithContent:(NSString *)content + companionId:(NSInteger)companionId completion:(AiVMMessageCompletion)completion; /// 根据 audioId 获取音频 URL diff --git a/keyBoard/Class/AiTalk/VM/AiVM.m b/keyBoard/Class/AiTalk/VM/AiVM.m index e9bb905..b03b6dd 100644 --- a/keyBoard/Class/AiTalk/VM/AiVM.m +++ b/keyBoard/Class/AiTalk/VM/AiVM.m @@ -95,6 +95,7 @@ autoShowBusinessError:NO } - (void)requestChatMessageWithContent:(NSString *)content + companionId:(NSInteger)companionId completion:(AiVMMessageCompletion)completion { if (content.length == 0) { NSError *error = [NSError @@ -111,9 +112,12 @@ autoShowBusinessError:NO [content stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]]; NSString *path = [NSString - stringWithFormat:@"%@?content=%@", API_AI_CHAT_MESSAGE, - encodedContent ?: @""]; - NSDictionary *params = @{ @"content" : content ?: @"" }; + stringWithFormat:@"%@?content=%@&companionId=%ld", API_AI_CHAT_MESSAGE, + encodedContent ?: @"", (long)companionId]; + NSDictionary *params = @{ + @"content" : content ?: @"", + @"companionId" : @(companionId) + }; [[KBNetworkManager shared] POST:path jsonBody:params