1
This commit is contained in:
@@ -64,6 +64,8 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
/// 评论弹窗
|
||||
@property (nonatomic, weak) LSTPopView *popView;
|
||||
|
||||
@property (nonatomic, strong) NSMutableDictionary<NSString *, KBAiChatMessage *> *pendingAssistantMessages;
|
||||
|
||||
@end
|
||||
|
||||
@implementation KBPersonaChatCell
|
||||
@@ -97,6 +99,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
// 重置加载状态标志(但不清空 hasLoadedData)
|
||||
self.isLoading = NO;
|
||||
self.canTriggerLoadMore = YES;
|
||||
[self.pendingAssistantMessages removeAllObjects];
|
||||
|
||||
// ✅ 移除了 self.hasLoadedData = NO;
|
||||
// 这样 Cell 复用时不会重复请求数据
|
||||
@@ -180,6 +183,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
self.canTriggerLoadMore = YES;
|
||||
self.currentPage = 1;
|
||||
self.hasMoreHistory = YES;
|
||||
[self.pendingAssistantMessages removeAllObjects];
|
||||
|
||||
// ⚠️ 临时禁用缓存,排查问题
|
||||
// NSArray *cachedMessages = [[KBAIChatMessageCacheManager shared] messagesForCompanionId:persona.personaId];
|
||||
@@ -506,6 +510,10 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
[self.chatView addMessage:message autoScroll:YES];
|
||||
}
|
||||
|
||||
- (void)appendUserMessage:(NSString *)text requestId:(NSString *)requestId {
|
||||
[self appendUserMessage:text];
|
||||
}
|
||||
|
||||
- (void)appendLoadingUserMessage {
|
||||
if (!self.messages) {
|
||||
self.messages = [NSMutableArray array];
|
||||
@@ -613,6 +621,33 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
[self.chatView addMessage:message autoScroll:YES];
|
||||
}
|
||||
|
||||
- (void)appendLoadingAssistantMessageWithRequestId:(NSString *)requestId {
|
||||
if (requestId.length == 0) {
|
||||
[self appendLoadingAssistantMessage];
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self.pendingAssistantMessages) {
|
||||
self.pendingAssistantMessages = [NSMutableDictionary dictionary];
|
||||
}
|
||||
|
||||
if (!self.messages) {
|
||||
self.messages = [NSMutableArray array];
|
||||
}
|
||||
|
||||
[self ensureOpeningMessageAtTop];
|
||||
KBAiChatMessage *message = [KBAiChatMessage loadingAssistantMessage];
|
||||
self.pendingAssistantMessages[requestId] = message;
|
||||
|
||||
if (self.chatView.inverted) {
|
||||
[self.messages insertObject:message atIndex:0];
|
||||
} else {
|
||||
[self.messages addObject:message];
|
||||
}
|
||||
|
||||
[self.chatView addMessage:message autoScroll:YES];
|
||||
}
|
||||
|
||||
/// 移除 loading AI 消息
|
||||
- (void)removeLoadingAssistantMessage {
|
||||
// 从数据源中移除
|
||||
@@ -638,6 +673,51 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
|
||||
[self.chatView removeLoadingAssistantMessage];
|
||||
}
|
||||
|
||||
- (void)removeLoadingAssistantMessageWithRequestId:(NSString *)requestId {
|
||||
if (requestId.length == 0) {
|
||||
[self removeLoadingAssistantMessage];
|
||||
return;
|
||||
}
|
||||
|
||||
KBAiChatMessage *target = self.pendingAssistantMessages[requestId];
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self.pendingAssistantMessages removeObjectForKey:requestId];
|
||||
|
||||
NSInteger idx = [self.messages indexOfObjectIdenticalTo:target];
|
||||
if (idx != NSNotFound) {
|
||||
[self.messages removeObjectAtIndex:idx];
|
||||
[self.chatView reloadWithMessages:self.messages keepOffset:NO scrollToBottom:NO];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updateAssistantMessageWithRequestId:(NSString *)requestId
|
||||
text:(NSString *)text
|
||||
audioId:(nullable NSString *)audioId {
|
||||
if (requestId.length == 0) {
|
||||
[self appendAssistantMessage:text audioId:audioId];
|
||||
return;
|
||||
}
|
||||
|
||||
KBAiChatMessage *target = self.pendingAssistantMessages[requestId];
|
||||
[self.pendingAssistantMessages removeObjectForKey:requestId];
|
||||
|
||||
if (!target) {
|
||||
[self appendAssistantMessage:text audioId:audioId];
|
||||
return;
|
||||
}
|
||||
|
||||
target.isLoading = NO;
|
||||
target.text = text ?: @"";
|
||||
target.audioId = audioId;
|
||||
target.needsTypewriterEffect = YES;
|
||||
target.isComplete = NO;
|
||||
|
||||
[self.chatView reloadMessage:target];
|
||||
}
|
||||
|
||||
- (void)updateChatViewBottomInset:(CGFloat)bottomInset {
|
||||
[self.chatView updateContentBottomInset:bottomInset];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user