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

## fetchPanelAgentChatHistory

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

> 💡 This API requires cloud capabilities. Configure authorization under Development Settings - Cloud Capabilities in the [Mini App Developer Platform](https://platform.tuya.com/miniapp/).

### Description

Query historical session records (supports cursor pagination).

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `params` | `FetchPanelAgentChatHistoryParams` | Yes | Request body |

### Return Value

Type: `Promise<FetchPanelAgentChatHistoryItem[]>`

FetchPanelAgentChatHistoryResult (See type definitions and minituya return parameters for field meanings)

##### FetchPanelAgentChatHistoryItem

| Property | Type | Description |
| --- | --- | --- |
| `docId` | `string` | Document ID |
| `createTime` | `string` | Reply time |
| `requestId` | `string` | Request ID |
| `question` | `ChatMessageFragment[]` | Question |
| `answer` | `ChatMessageFragment[]` | Answer |
| `gmtCreate` | `number` | Creation timestamp |

### Referenced Types

##### `interface` FetchPanelAgentChatHistoryParams

| Property | Type | Description |
| --- | --- | --- |
| `devId` | `string` | Device ID |
| `bindRoleType` | `0 \| 1 \| 2` | 0 - custom agent role, 1 - agent role template, 2 - default role for single-role scenarios |
| `roleId` | `string` | Role ID (its meaning depends on bindRoleType) |
| `gmtStart` | `number` | Start timestamp (13 digits; cannot be empty at the same time as the end timestamp) |
| `gmtEnd` | `number` | End timestamp (13 digits; cannot be empty at the same time as the start timestamp) |
| `fetchSize` | `number` | Number of items to retrieve |
| `timeAsc` | `boolean` | Whether to sort by time in ascending order (default: descending) |

##### `interface` ChatMessageFragment

| Property | Type | Description |
| --- | --- | --- |
| `id` | `string` | id |
| `context` | `string` | Session content |
| `type` | `string` | Type |


### Examples

#### Request example

```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('Chat history:', result);
  })
  .catch(error => {
    console.error('Failed to get chat history:', error);
  });
```

#### Response example

```json
[
  {
    "docId": "doc_001",
    "createTime": "2023-11-06 10:30:00",
    "requestId": "req_12345",
    "question": [
      {
        "id": "q_001",
        "context": "How is the weather today?",
        "type": "text"
      }
    ],
    "answer": [
      {
        "id": "a_001",
        "context": "It's sunny today, with a temperature of 22°C.",
        "type": "text"
      }
    ],
    "gmtCreate": 1699200000000
  }
]
```
