79 lines
1.8 KiB
TypeScript
79 lines
1.8 KiB
TypeScript
/**
|
|
* Dify Dataset 知识库 API 模块
|
|
*
|
|
* 提供浏览器端调用 Dify 知识库管理 API 的函数
|
|
*
|
|
* @module api/dify-dataset/api/datasetApi
|
|
*/
|
|
|
|
import axios from 'axios';
|
|
import type { Dataset, DatasetsResponse } from '../type';
|
|
|
|
/**
|
|
* API 基础 URL
|
|
*/
|
|
const API_URL = '/api/dataset';
|
|
|
|
/**
|
|
* 获取知识库列表
|
|
*
|
|
* @param page - 页码,默认 1
|
|
* @param limit - 每页数量,默认 20
|
|
* @returns 知识库列表响应
|
|
*/
|
|
export async function fetchDatasets(
|
|
page: number = 1,
|
|
limit: number = 20
|
|
): Promise<DatasetsResponse> {
|
|
const params = new URLSearchParams({
|
|
page: page.toString(),
|
|
limit: limit.toString(),
|
|
});
|
|
|
|
const response = await axios.get<DatasetsResponse>(
|
|
`${API_URL}/datasets?${params}`,
|
|
{ withCredentials: true }
|
|
);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* 获取单个知识库详情
|
|
*
|
|
* @param datasetId - 知识库 ID
|
|
* @returns 知识库详情
|
|
*/
|
|
export async function fetchDataset(datasetId: string): Promise<Dataset> {
|
|
const response = await axios.get<Dataset>(
|
|
`${API_URL}/datasets/${datasetId}`,
|
|
{ withCredentials: true }
|
|
);
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* 更新知识库名称
|
|
*
|
|
* 注意:仅允许修改知识库名称,其他字段不开放修改
|
|
*
|
|
* @param datasetId - 知识库 ID
|
|
* @param name - 新的知识库名称
|
|
* @returns 更新后的知识库详情
|
|
*/
|
|
export async function updateDatasetName(
|
|
datasetId: string,
|
|
name: string
|
|
): Promise<Dataset> {
|
|
console.log('[Dataset Client] 更新知识库名称:', { datasetId, name });
|
|
|
|
const response = await axios.patch<Dataset>(
|
|
`${API_URL}/datasets/${datasetId}`,
|
|
{ name },
|
|
{
|
|
headers: { 'Content-Type': 'application/json' },
|
|
withCredentials: true,
|
|
}
|
|
);
|
|
return response.data;
|
|
}
|