小程序开放平台

文档中心
智能体API
消费侧(小组件/小程序)API
sendMessage
getAgentInfo
getHistoryMessages
getConversations
queryTasks

getHistoryMessages

应用开发
>
API和SDK
>
消费侧(小组件/小程序)API
>
getHistoryMessages
>
更新时间:2025-11-13 17:39:55

getHistoryMessages

使用示例

 
  // 获取历史记录
  async getChatRecords() {
    const agent = this.getAgent();
    if (!agent) return;
 
    const response = await agent.getConversations();
    console.log("=== conversations", response);
    if (response.code !== 0) {
      xhs.showToast({
        title: "获取历史记录失败",
        icon: "none",
      });
      return;
    }
 
    const conversations = response.data.conversations;
    if (conversations && conversations.length > 0) {
      const res = await agent.getHistoryMessages({
        conversation_id: conversations[0].id,
        cursor: 1,
        count: 10,
        sort: "desc",
      });
      console.log("=== getHistoryMessages", res);
    }
  },
类型声明
const agentInfo = await agent.getHistoryMessages({
    conversation_id: conversations[0].id,
    cursor: 1,
    count: 10,
    sort: "desc"
});

入参说明

属性名
类型
字段描述及类型
最低支持版本
conversation_idstring会话 idx.x.x
cursorstring请求的游标x.x.x
countnumber分页大小x.x.x
sortstring历史记录排序x.x.x

返回值

 
export interface UserRecord {
    content: string;
    role: 'user';
}
export interface AssistantRecord {
    content: string;
    role: 'assistant';
    reasoningContent: string;
    type: MessageType;
}
export type CreateRecordPairResponse = BaseResponse;
export interface ParticipantInfo {
    id: string;
    name: string;
    desc: string;
    avatar_url: string;
}
export interface GetHistoryMessagesData {
    message_list: (UserRecord | AssistantRecord)[];
    conversation_id: string;
    participant_info_map: Record<string, ParticipantInfo>;
    next_cursor: number;
    has_more: boolean;
}