File size: 2,515 Bytes
19eca0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { query } from '@/shared/api/query';
import { QePrompt } from './types';

export const fetchQePrompts = async (): Promise<QePrompt[]> => {
  const response = await query<QePrompt[]>({
    url: '/qe_prompt/',
    method: 'get',
  });
  if ('error' in response) {
    throw new Error(`Ошибка получения промптов: ${response.error.status}`);
  }
  return response.data;
};

export const fetchQePromptById = async (id: number): Promise<QePrompt> => {
  const response = await query<QePrompt>({
    url: `/qe_prompt/${id}`,
    method: 'get',
  });
  if ('error' in response) {
    throw new Error(`Ошибка получения промпта: ${response.error.status}`);
  }
  return response.data;
};

export const createQePrompt = async (config: Omit<QePrompt, 'id' | 'created_at'>): Promise<QePrompt> => {
  const response = await query<QePrompt>({
    url: '/qe_prompt/',
    method: 'post',
    data: config,
  });
  if ('error' in response) {
    throw new Error(`Ошибка создания промпта: ${response.error.status}`);
  }
  return response.data;
};

export const updateQePrompt = async (config: QePrompt): Promise<void> => {
  const response = await query<void>({
    url: `/qe_prompt/${config.id}`,
    method: 'put',
    data: config,
  });
  if ('error' in response) {
    throw new Error(`Ошибка обновления промпта: ${response.error.status}`);
  }
};

export const setDefaultQePrompt = async (id: number): Promise<void> => {
  const response = await query<void>({
    url: `/qe_prompt/default/${id}`,
    method: 'put',
  });
  if ('error' in response) {
    throw new Error(`Ошибка установки промпта по умолчанию: ${response.error.status}`);
  }
};

export const deleteQePrompt = async (id: number): Promise<void> => {
  const response = await query<void>({
    url: `/qe_prompt/${id}`,
    method: 'delete',
  });
  if ('error' in response) {
    throw new Error(`Ошибка удаления промпта: ${response.error.status}`);
  }
};

export const fetchDefaultQePrompt = async (): Promise<QePrompt | null> => {
  const response = await query<QePrompt>({
    url: '/qe_prompt/default',
    method: 'get',
  });
  if ('error' in response) {
    if (response.error.status === 404) return null; // Если дефолтной записи нет
    throw new Error(`Ошибка получения дефолтной промпта: ${response.error.status}`);
  }
  return response.data;
};