Files
writer-work-flow/apps/web/components/chain/ChainPage.tsx
Yaojia Wang 9a623688e8 fix(qa): 前端错误文案——按 API 信封 surface 真实错因,去掉通用 toast
新增 friendlyFromApiError(apiError):兼容业务信封 {error:{code,message}}(ARCH §7.1)
与 FastAPI 422 {detail:[...]},422 映射为友好 VALIDATION 文案(不泄露技术细节)。
接入三处此前吞掉错因、永显通用文案的调用点:
- useOutline(QA #2):原 env.error.code 漏读 422 detail,永显「大纲生成失败」。
- ChainPage / ChainAdjudication(QA #11):原 friendlyError(undefined) 永显通用 toast,
  现 surface 真实 code(如 LLM_UNAVAILABLE→去设置 provider / CONFLICT→提示裁决)。
+ messages.test.ts 加 3 例(业务码带动作 / 422→VALIDATION / 未知形兜底)。
门禁绿:vitest / tsc / eslint 干净。
2026-06-24 18:01:15 +02:00

127 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useCallback, useEffect, useMemo, useState } from "react";
import { AppShell } from "@/components/AppShell";
import { useToast } from "@/components/Toast";
import { api } from "@/lib/api/client";
import { chainPhase, chainResultView, type ChainKind } from "@/lib/chain/chain";
import { friendlyFromApiError } from "@/lib/errors/messages";
import { useJobPoll } from "@/lib/jobs/useJobPoll";
import type { ProjectResponse } from "@/lib/api/types";
import { ChainAdjudication } from "./ChainAdjudication";
import { ChainProgress } from "./ChainProgress";
import { ChainStarter } from "./ChainStarter";
interface ChainPageProps {
project: ProjectResponse;
}
// 多章工作流链页Scope B B1发起链(POST run)→job 轮询进度→interrupt 命中则读该章冲突裁决→
// POST resume 续跑。复用 useJobPoll轮询+ ConflictCard/decisions裁决+ friendlyError文案
// 链是后台任务,走 job 轮询而非 SSE。awaiting 时停轮询(后端把 job 置 awaiting_input
// narrowJob 抹平成 queued 会无限轮询)→ 裁决面板 resume 后再重启轮询同一 job。
export function ChainPage({ project }: ChainPageProps) {
const poll = useJobPoll();
const toast = useToast();
const [jobId, setJobId] = useState<string | null>(null);
const [starting, setStarting] = useState(false);
const job = poll.job;
const result = useMemo(
() => (job ? chainResultView(job) : null),
[job],
);
const awaitingChapter = result?.awaitingChapter ?? null;
const phase = chainPhase(poll.status, awaitingChapter);
// awaiting 命中:停轮询(后端 job=awaiting_input 被 narrowJob 抹成 queued 会空转)。
useEffect(() => {
if (phase === "awaiting") poll.reset();
}, [phase, poll]);
const onStart = useCallback(
async (
chainKey: ChainKind,
startChapterNo: number,
count: number,
): Promise<void> => {
setStarting(true);
try {
const { data, error } = await api.POST(
"/projects/{project_id}/chains/{chain_key}/run",
{
params: {
path: { project_id: project.id, chain_key: chainKey },
},
body: { start_chapter_no: startChapterNo, count },
},
);
if (error || !data) {
toast(friendlyFromApiError(error).text, "error");
return;
}
setJobId(data.job_id);
poll.poll(data.job_id);
} catch {
toast("发起链请求异常,请检查网络。", "error");
} finally {
setStarting(false);
}
},
[project.id, poll, toast],
);
// 续跑成功:重启轮询同一 jobresume 已 202图从检查点续
const onResumed = useCallback((): void => {
if (jobId) poll.poll(jobId);
}, [jobId, poll]);
const showProgress = jobId !== null && result !== null;
// 链运行中/等待裁决时禁止重复发起。
const busy =
starting ||
(jobId !== null && (phase === "running" || phase === "awaiting"));
return (
<AppShell
title={project.title}
subtitle="多章工作流链"
projectId={project.id}
activeNav="chains"
>
<div className="mx-auto flex w-full max-w-3xl flex-col gap-6 p-6">
<ChainStarter
onStart={(k, s, c) => void onStart(k, s, c)}
disabled={busy}
/>
{showProgress && result ? (
<ChainProgress phase={phase} progress={poll.progress} result={result} />
) : null}
{phase === "awaiting" &&
awaitingChapter !== null &&
jobId !== null ? (
<ChainAdjudication
projectId={project.id}
jobId={jobId}
chapterNo={awaitingChapter}
onResumed={onResumed}
/>
) : null}
{phase === "failed" ? (
<p className="text-sm text-conflict">
{poll.error ?? "链运行失败,请重试。"}
</p>
) : null}
{phase === "completed" ? (
<p className="text-sm text-pass"></p>
) : null}
</div>
</AppShell>
);
}