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

@@ -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];
}