iOS

最近更新时间:2025-07-30 17:53:21

我的收藏

功能描述

该功能允许用户与 AI 机器人进行对话,实时接收机器人思考过程中的部分回复(流式输出)。
核心功能包括:
1. ​接收 AI 机器人的首次流式消息响应。​​
2. ​接收 AI 机器人流式消息的实时更新(内容追加)。​​
3. ​用户中断正在生成中的流式消息。​​
4. ​接收 AI 机器人的错误信息反馈。​​
所有相关消息均通过自定义消息(Custom Message)​​ 类型传输,消息内容为特定结构的 JSON 字符串。
说明:
AI 聊天机器人功能仅 8.6.7040 及以上版本支持。
历史消息数最大支持50条,同时受 IM 基础套餐中「漫游消息时长」限制,即超过漫游消息存储时长的消息无法作为上下文携带给 AI 服务。

消息流生命周期

​1. 首次响应

用户发起问题后,AI 机器人开始生成回复。当生成出第一段有效内容时,会立即通过一条新的自定义消息发送给用户,触发 onRecvNewMessage。

​2. 流式更新

AI 机器人持续生成内容。每当有新内容片段产生,它不会发送新消息,而是​修改之前发送的那条自定义消息,会触发 onRecvMessageModified。接收方看到的是同一消息的内容在不断追加更新。

​3. 完成标志

当 AI 机器人完成整个回复的生成,会在最后一次更新(或唯一一次消息,如果流很短)时,将消息中的 isFinished 标志置为 1。

​4. 用户中断

用户在消息生成过程中可以主动中断。需发送一条特定格式的自定义消息到该 AI 机器人。

​5. 错误反馈

如果 AI 机器人在处理请求或生成流式消息过程中发生错误,会通过 onRecvNewMessage ,发送给用户一条特定格式的错误消息 。

操作步骤

步骤1:创建机器人

集成 AI 聊天机器人组件之前,请先在控制台创建机器人

步骤2:添加机器人好友

使用 RestAPI 添加上一步创建的机器人为好友。
注意:
为避免您的 AI 资源被恶意消耗,现只支持使用 RestAPI 添加机器人好友,用户无法主动添加 AI 机器人为好友。

步骤3:向机器人发送文本消息

说明:
RBT_ID 为上一步添加的机器人好友的 ID;Text 为向机器人发送的文本内容。
NSString *msgID = [[V2TIMManager sharedInstance] sendC2CTextMessage:@"Text" to:@"RBT_ID" succ:^{
// success
} fail:^(int code, NSString *msg) {
// failed
}];

步骤4:接收 AI 流式消息

​监听回调

向 IMSDK 注册 onRecvNewMessage 和 onRecvMessageModified 回调监听器。

[[V2TIMManager sharedInstance] addAdvancedMsgListener:self];

#pragma mark V2TIMAdvancedMsgListener
- (void)onRecvNewMessage:(V2TIMMessage *)msg {
NSLog(@"%@",msg);
}

- (void)onRecvMessageModified:(V2TIMMessage *)msg {
NSLog(@"%@",msg);
}

处理消息

当您收到 onRecvNewMessage(首次)或 onRecvMessageModified(后续更新)回调,且消息来自 AI 机器人时,其自定义消息体结构(jsonString)如下:
{
"chatbotPlugin": 2,
"src": 2,
"chunks": [
"Hello",
],
"isFinished": 0
}
字段
类型
说明
chatbotPlugin
Integer
固定值 2,标识聊天机器人消息。
src
Integer
自定义消息类型 2:流式消息类型。
chunks
Array
流式消息内容。
isFinished
Integer
流式消息是否完结。 0:未发送完 1:已发送完。

步骤5:发送给 AI 的中断消息

当用户想要中断一个正在生成中的 AI 流式回复时,​用户客户端需要向该 AI 机器人主动发送一条自定义消息,自定义消息中携带想要中断的 AI 流式消息的 msgKey。
生成 msgKey

- (NSString *)generateMessageKey:(V2TIMMessage *)message {
uint64_t msgSeq = message.seq;
uint64_t random = message.random;
NSTimeInterval timestamp = message.timestamp ? [message.timestamp timeIntervalSince1970] : 0;
return [NSString stringWithFormat:@"%llu_%llu_%.0f", msgSeq, random, timestamp];
}


- (V2TIMMessage *)buildChatbotInterruptMessage {
// Build interrupt message content
NSDictionary *interruptMessageContent = @{
@"chatbotPlugin": @(2),
@"src": @(22),
@"msgKey": [self generateMessageKey:messageData]
};
// Convert to JSON
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:interruptMessageContent
options:0
error:&error];
if (error || !jsonData) {
NSLog(@"Failed to create interrupt message JSON: %@", error.localizedDescription);
return nil;
}
// Create custom message
V2TIMMessage *message = [[V2TIMManager sharedInstance] createCustomMessage:jsonData];
message.isExcludedFromLastMessage = YES;
message.isExcludedFromUnreadCount = YES;
return message;
}
自定义消息体(jsonString)结构如下:
{
"chatbotPlugin": 2,
"src": 22,
"msgKey": "xxx"
}
字段
类型
说明
chatbotPlugin
Integer
固定值 2,标识聊天机器人消息。
src
Integer
自定义消息类型 22:中断流式消息。
msgKey
String
要中断的 AI 消息的唯一标识 {seq}_{random}_{serverTime} ,生成方式参见 生成 MsgKey

步骤6:接收到 AI 报错消息

当 AI 机器人在处理请求或生成流式消息过程中遇到错误时,会发送一条错误消息,此时您会收到 onRecvNewMessage 回调。
[[V2TIMManager sharedInstance] addAdvancedMsgListener:self];
- (void)onRecvNewMessage:(V2TIMMessage *)msg {
NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data
options:NSJSONReadingAllowFragments error:nil];
if (param == nil) {
return @"";
}
if ([param[@"src"] doubleValue] == 23) {
NSString *errorInfo = param[@"errorInfo"];
NSLog(@"%@",errorInfo);
}
}
自定义消息体(jsonString)结构如下:
{
"chatbotPlugin": 2,
"src": 23,
"errorInfo": "xxxxxxxxxxx"
}
字段
类型
说明
chatbotPlugin
Integer
固定值 2,标识聊天机器人消息。
src
Integer
自定义消息类型 23:发消息或者配置 LLM 报错。
errorInfo
String
报错详细信息。