| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import type { AgentDecisionProjection, BranchStory, ConvergenceBatch, ExecutionViewV8, MultipathDecisionBatch, RetrievalStage, RoundStoryV8, StoryKind, StoryNode, StoryRole } from "@/lib/types";
- export function makeStory(id: string, role: StoryRole, kind: StoryKind = "decision", overrides: Partial<StoryNode> = {}): StoryNode {
- const card = { primary: { label: kind === "result" ? "产出" : kind === "execution" ? "结果" : "决定", value: `${roleTitle(role)}的业务摘要` }, secondary: [] };
- const story: StoryNode = { id, role, kind, title: roleTitle(role), hasChanges: kind === "result", card, ...overrides };
- if (kind === "decision" && !overrides.decision) story.decision = makeDecision(id, role, story.card);
- return story;
- }
- function makeDecision(id: string, role: StoryRole, card: StoryNode["card"]): AgentDecisionProjection {
- const decisionType = role === "data-decision" || role === "multipath-decision" ? "tradeoff" : role === "multipath-review" || role === "round-evaluation" ? "evaluation" : role === "candidate-output" ? "creative" : "direction";
- const actor = role === "data-decision" || role === "candidate-output" ? { role: "implementer" as const, label: "实现 Agent" } : role === "multipath-review" ? { role: "multipath-evaluator" as const, label: "多方案评审 Agent" } : role === "round-evaluation" ? { role: "overall-evaluator" as const, label: "整体评审 Agent" } : { role: "main" as const, label: "主 Agent" };
- const authority = actor.role === "main" ? "final" as const : actor.role.includes("evaluator") ? "recommendation" as const : "implementation-scope" as const;
- return { id, semanticKind: "decision", decisionType, subtype: role, actor, authority, question: `${roleTitle(role)}要回答什么?`, inputs: [], body: { type: decisionType }, card, detailRef: id, promptRef: actor.role === "main" ? "prompt:main" : `prompt:event:${id.replace(/\D/g, "") || 1}`, completeness: "complete", notices: [] };
- }
- export function makeRetrievalStage(roundIndex: number, branchId: number, agentCount = 2, attemptCount = 3, mode: RetrievalStage["observedMode"] = "sequential"): RetrievalStage {
- const direct = { id: `retrieval-direct-${roundIndex}-${branchId}`, kind: "direct-tools" as const, owner: "script_implementer" as const, callCount: 2, successCount: 2, failureCount: 0, runningCount: 0, sources: [{ businessLabel: "当前脚本", callCount: 1 }, { businessLabel: "选题", callCount: 1 }], calls: [], waveIndex: 0, startedAt: "2026-07-13T08:00:00Z", endedAt: "2026-07-13T08:00:05Z", detailRef: `retrieval-direct:${roundIndex}${branchId}` };
- const agentRuns = Array.from({ length: agentCount }, (_, index) => ({
- id: `retrieval-agent-${roundIndex}-${branchId}-${index + 1}`,
- kind: "retrieval-agent" as const,
- eventId: roundIndex * 1000 + branchId * 10 + index,
- agentType: index % 2 ? "retrieve_data_knowledge" : "retrieve_data_decode_case",
- businessLabel: index % 2 ? "知识 Agent" : "解构 Case Agent",
- taskPreview: `为方案 ${branchId} 查找可靠依据`,
- status: "completed" as const,
- querySummary: { total: attemptCount, hit: Math.ceil(attemptCount / 2), empty: Math.floor(attemptCount / 2), failure: 0, unknown: 0 },
- attempts: Array.from({ length: attemptCount }, (_, attempt) => ({ id: `event:${roundIndex}${branchId}${index}${attempt}`, eventId: roundIndex * 10000 + branchId * 100 + index * 10 + attempt, sequence: attempt, queryLabel: `查询关键词 ${attempt + 1}`, status: attempt % 2 ? "empty" as const : "hit" as const, resultCount: attempt % 2 ? 0 : 2, detailRef: `event:${roundIndex}${branchId}${index}${attempt}` })),
- screening: { state: "available" as const, preview: "已经归纳可用证据和待核实内容。" },
- waveIndex: mode === "parallel" ? 1 : index + 1,
- startedAt: `2026-07-13T08:0${index + 1}:00Z`,
- endedAt: `2026-07-13T08:0${index + 1}:30Z`,
- detailRef: `retrieval-agent:${roundIndex * 1000 + branchId * 10 + index}`,
- }));
- const operationIds = [direct.id, ...agentRuns.map((run) => run.id)];
- const waves = mode === "parallel"
- ? [{ index: 0, operationIds, startedAt: direct.startedAt, endedAt: agentRuns.at(-1)?.endedAt }]
- : operationIds.map((id, index) => ({ index, operationIds: [id] }));
- if (mode === "parallel") { direct.waveIndex = 0; agentRuns.forEach((run) => { run.waveIndex = 0; }); }
- return { id: `round:${roundIndex}:branch:${branchId}:retrieval`, roundIndex, branchId, implementerEventId: roundIndex * 100 + branchId, observedMode: mode, waves, directToolGroups: [direct], agentRuns, status: "completed", detailRef: `round:${roundIndex}:branch:${branchId}:retrieval` };
- }
- export function makeBranch(roundIndex: number, branchId: number): BranchStory {
- const prefix = `r${roundIndex}-b${branchId}`;
- const pathType = branchId % 4 === 0 ? "领域信息" as const : "内容" as const;
- const dataNode = makeStory(`${prefix}-data-1`, "data-decision");
- return {
- id: `${prefix}-branch`, branchId, pathType, status: branchId % 3 === 0 ? "discarded" : "merged", title: `方案 ${branchId}`,
- summary: { task: `完成候选任务 ${branchId}`, output: pathType === "领域信息" ? `新增 ${branchId} 条领域事实` : `形成候选脚本 ${branchId}` },
- task: makeStory(`${prefix}-task`, "branch-task", "execution"), retrievalStage: makeRetrievalStage(roundIndex, branchId),
- dataDecisions: [{ id: `${prefix}-data-1`, recordId: branchId * 100, decision: "采用核实数据", sources: [], node: dataNode }],
- output: { type: pathType === "领域信息" ? "domain-info" : "script-artifact", summary: pathType === "领域信息" ? `新增 ${branchId} 条领域事实` : `形成候选脚本 ${branchId}`, snapshotRef: pathType === "内容" ? `artifact:branch:${branchId}` : undefined, factCount: pathType === "领域信息" ? branchId : undefined, node: makeStory(`${prefix}-output`, pathType === "领域信息" ? "domain-output" : "candidate-output", "result", { artifactRef: pathType === "内容" ? `artifact:branch:${branchId}` : undefined }) },
- outcome: { status: branchId % 3 === 0 ? "discarded" : "merged", label: branchId % 3 === 0 ? "未采用" : "已采用" },
- detailRef: `round:${roundIndex}:branch:${branchId}`,
- };
- }
- export function makeRound(roundIndex: number, branchCount = 2, batchCount = 1): RoundStoryV8 {
- const branches = Array.from({ length: branchCount }, (_, index) => makeBranch(roundIndex, index + 1));
- const batches: MultipathDecisionBatch[] = Array.from({ length: batchCount }, (_, index) => {
- const branchIds = branches.filter((_, branchIndex) => branchIndex % batchCount === index).map((branch) => branch.branchId);
- const id = `multipath-${roundIndex}-${index + 1}`;
- const decision = `第 ${index + 1} 批决策`;
- const node = makeStory(id, "multipath-decision", "decision", { card: { primary: { label: "决定", value: decision }, secondary: [{ key: "branches", label: "比较方案", value: branchIds.map((branchId) => `方案 ${branchId}`).join("、") }] } });
- node.decision = makeDecision(id, "multipath-decision", node.card);
- return { id, recordId: roundIndex * 100 + index, roundIndex, branchIds, decision, reasoning: "来自主 Agent 真实决策记录", completeness: "complete", missingBranchIds: [], branchOutcomes: branchIds.map((branchId) => ({ branchId, status: branches[branchId - 1].status, label: branches[branchId - 1].outcome.label })), node, decisionProjection: node.decision };
- });
- const convergenceBatches: ConvergenceBatch[] = batches.length ? batches.map((decision, index) => ({
- id: `convergence-${roundIndex}-${index + 1}`,
- roundIndex,
- branchIds: decision.branchIds,
- review: {
- id: `multipath-review-${roundIndex}-${index + 1}`,
- eventId: roundIndex * 1000 + index,
- roundIndex,
- branchIds: decision.branchIds,
- branchConclusions: decision.branchIds.map((branchId) => ({ branchId, conclusion: "通过", summary: `方案 ${branchId} 满足评审要求` })),
- comparison: "候选方案各有侧重",
- recommendation: `建议结合第 ${index + 1} 批评审`,
- status: "completed",
- detailRef: `event:${roundIndex * 1000 + index}`,
- decisionProjection: makeDecision(`multipath-review-${roundIndex}-${index + 1}`, "multipath-review", { primary: { label: "评审结论", value: "候选方案各有侧重" }, secondary: [{ key: "recommendation", label: "评审建议", value: `建议结合第 ${index + 1} 批评审` }] }),
- },
- decision,
- association: "exact-branch-set-and-time",
- })) : [{
- id: `convergence-${roundIndex}-missing`,
- roundIndex,
- branchIds: branches.map((branch) => branch.branchId),
- missingReview: makeStory(`r${roundIndex}-missing-review`, "multipath-review", "missing"),
- missingDecision: makeStory(`r${roundIndex}-missing-decision`, "multipath-decision", "missing"),
- association: "records-missing",
- }];
- return {
- id: `round:${roundIndex}`, roundIndex, state: "passed",
- summary: { title: `第 ${roundIndex} 轮`, goal: { headline: `本轮目标 ${roundIndex}`, itemCount: 3, previewItems: ["补充执行形式", "完善脚本元素", "校正结构约束"], truncated: true }, approach: { mode: "分工", candidateCount: branchCount, plannedPathCount: branchCount, contentBranchCount: branches.filter((branch) => branch.pathType === "内容").length, domainBranchCount: branches.filter((branch) => branch.pathType === "领域信息").length, summary: `分工:${branchCount} 个候选方案` }, decision: { batchCount, branchIds: [...new Set(batches.flatMap((batch) => batch.branchIds))].sort((a, b) => a - b), summary: batchCount ? `${batchCount} 批主 Agent 决策` : "未记录主 Agent 多路决策" }, output: { mergedContentCount: branches.filter((branch) => branch.pathType === "内容" && branch.status === "merged").length, domainFactCount: branches.filter((branch) => branch.pathType === "领域信息").reduce((sum, branch) => sum + (branch.output.factCount || 0), 0), scriptSummary: `形成第 ${roundIndex} 轮内容产出`, domainSummary: "没有新增领域事实记录", summary: `形成第 ${roundIndex} 轮产出` } },
- goal: makeStory(`r${roundIndex}-goal`, "goal"), plan: { mode: "分工", paths: branches.map((branch) => ({ target: branch.title })), currentOnly: true, runtimeRevisionCount: 0, runtimeRevisionRefs: [], node: makeStory(`r${roundIndex}-plan`, "plan") }, branches, convergenceBatches, overallEvaluation: makeStory(`r${roundIndex}-evaluation`, "round-evaluation", "decision", { title: "主脚本整体评审" }), result: makeStory(`r${roundIndex}-result`, "round-result", "result"), detailRef: `round:${roundIndex}`,
- };
- }
- export function makeView(roundCount = 2, branchCount = 2, batchCount = 1, status = "success"): ExecutionViewV8 {
- const id = String(7000 + roundCount * 17 + branchCount);
- return { schemaVersion: "8", capturedAt: "2026-07-13T08:00:00Z", dataShape: "current", header: { id, title: "脚本构建", status, model: "test-model", currentRound: roundCount || undefined, createdAt: "2026-07-13T07:00:00Z", direction: { currentValue: "制作一个清晰可执行的脚本" } }, objective: makeStory("objective", "objective", "decision", { title: "创作目标", card: { primary: { label: "目标", value: "制作一个清晰可执行的脚本" }, secondary: [] } }), rounds: Array.from({ length: roundCount }, (_, index) => makeRound(index + 1, branchCount, batchCount)), finalResult: { id: "run:final-result", role: "final-result", kind: "result", title: "最终结果", status, terminal: status !== "running", roundCount, branchCounts: { total: roundCount * branchCount, merged: roundCount * branchCount, parked: 0, discarded: 0, open: 0 }, summary: "本次构建已经形成当前主脚本。", artifact: { snapshotRef: "artifact:base:current", paragraphCount: 3, elementCount: 5, linkCount: 4, historicalVersion: "current-only" }, detailRef: "artifact:base:current" }, unassigned: { runtimeEvents: [], businessRecords: [] }, warnings: [] };
- }
- function roleTitle(role: StoryRole) { return ({ "planning-analysis": "创作规划解析", objective: "创作目标", goal: "本轮目标", plan: "实现规划", "branch-task": "实现任务", "data-decision": "数据取舍", "candidate-output": "候选脚本", "domain-output": "领域事实", "multipath-review": "多方案评审", "multipath-decision": "主 Agent 多路决策", "round-evaluation": "主脚本整体评审", "round-result": "本轮产出", "final-result": "最终结果" } as Record<StoryRole, string>)[role]; }
|