feat: add fixed questions and improve authentication

This commit is contained in:
吉鑫升 committed 2026-08-21 17:58:39 +08:00
1 parent 8af4fed89d
commit 4c9f75f31e
42 files changed
+4008 -200

No files matched your search

@@ -0,0 +1,122 @@
/** Administrator-managed exact-match fixed question/answer API. */
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { apiFetch } from "@/core/api/fetch-client";
import { getBackendBaseURL } from "@/core/config";
import { OptType, writeYLog } from "@/core/log/y-log";
const BASE = () => `${getBackendBaseURL()}/api/fixed-questions`;
export interface FixedQuestion {
id: string;
question: string;
answer: string;
enabled: boolean;
tokensPerSecond: number;
sortOrder: number;
updatedBy?: string | null;
updatedAt?: string | null;
}
interface ServerFixedQuestion {
id: string;
question: string;
answer: string;
enabled: boolean;
tokens_per_second: number;
sort_order: number;
updated_by?: string | null;
updated_at?: string | null;
}
function fromServer(row: ServerFixedQuestion): FixedQuestion {
return {
id: row.id,
question: row.question ?? "",
answer: row.answer ?? "",
enabled: row.enabled ?? true,
tokensPerSecond: row.tokens_per_second ?? 100,
sortOrder: row.sort_order ?? 0,
updatedBy: row.updated_by ?? null,
updatedAt: row.updated_at ?? null,
};
}
function toServer(row: FixedQuestion): ServerFixedQuestion {
return {
id: row.id,
question: row.question,
answer: row.answer,
enabled: row.enabled,
tokens_per_second: row.tokensPerSecond,
sort_order: row.sortOrder,
};
}
async function readJson<T>(response: Response, context: string): Promise<T> {
if (!response.ok) {
const data = await response.json().catch(() => null as unknown);
const detail =
data && typeof data === "object" && "detail" in data
? String((data as { detail: unknown }).detail)
: `${context} failed (${response.status})`;
throw new Error(detail);
}
return (await response.json()) as T;
}
export async function listFixedQuestions(): Promise<FixedQuestion[]> {
const response = await apiFetch(BASE(), { method: "GET" });
const data = await readJson<{ questions: ServerFixedQuestion[] }>(
response,
"list fixed questions",
);
return (data.questions ?? []).map(fromServer);
}
export async function replaceFixedQuestions(
questions: FixedQuestion[],
): Promise<FixedQuestion[]> {
const response = await apiFetch(BASE(), {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ questions: questions.map(toServer) }),
});
const data = await readJson<{ questions: ServerFixedQuestion[] }>(
response,
"save fixed questions",
);
const saved = (data.questions ?? []).map(fromServer);
writeYLog({
optType: OptType.edit,
optDetail: "CM助手-全量-问题配置-保存",
url: "/api/fixed-questions",
reqParam: { questions },
returnParam: saved,
});
return saved;
}
const FIXED_QUESTIONS_KEY = "fixed-questions";
export function useFixedQuestions() {
return useQuery({
queryKey: [FIXED_QUESTIONS_KEY],
queryFn: listFixedQuestions,
refetchOnWindowFocus: false,
staleTime: 30_000,
});
}
export function useFixedQuestionActions() {
const queryClient = useQueryClient();
return {
save: useMutation({
mutationFn: replaceFixedQuestions,
onSuccess: (questions) => {
queryClient.setQueryData([FIXED_QUESTIONS_KEY], questions);
},
}),
};
}