@njuptlzf/agent-chat-web 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AgentChat.d.ts +28 -0
- package/dist/AgentChat.js +355 -0
- package/dist/AgentChatApp.d.ts +31 -0
- package/dist/AgentChatApp.js +90 -0
- package/dist/DockPanel.d.ts +22 -0
- package/dist/DockPanel.js +22 -0
- package/dist/PersonasPage.d.ts +1 -0
- package/dist/PersonasPage.js +318 -0
- package/dist/adapter.d.ts +52 -0
- package/dist/adapter.js +242 -0
- package/dist/app-def-check.d.ts +50 -0
- package/dist/app-def-check.js +184 -0
- package/dist/app.d.ts +112 -0
- package/dist/app.js +174 -0
- package/dist/auth.d.ts +41 -0
- package/dist/auth.js +86 -0
- package/dist/dock.d.ts +52 -0
- package/dist/dock.js +132 -0
- package/dist/docs.d.ts +48 -0
- package/dist/docs.js +178 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +8 -0
- package/dist/mount.d.ts +50 -0
- package/dist/mount.js +38 -0
- package/dist/qual.d.ts +21 -0
- package/dist/qual.js +53 -0
- package/dist/sse.d.ts +14 -0
- package/dist/sse.js +99 -0
- package/dist/styles.css +1478 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +14 -0
- package/dist/ui/CodeCopyHeader.d.ts +7 -0
- package/dist/ui/CodeCopyHeader.js +22 -0
- package/dist/ui/ConfirmCard.d.ts +15 -0
- package/dist/ui/ConfirmCard.js +9 -0
- package/dist/ui/Lightbox.d.ts +6 -0
- package/dist/ui/Lightbox.js +31 -0
- package/dist/ui/QueueBar.d.ts +44 -0
- package/dist/ui/QueueBar.js +17 -0
- package/dist/ui/Splitter.d.ts +27 -0
- package/dist/ui/Splitter.js +61 -0
- package/dist/ui/ThemeToggle.d.ts +2 -0
- package/dist/ui/ThemeToggle.js +16 -0
- package/dist/ui/TierPicker.d.ts +14 -0
- package/dist/ui/TierPicker.js +54 -0
- package/dist/ui/UIPromptCard.d.ts +17 -0
- package/dist/ui/UIPromptCard.js +18 -0
- package/dist/ui/timefmt.d.ts +35 -0
- package/dist/ui/timefmt.js +110 -0
- package/dist/ui/useNarrow.d.ts +7 -0
- package/dist/ui/useNarrow.js +15 -0
- package/package.json +72 -6
- package/src/App.tsx +13 -0
- package/src/agent-chat/AgentChat.tsx +692 -0
- package/src/agent-chat/AgentChatApp.tsx +206 -0
- package/src/agent-chat/DockPanel.tsx +74 -0
- package/src/agent-chat/PersonasPage.tsx +678 -0
- package/src/agent-chat/adapter.ts +272 -0
- package/src/agent-chat/app-def-check.ts +215 -0
- package/src/agent-chat/app.tsx +327 -0
- package/src/agent-chat/auth.ts +83 -0
- package/src/agent-chat/dock.ts +162 -0
- package/src/agent-chat/docs.ts +179 -0
- package/src/agent-chat/index.ts +17 -0
- package/src/agent-chat/mount.tsx +123 -0
- package/src/agent-chat/qual.ts +55 -0
- package/src/agent-chat/sse.ts +104 -0
- package/src/agent-chat/theme.ts +23 -0
- package/src/agent-chat/ui/CodeCopyHeader.tsx +29 -0
- package/src/agent-chat/ui/ConfirmCard.tsx +32 -0
- package/src/agent-chat/ui/Lightbox.tsx +32 -0
- package/src/agent-chat/ui/QueueBar.tsx +70 -0
- package/src/agent-chat/ui/Splitter.tsx +81 -0
- package/src/agent-chat/ui/ThemeToggle.tsx +25 -0
- package/src/agent-chat/ui/TierPicker.tsx +112 -0
- package/src/agent-chat/ui/UIPromptCard.tsx +68 -0
- package/src/agent-chat/ui/timefmt.ts +110 -0
- package/src/agent-chat/ui/useNarrow.ts +20 -0
- package/src/fake-product/entry.tsx +54 -0
- package/src/main.tsx +8 -0
- package/src/styles.css +1478 -0
- package/README.md +0 -3
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
|
|
2
|
+
import type { BrandingResponse, ChatCommand, HistoryMessage, SessionSummary } from "@njuptlzf/agent-chat-protocol";
|
|
3
|
+
import type { ThreadMessageLike } from "@assistant-ui/react";
|
|
4
|
+
import ChatApp from "./AgentChat.tsx";
|
|
5
|
+
import { auth } from "./auth.ts";
|
|
6
|
+
import { confirmInterrupt } from "./dock.ts";
|
|
7
|
+
import { fetchJson } from "./sse.ts";
|
|
8
|
+
import { Splitter, usePaneWidth } from "./ui/Splitter.tsx";
|
|
9
|
+
import { groupSessions, relativeTime } from "./ui/timefmt.ts";
|
|
10
|
+
import { useNarrow } from "./ui/useNarrow.ts";
|
|
11
|
+
|
|
12
|
+
/** The thread on screen: a fresh one, or a resumed session with restored messages. */
|
|
13
|
+
interface ActiveSession {
|
|
14
|
+
key: number;
|
|
15
|
+
sessionId?: string;
|
|
16
|
+
initialMessages?: readonly ThreadMessageLike[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* agentChat 本体(可独立开源的一层):自己的侧栏(新会话+历史会话)加对话。
|
|
21
|
+
* 零业务知识——标题/欢迎语/档位/指令全部来自服务端 branding 与协议。
|
|
22
|
+
*/
|
|
23
|
+
export default function AgentChatApp({
|
|
24
|
+
branding,
|
|
25
|
+
scope,
|
|
26
|
+
renderCard,
|
|
27
|
+
persona,
|
|
28
|
+
title,
|
|
29
|
+
policyNote,
|
|
30
|
+
welcome,
|
|
31
|
+
suggestions,
|
|
32
|
+
compact = false,
|
|
33
|
+
showBrand = false,
|
|
34
|
+
assistantLabel = "助手",
|
|
35
|
+
}: {
|
|
36
|
+
branding: BrandingResponse;
|
|
37
|
+
/** Qualification the default persona answers for; undefined runs the product default. */
|
|
38
|
+
scope?: string;
|
|
39
|
+
/** 产物卡渲染器(产品注入,透传给平台聊天组件)。 */
|
|
40
|
+
renderCard?: (details: unknown) => ReactNode;
|
|
41
|
+
/**
|
|
42
|
+
* 人格:undefined = 产品默认助手。会话列表也按它过滤(见 visibleSessions)。
|
|
43
|
+
* id 由产品 manifest 声明,core 不认识具体名字(通用化)。
|
|
44
|
+
*/
|
|
45
|
+
persona?: string;
|
|
46
|
+
/** 对话区列头:产品随人格给(「人格 B」/「默认助手」),缺省回落产品名。 */
|
|
47
|
+
title?: string;
|
|
48
|
+
policyNote?: string;
|
|
49
|
+
welcome?: string;
|
|
50
|
+
suggestions?: string[];
|
|
51
|
+
/**
|
|
52
|
+
* 助手在界面上的叫法:core 只用于「正在回答」这类提示语,默认「助手」。
|
|
53
|
+
* 产品传自己的(manifest 里的人格 label),core 不认识具体名字。
|
|
54
|
+
*/
|
|
55
|
+
assistantLabel?: string;
|
|
56
|
+
/** 坞里窄栏形态:默认收起会话侧栏(用户手动展开仍然优先)。 */
|
|
57
|
+
compact?: boolean;
|
|
58
|
+
showBrand?: boolean;
|
|
59
|
+
}) {
|
|
60
|
+
const [active, setActive] = useState<ActiveSession>({ key: 0 });
|
|
61
|
+
const [sessions, setSessions] = useState<SessionSummary[] | null>(null);
|
|
62
|
+
// 不同人格各看各的历史:混在一起的话,点开对方那条就是 persona_mismatch。
|
|
63
|
+
// 老会话 persona 为 null ⇒ 按**产品默认助手**算。默认助手的 id 由 branding 下发
|
|
64
|
+
// (通用化),core 不假定它叫什么;老服务端没这个字段时退回协议兜底 id。
|
|
65
|
+
const defaultPersona = branding.defaultPersona ?? "default";
|
|
66
|
+
const visibleSessions = (sessions ?? []).filter((s) => (s.persona ?? defaultPersona) === (persona ?? defaultPersona));
|
|
67
|
+
// Default to the first declared tier so the shown price is always the charged price.
|
|
68
|
+
const [tier, setTier] = useState<string | undefined>(branding.tiers?.[0]?.id);
|
|
69
|
+
const [commands, setCommands] = useState<ChatCommand[]>([]);
|
|
70
|
+
// 侧栏可拖宽(参考 DSH):默认 210px,160..420px 夹逼,记忆在 localStorage。
|
|
71
|
+
const nav = usePaneWidth("acnav.width", 210, 160, 420);
|
|
72
|
+
// ⑦ 侧栏折叠:状态持久在 localStorage;折叠后由左缘竖条唤回。
|
|
73
|
+
const [collapsed, setCollapsed] = useState(() => localStorage.getItem("acnav.collapsed") === "1");
|
|
74
|
+
// 窄屏自动折叠:150px 业务导航 + 210px 会话栏会把对话区挤没(实测 744px 宽时
|
|
75
|
+
// 会话栏占掉近半)。窄屏只看对话,但用户的显式操作优先——手动展开过就不再自动收。
|
|
76
|
+
const narrow = useNarrow(900);
|
|
77
|
+
const [userPinned, setUserPinned] = useState(false);
|
|
78
|
+
const shownCollapsed = collapsed || (compact ? !userPinned : narrow && !userPinned);
|
|
79
|
+
const toggleCollapsed = () => {
|
|
80
|
+
const next = !shownCollapsed;
|
|
81
|
+
setCollapsed(next);
|
|
82
|
+
setUserPinned(true);
|
|
83
|
+
localStorage.setItem("acnav.collapsed", next ? "1" : "0");
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const reloadSessions = useCallback(() => {
|
|
87
|
+
// Entry decides the visible range: this drawer is reached from a
|
|
88
|
+
// qualification, so it only lists that qualification's sessions. A session
|
|
89
|
+
// is bound to its scope server-side, so a cross-scope row here would only
|
|
90
|
+
// fail to resume.
|
|
91
|
+
const q = scope ? `?scope=${encodeURIComponent(scope)}` : "";
|
|
92
|
+
fetchJson<{ sessions: SessionSummary[] }>(`/api/sessions${q}`, auth.token)
|
|
93
|
+
.then((r) => setSessions(r.sessions))
|
|
94
|
+
.catch(() => setSessions([]));
|
|
95
|
+
}, [scope]);
|
|
96
|
+
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
// The slash surface is the scope's (S1): refetch when the qualification changes.
|
|
99
|
+
const q = scope ? `?qual=${encodeURIComponent(scope)}` : "";
|
|
100
|
+
fetchJson<{ commands: ChatCommand[] }>(`/api/commands${q}`)
|
|
101
|
+
.then((r) => setCommands(r.commands))
|
|
102
|
+
.catch(() => setCommands([]));
|
|
103
|
+
reloadSessions();
|
|
104
|
+
}, [reloadSessions]);
|
|
105
|
+
|
|
106
|
+
// Switching scope must start a fresh thread: the previous one belongs to the
|
|
107
|
+
// another persona and the server would reject resuming it (400 scope_mismatch).
|
|
108
|
+
// The "are you sure" prompt lives in App.pick(), before the scope even changes.
|
|
109
|
+
const scopeRef = useRef(scope);
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
if (scopeRef.current === scope) return;
|
|
112
|
+
scopeRef.current = scope;
|
|
113
|
+
setActive((a) => ({ key: a.key + 1 }));
|
|
114
|
+
}, [scope]);
|
|
115
|
+
|
|
116
|
+
// 换会话会卸掉正在跑的那一轮,先确认(切业务页不算,坞是常驻的)。
|
|
117
|
+
const newSession = () => {
|
|
118
|
+
if (!confirmInterrupt("新建会话", assistantLabel)) return;
|
|
119
|
+
setActive((a) => ({ key: a.key + 1 }));
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const openSession = async (s: SessionSummary) => {
|
|
123
|
+
if (!confirmInterrupt("切换历史会话", assistantLabel)) return;
|
|
124
|
+
// If the display fetch fails we still resume the session: the server holds the context.
|
|
125
|
+
let initialMessages: readonly ThreadMessageLike[] | undefined;
|
|
126
|
+
try {
|
|
127
|
+
const r = await fetchJson<{ messages: HistoryMessage[] }>(
|
|
128
|
+
`/api/history?sessionId=${encodeURIComponent(s.sessionId)}`,
|
|
129
|
+
auth.token,
|
|
130
|
+
);
|
|
131
|
+
initialMessages = r.messages.map((m) => ({ role: m.role, content: m.text, createdAt: new Date(m.at) }));
|
|
132
|
+
} catch {
|
|
133
|
+
initialMessages = undefined;
|
|
134
|
+
}
|
|
135
|
+
setActive((a) => ({ key: a.key + 1, sessionId: s.sessionId, initialMessages }));
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const chat = (
|
|
139
|
+
<ChatApp
|
|
140
|
+
key={active.key}
|
|
141
|
+
branding={branding}
|
|
142
|
+
persona={persona}
|
|
143
|
+
title={title}
|
|
144
|
+
policyNote={policyNote}
|
|
145
|
+
welcome={welcome}
|
|
146
|
+
suggestions={suggestions}
|
|
147
|
+
sessionId={active.sessionId}
|
|
148
|
+
initialMessages={active.initialMessages}
|
|
149
|
+
tier={tier}
|
|
150
|
+
scope={scope}
|
|
151
|
+
renderCard={renderCard}
|
|
152
|
+
onTier={setTier}
|
|
153
|
+
commands={commands}
|
|
154
|
+
onRunEnd={reloadSessions}
|
|
155
|
+
/>
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<div className="ac-row">
|
|
160
|
+
<aside className={`side acnav${shownCollapsed ? " collapsed" : ""}`} style={{ flexBasis: nav.width }}>
|
|
161
|
+
{showBrand && (
|
|
162
|
+
<div className="brand">
|
|
163
|
+
{branding.logo ? <img className="logo" src={branding.logo} alt="" /> : null}
|
|
164
|
+
<span>{branding.title}</span>
|
|
165
|
+
</div>
|
|
166
|
+
)}
|
|
167
|
+
<button className="newchat" onClick={newSession} title="重新开始一个会话">
|
|
168
|
+
+ 新会话
|
|
169
|
+
</button>
|
|
170
|
+
<div className="hist-h">
|
|
171
|
+
历史会话
|
|
172
|
+
<button className="collapse-btn" onClick={toggleCollapsed} title="收起侧栏">«</button>
|
|
173
|
+
</div>
|
|
174
|
+
<div className="sessions">
|
|
175
|
+
{sessions === null ? (
|
|
176
|
+
<div className="s-empty">加载中…</div>
|
|
177
|
+
) : visibleSessions.length === 0 ? (
|
|
178
|
+
<div className="s-empty">暂无历史会话</div>
|
|
179
|
+
) : (
|
|
180
|
+
groupSessions(visibleSessions, new Date()).map((g) => (
|
|
181
|
+
<div key={g.label}>
|
|
182
|
+
<div className="s-group">{g.label}</div>
|
|
183
|
+
{g.items.map((s) => (
|
|
184
|
+
<button key={s.sessionId} className="s-item" title={s.title} onClick={() => void openSession(s)}>
|
|
185
|
+
<span className="s-title">{s.title}</span>
|
|
186
|
+
<span className="s-meta">
|
|
187
|
+
{relativeTime(s.updatedAt, new Date())} · {s.messageCount} 条
|
|
188
|
+
</span>
|
|
189
|
+
</button>
|
|
190
|
+
))}
|
|
191
|
+
</div>
|
|
192
|
+
))
|
|
193
|
+
)}
|
|
194
|
+
</div>
|
|
195
|
+
</aside>
|
|
196
|
+
{!shownCollapsed && <Splitter {...nav.handlers} />}
|
|
197
|
+
{shownCollapsed && (
|
|
198
|
+
<button className="side-open" onClick={toggleCollapsed} title="展开历史会话侧栏" aria-label="展开历史会话侧栏">
|
|
199
|
+
<span className="side-open-arrow">»</span>
|
|
200
|
+
<span className="side-open-text">历史</span>
|
|
201
|
+
</button>
|
|
202
|
+
)}
|
|
203
|
+
<main className="content">{chat}</main>
|
|
204
|
+
</div>
|
|
205
|
+
);
|
|
206
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { dockStore, reopenMode, useDockMode, type DockMode } from "./dock.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 聊天坞(agentChat Dock):把对话做成一个"能力",挂在任何业务页右边。
|
|
6
|
+
*
|
|
7
|
+
* 三种形态由 `dock.ts` 的状态机决定:
|
|
8
|
+
* docked 挤压内容区(宽屏默认)
|
|
9
|
+
* floating 悬浮在内容区右下角,不改布局
|
|
10
|
+
* closed 收成右缘竖排唤回按钮(面板用 display:none 藏起来而不是卸载——
|
|
11
|
+
* 对话流、滚动位置、会话视图都要在开关之间活着)
|
|
12
|
+
*
|
|
13
|
+
* `full=true` 是「对话」路由:坞撑满内容区,业务页不渲染。
|
|
14
|
+
*/
|
|
15
|
+
export default function Dock({
|
|
16
|
+
chat,
|
|
17
|
+
full = false,
|
|
18
|
+
onClose,
|
|
19
|
+
title = "助手",
|
|
20
|
+
children,
|
|
21
|
+
}: {
|
|
22
|
+
/** 对话组件(只挂一份,开关形态之间不重挂)。 */
|
|
23
|
+
chat: ReactNode;
|
|
24
|
+
full?: boolean;
|
|
25
|
+
/** 坞的标题:换人格时这里也要换,否则点了「声明人格」还写着「默认助手」。 */
|
|
26
|
+
title?: string;
|
|
27
|
+
/** 聊天路由下点收起:由宿主决定退到哪个业务页。 */
|
|
28
|
+
onClose?: () => void;
|
|
29
|
+
children?: ReactNode;
|
|
30
|
+
}) {
|
|
31
|
+
const mode = useDockMode();
|
|
32
|
+
const shown: DockMode | "full" = full ? "full" : mode;
|
|
33
|
+
|
|
34
|
+
const close = () => {
|
|
35
|
+
dockStore.setMode("closed");
|
|
36
|
+
onClose?.();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<div className={`dock-wrap ${shown}`}>
|
|
41
|
+
{!full && <main className="content">{children}</main>}
|
|
42
|
+
<aside className={`dock-pane${mode === "closed" && !full ? " gone" : ""}`} aria-label={title}>
|
|
43
|
+
<div className="dock-head">
|
|
44
|
+
<span className="dock-title">{title}</span>
|
|
45
|
+
{full ? null : (
|
|
46
|
+
<button
|
|
47
|
+
className="dock-btn"
|
|
48
|
+
onClick={() => dockStore.setMode(mode === "floating" ? "docked" : "floating")}
|
|
49
|
+
title={mode === "floating" ? "停靠到右侧" : "改为悬浮"}
|
|
50
|
+
aria-label={mode === "floating" ? "停靠到右侧" : "改为悬浮"}
|
|
51
|
+
>
|
|
52
|
+
{mode === "floating" ? "⇥" : "⤢"}
|
|
53
|
+
</button>
|
|
54
|
+
)}
|
|
55
|
+
<button className="dock-btn" onClick={close} title={`收起${title}`} aria-label={`收起${title}`}>
|
|
56
|
+
»
|
|
57
|
+
</button>
|
|
58
|
+
</div>
|
|
59
|
+
<div className="dock-body">{chat}</div>
|
|
60
|
+
</aside>
|
|
61
|
+
{mode === "closed" && !full && (
|
|
62
|
+
<button
|
|
63
|
+
className="side-open dock-open"
|
|
64
|
+
onClick={() => dockStore.setMode(reopenMode(mode, dockStore.lastOpen(), window.innerWidth > 1100))}
|
|
65
|
+
title={`打开${title}`}
|
|
66
|
+
aria-label={`打开${title}`}
|
|
67
|
+
>
|
|
68
|
+
<span className="side-open-arrow">«</span>
|
|
69
|
+
<span className="side-open-text">{title}</span>
|
|
70
|
+
</button>
|
|
71
|
+
)}
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
}
|