调用删除接口

This commit is contained in:
2026-02-04 19:01:58 +08:00
parent 619d356d31
commit bacaf537f3
2 changed files with 48 additions and 1 deletions

View File

@@ -22,6 +22,9 @@ typedef NS_ENUM(NSInteger, KBAiChatMessageType) {
/// 消息类型 /// 消息类型
@property (nonatomic, assign) KBAiChatMessageType type; @property (nonatomic, assign) KBAiChatMessageType type;
/// 聊天记录 ID服务端返回的 id用于删除等操作0 表示本地临时消息)
@property (nonatomic, assign) NSInteger historyId;
/// 文本内容 /// 文本内容
@property (nonatomic, copy) NSString *text; @property (nonatomic, copy) NSString *text;

View File

@@ -354,6 +354,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
message.isComplete = YES; message.isComplete = YES;
message.needsTypewriterEffect = NO; message.needsTypewriterEffect = NO;
message.historyId = item.messageId;
[newMessages addObject:message]; [newMessages addObject:message];
// [newMessages insertObject:message atIndex:0]; // [newMessages insertObject:message atIndex:0];
} }
@@ -931,7 +932,13 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
} break; } break;
case KBChatMessageActionTypeDelete: { case KBChatMessageActionTypeDelete: {
NSInteger idx = [self.messages indexOfObjectIdenticalTo:message]; 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.messages removeObjectAtIndex:idx];
[self.chatView reloadWithMessages:self.messages [self.chatView reloadWithMessages:self.messages
keepOffset:YES keepOffset:YES
@@ -944,7 +951,44 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
[[KBAIChatMessageCacheManager shared] clearMessagesForCompanionId:self.persona.personaId]; [[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; } break;
case KBChatMessageActionTypeReport: { case KBChatMessageActionTypeReport: {
if (self.persona.personaId <= 0) { if (self.persona.personaId <= 0) {