# 查询 LLM 会话消息

```{raw} html
<div class="mo-api-page-show-toc" aria-hidden="true"></div>
```

按消息 ID 游标读取当前用户的会话消息。


```http
GET https://api.moi.matrixorigin.cn/v5/workspaces/{workspace_id}/llm/sessions/{session_id}/messages
```

## 调用前准备

`BASE_URL` 使用产品 API 前缀 `https://api.moi.matrixorigin.cn/v5`；专有部署填写对应的产品 API 地址。

[配置同一环境的工作区身份与变量](objects.md#request-prerequisites)。

## 请求

:::::::{div} mo-api-tabs
::::::{tab-set}
:::::{tab-item} 输入示例

```bash
curl --request GET \
  "$BASE_URL/workspaces/$WORKSPACE_ID/llm/sessions/$SESSION_ID/messages?after=0&limit=50" \
  --header "X-API-Key: $MOI_KEY" \
  --header "X-Workspace-ID: $WORKSPACE_ID"
```

将上一页末条消息的 id 保存为 AFTER_MESSAGE_ID，再读取下一页：

```bash
curl --request GET \
  "$BASE_URL/workspaces/$WORKSPACE_ID/llm/sessions/$SESSION_ID/messages?after=$AFTER_MESSAGE_ID&limit=50" \
  --header "X-API-Key: $MOI_KEY" \
  --header "X-Workspace-ID: $WORKSPACE_ID"
```

:::::
:::::{tab-item} 参数说明

| 参数 | 位置 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- | --- |
| `workspace_id` | 路径 | string | 是 | 目标工作区，与 X-Workspace-ID 一致。 |
| `session_id` | 路径 | integer | 是 | 本服务返回的正整数 ID。 |
| `after` | 查询 | integer | 否 | 返回 ID 大于此值的消息；默认为 0，非正值按 0。 |
| `limit` | 查询 | integer | 否 | 每次最多返回数量，默认为 50；小于等于 0 或大于 100 时按 50。 |
| `role` | 查询 | integer | 否 | 单个角色筛选：0=user、1=system、2=assistant；3 被当前服务当作不筛选。 |
| `status` | 查询 | string | 否 | 按记录状态筛选，去掉前后空白。 |

消息按 ID 升序。下一页 after 取上一页末条消息 id，保持筛选条件，直到返回 messages=[]；没有 next_cursor 字段。total 是角色和状态筛选后的总数，不随 after 缩小。role 必须是单个可解析为 0～3 的整数，重复值、空值或范围外值返回 400；省略表示不筛选。source/model 查询参数当前不参与筛选。

:::::
::::::
:::::::

## 成功响应

:::::::{div} mo-api-tabs mo-api-response-tabs
::::::{tab-set}
:::::{tab-item} 响应示例

```json
{
  "code": 0,
  "data": {
    "messages": [
      {
        "id": 71,
        "user_id": "user-example",
        "session_id": 41,
        "source": "moi",
        "role": 0,
        "original_content": "",
        "content": "保存这条消息",
        "model": "",
        "status": "pending",
        "response": "",
        "modified_response": "",
        "config": "",
        "created_at": 1794445200,
        "updated_at": 1794445200
      }
    ],
    "total": 1
  }
}
```

:::::
:::::{tab-item} 字段说明

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `code` | integer | 成功为 0；HTTP 状态码为 200。 |
| `data.messages` | array（对象数组） | 本页[消息记录](objects.md#message-object)。 |
| `data.total` | integer | 符合角色和状态筛选的消息总数，不受 after 限制。 |

:::::
::::::
:::::::

## 错误响应

[错误格式与读取范围](objects.md#error-contract)说明身份、参数、资源读取与存储失败的不同结果。
