File size: 3,841 Bytes
f8b5d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { API_BASE } from "@/utils/constants";
import { baseHeaders } from "@/utils/request";

/**
 * @typedef {Object} SystemPromptVariable
 * @property {number|null} id - The ID of the system prompt variable
 * @property {string} key - The key of the system prompt variable
 * @property {string} value - The value of the system prompt variable
 * @property {string} description - The description of the system prompt variable
 * @property {string} type - The type of the system prompt variable
 */

const SystemPromptVariable = {
  /**
   * Get all system prompt variables
   * @returns {Promise<{variables: SystemPromptVariable[]}>} - An array of system prompt variables
   */
  getAll: async function () {
    try {
      return await fetch(`${API_BASE}/system/prompt-variables`, {
        method: "GET",
        headers: baseHeaders(),
      })
        .then((res) => res.json())
        .catch((error) => {
          console.error("Error fetching system prompt variables:", error);
          return { variables: [] };
        });
    } catch (error) {
      console.error("Error fetching system prompt variables:", error);
      return { variables: [] };
    }
  },

  /**
   * Create a new system prompt variable
   * @param {SystemPromptVariable} variable - The system prompt variable to create
   * @returns {Promise<{success: boolean, error: string}>} - A promise that resolves to an object containing a success flag and an error message
   */
  create: async function (variable = {}) {
    try {
      return await fetch(`${API_BASE}/system/prompt-variables`, {
        method: "POST",
        headers: baseHeaders(),
        body: JSON.stringify(variable),
      })
        .then((res) => res.json())
        .catch((error) => {
          console.error("Error creating system prompt variable:", error);
          return { success: false, error };
        });
    } catch (error) {
      console.error("Error creating system prompt variable:", error);
      return { success: false, error };
    }
  },

  /**
   * Update a system prompt variable
   * @param {string} id - The ID of the system prompt variable to update
   * @param {SystemPromptVariable} variable - The system prompt variable to update
   * @returns {Promise<{success: boolean, error: string}>} - A promise that resolves to an object containing a success flag and an error message
   */
  update: async function (id, variable = {}) {
    try {
      return await fetch(`${API_BASE}/system/prompt-variables/${id}`, {
        method: "PUT",
        headers: baseHeaders(),
        body: JSON.stringify(variable),
      })
        .then((res) => res.json())
        .catch((error) => {
          console.error("Error updating system prompt variable:", error);
          return { success: false, error };
        });
    } catch (error) {
      console.error("Error updating system prompt variable:", error);
      return { success: false, error };
    }
  },

  /**
   * Delete a system prompt variable
   * @param {string} id - The ID of the system prompt variable to delete
   * @returns {Promise<{success: boolean, error: string}>} - A promise that resolves to an object containing a success flag and an error message
   */
  delete: async function (id = null) {
    try {
      if (id === null) return { success: false, error: "ID is required" };
      return await fetch(`${API_BASE}/system/prompt-variables/${id}`, {
        method: "DELETE",
        headers: baseHeaders(),
      })
        .then((res) => res.json())
        .catch((error) => {
          console.error("Error deleting system prompt variable:", error);
          return { success: false, error };
        });
    } catch (error) {
      console.error("Error deleting system prompt variable:", error);
      return { success: false, error };
    }
  },
};

export default SystemPromptVariable;