refac: prompt endpoints

This commit is contained in:
Timothy Jaeryang Baek
2026-01-24 03:08:48 +04:00
parent dff0141160
commit 5ad593e465
6 changed files with 172 additions and 114 deletions
+19 -35
View File
@@ -1,6 +1,7 @@
import { WEBUI_API_BASE_URL } from '$lib/constants';
type PromptItem = {
id?: string; // Prompt ID
command: string;
name: string; // Changed from title
content: string;
@@ -9,6 +10,7 @@ type PromptItem = {
access_control?: null | object;
version_id?: string | null; // Active version
commit_message?: string | null; // For history tracking
is_production?: boolean; // Whether to set new version as production
};
type PromptHistoryItem = {
@@ -203,22 +205,17 @@ export const getPromptById = async (token: string, promptId: string) => {
return res;
};
export const updatePromptByCommand = async (token: string, prompt: PromptItem) => {
export const updatePromptById = async (token: string, prompt: PromptItem) => {
let error = null;
const command = prompt.command.charAt(0) === '/' ? prompt.command.slice(1) : prompt.command;
const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${command}/update`, {
const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/id/${prompt.id}/update`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({
...prompt,
command: command
})
body: JSON.stringify(prompt)
})
.then(async (res) => {
if (!res.ok) throw await res.json();
@@ -243,12 +240,12 @@ export const updatePromptByCommand = async (token: string, prompt: PromptItem) =
export const setProductionPromptVersion = async (
token: string,
command: string,
promptId: string,
version_id: string
) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${command}/set/version`, {
const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/id/${promptId}/set/version`, {
method: 'POST',
headers: {
Accept: 'application/json',
@@ -276,12 +273,10 @@ export const setProductionPromptVersion = async (
return res;
};
export const deletePromptByCommand = async (token: string, command: string) => {
export const deletePromptById = async (token: string, promptId: string) => {
let error = null;
command = command.charAt(0) === '/' ? command.slice(1) : command;
const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${command}/delete`, {
const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/id/${promptId}/delete`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
@@ -316,16 +311,14 @@ export const deletePromptByCommand = async (token: string, command: string) => {
export const getPromptHistory = async (
token: string,
command: string,
promptId: string,
limit: number = 50,
offset: number = 0
): Promise<PromptHistoryItem[]> => {
let error = null;
command = command.charAt(0) === '/' ? command.slice(1) : command;
const res = await fetch(
`${WEBUI_API_BASE_URL}/prompts/command/${command}/history?limit=${limit}&offset=${offset}`,
`${WEBUI_API_BASE_URL}/prompts/id/${promptId}/history?limit=${limit}&offset=${offset}`,
{
method: 'GET',
headers: {
@@ -354,15 +347,13 @@ export const getPromptHistory = async (
export const deletePromptHistoryVersion = async (
token: string,
command: string,
promptId: string,
historyId: string
): Promise<boolean> => {
let error = null;
command = command.charAt(0) === '/' ? command.slice(1) : command;
const res = await fetch(
`${WEBUI_API_BASE_URL}/prompts/command/${command}/history/${historyId}`,
`${WEBUI_API_BASE_URL}/prompts/id/${promptId}/history/${historyId}`,
{
method: 'DELETE',
headers: {
@@ -391,15 +382,13 @@ export const deletePromptHistoryVersion = async (
export const getPromptHistoryEntry = async (
token: string,
command: string,
promptId: string,
historyId: string
): Promise<PromptHistoryItem> => {
let error = null;
command = command.charAt(0) === '/' ? command.slice(1) : command;
const res = await fetch(
`${WEBUI_API_BASE_URL}/prompts/command/${command}/history/${historyId}`,
`${WEBUI_API_BASE_URL}/prompts/id/${promptId}/history/${historyId}`,
{
method: 'GET',
headers: {
@@ -428,16 +417,14 @@ export const getPromptHistoryEntry = async (
export const restorePromptFromHistory = async (
token: string,
command: string,
promptId: string,
historyId: string,
commitMessage?: string
) => {
let error = null;
command = command.charAt(0) === '/' ? command.slice(1) : command;
const res = await fetch(
`${WEBUI_API_BASE_URL}/prompts/command/${command}/history/${historyId}/restore`,
`${WEBUI_API_BASE_URL}/prompts/id/${promptId}/history/${historyId}/restore`,
{
method: 'POST',
headers: {
@@ -469,16 +456,14 @@ export const restorePromptFromHistory = async (
export const getPromptDiff = async (
token: string,
command: string,
promptId: string,
fromId: string,
toId: string
): Promise<PromptDiff> => {
let error = null;
command = command.charAt(0) === '/' ? command.slice(1) : command;
const res = await fetch(
`${WEBUI_API_BASE_URL}/prompts/command/${command}/history/diff?from_id=${fromId}&to_id=${toId}`,
`${WEBUI_API_BASE_URL}/prompts/id/${promptId}/history/diff?from_id=${fromId}&to_id=${toId}`,
{
method: 'GET',
headers: {
@@ -505,4 +490,3 @@ export const getPromptDiff = async (
return res;
};
+2 -2
View File
@@ -9,7 +9,7 @@
import {
createNewPrompt,
deletePromptByCommand,
deletePromptById,
getPrompts,
getPromptList
} from '$lib/apis/prompts';
@@ -121,7 +121,7 @@
const deleteHandler = async (prompt) => {
const command = prompt.command;
const res = await deletePromptByCommand(localStorage.token, command).catch((err) => {
const res = await deletePromptById(localStorage.token, prompt.id).catch((err) => {
toast.error(err);
return null;
});
@@ -13,7 +13,6 @@
import XMark from '$lib/components/icons/XMark.svelte';
import {
getPromptHistory,
updatePromptByCommand,
setProductionPromptVersion,
deletePromptHistoryVersion
} from '$lib/apis/prompts';
@@ -66,6 +65,7 @@
if (validateCommandString(command)) {
await onSubmit({
id: prompt?.id,
name,
command,
content,
@@ -95,7 +95,7 @@
if (!prompt?.command || !edit) return;
historyLoading = true;
try {
history = await getPromptHistory(localStorage.token, prompt.command);
history = await getPromptHistory(localStorage.token, prompt.id);
} catch (error) {
console.error('Failed to load history:', error);
history = [];
@@ -110,7 +110,7 @@
}
try {
await setProductionPromptVersion(localStorage.token, prompt.command, historyEntry.id);
await setProductionPromptVersion(localStorage.token, prompt.id, historyEntry.id);
// Update local prompt object to trigger reactivity
prompt = { ...prompt, version_id: historyEntry.id };
toast.success($i18n.t('Production version updated'));
@@ -123,7 +123,7 @@
if (disabled) return;
try {
await deletePromptHistoryVersion(localStorage.token, prompt.command, historyId);
await deletePromptHistoryVersion(localStorage.token, prompt.id, historyId);
toast.success($i18n.t('Version deleted'));
// Reload history
await loadHistory();
@@ -6,7 +6,7 @@
const i18n = getContext('i18n');
import { getPromptById, getPrompts, updatePromptByCommand } from '$lib/apis/prompts';
import { getPromptById, getPrompts, updatePromptById } from '$lib/apis/prompts';
import { page } from '$app/stores';
import PromptEditor from '$lib/components/workspace/Prompts/PromptEditor.svelte';
@@ -19,7 +19,7 @@
const onSubmit = async (_prompt) => {
console.log(_prompt);
const updatedPrompt = await updatePromptByCommand(localStorage.token, _prompt).catch((error) => {
const updatedPrompt = await updatePromptById(localStorage.token, _prompt).catch((error) => {
toast.error(`${error}`);
return null;
});