完善提示词管理和配置管理的增删改查
This commit is contained in:
@@ -0,0 +1,493 @@
|
||||
import { postgrestGet, postgrestPut, postgrestPost, postgrestDelete, type PostgrestParams } from '../postgrest-client';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
// 提示词模板接口
|
||||
export interface PromptTemplate {
|
||||
id: number;
|
||||
template_name: string;
|
||||
template_type: string;
|
||||
description: string | null;
|
||||
template_content: string;
|
||||
variables: Record<string, string>; // 变量定义
|
||||
status: number;
|
||||
version: string;
|
||||
created_by: number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
// 提示词模板前端接口
|
||||
export interface PromptTemplateUI {
|
||||
id: string;
|
||||
template_name: string;
|
||||
template_type: 'Extraction' | 'Evaluation' | 'Summary' | 'Common';
|
||||
description: string;
|
||||
template_content: string;
|
||||
variables: Record<string, string>; // 变量定义
|
||||
status: 'active' | 'inactive' | 'system';
|
||||
version: string;
|
||||
created_by: number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
// 搜索参数接口
|
||||
export interface PromptSearchParams {
|
||||
name?: string;
|
||||
type?: string;
|
||||
status?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
* @param dateString 日期字符串
|
||||
* @returns 格式化后的日期字符串
|
||||
*/
|
||||
function formatDate(dateString: string): string {
|
||||
if (!dateString) return '';
|
||||
try {
|
||||
return dayjs(dateString).format('YYYY-MM-DD HH:mm:ss');
|
||||
} catch (error) {
|
||||
console.error('日期格式化失败:', error);
|
||||
return dateString;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从不同格式的 API 响应中提取数据
|
||||
* @param responseData API 响应数据
|
||||
* @returns 提取后的数据或 null
|
||||
*/
|
||||
function extractApiData<T>(responseData: unknown): T | null {
|
||||
if (!responseData) return null;
|
||||
|
||||
// 格式1: { code: number, msg: string, data: T }
|
||||
if (typeof responseData === 'object' && responseData !== null &&
|
||||
'code' in responseData &&
|
||||
'data' in responseData &&
|
||||
(responseData as { data: unknown }).data) {
|
||||
return (responseData as { data: T }).data;
|
||||
}
|
||||
|
||||
// 格式2: 直接是数据对象
|
||||
return responseData as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将API状态值转换为UI状态值
|
||||
* @param status API状态值(数字)
|
||||
* @returns UI状态值(字符串)
|
||||
*/
|
||||
function mapStatusToUI(status: number): 'active' | 'inactive' | 'system' {
|
||||
switch(status) {
|
||||
case 0: return 'inactive';
|
||||
case 1: return 'active';
|
||||
case 2: return 'system';
|
||||
default: return 'inactive';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将UI状态值转换为API状态值
|
||||
* @param status UI状态值(字符串)
|
||||
* @returns API状态值(数字)
|
||||
*/
|
||||
function mapStatusToAPI(status: string): number {
|
||||
switch(status) {
|
||||
case 'active': return 1;
|
||||
case 'inactive': return 0;
|
||||
case 'system': return 2;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据库模板转换为UI模板
|
||||
* @param template 数据库模板
|
||||
* @returns UI模板
|
||||
*/
|
||||
export function convertToUITemplate(template: PromptTemplate): PromptTemplateUI {
|
||||
return {
|
||||
id: template.id ? template.id.toString() : '',
|
||||
template_name: template.template_name,
|
||||
template_type: template.template_type as "Extraction" | "Evaluation" | "Summary" | "Common",
|
||||
description: template.description || '',
|
||||
template_content: template.template_content,
|
||||
variables: template.variables,
|
||||
status: mapStatusToUI(template.status),
|
||||
version: template.version,
|
||||
created_by: template.created_by,
|
||||
created_at: formatDate(template.created_at),
|
||||
updated_at: formatDate(template.updated_at)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取提示词模板列表
|
||||
* @param searchParams 搜索参数
|
||||
* @returns 提示词模板列表和总数
|
||||
*/
|
||||
export async function getPromptTemplates(searchParams: PromptSearchParams = {}): Promise<{
|
||||
data?: { templates: PromptTemplateUI[], total: number };
|
||||
error?: string;
|
||||
status?: number;
|
||||
}> {
|
||||
try {
|
||||
console.log('获取提示词模板列表,参数:', searchParams);
|
||||
|
||||
const page = searchParams.page || 1;
|
||||
const pageSize = searchParams.pageSize || 10;
|
||||
|
||||
// 构建查询参数
|
||||
const params: PostgrestParams = {
|
||||
select: `
|
||||
id,
|
||||
template_name,
|
||||
template_type,
|
||||
description,
|
||||
template_content,
|
||||
variables,
|
||||
status,
|
||||
version,
|
||||
created_by,
|
||||
created_at,
|
||||
updated_at
|
||||
`,
|
||||
order: 'updated_at.desc',
|
||||
headers: {
|
||||
'Prefer': 'count=exact'
|
||||
},
|
||||
limit: pageSize,
|
||||
offset: (page - 1) * pageSize,
|
||||
filter: {} as Record<string, string>
|
||||
};
|
||||
|
||||
// 添加筛选条件
|
||||
const filter: Record<string, string> = {};
|
||||
filter['status'] = `gte.0`;
|
||||
if (searchParams.name) {
|
||||
filter['template_name'] = `ilike.%${searchParams.name}%`;
|
||||
}
|
||||
|
||||
if (searchParams.type) {
|
||||
filter['template_type'] = `eq.${searchParams.type}`;
|
||||
}
|
||||
|
||||
if (searchParams.status) {
|
||||
let statusValue: number;
|
||||
switch (searchParams.status) {
|
||||
case 'active': statusValue = 1; break;
|
||||
case 'inactive': statusValue = 0; break;
|
||||
case 'system': statusValue = 2; break;
|
||||
default: statusValue = -1;
|
||||
}
|
||||
|
||||
if (statusValue >= 0) {
|
||||
filter['status'] = `eq.${statusValue}`;
|
||||
}
|
||||
}
|
||||
params.filter = filter;
|
||||
|
||||
// 发送API请求
|
||||
console.log('API请求参数:', params);
|
||||
const response = await postgrestGet<PromptTemplate[]>('prompt_templates', params);
|
||||
|
||||
if (response.error) {
|
||||
console.error('API返回错误:', response.error);
|
||||
return { error: response.error, status: response.status };
|
||||
}
|
||||
|
||||
// 提取API返回的数据
|
||||
const extractedData = extractApiData<PromptTemplate[]>(response.data);
|
||||
|
||||
if (!extractedData) {
|
||||
console.error('提取数据失败,原始响应:', response.data);
|
||||
return { error: '获取提示词模板数据失败', status: 500 };
|
||||
}
|
||||
|
||||
console.log(`成功获取${extractedData.length}条提示词模板数据`);
|
||||
|
||||
// 从响应头中获取总数
|
||||
let totalCount = 0;
|
||||
const responseWithHeaders = response as { data: PromptTemplate[]; headers: Record<string, string> };
|
||||
if(responseWithHeaders.headers){
|
||||
const rangeHeader = responseWithHeaders.headers['content-range'];
|
||||
if(rangeHeader){
|
||||
const total = rangeHeader.split('/')[1];
|
||||
if(total !== '*'){
|
||||
totalCount = parseInt(total, 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 返回转换后的数据
|
||||
return {
|
||||
data: {
|
||||
templates: extractedData.map(convertToUITemplate),
|
||||
total: totalCount
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('获取提示词模板出错:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '获取提示词模板失败',
|
||||
status: 500
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取提示词模板详情
|
||||
* @param id 模板ID
|
||||
* @returns 提示词模板详情
|
||||
*/
|
||||
export async function getPromptTemplate(id: string): Promise<{
|
||||
data?: PromptTemplateUI;
|
||||
error?: string;
|
||||
status?: number;
|
||||
}> {
|
||||
try {
|
||||
if (!id) {
|
||||
return { error: '模板ID不能为空', status: 400 };
|
||||
}
|
||||
|
||||
const params: PostgrestParams = {
|
||||
select: `
|
||||
id,
|
||||
template_name,
|
||||
template_type,
|
||||
description,
|
||||
template_content,
|
||||
variables,
|
||||
status,
|
||||
version,
|
||||
created_by,
|
||||
created_at,
|
||||
updated_at
|
||||
`,
|
||||
filter: {
|
||||
'id': `eq.${id}`
|
||||
}
|
||||
};
|
||||
|
||||
const response = await postgrestGet<PromptTemplate[]>('prompt_templates', params);
|
||||
|
||||
if (response.error) {
|
||||
return { error: response.error, status: response.status };
|
||||
}
|
||||
|
||||
const extractedData = extractApiData<PromptTemplate[]>(response.data);
|
||||
|
||||
if (!extractedData || extractedData.length === 0) {
|
||||
return { error: '未找到指定模板', status: 404 };
|
||||
}
|
||||
|
||||
return { data: convertToUITemplate(extractedData[0]) };
|
||||
} catch (error) {
|
||||
console.error('获取提示词模板详情失败:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '获取提示词模板详情失败',
|
||||
status: 500
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建提示词模板
|
||||
* @param template 提示词模板数据
|
||||
* @returns 创建的提示词模板
|
||||
*/
|
||||
export async function createPromptTemplate(template: Partial<PromptTemplateUI>): Promise<{
|
||||
data?: PromptTemplateUI;
|
||||
error?: string;
|
||||
status?: number;
|
||||
}> {
|
||||
try {
|
||||
// 验证必填字段
|
||||
if (!template.template_name || !template.template_type || !template.template_content) {
|
||||
return { error: '模板名称、类型和内容不能为空', status: 400 };
|
||||
}
|
||||
|
||||
// 准备变量数据
|
||||
let variablesData: Record<string, string> = {};
|
||||
if (typeof template.variables === 'string') {
|
||||
try {
|
||||
variablesData = JSON.parse(template.variables);
|
||||
} catch (e) {
|
||||
console.error('解析变量JSON失败:', e);
|
||||
}
|
||||
} else if (template.variables) {
|
||||
variablesData = template.variables;
|
||||
}
|
||||
|
||||
// 准备API数据
|
||||
const apiTemplate: Partial<PromptTemplate> = {
|
||||
template_name: template.template_name,
|
||||
template_type: template.template_type,
|
||||
description: template.description || null,
|
||||
template_content: template.template_content,
|
||||
variables: variablesData,
|
||||
status: mapStatusToAPI(template.status || 'active'),
|
||||
version: template.version || 'v1.0',
|
||||
created_by: 1 // 固定创建者为1
|
||||
};
|
||||
|
||||
if(apiTemplate){
|
||||
console.log('apiTemplate', apiTemplate);
|
||||
// throw new Error('测试错误');
|
||||
}
|
||||
|
||||
const response = await postgrestPost<PromptTemplate, Partial<PromptTemplate>>(
|
||||
'prompt_templates',
|
||||
apiTemplate
|
||||
);
|
||||
|
||||
if (response.error) {
|
||||
return { error: response.error, status: response.status };
|
||||
}
|
||||
|
||||
const extractedData = extractApiData<PromptTemplate>(response.data);
|
||||
|
||||
if (!extractedData) {
|
||||
return { error: '创建提示词模板失败', status: 500 };
|
||||
}
|
||||
|
||||
return { data: convertToUITemplate(extractedData) };
|
||||
} catch (error) {
|
||||
console.error('创建提示词模板失败:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '创建提示词模板失败',
|
||||
status: 500
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新提示词模板
|
||||
* @param id 模板ID
|
||||
* @param template 提示词模板数据
|
||||
* @returns 更新后的提示词模板
|
||||
*/
|
||||
export async function updatePromptTemplate(id: string, template: Partial<PromptTemplateUI>): Promise<{
|
||||
data?: PromptTemplateUI;
|
||||
error?: string;
|
||||
status?: number;
|
||||
}> {
|
||||
try {
|
||||
if (!id) {
|
||||
return { error: '模板ID不能为空', status: 400 };
|
||||
}
|
||||
|
||||
// 准备变量数据
|
||||
let variablesData: Record<string, string> = {};
|
||||
if (typeof template.variables === 'string') {
|
||||
try {
|
||||
variablesData = JSON.parse(template.variables);
|
||||
} catch (e) {
|
||||
console.error('解析变量JSON失败:', e);
|
||||
}
|
||||
} else if (template.variables) {
|
||||
variablesData = template.variables;
|
||||
}
|
||||
|
||||
// 准备API数据
|
||||
const apiTemplate: Partial<PromptTemplate> = {};
|
||||
|
||||
if (template.template_name !== undefined) {
|
||||
apiTemplate.template_name = template.template_name;
|
||||
}
|
||||
|
||||
if (template.template_type !== undefined) {
|
||||
apiTemplate.template_type = template.template_type;
|
||||
}
|
||||
|
||||
if (template.description !== undefined) {
|
||||
apiTemplate.description = template.description;
|
||||
}
|
||||
|
||||
if (template.template_content !== undefined) {
|
||||
apiTemplate.template_content = template.template_content;
|
||||
}
|
||||
|
||||
if (template.variables !== undefined) {
|
||||
apiTemplate.variables = variablesData;
|
||||
}
|
||||
|
||||
if (template.status !== undefined) {
|
||||
apiTemplate.status = mapStatusToAPI(template.status);
|
||||
}
|
||||
|
||||
if (template.version !== undefined) {
|
||||
apiTemplate.version = template.version;
|
||||
}
|
||||
|
||||
// if(apiTemplate){
|
||||
// console.log('apiTemplate', apiTemplate);
|
||||
// throw new Error('测试错误');
|
||||
// }
|
||||
|
||||
const response = await postgrestPut<PromptTemplate, Partial<PromptTemplate>>(
|
||||
'prompt_templates',
|
||||
apiTemplate,
|
||||
{ id }
|
||||
);
|
||||
|
||||
if (response.error) {
|
||||
return { error: response.error, status: response.status };
|
||||
}
|
||||
|
||||
const extractedData = extractApiData<PromptTemplate>(response.data);
|
||||
|
||||
if (!extractedData) {
|
||||
return { error: '更新提示词模板失败', status: 500 };
|
||||
}
|
||||
|
||||
return { data: convertToUITemplate(extractedData) };
|
||||
} catch (error) {
|
||||
console.error('更新提示词模板失败:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '更新提示词模板失败',
|
||||
status: 500
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除提示词模板
|
||||
* @param id 模板ID
|
||||
* @returns 成功或失败信息
|
||||
*/
|
||||
export async function deletePromptTemplate(id: string): Promise<{
|
||||
success?: boolean;
|
||||
error?: string;
|
||||
status?: number;
|
||||
}> {
|
||||
try {
|
||||
if (!id) {
|
||||
return { error: '模板ID不能为空', status: 400 };
|
||||
}
|
||||
|
||||
// 使用真实删除替代状态更新
|
||||
const response = await postgrestDelete<PromptTemplate>(
|
||||
'prompt_templates',
|
||||
{
|
||||
filter: {
|
||||
'id': `eq.${id}`
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (response.error) {
|
||||
return { error: response.error, status: response.status };
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error('删除提示词模板失败:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '删除提示词模板失败',
|
||||
status: 500
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ export interface ConfigItem {
|
||||
description: string;
|
||||
environment: string;
|
||||
config: Record<string, unknown>;
|
||||
remark: string;
|
||||
is_active: boolean;
|
||||
version: string;
|
||||
created_by: number;
|
||||
@@ -229,7 +230,7 @@ export async function createConfig(data: {
|
||||
environment: string;
|
||||
config: Record<string, unknown>;
|
||||
is_active: boolean;
|
||||
remarks?: string;
|
||||
remark?: string;
|
||||
}): Promise<{data: ConfigItem; error?: never} | {data?: never; error: string}> {
|
||||
try {
|
||||
const response = await postgrestPost<ConfigItem, typeof data>('configurations', data);
|
||||
@@ -263,7 +264,7 @@ export async function updateConfig(id: string, data: {
|
||||
environment: string;
|
||||
config: Record<string, unknown>;
|
||||
is_active: boolean;
|
||||
remarks?: string;
|
||||
remark?: string;
|
||||
}): Promise<{data: ConfigItem; error?: never} | {data?: never; error: string}> {
|
||||
try {
|
||||
const response = await postgrestPut<ConfigItem, typeof data>('configurations', data, {
|
||||
|
||||
Reference in New Issue
Block a user