This commit is contained in:
2026-02-02 13:26:38 +08:00
parent 81bc50ce17
commit fe59a0cb45
3 changed files with 133 additions and 17 deletions

View File

@@ -175,6 +175,13 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
#pragma mark - Setter
- (NSString *)currentPrologueText {
if (self.persona.prologue.length > 0) {
return self.persona.prologue;
}
return self.persona.introText ?: @"";
}
- (void)setPersona:(KBPersonaModel *)persona {
_persona = persona;
@@ -203,13 +210,10 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:persona.avatarUrl]
placeholderImage:[UIImage imageNamed:@"placeholder_avatar"]];
self.nameLabel.text = persona.name;
self.openingLabel.text = persona.shortDesc.length > 0 ? persona.shortDesc : persona.introText;
self.openingLabel.text = persona.shortDesc.length > 0 ? persona.shortDesc : persona.prologue;
//
[self.chatView stopPlayingAudio];
//
[self ensureOpeningMessageAtTop];
NSLog(@"[KBPersonaChatCell] ========== setPersona 调试 ==========");
NSLog(@"[KBPersonaChatCell] personaId: %ld", (long)persona.personaId);
@@ -217,8 +221,9 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
NSLog(@"[KBPersonaChatCell] chatView.frame: %@", NSStringFromCGRect(self.chatView.frame));
NSLog(@"[KBPersonaChatCell] contentView.frame: %@", NSStringFromCGRect(self.contentView.frame));
//
if (self.messages.count > 0) {
[self.chatView updateIntroFooterText:nil];
[self ensureOpeningMessageAtTop];
//
[[KBAIChatMessageCacheManager shared] saveMessages:self.messages
forCompanionId:persona.personaId];
@@ -227,6 +232,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
scrollToBottom:YES];
} else {
[self.chatView clearMessages];
[self.chatView updateIntroFooterText:persona.prologue];
}
NSLog(@"[KBPersonaChatCell] ========== setPersona 结束 ==========");
@@ -276,7 +282,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf.isLoading = NO;
[strongSelf.chatView endLoadMoreWithHasMoreData:strongSelf.hasMoreHistory];
if (strongSelf.currentPage == 1 && strongSelf.persona.introText.length > 0) {
if (strongSelf.currentPage == 1 && strongSelf.persona.prologue.length > 0) {
[strongSelf showOpeningMessage];
}
});
@@ -286,6 +292,18 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
strongSelf.hasLoadedData = YES;
strongSelf.hasMoreHistory = pageModel.hasMore;
NSInteger loadedPage = strongSelf.currentPage;
if (loadedPage == 1 && pageModel.total == 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf.chatView clearMessages];
[strongSelf.chatView updateIntroFooterText:strongSelf.persona.prologue];
[strongSelf.chatView endLoadMoreWithHasMoreData:strongSelf.hasMoreHistory];
strongSelf.isLoading = NO;
});
strongSelf.currentPage++;
return;
}
// KBAiChatMessage
NSMutableArray *newMessages = [NSMutableArray array];
for (KBChatHistoryModel *item in pageModel.records) {
@@ -313,8 +331,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
}
//
// dispatch_async currentPage
NSInteger loadedPage = strongSelf.currentPage;
[strongSelf.chatView updateIntroFooterText:nil];
if (loadedPage == 1) {
//
@@ -384,7 +401,13 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
}
- (void)showOpeningMessage {
//
if (self.messages.count == 0) {
[self.chatView clearMessages];
[self.chatView updateIntroFooterText:self.persona.prologue];
return;
}
[self.chatView updateIntroFooterText:nil];
[self ensureOpeningMessageAtTop];
dispatch_async(dispatch_get_main_queue(), ^{
@@ -408,16 +431,16 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
if (!message) {
return NO;
}
NSString *introText = self.persona.introText ?: @"";
if (introText.length == 0) {
NSString *prologue = [self currentPrologueText];
if (prologue.length == 0) {
return NO;
}
return (message.type == KBAiChatMessageTypeAssistant) && [message.text isEqualToString:introText];
return (message.type == KBAiChatMessageTypeAssistant) && [message.text isEqualToString:prologue];
}
- (void)ensureOpeningMessageAtTop {
NSString *introText = self.persona.introText ?: @"";
if (introText.length == 0) {
NSString *prologue = [self currentPrologueText];
if (prologue.length == 0) {
return;
}
if (!self.messages) {
@@ -426,7 +449,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
if ([self hasOpeningMessageAtTop]) {
return;
}
KBAiChatMessage *openingMsg = [KBAiChatMessage assistantMessageWithText:introText];
KBAiChatMessage *openingMsg = [KBAiChatMessage assistantMessageWithText:prologue];
openingMsg.isComplete = YES;
openingMsg.needsTypewriterEffect = NO;
if (self.chatView.inverted) {
@@ -445,8 +468,8 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
}
- (NSInteger)openingMessageIndexInMessages {
NSString *introText = self.persona.introText ?: @"";
if (introText.length == 0 || self.messages.count == 0) {
NSString *prologue = [self currentPrologueText];
if (prologue.length == 0 || self.messages.count == 0) {
return NSNotFound;
}
@@ -500,6 +523,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
self.messages = [NSMutableArray array];
}
[self.chatView updateIntroFooterText:nil];
[self ensureOpeningMessageAtTop];
KBAiChatMessage *message = [KBAiChatMessage userMessageWithText:text];
if (self.chatView.inverted) {
@@ -519,6 +543,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
self.messages = [NSMutableArray array];
}
[self.chatView updateIntroFooterText:nil];
[self ensureOpeningMessageAtTop];
KBAiChatMessage *message = [KBAiChatMessage loadingUserMessage];
if (self.chatView.inverted) {
@@ -589,6 +614,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
self.messages = [NSMutableArray array];
}
[self.chatView updateIntroFooterText:nil];
[self ensureOpeningMessageAtTop];
// loading
@@ -611,6 +637,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
self.messages = [NSMutableArray array];
}
[self.chatView updateIntroFooterText:nil];
[self ensureOpeningMessageAtTop];
KBAiChatMessage *message = [KBAiChatMessage loadingAssistantMessage];
if (self.chatView.inverted) {
@@ -635,6 +662,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
self.messages = [NSMutableArray array];
}
[self.chatView updateIntroFooterText:nil];
[self ensureOpeningMessageAtTop];
KBAiChatMessage *message = [KBAiChatMessage loadingAssistantMessage];
self.pendingAssistantMessages[requestId] = message;