import { z } from 'zod'; export const researchQuestionSchema = z.object({ id: z.string().min(1).max(80), question: z.string().min(1).max(2000), scope: z.array(z.string().min(1).max(1000)).max(20), stopWhen: z.string().min(1).max(1000), }); export const researchFindingSchema = z.object({ questionId: z.string().min(1).max(80), kind: z.enum(['observation', 'inference', 'recommendation']), conclusion: z.string().min(1).max(2000), evidenceRefs: z.array(z.string().min(1).max(200)).max(20), locations: z.array(z.string().min(1).max(1000)).max(20), limitations: z.array(z.string().min(1).max(1000)).max(10), conflicts: z.array(z.string().min(1).max(1000)).max(10), }).superRefine((finding, ctx) => { if (JSON.stringify(finding).length > 6000) ctx.addIssue({ code: 'custom', message: '单条研究结论最多 6000 字符;保留必要风险,详细原文引用证据' }); if (finding.kind === 'observation' && !finding.evidenceRefs.length) ctx.addIssue({ code: 'custom', path: ['evidenceRefs'], message: '观察事实必须引用真实工具证据;未验证判断使用 inference' }); }); export type ResearchFinding = z.infer; export function researchPage(findings: readonly ResearchFinding[], offset = 0) { const start = Math.min(Math.max(0, offset), findings.length); const page: ResearchFinding[] = []; let chars = 0; for (const finding of findings.slice(start)) { const size = JSON.stringify(finding).length; if (page.length && chars + size > 12000) break; page.push(finding); chars += size; } return { findings: page, totalFindings: findings.length, nextFindingOffset: start + page.length < findings.length ? start + page.length : null }; } /** A bounded preview is not the durable handoff. Never slice JSON mid-field. */ export function researchPreview(packet: { summary: string; findings?: ResearchFinding[] }, maxChars: number): string { const selected: ResearchFinding[] = []; const envelope = () => JSON.stringify({ summary: packet.summary.slice(0, Math.min(1000, Math.floor(maxChars / 3))), findings: selected, omittedFindings: (packet.findings?.length ?? 0) - selected.length, notice: '预览不代表完整交接;完整报告和证据留在 TaskOutput(handoff=true)。未显示的风险不能视为不存在。', }); for (const finding of packet.findings ?? []) { selected.push(finding); if (envelope().length > maxChars) selected.pop(); } return envelope(); }