AskTurn.tsx
13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/** 回答区:欢迎页、一轮问答(问题、推理摘要、答案、图表、建议卡、追问)、用到的经验、点赞点踩。 */
import {
BookOpen, Database, History, Pin, RotateCcw, Sparkles, ThumbsDown, ThumbsUp, Workflow, XCircle, Zap,
} from "lucide-react";
import { useMemo, useState } from "react";
import { Badge, Collapse, Empty, Modal } from "@/components/ui";
import { go } from "@/lib/nav";
import { errText, onto } from "@/lib/onto";
import { cn } from "@/lib/utils";
import { toast } from "@/store/useAppStore";
import { ThinkingCard } from "./AgentTrace";
import { AskAnswer, DataRefView } from "./AskAnswer";
import { DataTable, RankChart } from "./DataTable";
import { SuggestedActionCard, SuggestedSopCard } from "./ActionCard";
import type { LiveTurn } from "./askTypes";
export function Welcome({ examples, onAsk }: { examples: string[]; onAsk: (q: string) => void }) {
const cards = [
{ icon: <BookOpen className="h-4 w-4" />, color: "#10b981", title: "查资料", desc: "指标怎么算、规则怎么定,从业务文档里找原文" },
{ icon: <Database className="h-4 w-4" />, color: "#0ea5e9", title: "查数据", desc: "现在的数是多少,按不同维度拆开看" },
{ icon: <Zap className="h-4 w-4" />, color: "#f43f5e", title: "给办法", desc: "该做什么动作、跑哪张流程卡,核对后一键发起" },
];
return (
<div className="mx-auto mt-6 max-w-2xl">
<Empty icon={<Sparkles className="h-10 w-10" />} title="问知识、问数据、问怎么办"
/>
<div className="mt-1 grid gap-2 sm:grid-cols-3">
{cards.map((c) => (
<div key={c.title} className="rounded-xl border border-line bg-surface p-3">
<p className="flex items-center gap-1.5 text-label font-semibold" style={{ color: c.color }}>{c.icon}{c.title}</p>
<p className="mt-1 text-label leading-relaxed text-muted">{c.desc}</p>
</div>
))}
</div>
<div className="mt-4 flex flex-wrap justify-center gap-2">
{examples.map((e) => (
<button key={e} className="chip hover:border-brand/40" onClick={() => onAsk(e)}>{e}</button>
))}
</div>
</div>
);
}
export function TurnView({ t, onAsk, active, onSelect, onReplay, disabled }: {
t: LiveTurn; onAsk: (q: string) => void; active: boolean; onSelect: () => void; onReplay: () => void; disabled: boolean;
}) {
const [dataOpen, setDataOpen] = useState(false);
const busy = t.status === "running" || !!t.replaying;
const clarify = !!t.plan?.clarify && !(t.plan?.delegations || []).length;
const memory = t.trace.memory;
const showAnswer = !t.replaying;
// 答案里引用到的那份数据(没有引用就取第一份):直接摆在答案下面,不用点开才看得到
const primary = t.data_refs.find((d) => d.cited) || t.data_refs[0];
return (
<div className="space-y-2">
<div className="flex justify-end">
<div className="max-w-[80%] whitespace-pre-wrap break-words rounded-2xl rounded-br-sm bg-brand px-3.5 py-2 text-body text-white">{t.question}</div>
</div>
<div className={cn(active && "rounded-xl ring-1 ring-brand/20")}>
<ThinkingCard trace={t.trace} running={busy} durationMs={t.duration_ms}
onOpenPanel={onSelect} onReplay={onReplay} canReplay={t.events.length > 0 && !disabled} />
</div>
{showAnswer && (
<div className="card p-4">
{t.status === "aborted" && (
<div className="mb-2 flex items-center gap-2 rounded-lg border border-warn/30 bg-warn/5 px-3 py-2 text-label">
<XCircle className="h-4 w-4 shrink-0 text-warn" />
<span className="flex-1">这次回答被中断了(点了停止、切换了对话或网络断开){t.answer ? ",下面是中断前的内容" : ""}。</span>
<button className="btn btn-sm" disabled={disabled} onClick={() => onAsk(t.question)}><RotateCcw className="h-3.5 w-3.5" /> 重新提问</button>
</div>
)}
{t.status === "error" && (
<div className="mb-2 flex items-center gap-2 rounded-lg border border-bad/30 bg-bad/5 px-3 py-2 text-label">
<XCircle className="h-4 w-4 shrink-0 text-bad" />
<span className="min-w-0 flex-1 break-words">没能完成回答:{t.error || "服务出错"}</span>
<button className="btn btn-sm" disabled={disabled} onClick={() => onAsk(t.question)}><RotateCcw className="h-3.5 w-3.5" /> 重试</button>
</div>
)}
{clarify && <Badge tone="bg-warn/10 text-warn">需要补充信息</Badge>}
{t.plan?.lane && (
<p className="mb-1.5 flex items-center gap-1.5 text-caption text-muted" title={t.plan.why}>
<Badge variant="info"><Zap className="h-3 w-3" />{t.plan.lane_label || "快速通道"}</Badge>
<span className="truncate">{t.plan.why}</span>
</p>
)}
{(t.answer || t.status === "running") && (
<AskAnswer text={t.answer} citations={t.citations} dataRefs={t.data_refs} streaming={t.status === "running"} />
)}
{t.status !== "running" && memory && <UsedMemory memory={memory} onOpen={onSelect} />}
{t.status !== "running" && primary && (primary.rows.length > 1 || primary.columns.length > 2) && (
<div className="mt-3 space-y-2">
<RankChart columns={primary.columns} rows={primary.rows} unit={primary.metric?.unit || ""} />
<DataTable columns={primary.columns} rows={primary.rows} name={primary.metric?.name || t.question.slice(0, 20)} />
</div>
)}
{t.status !== "running" && t.data_refs.length > 0 && (
<div className="mt-3">
<Collapse open={dataOpen} onToggle={() => setDataOpen((v) => !v)} icon={<Database className="h-3.5 w-3.5" />}
title="依据:口径与取数方式" summary={`${t.data_refs.length} 次取数,均经只读校验,可追溯到 SQL 与口径`}>
<div className="space-y-4">
{t.data_refs.map((d) => (
<div key={d.run_id}>
<p className="mb-1 text-label font-medium">
{d.metric?.name || "自由取数"} <span className="text-muted">· 取数记录 #{d.run_id}</span>
</p>
<DataRefView d={d} />
</div>
))}
</div>
</Collapse>
</div>
)}
{/* 动作卡:核对参数后提交审批;流程卡:核对参数后本人确认即发起 */}
{t.actions.length + t.sops.length > 0 && t.status !== "running" && (
<div className="mt-3 space-y-2">
{t.actions.map((c, i) => <SuggestedActionCard key={`a${i}`} card={c} turnId={t.id} index={i} evidence={
t.data_refs.slice(0, 5).map((d) => ({
kind: "data", ref: d.sql || "",
text: `${d.metric?.name || "自由取数"}:${d.rows.length} 行${d.calibre?.text ? `;口径:${d.calibre.text}` : ""}`.slice(0, 900) }))} />)}
{t.sops.map((c, i) => <SuggestedSopCard key={`s${i}`} card={c} turnId={t.id} index={i} />)}
</div>
)}
{t.status === "done" && t.follow_ups.length > 0 && (
<div className="mt-3 flex flex-wrap items-center gap-1.5">
<span className="text-label text-muted">接着问:</span>
{t.follow_ups.map((q) => (
<button key={q} className="chip text-label hover:border-brand/40" disabled={disabled} onClick={() => onAsk(q)}>{q}</button>
))}
</div>
)}
{t.status === "done" && t.id && <Feedback turnId={t.id} question={t.question} initial={t.rating || 0} />}
</div>
)}
</div>
);
}
/** 本次参考的经验:用了哪些以前的问答、哪些组织记忆。觉得哪条不对,当场点掉,它就不再被参考。 */
function UsedMemory({ memory, onOpen }: { memory: { cases: any[]; facts?: any[]; fast?: string }; onOpen: () => void }) {
const [gone, setGone] = useState<string[]>([]);
const cases = (memory.cases || []).filter((c) => c.status === "verified");
const facts = memory.facts || [];
if (!cases.length && !facts.length) return null;
const drop = async (kind: "case" | "fact", id: string) => {
try {
if (kind === "case") await onto.saveMemCase(id, { status: "deprecated" } as any);
else await onto.saveMemFact(id, { status: "deprecated" } as any);
setGone((g) => [...g, id]);
toast("ok", "已标记为不对", "以后的回答不会再参考它");
} catch (e) { toast("error", "操作失败", errText(e)); }
};
return (
<div className="mt-2 flex flex-wrap items-center gap-1.5 text-caption text-muted">
<button className="inline-flex items-center gap-1 hover:text-brand" onClick={onOpen}>
<History className="h-3.5 w-3.5" />{memory.fast ? "复用了已验证案例的分析路径:" : "本次参考的经验:"}
</button>
{[...cases.map((c) => ({ kind: "case" as const, id: c.id, text: `问答「${c.question}」` })),
...facts.map((f) => ({ kind: "fact" as const, id: f.id, text: f.text }))].map((x) => (
<span key={x.id} className={cn("chip max-w-[18rem]", gone.includes(x.id) && "line-through opacity-50")} title={x.text}>
<span className="truncate">{x.text}</span>
{!gone.includes(x.id) && (
<button className="shrink-0 text-muted hover:text-bad" title="这条不对,以后别再参考" onClick={() => void drop(x.kind, x.id)}>
<XCircle className="h-3 w-3" />
</button>
)}
</span>
))}
</div>
);
}
function Feedback({ turnId, question, initial }: { turnId: string; question: string; initial: number }) {
const [rating, setRating] = useState(initial);
const [open, setOpen] = useState(false);
const [comment, setComment] = useState("");
const [correction, setCorrection] = useState("");
const [pinned, setPinned] = useState(false);
const send = async (r: number, c = "", corr = "") => {
try {
const out = await onto.feedback(turnId, r, c, corr);
setRating(r);
if (r > 0) {
const n = out.learned?.sql || 0;
toast("ok", "已记住这个回答", `下次遇到类似问题会直接参考它${n > 0 ? `;这次用到的 ${n} 条取数 SQL 已进入问数示例库` : ""}`);
} else {
toast("ok", "已提交纠错", out.review_item ? "实施人员审核后,会回流成口径、同义词和评测用例,同类错误不再出现" : undefined);
}
} catch (e) { toast("error", "反馈失败", errText(e)); }
};
const pin = async () => {
try {
await onto.pinQuestion(question);
setPinned(true);
toast("ok", "已加入常用问题", "工作台会置顶显示,一键就能再问");
} catch (e) { toast("error", "保存失败", errText(e)); }
};
const tips = useMemo(() => ({
up: rating > 0 ? "已标记为好回答" : "回答得好:记住它,下次类似问题更快更准",
down: "回答有问题:告诉系统哪里不对",
}), [rating]);
return (
<div className="mt-3 flex flex-wrap items-center gap-1 border-t border-line pt-2">
<button className={cn("btn btn-ghost btn-sm", rating > 0 && "text-ok")} onClick={() => void send(1)} title={tips.up} aria-label="有用">
<ThumbsUp className="h-3.5 w-3.5" />
</button>
<button className={cn("btn btn-ghost btn-sm", rating < 0 && "text-bad")} onClick={() => setOpen(true)} title={tips.down} aria-label="有问题">
<ThumbsDown className="h-3.5 w-3.5" />
</button>
<span className="mx-1 h-4 w-px bg-line" />
<button className="btn btn-ghost btn-sm text-muted" disabled={pinned} onClick={() => void pin()} title="加入工作台的常用问题">
<Pin className="h-3.5 w-3.5" /> {pinned ? "已加入常用问题" : "存为常用问题"}
</button>
<button className="btn btn-ghost btn-sm text-muted" onClick={() => go("sop", { draft: question })} title="让 AI 按这个问题起草一张流程卡,以后定时自动跑">
<Workflow className="h-3.5 w-3.5" /> 做成流程卡
</button>
<Modal open={open} onClose={() => setOpen(false)} title="这个回答哪里不对?"
desc="会交给管理员确认,确认后系统以后按正确的答"
footer={<>
<button className="btn" onClick={() => setOpen(false)}>取消</button>
<button className="btn btn-primary" disabled={!comment.trim()} onClick={() => {
setOpen(false);
void send(-1, comment, correction);
}}>提交纠错</button>
</>}>
<div className="space-y-3">
<textarea className="input min-h-[70px]" placeholder="问题描述(必填):口径不对 / 数字错了 / 没回答到点上…"
value={comment} onChange={(e) => setComment(e.target.value)} />
<textarea className="input min-h-[70px]" placeholder="正确的说法(选填):例如「营业额就是成交金额」「缺货要算上在途」"
value={correction} onChange={(e) => setCorrection(e.target.value)} />
</div>
</Modal>
</div>
);
}