This commit is contained in:
2026-01-31 23:17:58 +08:00
parent 6ae504823b
commit 81bc50ce17
5 changed files with 133 additions and 17 deletions

View File

@@ -85,6 +85,8 @@
/// AI
@property (nonatomic, assign) BOOL isWaitingForAIResponse;
@property (nonatomic, assign) NSInteger pendingAIRequestCount;
///
@property (nonatomic, strong) UIButton *messageButton;
@@ -136,6 +138,7 @@
self.preloadedIndexes = [NSMutableSet set];
self.aiVM = [[AiVM alloc] init];
self.isWaitingForAIResponse = NO;
self.pendingAIRequestCount = 0;
self.isTextInputMode = NO;
[self setupUI];
@@ -962,15 +965,18 @@
}
KBPersonaChatCell *currentCell = [self currentPersonaCell];
NSString *requestId = [NSUUID UUID].UUIDString;
if (currentCell && appendToUI) {
[currentCell appendUserMessage:text];
// loading AI
[currentCell appendLoadingAssistantMessage];
[currentCell appendUserMessage:text requestId:requestId];
[currentCell appendLoadingAssistantMessageWithRequestId:requestId];
}
self.isWaitingForAIResponse = YES;
self.collectionView.scrollEnabled = NO;
NSLog(@"[KBAIHomeVC] 开始等待 AI 回复,禁止 CollectionView 滚动");
self.pendingAIRequestCount += 1;
self.isWaitingForAIResponse = (self.pendingAIRequestCount > 0);
if (self.pendingAIRequestCount == 1) {
self.collectionView.scrollEnabled = NO;
NSLog(@"[KBAIHomeVC] 开始等待 AI 回复,禁止 CollectionView 滚动");
}
__weak typeof(self) weakSelf = self;
[self.aiVM requestChatMessageWithContent:text
@@ -982,19 +988,21 @@
}
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf.isWaitingForAIResponse = NO;
strongSelf.collectionView.scrollEnabled = YES;
NSLog(@"[KBAIHomeVC] AI 回复完成,恢复 CollectionView 滚动");
if (strongSelf.pendingAIRequestCount > 0) {
strongSelf.pendingAIRequestCount -= 1;
}
strongSelf.isWaitingForAIResponse = (strongSelf.pendingAIRequestCount > 0);
if (strongSelf.pendingAIRequestCount == 0) {
strongSelf.collectionView.scrollEnabled = YES;
NSLog(@"[KBAIHomeVC] AI 回复完成,恢复 CollectionView 滚动");
}
KBPersonaChatCell *cell = [strongSelf currentPersonaCell];
if (cell) {
[cell markLastUserMessageLoadingComplete];
}
if (response.code == 50030) {
// loading
if (cell) {
[cell removeLoadingAssistantMessage];
[cell removeLoadingAssistantMessageWithRequestId:requestId];
}
NSString *message = response.message ?: @"";
[strongSelf showChatLimitPopWithMessage:message];
@@ -1004,7 +1012,7 @@
if (!response || !response.data) {
// loading
if (cell) {
[cell removeLoadingAssistantMessage];
[cell removeLoadingAssistantMessageWithRequestId:requestId];
}
NSString *message = response.message ?: @"聊天响应为空";
NSLog(@"[KBAIHomeVC] 聊天响应为空:%@", message);
@@ -1019,15 +1027,14 @@
if (aiResponse.length == 0) {
// loading
if (cell) {
[cell removeLoadingAssistantMessage];
[cell removeLoadingAssistantMessageWithRequestId:requestId];
}
NSLog(@"[KBAIHomeVC] AI 回复为空");
return;
}
if (cell) {
// appendAssistantMessage loading
[cell appendAssistantMessage:aiResponse audioId:audioId];
[cell updateAssistantMessageWithRequestId:requestId text:aiResponse audioId:audioId];
}
});
}];