Skip to content

World's first open-source enterprise world model.

A conversation says where it stands (#608)

WaylandYange943c36a241d
diff --git a/web/src/pages/Chat.tsx b/web/src/pages/Chat.tsx
index 41bf9bb4..539b2876 100644
--- a/web/src/pages/Chat.tsx
+++ b/web/src/pages/Chat.tsx
@@ -46,6 +46,7 @@ import { useKb, useKbId } from "../kb";
import {
Button,
cn,
+ ConvMark,
DangerConfirm,
IconButton,
Input,
@@ -55,6 +56,7 @@ import {
ROW_HOVER,
Textarea,
} from "../ui";
+import { convMarks, useLive, useUnread } from "../unread";
import { liveAnswer, type LiveHandle, type Turn } from "../liveAnswer";
import { NodCard } from "./PendingFacts";
import { NextStep, nextStep, useReadiness } from "./NextStep";
@@ -120,10 +122,21 @@ export function Chat() {
// 三点菜单展开的是哪一条。同时只开一个
// 「最近」这一组收起来没有。默认展开:左栏本来就是为了看见这些会话
const [recentOpen, setRecentOpen] = useState(true);
+ /* 左栏每一条会话的记号(正在写 / 写完了还没看)。两张表都活在组件外面——
+ "写完了"常常发生在这个组件已经卸载的时候(见 unread.ts 开头) */
+ const liveIds = useLive();
+ const unreadIds = useUnread();
const scopeRef = useRef<HTMLDivElement>(null);
const bottomRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
+ /* 打开哪一场就等于看过哪一场:记号消掉,并且告诉 unread「此刻看的是它」——
+ 正看着的那一场写完时不该变成未读。传 null(新对话、删掉了当前这场)
+ 同样有意义:那之后写完的任何一场都算未读 */
+ useEffect(() => {
+ convMarks.view(activeId);
+ }, [activeId]);
+
// 作用域弹层:点外面 / Esc 关闭(与 ui/Dropdown 同惯例)
useEffect(() => {
if (!scopeOpen) return;
@@ -309,6 +322,8 @@ export function Chat() {
const removeConversation = async (id: string) => {
await conversationsApi.remove(kb!.id, id);
+ // 记号跟着会话走,否则这个 id 会一直留在浏览器的那张表里
+ convMarks.forget(id);
if (sessionStorage.getItem(lastKey(kb!.id)) === id) {
sessionStorage.removeItem(lastKey(kb!.id));
}
@@ -549,6 +564,17 @@ export function Chat() {
) : (
<Row
density="nav"
+ icon={
+ <ConvMark
+ state={
+ liveIds.has(c.id)
+ ? "live"
+ : unreadIds.has(c.id)
+ ? "unread"
+ : "rest"
+ }
+ />
+ }
active={c.id === activeId}
className="pr-8"
onClick={() => openConversation(c.id)}
@@ -607,7 +633,11 @@ export function Chat() {
{/* 文字从 20 起:栏的 px-2(8)加行自己的 px-3(12),与「最近」和
上面每条会话的标题同一条线。写成 px-2 就落在 16,差那 4px 一眼看得出 */}
{convs.data?.conversations.length === 0 && (
- <p className="px-3 py-2 text-small text-ink-2">{S.ask.noConversations}</p>
+ /* 34 = 行内距 12 + 图标 14 + 间距 8:这句话与上面每一条会话的
+ 标题同一条竖线,而不是自己另起一列 */
+ <p className="py-2 pl-[34px] pr-3 text-small text-ink-2">
+ {S.ask.noConversations}
+ </p>
)}
</div>
)}
diff --git a/web/src/styles.css b/web/src/styles.css
index 147d38e3..1b6ded15 100644
--- a/web/src/styles.css
+++ b/web/src/styles.css
@@ -49,6 +49,7 @@
--color-contest: var(--u-contest);
--color-violet: var(--u-violet);
--color-accent: var(--u-accent);
+ --color-unread: var(--u-unread);
--color-bar: var(--u-bar);
/* 圆角四档(规矩 3)。**按角色命名,数由预设给**——名字说得出「这是个面板」,
@@ -218,6 +219,11 @@
--u-on-danger: #ffffff;
/* 浮岛的投影:暗底下要重,浅底下要轻 */
--u-shadow: rgba(0, 0, 0, 0.5);
+ /* 未读那一枚记号的蓝。**chrome 本来零色相,这是唯一一处例外**,而且是
+ 故意的:ok / warn / danger 说的是"这件事好不好",未读说的是"这里有你
+ 还没看过的东西",借用三档语义色里的任何一档都会被读成一个判断。蓝在
+ 这套语汇里还没有别的意思,于是它能只说这一件事。 */
+ --u-unread: #4d8dff;
/* 浮起两档。**从前这一档根本不在令牌里**:页面写的是 Tailwind 自带的
`shadow-lg` / `shadow-2xl`,它们是按浅色界面调的黑影,压在近黑的底上
几乎看不见,于是没人发现界面里还有一套不归自己管的颜色——翻成纸底之后
@@ -367,6 +373,8 @@
在纸上要收一档。深灰仍然与指到的那块浅灰分得清清楚楚 */
--u-pill-invert: #3f3f3f;
--u-shadow: rgba(0, 0, 0, 0.18);
+ /* 纸底上同一枚蓝要压深一档才立得住(浅蓝在白纸上是一团雾) */
+ --u-unread: #2563eb;
--u-scrub-head-ring: 0 0 0 1.5px rgba(var(--u-ground-rgb), 0.95);
--u-ok-rgb: 26, 127, 75;
--u-warn-rgb: 154, 106, 18;
@@ -1073,6 +1081,45 @@ body::before {
color: var(--u-text);
}
+/* 正在写的那一场,左栏那一格里跳三个点。**它替的是那枚空心小方块**,不是
+ 加在它旁边——同一格、同一个位置,一列会话的字因此不会因为某一场在写而错开。
+ 点用文字的次色:这一格说的是状态,不是内容,不该比标题还重。 */
+.u-dots {
+ display: inline-flex;
+ align-items: center;
+ gap: 2px;
+}
+.u-dots > span {
+ width: 3px;
+ height: 3px;
+ border-radius: 9999px;
+ background: var(--u-text-2);
+ animation: u-dots-kf 1.1s ease-in-out infinite;
+}
+.u-dots > span:nth-child(2) {
+ animation-delay: 0.18s;
+}
+.u-dots > span:nth-child(3) {
+ animation-delay: 0.36s;
+}
+@keyframes u-dots-kf {
+ 0%,
+ 60%,
+ 100% {
+ opacity: 0.3;
+ }
+ 30% {
+ opacity: 1;
+ }
+}
+/* 设了减少动效就不跳:三个点站着也说得清"这一场在写" */
+@media (prefers-reduced-motion: reduce) {
+ .u-dots > span {
+ animation: none;
+ opacity: 0.65;
+ }
+}
+
/* 引用定位闪现:进场强光收敛到持有亮度(运动比静态边框更抓眼)。
fill-mode both 让 100% 帧在类存续期间保持;移除类后由元素自身的
transition 淡回普通卡片 */
diff --git a/web/src/ui/index.tsx b/web/src/ui/index.tsx
index eb81e579..3be88258 100644
--- a/web/src/ui/index.tsx
+++ b/web/src/ui/index.tsx
@@ -1424,6 +1424,39 @@ export function Row({
都是它。**与列表里的行同一副样子**(Row density="nav",圆角、缩进)——
钉在底部的靠外面那条分隔线说明「这几个是常驻的」,不靠把行本身画成方的。
计数给了就显示,0 也显示(灰一档):队列清空了是个有意义的事实。 */
+/** 一条会话此刻站在哪儿,画在左栏行的图标格里(14 宽,与别处的图标同一格,
+ * 所以标题仍落在同一条竖线上,见 rowClass 的说明)。
+ *
+ * 三档:`rest` 空心小方块——它本身也是个记号,说明这一列每一行都是一场对话;
+ * `live` 三个点在跳;`unread` 实心蓝方块,写完了而你还没回来看。
+ * **形状一档不变**(都是 10 的方块、`rounded-cell`),只有里外与颜色在变——
+ * 与图上那块名字牌同一个道理:换状态不换形状,余光里才认得出是同一样东西。 */
+export function ConvMark({ state }: { state: "rest" | "live" | "unread" }) {
+ return (
+ <span
+ className="inline-flex h-3.5 w-3.5 shrink-0 items-center justify-center"
+ aria-hidden
+ >
+ {state === "live" ? (
+ <span className="u-dots">
+ <span />
+ <span />
+ <span />
+ </span>
+ ) : (
+ <span
+ className={cn(
+ // 12 的方块配 `rounded-cell`(4)才读成"圆角方块":10 上的 4
+ // 已经接近一个圆,而圆点在这套语汇里是状态灯,不是一场对话
+ "h-3 w-3 rounded-cell border",
+ state === "unread" ? "border-unread bg-unread" : "border-ink-2",
+ )}
+ />
+ )}
+ </span>
+ );
+}
+
export function RailItem({
active,
icon,
diff --git a/web/src/unread.ts b/web/src/unread.ts
new file mode 100644
index 00000000..62c7f352
--- /dev/null
+++ b/web/src/unread.ts
@@ -0,0 +1,110 @@
+// 左栏那一列会话此刻各自处在什么状态:正在写 / 写完了你还没看 / 看过了。
+//
+// **为什么另起一个模块,而不是放进 Chat。** 回答不随离开而停(见 liveAnswer 的
+// 开头):人问完一句话就切去图谱页,Chat 组件卸载,那条流还在跑。"写完了"这个
+// 时刻因此常常发生在没有任何组件挂着的时候——判断挂在组件里就等于漏掉它正要
+// 记录的那一类事。这里在模块加载时就订上 liveAnswer,此后无人问津也照记。
+//
+// **"未读"是客户端的事,不进库。** 它说的是"这台机器上的这个人还没看过这一条",
+// 换台机器重新算一遍反而是对的;落进 conversations 表就成了跨设备的一个状态,
+// 那是另一件事(要的话得有 read_at 和一条接口)。放 localStorage:刷新还在,
+// 清了也只是少一个记号,不丢任何内容。
+import { useSyncExternalStore } from "react";
+import { liveAnswer } from "./liveAnswer";
+
+const KEY = "utopia.unread";
+
+function load(): string[] {
+ try {
+ const raw = localStorage.getItem(KEY);
+ const v = raw ? JSON.parse(raw) : [];
+ return Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
+ } catch {
+ return [];
+ }
+}
+
+const ids = new Set<string>(load());
+const listeners = new Set<() => void>();
+
+/* useSyncExternalStore 靠引用相等跳过重渲染,所以两张表各自留一份快照,
+ 只在**真的变了**的时候换新对象:一场回答逐字增长期间 liveAnswer 每个
+ token 都在 emit,而这两张表在那期间一动不动,左栏不该跟着重画。 */
+let unreadSnap: ReadonlySet<string> = new Set(ids);
+let liveSnap: ReadonlySet<string> = new Set<string>();
+
+function save() {
+ try {
+ localStorage.setItem(KEY, JSON.stringify([...ids]));
+ } catch {
+ /* 隐私模式下存不了:这一会话内照常工作,刷新后从零开始 */
+ }
+}
+
+function emitUnread() {
+ unreadSnap = new Set(ids);
+ save();
+ for (const l of listeners) l();
+}
+
+function same(a: ReadonlySet<string>, b: ReadonlySet<string>): boolean {
+ if (a.size !== b.size) return false;
+ for (const x of a) if (!b.has(x)) return false;
+ return true;
+}
+
+/** 此刻正看着的那一场。它写完时不该变成"未读"——人就在那儿看着 */
+let viewing: string | null = null;
+
+liveAnswer.subscribe(() => {
+ const now = new Set<string>();
+ for (const e of liveAnswer.get()) {
+ if (e.streaming && e.conversationId) now.add(e.conversationId);
+ }
+ /* 上一拍在流、这一拍不在了 = 那一场刚写完(正常结束、出错、或人按了停止,
+ 三者对左栏是同一件事:它不再动了,而你可能没在看)。liveAnswer 的条目
+ 结束后仍留在表里(只把 streaming 落下来),所以这个差集是可靠的。 */
+ let changed = false;
+ for (const id of liveSnap) {
+ if (now.has(id) || id === viewing) continue;
+ if (!ids.has(id)) {
+ ids.add(id);
+ changed = true;
+ }
+ }
+ if (!same(now, liveSnap)) {
+ liveSnap = now;
+ if (!changed) for (const l of listeners) l();
+ }
+ if (changed) emitUnread();
+});
+
+export const convMarks = {
+ unread: (): ReadonlySet<string> => unreadSnap,
+ live: (): ReadonlySet<string> => liveSnap,
+ subscribe(l: () => void) {
+ listeners.add(l);
+ return () => {
+ listeners.delete(l);
+ };
+ },
+ /** 打开了某一场(传 null = 谁也没打开,比如新对话)。打开就是看过了 */
+ view(conversationId: string | null) {
+ viewing = conversationId;
+ if (conversationId && ids.delete(conversationId)) emitUnread();
+ },
+ /** 会话删了,记号跟着走——否则这个 id 永远留在存储里 */
+ forget(conversationId: string) {
+ if (ids.delete(conversationId)) emitUnread();
+ },
+};
+
+/** 有未读的那些会话 */
+export function useUnread(): ReadonlySet<string> {
+ return useSyncExternalStore(convMarks.subscribe, convMarks.unread);
+}
+
+/** 此刻正在写的那些会话 */
+export function useLive(): ReadonlySet<string> {
+ return useSyncExternalStore(convMarks.subscribe, convMarks.live);
+}