---
name: "fetchPanelAgentChatHistory"
mode: "kit"
versionRequirements:
  - { name: "@tuya-miniapp/cloud-api", version: "1.0.6" }
---

## fetchPanelAgentChatHistory

> [VERSION] @tuya-miniapp/cloud-api >= 1.0.6

> 💡 接口依赖云能力，需在 [小程序开发者平台](https://platform.tuya.com/miniapp/)开发设置 - 云能力 进行授权配置。

### 描述

查询历史会话记录（支持游标分页）。

### 参数

`Params`

| 参数 | 类型 | 必填 | 默认值 | 描述 |
| --- | --- | --- | --- | --- |
| `params` | `FetchPanelAgentChatHistoryParams` | 是 | - | 请求体 |

### 引用对象

##### `interface` FetchPanelAgentChatHistoryParams

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `devId` | `string` | 设备ID |
| `bindRoleType` | `0 \| 1 \| 2` | 0-自定义智能体角色,1-智能体角色模板,2-单角色场景默认角色 |
| `roleId` | `string` | 角色ID（会根据bindRoleType决定具体含义） |
| `gmtStart` | `number` | 起始时间戳（13位，不能和结束时间戳同时为空） |
| `gmtEnd` | `number` | 结束时间戳（13位，不能和起始时间戳同时为空） |
| `fetchSize` | `number` | 所需条数 |
| `timeAsc` | `boolean` | 是否按时间升序（默认倒序） |

##### `interface` ChatMessageFragment

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `id` | `string` | id |
| `context` | `string` | 会话内容 |
| `type` | `string` | 类型 |


### 示例代码

#### 请求示例

```typescript
import { fetchPanelAgentChatHistory } from '@tuya-miniapp/cloud-api';

const params = {
  devId: 'your_device_id',
  bindRoleType: 0,
  roleId: 'role_12345',
  gmtStart: 1699200000000,
  gmtEnd: 1699286400000,
  fetchSize: 20,
  timeAsc: false,
};

fetchPanelAgentChatHistory(params)
  .then(result => {
    console.log('历史会话记录:', result);
  })
  .catch(error => {
    console.error('获取历史会话记录失败:', error);
  });
```

#### 返回示例

```json
[
  {
    "docId": "doc_001",
    "createTime": "2023-11-06 10:30:00",
    "requestId": "req_12345",
    "question": [
      {
        "id": "q_001",
        "context": "今天天气怎么样？",
        "type": "text"
      }
    ],
    "answer": [
      {
        "id": "a_001",
        "context": "今天是晴天，气温22度。",
        "type": "text"
      }
    ],
    "gmtCreate": 1699200000000
  }
]
```
