Elffuss BraidHub

kikocis/utopia public

dev · 5.8 KiBRawPermalink
1/* 等人点头的事实(docs/decisions/0015)。
2 一句 remember 抽出的三元组先进待确认队列,不上图;人在这里点头它才进账本。
3 **原句在上,三元组在下**:只列三元组等于要人凭空判断它对不对——
4 实测里 `Acme --?--> 深圳` 那条,人一看原句就知道该拒。
5 两处共用同一行组件:Review 页的「待确认」一档,与 Chat 里跟在 remember 步骤后面的那张卡。 */
6import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
7import { api, type PendingFactItem } from "../api";
8import { S } from "../i18n";
9import { toast } from "../toast";
10
11function ym(iso: string | null): string | null {
12 return iso ? iso.slice(0, 7) : null;
13}
14
15/** 记忆落库时正文前面带着 `[YYYY-MM-DD HH:MM] `(`memory::append_episode` 加的时间戳)。
16 * 卡片上要的是那句话本身,时间戳是索引用的,剥掉 */
17function sentence(quote: string): string {
18 return quote.replace(/^\[\d{4}-\d{2}-\d{2}(?: \d{2}:\d{2})?\]\s*/, "");
19}
20
21function objectText(f: PendingFactItem): string {
22 if (f.object_name) return f.object_name;
23 const v = f.object_value;
24 if (!v) return "?";
25 if (v.summary) return v.summary;
26 const val = v.value === undefined || v.value === null ? "?" : String(v.value);
27 return v.unit ? `${val} ${v.unit}` : val;
28}
29
30/** 点头是写图的动作,Editor 起步——与服务端 `require_kb(Role::Editor)` 同一口径。
31 * Viewer 看得见提议、看不见按钮:按钮亮着却点不动,等于让人猜自己有没有权限。
32 * 查询键与 Library 页共用(`kbOne`),不多打一次接口 */
33export function useCanDecide(kbId: string | undefined): boolean {
34 const kbDetail = useQuery({
35 queryKey: ["kbOne", kbId],
36 queryFn: () => api.kbDetail(kbId!),
37 enabled: !!kbId,
38 });
39 return ["editor", "admin", "owner"].includes(kbDetail.data?.my_role ?? "");
40}
41
42export function PendingFactRow({
43 fact,
44 busy,
45 canDecide,
46 onConfirm,
47 onReject,
48}: {
49 fact: PendingFactItem;
50 busy: boolean;
51 canDecide: boolean;
52 onConfirm: () => void;
53 onReject: () => void;
54}) {
55 const from = ym(fact.valid_from);
56 const to = ym(fact.valid_to);
57 const range = from || to ? `${from ?? "…"} → ${to ?? S.review.ongoing}` : null;
58 return (
59 <div className="glass rounded-xl p-4">
60 {/* 原句先出。它是人自己说的,判断的依据就是它 */}
61 <p className="text-xs text-neutral-400 italic">“{sentence(fact.quote)}”</p>
62 <div className="mt-2.5 flex items-center gap-2 flex-wrap">
63 <span className="text-sm font-medium text-white">{fact.subject_name}</span>
64 <span className="text-xs text-neutral-500">
65 —{" "}
66 {fact.predicate_label ? (
67 <span>{fact.predicate_label}</span>
68 ) : (
69 /* 本体里没有这个关系:显示原话,斜体标明它不是词表里的词(0010) */
70 <span
71 className="italic text-neutral-600"
72 title={S.review.pendingNoPredicate}
73 >
74 {fact.proposed_predicate ?? S.graph.unknownPredicate}
75 </span>
76 )}{" "}
77 →
78 </span>
79 <span className="text-sm font-medium text-white">{objectText(fact)}</span>
80 {range && <span className="text-xs text-neutral-500">({range})</span>}
81 {!fact.predicate_label && (
82 <span className="u-chip u-chip-warn ml-auto">{S.review.pendingNoPredicateChip}</span>
83 )}
84 </div>
85 <div className="mt-3 flex items-center gap-2">
86 {fact.proposed_by_name && (
87 <span className="text-[11px] text-neutral-600">
88 {S.review.pendingSaidBy(fact.proposed_by_name)}
89 </span>
90 )}
91 {canDecide && (
92 <div className="ml-auto flex gap-2">
93 <button
94 className="u-btn u-btn-ghost px-3 py-1.5 text-xs text-[var(--u-danger)]"
95 disabled={busy}
96 onClick={onReject}
97 >
98 {S.review.reject}
99 </button>
100 <button
101 className="u-btn u-btn-ghost px-3 py-1.5 text-xs"
102 disabled={busy}
103 onClick={onConfirm}
104 >
105 {S.review.confirm}
106 </button>
107 </div>
108 )}
109 </div>
110 </div>
111 );
112}
113
114/** 跟在 remember 步骤后面的确认卡。
115 * 抽取是异步的,卡片在任务完成时才长出来(SSE `pending` 事件让查询失效重取);
116 * 回放旧会话时同样按 chunk 取——还有没点头的就照样显示,都处理完了就不占地方。 */
117export function NodCard({ kbId, chunkId }: { kbId: string; chunkId: string }) {
118 const queryClient = useQueryClient();
119 const canDecide = useCanDecide(kbId);
120 const q = useQuery({
121 queryKey: ["pending", kbId, chunkId],
122 queryFn: () => api.pendingForChunk(kbId, chunkId),
123 });
124 const decide = useMutation({
125 mutationFn: ({ id, action }: { id: string; action: "confirm" | "reject" }) =>
126 api.decidePending(kbId, id, action),
127 onError: (e: Error) => toast.error(e.message),
128 onSettled: () => {
129 queryClient.invalidateQueries({ queryKey: ["pending", kbId] });
130 queryClient.invalidateQueries({ queryKey: ["review", kbId] });
131 },
132 });
133 const items = q.data?.items ?? [];
134 if (items.length === 0) return null;
135 return (
136 <div className="my-2 space-y-2">
137 <div className="text-xs text-neutral-500">{S.review.nodCardTitle(items.length)}</div>
138 {items.map((f) => (
139 <PendingFactRow
140 key={f.id}
141 fact={f}
142 busy={decide.isPending && decide.variables?.id === f.id}
143 canDecide={canDecide}
144 onConfirm={() => decide.mutate({ id: f.id, action: "confirm" })}
145 onReject={() => decide.mutate({ id: f.id, action: "reject" })}
146 />
147 ))}
148 </div>
149 );
150}