diff --git a/keyBoard/Class/AiTalk/M/KBAiChatMessage.h b/keyBoard/Class/AiTalk/M/KBAiChatMessage.h index ca5efd1..ef66c23 100644 --- a/keyBoard/Class/AiTalk/M/KBAiChatMessage.h +++ b/keyBoard/Class/AiTalk/M/KBAiChatMessage.h @@ -22,6 +22,9 @@ typedef NS_ENUM(NSInteger, KBAiChatMessageType) { /// 消息类型 @property (nonatomic, assign) KBAiChatMessageType type; +/// 聊天记录 ID(服务端返回的 id,用于删除等操作;0 表示本地临时消息) +@property (nonatomic, assign) NSInteger historyId; + /// 文本内容 @property (nonatomic, copy) NSString *text; diff --git a/keyBoard/Class/AiTalk/V/Chat/KBPersonaChatCell.m b/keyBoard/Class/AiTalk/V/Chat/KBPersonaChatCell.m index 8d11c4b..2c47029 100644 --- a/keyBoard/Class/AiTalk/V/Chat/KBPersonaChatCell.m +++ b/keyBoard/Class/AiTalk/V/Chat/KBPersonaChatCell.m @@ -354,6 +354,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe message.isComplete = YES; message.needsTypewriterEffect = NO; + message.historyId = item.messageId; [newMessages addObject:message]; // [newMessages insertObject:message atIndex:0]; } @@ -931,7 +932,13 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe } break; case KBChatMessageActionTypeDelete: { NSInteger idx = [self.messages indexOfObjectIdenticalTo:message]; - if (idx != NSNotFound) { + if (idx == NSNotFound) { + return; + } + + NSInteger historyId = message.historyId; + // 没有服务端 id 的消息(本地临时/开场白等),直接本地删除即可 + if (historyId <= 0) { [self.messages removeObjectAtIndex:idx]; [self.chatView reloadWithMessages:self.messages keepOffset:YES @@ -944,7 +951,44 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe [[KBAIChatMessageCacheManager shared] clearMessagesForCompanionId:self.persona.personaId]; } } + return; } + + [KBHUD show]; + __weak typeof(self) weakSelf = self; + [self.aiVM deleteChatHistoryWithId:historyId completion:^(BOOL success, NSError * _Nullable error) { + dispatch_async(dispatch_get_main_queue(), ^{ + [KBHUD dismiss]; + if (!success || error) { + NSString *msg = error.localizedDescription ?: KBLocalized(@"删除失败,请重试"); + [KBHUD showError:msg]; + return; + } + + __strong typeof(weakSelf) strongSelf = weakSelf; + if (!strongSelf) { + return; + } + + NSInteger idx2 = [strongSelf.messages indexOfObjectIdenticalTo:message]; + if (idx2 == NSNotFound) { + return; + } + + [strongSelf.messages removeObjectAtIndex:idx2]; + [strongSelf.chatView reloadWithMessages:strongSelf.messages + keepOffset:YES + scrollToBottom:NO]; + if (strongSelf.persona.personaId > 0) { + if (strongSelf.messages.count > 0) { + [[KBAIChatMessageCacheManager shared] saveMessages:strongSelf.messages + forCompanionId:strongSelf.persona.personaId]; + } else { + [[KBAIChatMessageCacheManager shared] clearMessagesForCompanionId:strongSelf.persona.personaId]; + } + } + }); + }]; } break; case KBChatMessageActionTypeReport: { if (self.persona.personaId <= 0) {