World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Only the sources an answer cites are listed (#649)
WaylandYang489e86efa229
diff --git a/web/src/liveAnswer.test.ts b/web/src/liveAnswer.test.tsindex 443f2def..b827a40e 100644--- a/web/src/liveAnswer.test.ts+++ b/web/src/liveAnswer.test.ts@@ -1,21 +1,49 @@import { describe, expect, it } from "vitest";import type { Source } from "./api";-import { answeredWithoutSources, type Turn } from "./liveAnswer";+import { answeredWithoutSources, citedSources, type Turn } from "./liveAnswer";const answer = (over: Partial<Turn> = {}): Turn => ({role: "assistant",content: "以下是我找到的内容",...over,});-const source = { n: 1, kind: "chunk" } as unknown as Source;+const source = (n: number) => ({ n, kind: "chunk" }) as unknown as Source;+const six = [1, 2, 3, 4, 5, 6].map(source);++describe("citedSources", () => {+ it("lists only the sources the text points at, numbers unchanged", () => {+ const t = answer({ content: "营收增长 [2],毛利下降 [5]。", sources: six });+ expect(citedSources(t).map((s) => s.n)).toEqual([2, 5]);+ });++ it("reads adjacent and comma-separated markers", () => {+ const t = answer({ content: "见 [1][3] 与 [4, 6],另见 [2,5]", sources: six });+ expect(citedSources(t).map((s) => s.n)).toEqual([1, 2, 3, 4, 5, 6]);+ });++ it("lists nothing when a greeting cites nothing it retrieved", () => {+ const t = answer({ content: "你好!我是 Utopia 的知识助手。", sources: six });+ expect(citedSources(t)).toEqual([]);+ });++ it("ignores a number that matches no source", () => {+ const t = answer({ content: "见 [9]", sources: six });+ expect(citedSources(t)).toEqual([]);+ });+});describe("answeredWithoutSources (#547)", () => {it("marks a finished answer with no sources", () => {expect(answeredWithoutSources(answer({ sources: [] }), false)).toBe(true);});+ it("marks an answer that retrieved sources but cites none", () => {+ expect(answeredWithoutSources(answer({ sources: six }), false)).toBe(true);+ });+it("does not mark an answer that cites something", () => {- expect(answeredWithoutSources(answer({ sources: [source] }), false)).toBe(false);+ const t = answer({ content: "见 [1]", sources: [source(1)] });+ expect(answeredWithoutSources(t, false)).toBe(false);});it("waits for the stream to finish before deciding", () => {diff --git a/web/src/liveAnswer.ts b/web/src/liveAnswer.tsindex 3501ed18..0d5b7490 100644--- a/web/src/liveAnswer.ts+++ b/web/src/liveAnswer.ts@@ -28,16 +28,31 @@ export interface Turn {error?: string;}+/** 正文里真正引到的那几条来源。+ *+ * `sources` 是这一轮**检索到**的全部,不是回答**用到**的:打个招呼也可能顺手搜了+ * 一次,六条摘录挂在「你好」下面,读起来像是这句问候有六个出处。所以只列正文里+ * 出现过 `[n]` 的那几条,编号照原样不重排,与正文里的标记对得上。+ * `[1][2]`、`[1, 2]`、`[1,2]` 都认 */+export function citedSources(turn: Turn): Source[] {+ if (!turn.sources?.length) return [];+ const cited = new Set<number>();+ for (const m of turn.content.matchAll(/\[(\d+(?:\s*[,,]\s*\d+)*)\]/g)) {+ for (const n of m[1].split(/[,,]/)) cited.add(Number(n.trim()));+ }+ return turn.sources.filter((s) => cited.has(s.n));+}+/** 这条回答要不要挂「未引用任何来源」(#547)。*- * 判据只看数据:说完了、背后一条来源都没有,就挂——招呼、拒答挂着无害,- * 而「以下是我找到的内容」配零来源的那条,靠它露馅。还在流的不挂:来源是- * 增量到的,挂上又撤下比晚一点出现更糟。只有报错、一个字没说的那条也不挂,- * 它不是回答,红字已经交代了。回放时 `sources` 空数组存成 undefined,同样算零 */+ * 判据只看数据:说完了、正文一条来源都没引,就挂——招呼、拒答挂着无害,+ * 而「以下是我找到的内容」配零引用的那条,靠它露馅。检索到了却一条没引,+ * 同样算没有来源。还在流的不挂:来源是增量到的,挂上又撤下比晚一点出现更糟。+ * 只有报错、一个字没说的那条也不挂,它不是回答,红字已经交代了 */export function answeredWithoutSources(turn: Turn, live: boolean): boolean {if (turn.role !== "assistant" || live) return false;if (turn.error && !turn.content) return false;- return !turn.sources?.length;+ return citedSources(turn).length === 0;}/** 快照条目:纯数据,给渲染看。abort 不进快照——渲染不该顺手摸到它 */diff --git a/web/src/pages/Chat.tsx b/web/src/pages/Chat.tsxindex 4c1eecc2..590a723a 100644--- a/web/src/pages/Chat.tsx+++ b/web/src/pages/Chat.tsx@@ -59,6 +59,7 @@ import {import { convMarks, useLive, useUnread } from "../unread";import {answeredWithoutSources,+ citedSources,liveAnswer,type LiveHandle,type Turn,@@ -806,6 +807,7 @@ function TurnView({ turn, live }: { turn: Turn; live?: boolean }) {}const thinking = live && !turn.content && !turn.error;+ const cited = citedSources(turn);const lastStep = turn.steps?.[turn.steps.length - 1];return (@@ -854,10 +856,11 @@ function TurnView({ turn, live }: { turn: Turn; live?: boolean }) {`sources` 是随检索一次次增量发来的,跟着渲染的话,一份还在生长的清单就挂在一段还没写完的话下面,一边长一边把正文往上推。它是答案的落款,不是过程的一部分——过程已经由上面的轨迹交代了 */}- {/* 一个面板装多行(DESIGN.md 6):引用是同构的一组,悬停归行 */}- {!live && turn.sources && turn.sources.length > 0 && (+ {/* 一个面板装多行(DESIGN.md 6):引用是同构的一组,悬停归行。+ 只列正文引到的那几条(见 citedSources):检索到的不等于用到的 */}+ {!live && cited.length > 0 && (<div className="mt-2 glass rounded-panel divide-y divide-line">- {turn.sources.map((s) =>+ {cited.map((s) =>s.kind === "charter" ? (/* 手册引用:视觉上与数据引用隔离(BookOpen),跳排版好的 /docs 小节 */<Link