82 lines
3.5 KiB
TypeScript
82 lines
3.5 KiB
TypeScript
import type { Spec } from "@json-render/core";
|
|
import { z } from "zod";
|
|
import { answerUiCatalog, ANSWER_UI_VERSION } from "./catalog";
|
|
|
|
const key = z.string().regex(/^[a-zA-Z0-9_-]{1,80}$/);
|
|
const element = z
|
|
.object({ type: z.string(), props: z.record(z.unknown()), children: z.array(key).max(48) })
|
|
.strict();
|
|
const specSchema = z.object({ root: key, elements: z.record(element) }).strict();
|
|
const payloadSchema = z
|
|
.object({
|
|
version: z.literal(ANSWER_UI_VERSION),
|
|
block_id: key,
|
|
phase: z.enum(["process", "final"]).optional(),
|
|
fallback_text: z.string().min(1).max(4000),
|
|
spec: specSchema
|
|
})
|
|
.strict();
|
|
|
|
// Stored reports remain readable if one presentation component is no longer supported.
|
|
export const storedAnswerUiBlockSchema = z.object({
|
|
version: z.string(),
|
|
block_id: key,
|
|
phase: z.enum(["process", "final"]).optional(),
|
|
fallback_text: z.string().min(1).max(4000),
|
|
spec: z.unknown()
|
|
});
|
|
export const reportUiBlocksSchema = z.array(z.object({
|
|
block: storedAnswerUiBlockSchema,
|
|
evidence_refs: z.array(z.string().min(1).max(128)).min(1).max(100)
|
|
}).strict().refine((item) => item.block.phase !== "process", "过程界面不能发布为正式报告")).max(8);
|
|
export type StoredReportUiBlock = z.infer<typeof reportUiBlocksSchema>[number];
|
|
|
|
export type AnswerUiBlock = {
|
|
version: typeof ANSWER_UI_VERSION;
|
|
block_id: string;
|
|
phase?: "process" | "final";
|
|
fallback_text: string;
|
|
spec: Spec;
|
|
};
|
|
export type AnswerUiPart =
|
|
{ type: "text"; text: string } | { type: "answer-ui"; block: AnswerUiBlock };
|
|
|
|
export function parseAnswerUiBlock(value: unknown): AnswerUiBlock | null {
|
|
const parsed = payloadSchema.safeParse(value);
|
|
if (!parsed.success || JSON.stringify(parsed.data).length > 100000) return null;
|
|
const { spec } = parsed.data;
|
|
const nodes = Object.keys(spec.elements);
|
|
if (!nodes.length || nodes.length > 48 || nodes.some((name) => !key.safeParse(name).success))
|
|
return null;
|
|
const definitions = answerUiCatalog.data.components;
|
|
for (const node of Object.values(spec.elements)) {
|
|
const definition = Object.hasOwn(definitions, node.type)
|
|
? definitions[node.type as keyof typeof definitions]
|
|
: undefined;
|
|
if (!definition?.props.safeParse(node.props).success) return null;
|
|
if (!["Stack", "Section"].includes(node.type) && node.children.length) return null;
|
|
if (node.type === "Chart") {
|
|
const data = node.props as { x_data: unknown[]; series: Array<{ data: unknown[]; sample_times?: Array<string | null> }> };
|
|
if (data.series.some((series) => series.data.length !== data.x_data.length ||
|
|
series.sample_times && (series.sample_times.length !== data.x_data.length ||
|
|
series.sample_times.some((time, index) => time !== null && series.data[index] === null)))) return null;
|
|
}
|
|
if (node.type === "Table") {
|
|
const data = node.props as { columns: unknown[]; rows: unknown[][] };
|
|
if (data.rows.some((row) => row.length !== data.columns.length)) return null;
|
|
}
|
|
if (node.type === "FollowUpForm") {
|
|
const data = node.props as { fields: Array<{ name: string }> };
|
|
if (new Set(data.fields.map((field) => field.name)).size !== data.fields.length) return null;
|
|
}
|
|
}
|
|
const visited = new Set<string>();
|
|
const visit = (id: string, depth: number): boolean => {
|
|
if (depth > 12 || visited.has(id) || !Object.hasOwn(spec.elements, id)) return false;
|
|
visited.add(id);
|
|
return spec.elements[id].children.every((child) => visit(child, depth + 1));
|
|
};
|
|
if (!visit(spec.root, 0) || visited.size !== nodes.length) return null;
|
|
return parsed.data;
|
|
}
|