72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { useState, useEffect, useCallback } from "react";
|
|
|
|
export type SystemPrompts = {
|
|
all: string;
|
|
partial: string;
|
|
style_normal: string;
|
|
style_crazy: string;
|
|
lyrics_partial: string;
|
|
};
|
|
|
|
export function useSystemPrompts() {
|
|
const [prompts, setPrompts] = useState<SystemPrompts | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const fetchPrompts = useCallback(async () => {
|
|
try {
|
|
setLoading(true);
|
|
const res = await fetch("/api/system-prompts");
|
|
if (!res.ok) throw new Error("Failed to fetch system prompts");
|
|
const data = await res.json();
|
|
setPrompts(data);
|
|
setError(null);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : "Unknown error");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
const savePrompts = async (newPrompts: SystemPrompts) => {
|
|
try {
|
|
setLoading(true);
|
|
const res = await fetch("/api/system-prompts", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(newPrompts),
|
|
});
|
|
if (!res.ok) throw new Error("Failed to save system prompts");
|
|
setPrompts(newPrompts);
|
|
setError(null);
|
|
return true;
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : "Unknown error");
|
|
return false;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const resetPrompts = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const res = await fetch("/api/system-prompts", { method: "DELETE" });
|
|
if (!res.ok) throw new Error("Failed to reset system prompts");
|
|
await fetchPrompts();
|
|
return true;
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : "Unknown error");
|
|
return false;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchPrompts();
|
|
}, [fetchPrompts]);
|
|
|
|
return { prompts, loading, error, savePrompts, resetPrompts };
|
|
}
|