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