feat: 会商链条启停与深度研究报告结构,并完善问答展示与 8BF 返回。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
be4851555d
commit
0ce060693b
42 files changed
+5320
-3247
No files matched your search
@@ -0,0 +1,180 @@
|
||||
/**
|
||||
* 深度研究「工作流配置 / 报告结构」API 客户端 + React Query hooks。
|
||||
*
|
||||
* 后端:`/api/report-structures`(见 app/gateway/routers/report_structures.py)。
|
||||
* 全局共享、无用户隔离;新增永远默认启用(服务端强制,不收 enabled 字段)。
|
||||
* snake_case ⇄ camelCase 在此边界收敛。
|
||||
*/
|
||||
|
||||
import {
|
||||
keepPreviousData,
|
||||
useMutation,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
} from "@tanstack/react-query";
|
||||
|
||||
import { apiFetch } from "@/core/api/fetch-client";
|
||||
import { getBackendBaseURL } from "@/core/config";
|
||||
|
||||
export interface ReportStructure {
|
||||
id: string;
|
||||
/** 标题:卡片主标签,也是列表「标题筛选」的目标。 */
|
||||
title: string;
|
||||
/** 描述:一句话说明这套结构的适用场景(存储列沿用 name)。 */
|
||||
name: string;
|
||||
/** 接口内容:选中卡片后应用到报告结构(custom_outline)的 Markdown 文本。 */
|
||||
content: string;
|
||||
enabled: boolean;
|
||||
/** 创建用户名(本部署为邮箱;系统内置种子为“系统内置”)。 */
|
||||
createdBy: string | null;
|
||||
createdAt: string | null;
|
||||
/** 最后修改时间(编辑、启停都会刷新)。 */
|
||||
updatedAt: string | null;
|
||||
}
|
||||
|
||||
export type ReportStructureInput = {
|
||||
title: string;
|
||||
name: string;
|
||||
content: string;
|
||||
};
|
||||
|
||||
export type ReportStructurePatch = Partial<ReportStructureInput>;
|
||||
|
||||
export type ReportStructureStatusFilter = "all" | "enabled" | "disabled";
|
||||
|
||||
const BASE = () => `${getBackendBaseURL()}/api/report-structures`;
|
||||
|
||||
interface ServerStructure {
|
||||
id: string;
|
||||
title: string;
|
||||
name: string;
|
||||
content: string;
|
||||
enabled: boolean;
|
||||
created_by: string | null;
|
||||
created_at: string | null;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
function toStructure(s: ServerStructure): ReportStructure {
|
||||
return {
|
||||
id: s.id,
|
||||
title: s.title,
|
||||
name: s.name ?? "",
|
||||
content: s.content,
|
||||
enabled: s.enabled ?? true,
|
||||
createdBy: s.created_by ?? null,
|
||||
createdAt: s.created_at ?? null,
|
||||
updatedAt: s.updated_at ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
async function readJson<T>(res: Response, ctx: string): Promise<T> {
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => null as unknown);
|
||||
const detail =
|
||||
data && typeof data === "object" && "detail" in data
|
||||
? String((data as { detail: unknown }).detail)
|
||||
: `${ctx} failed (${res.status})`;
|
||||
throw new Error(detail);
|
||||
}
|
||||
return (await res.json()) as T;
|
||||
}
|
||||
|
||||
export async function listReportStructures(params: {
|
||||
status?: ReportStructureStatusFilter;
|
||||
title?: string;
|
||||
} = {}): Promise<ReportStructure[]> {
|
||||
const qs = new URLSearchParams();
|
||||
if (params.status === "enabled") qs.set("enabled", "true");
|
||||
if (params.status === "disabled") qs.set("enabled", "false");
|
||||
const title = params.title?.trim();
|
||||
if (title) qs.set("title", title);
|
||||
const query = qs.toString();
|
||||
const res = await apiFetch(`${BASE()}${query ? `?${query}` : ""}`, { method: "GET" });
|
||||
const data = await readJson<{ structures: ServerStructure[] }>(res, "list report structures");
|
||||
return (data.structures ?? []).map(toStructure);
|
||||
}
|
||||
|
||||
export async function createReportStructure(input: ReportStructureInput): Promise<ReportStructure> {
|
||||
const res = await apiFetch(BASE(), {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ title: input.title, name: input.name, content: input.content }),
|
||||
});
|
||||
return toStructure(await readJson<ServerStructure>(res, "create report structure"));
|
||||
}
|
||||
|
||||
export async function updateReportStructure(
|
||||
id: string,
|
||||
patch: ReportStructurePatch,
|
||||
): Promise<ReportStructure> {
|
||||
const body: Record<string, unknown> = {};
|
||||
if (patch.title !== undefined) body.title = patch.title;
|
||||
if (patch.name !== undefined) body.name = patch.name;
|
||||
if (patch.content !== undefined) body.content = patch.content;
|
||||
const res = await apiFetch(`${BASE()}/${encodeURIComponent(id)}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return toStructure(await readJson<ServerStructure>(res, "update report structure"));
|
||||
}
|
||||
|
||||
export async function setReportStructureEnabled(id: string, enabled: boolean): Promise<ReportStructure> {
|
||||
const res = await apiFetch(`${BASE()}/${encodeURIComponent(id)}/enabled`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ enabled }),
|
||||
});
|
||||
return toStructure(await readJson<ServerStructure>(res, "toggle report structure"));
|
||||
}
|
||||
|
||||
export async function deleteReportStructure(id: string): Promise<void> {
|
||||
const res = await apiFetch(`${BASE()}/${encodeURIComponent(id)}`, { method: "DELETE" });
|
||||
// 204 无响应体,不能再走 readJson 的 res.json()。
|
||||
if (!res.ok) {
|
||||
const data = (await res.json().catch(() => null)) as { detail?: unknown } | null;
|
||||
throw new Error(
|
||||
data && typeof data.detail === "string" ? data.detail : `delete report structure failed (${res.status})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── React Query hooks(工作流配置弹窗内各视图共用缓存) ──────────────────────
|
||||
|
||||
const REPORT_STRUCTURES_KEY = "report-structures";
|
||||
|
||||
export function useReportStructures(params: { status?: ReportStructureStatusFilter; title?: string } = {}) {
|
||||
return useQuery({
|
||||
queryKey: [REPORT_STRUCTURES_KEY, "list", params.status ?? "all", params.title ?? ""],
|
||||
queryFn: () => listReportStructures(params),
|
||||
placeholderData: keepPreviousData,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useReportStructureMutations() {
|
||||
const queryClient = useQueryClient();
|
||||
const invalidate = () => queryClient.invalidateQueries({ queryKey: [REPORT_STRUCTURES_KEY] });
|
||||
|
||||
const create = useMutation({
|
||||
mutationFn: (input: ReportStructureInput) => createReportStructure(input),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
const update = useMutation({
|
||||
mutationFn: ({ id, patch }: { id: string; patch: ReportStructurePatch }) =>
|
||||
updateReportStructure(id, patch),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
const toggleEnabled = useMutation({
|
||||
mutationFn: ({ id, enabled }: { id: string; enabled: boolean }) =>
|
||||
setReportStructureEnabled(id, enabled),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
const remove = useMutation({
|
||||
mutationFn: (id: string) => deleteReportStructure(id),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
|
||||
return { create, update, toggleEnabled, remove };
|
||||
}
|
||||
Reference in new issue
Block a user