# 预览待添加的数据源

按数据库或卷选择来源前，预览去重后的文件和表数量。确认数量后再添加来源。

```text
POST https://moi.matrixorigin.cn/newmoi/semantic-models/{model_id}/source-selections/preview
```

## 调用前准备

先在 Catalog 中确认要选择的数据库、卷及其表和文件的标识。准备有目标工作区访问权限的个人访问令牌和目标工作区 ID。

下方示例使用：

- `$AI_STUDIO_API_KEY`：实际个人访问令牌，通过 `X-API-Key` Header 传递。
- `$WORKSPACE_ID`：目标工作区 ID，通过 `X-Workspace-ID` Header 传递。
- `$MODEL_ID`：要预览来源选择的知识库 ID。
- `$DATABASE_ID`：要预览的 Catalog 数据库 ID。
- `$TABLE_ID`：数据库中要显式选择的表 ID。

该接口只预览 Catalog 数据。

## 路径参数

| 参数 | 类型 | 是否必填 | 说明 |
| --- | --- | --- | --- |
| `model_id` | integer | 是 | 要预览来源选择的知识库 ID。 |

## 请求体

字段路径中的 `[]` 表示数组中的每一项。例如，`source_selections[].kind` 表示 `source_selections` 数组中每一项的 `kind` 字段。

| 字段 | 类型 | 是否必填 | 说明 |
| --- | --- | --- | --- |
| `source_selections` | object（对象数组） | 是 | 要预览的来源选择。 |
| `source_selections[].kind` | string | 是 | `database_tables` 或 `volume_files`。 |
| `source_selections[].database_id` | integer | 条件必填 | `database_tables` 的 Catalog Database ID。 |
| `source_selections[].volume_id` | integer | 条件必填 | `volume_files` 的 Catalog Volume ID。 |
| `source_selections[].all_selected` | boolean | 是 | `true` 选择当前范围内全部对象；`false` 时必须提供对应的显式选择 ID。 |
| `source_selections[].selected_table_ids` | array | 条件必填 | `all_selected` 为 `false` 时，显式选中的 Catalog 表 ID。 |
| `source_selections[].selected_file_ids` | array | 条件必填 | `all_selected` 为 `false` 时，显式选中的 Catalog 文件 ID。 |
| `source_selections[].excluded_table_ids` | array | 否 | 全选时要排除的 Catalog 表 ID。 |
| `source_selections[].excluded_file_ids` | array | 否 | 全选时要排除的 Catalog 文件 ID。 |
| `source_selections[].filters` | object | 否 | 按表名、文件名或扩展名进一步筛选。 |

### 选择范围

| 范围 | `kind` | 范围标识 | 显式选择字段 |
| --- | --- | --- | --- |
| Catalog Database | `database_tables` | `database_id` | `selected_table_ids` |
| Catalog Volume | `volume_files` | `volume_id` | `selected_file_ids` |

不能使用其他 `kind` 或非 Catalog 数据源。

## 请求示例

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/semantic-models/$MODEL_ID/source-selections/preview" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H 'Content-Type: application/json' \
  -d '{
    "source_selections": [
      {
        "kind": "database_tables",
        "database_id": '"$DATABASE_ID"',
        "all_selected": false,
        "selected_table_ids": ['"$TABLE_ID"']
      }
    ]
  }'
```

## 成功响应

成功时返回 `200`。`data` 返回去重后的来源数量；确认数量后再添加来源。

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "file_count": 2,
    "table_count": 1,
    "total_count": 3
  }
}
```

响应字段如下。

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `code` | string | 成功时为 `OK`。 |
| `msg` | string | 成功时为 `OK`。 |
| `data.file_count` | integer | 去重后将添加的文件来源数。 |
| `data.table_count` | integer | 去重后将添加的表来源数。 |
| `data.total_count` | integer | 文件和表来源总数。 |

## 错误响应

```json
{
  "code": "ErrParamInvalid",
  "msg": "invalid source selection",
  "data": null
}
```

### 常见 HTTP 错误

```{list-table}
:header-rows: 1
:widths: 12 22 32 34

* - HTTP 状态码
  - 错误代码
  - 常见原因
  - 建议操作
* - `400`
  - `ErrParamInvalid`
  - `source_selections` 缺失，或选择类型和 ID 无效。
  - 修正选择项后重试。
* - `401`
  - `ErrUnauthorized`
  - API Key 无效或已失效。
  - 检查 API Key。
* - `403`
  - `ErrForbidden`
  - 调用者没有更新知识库或读取所选来源的权限。
  - 检查工作区和对象授权。
* - `404`
  - `ErrNotFound`
  - 知识库不存在或不可见。
  - 重新确认 `model_id`。
* - `500`
  - `ErrServer`
  - 服务未能预览来源选择。
  - 保留脱敏后的响应信息后重试。
```

## 后续操作

确认数量后[添加数据源](add-data-sources.md)。
