@tbox.cn/app-agent-ui-kit 1.0.0-alpha.1 → 1.0.0-alpha.11
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/README.md +56 -3
- package/package.json +22 -7
- package/src/ChatPage.tsx +115 -20
- package/src/card/action-list-pending.test.tsx +91 -0
- package/src/card/define-card.test.ts +25 -0
- package/src/card/define-card.ts +47 -0
- package/src/card/primitives.test.tsx +157 -0
- package/src/card/primitives.tsx +717 -0
- package/src/card/use-card-actions.ts +86 -0
- package/src/card/view-card-inline.test.tsx +42 -0
- package/src/card/view-card.test.tsx +79 -0
- package/src/card/view-card.tsx +250 -0
- package/src/chat-config.test.ts +35 -71
- package/src/chat-config.ts +29 -7
- package/src/components/ActionBar/DebugButton.test.tsx +3 -0
- package/src/components/ActionBar/DebugButton.tsx +5 -10
- package/src/components/ActionBar/index.tsx +13 -14
- package/src/components/BottomSheet/index.test.tsx +97 -0
- package/src/components/BottomSheet/index.tsx +49 -0
- package/src/components/ChatHeader/index.tsx +19 -17
- package/src/components/ChatInput/index.test.tsx +2 -2
- package/src/components/ChatInput/index.tsx +44 -33
- package/src/components/ChatInput/voice/VoicePressField.tsx +12 -3
- package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +9 -9
- package/src/components/ChatInput/voice/useVoicePress.ts +1 -1
- package/src/components/HistoryEntry/index.tsx +4 -4
- package/src/components/MediaList/index.tsx +42 -28
- package/src/components/MessageBubble/index.tsx +59 -28
- package/src/components/PullDownIndicator/index.tsx +3 -4
- package/src/components/QuickCommands/index.test.tsx +3 -1
- package/src/components/QuickCommands/index.tsx +33 -10
- package/src/components/ReasoningBlock/index.tsx +14 -9
- package/src/components/ScrollToBottomButton/index.tsx +7 -5
- package/src/components/ToolCallBlock/index.tsx +58 -38
- package/src/icons.ts +39 -0
- package/src/index.ts +75 -1
- package/src/overlay/Overlay.tsx +154 -0
- package/src/overlay/focus.ts +53 -0
- package/src/overlay/index.ts +16 -0
- package/src/overlay/inert.ts +27 -0
- package/src/overlay/layers.ts +21 -0
- package/src/overlay/overlay.ssr.test.tsx +27 -0
- package/src/overlay/overlay.test.tsx +209 -0
- package/src/overlay/presence.ts +86 -0
- package/src/overlay/scroll-lock.ts +62 -0
- package/src/overlay/stack.ts +111 -0
- package/src/overlay/use-overlay.ts +80 -0
- package/src/preview-provider.tsx +47 -0
- package/src/shell.contract.json +75 -0
- package/src/shell.test.ts +72 -0
- package/src/shell.ts +60 -0
- package/src/surface.test.tsx +35 -0
- package/src/surface.tsx +44 -0
- package/src/theme/base.css +167 -0
- package/src/theme/contract.json +123 -2
- package/src/theme/theme-contract.test.ts +19 -3
- package/src/theme/{tokens.less → tokens.css} +19 -20
- package/src/utils/image-preview-host.tsx +95 -0
- package/src/utils/imagePreview.test.ts +32 -0
- package/src/utils/imagePreview.ts +14 -5
- package/src/utils/toast.test.ts +64 -0
- package/src/utils/toast.ts +100 -7
- package/src/ChatPage.less +0 -62
- package/src/components/ActionBar/index.less +0 -123
- package/src/components/ChatHeader/index.less +0 -148
- package/src/components/ChatInput/index.less +0 -394
- package/src/components/HistoryEntry/index.less +0 -49
- package/src/components/MediaList/index.less +0 -241
- package/src/components/MessageBubble/index.less +0 -325
- package/src/components/PullDownIndicator/index.less +0 -32
- package/src/components/QuickCommands/index.less +0 -63
- package/src/components/ReasoningBlock/index.less +0 -90
- package/src/components/ScrollToBottomButton/index.less +0 -32
- package/src/components/ToolCallBlock/index.less +0 -312
- package/src/theme/global.less +0 -99
- /package/src/theme/{motion.less → motion.css} +0 -0
package/README.md
CHANGED
|
@@ -3,12 +3,65 @@
|
|
|
3
3
|
Agent 对话壳 UI 组件包(源码发布)——单一模板统一化 W2-1 自 mall 模板抽取。
|
|
4
4
|
|
|
5
5
|
- **真源**:本包(`packages/agent-ui-kit`)。模板出厂副本经 `scripts/sync-agent-ui-kit.mjs` 派生
|
|
6
|
-
(name 改写 `@tbox.cn/app-agent-ui-kit` → `@app
|
|
6
|
+
(name 改写 `@tbox.cn/app-agent-ui-kit` → `@tbox.cn/app-agent-ui-kit`),guard identity 锁定一致。
|
|
7
7
|
- **消费面**:应用 `<ChatPage store={useChatStore} cards={CHAT_REGISTRY} slots={clientCtx.slots}/>`
|
|
8
|
-
|
|
9
|
-
- **主题**:kit 默认 tokens(`src/theme/tokens.
|
|
8
|
+
单点装配;应用首页经 home 槽渲染,会话级浮层经 conversationOverlay 槽独立挂载。
|
|
9
|
+
- **主题**:kit 默认 tokens(`src/theme/tokens.css`);品牌资产包经 `html[data-theme]` 级联覆写 +
|
|
10
10
|
接线三行(`import '<pkg>/index.css'` + `import '<pkg>/brand.js'`[可选] + `applyTheme(id)`;见 docs/reference/templates.md)。
|
|
11
11
|
|
|
12
|
+
## 弹层(`./overlay`)
|
|
13
|
+
|
|
14
|
+
`Overlay` 是唯一弹层外壳:传送、遮罩、层级、滚动锁、应用根惰性(`inert`)、Escape、焦点循环与恢复、进出场动画都在这里。业务只决定版式(`placement`)与面板样式,不再各写一套。
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { Overlay } from '@tbox.cn/app-agent-ui-kit/overlay';
|
|
18
|
+
|
|
19
|
+
<Overlay
|
|
20
|
+
open={open}
|
|
21
|
+
onDismiss={() => setOpen(false)}
|
|
22
|
+
placement="bottom"
|
|
23
|
+
labelledBy={titleId}
|
|
24
|
+
initialFocus={closeRef}
|
|
25
|
+
rootClassName="my-sheet group"
|
|
26
|
+
panelClassName="my-sheet__panel ..."
|
|
27
|
+
>
|
|
28
|
+
…
|
|
29
|
+
</Overlay>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- **层级三档**:modal `1100` / preview `1200` / toast `1300`(`OVERLAY_LAYER_CLASS`)。同档内按打开先后叠放,嵌套自然成立;不再按组件分配层级。
|
|
33
|
+
- **栈语义**:全实例一个弹层栈,键盘只挂一个 `document` 捕获监听——Escape 与 Tab 只作用于栈顶,嵌套弹层不会一起关;下层根节点自动 `inert`。
|
|
34
|
+
- **滚动锁**:计数式(嵌套按最后关闭者解锁)。滚动宿主由 `ChatPage` 经 `registerScrollHost` 登记,模块不读壳内部类名;无宿主时锁 `body`。
|
|
35
|
+
- **进出场**:根节点 `data-state="open|closed"`;退出动画期间面板不带 `role=dialog`,动画播完才卸载(`motion-reduce` 直接收尾)。自管挂载的宿主用 `onExited` 把 `onClose` 推迟到动画之后。
|
|
36
|
+
- **服务端**:无 `document` 时就地渲染(不 portal),`renderToStaticMarkup` 证据链可直接用。
|
|
37
|
+
- **自管 DOM** 的复杂弹层(如 FLIP 形变详情页)用 `useOverlay` + `usePresence` 组合,行为面与 `Overlay` 相同。
|
|
38
|
+
|
|
39
|
+
### 半屏弹层
|
|
40
|
+
|
|
41
|
+
`BottomSheet` 是 `Overlay` 之上的受控底部弹层:外壳负责遮罩、滚动锁、安全区、标题、关闭按钮、Escape 与焦点循环/恢复,业务只给内容与开关。
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
import { BottomSheet } from '@tbox.cn/app-agent-ui-kit/bottom-sheet';
|
|
45
|
+
|
|
46
|
+
<BottomSheet open={open} title="详情" onClose={() => setOpen(false)}>
|
|
47
|
+
<DetailContent />
|
|
48
|
+
</BottomSheet>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
支持 `className` 定制内容区,默认最大高度为视口的 65%,长内容在弹层内部滚动。`preloadBottomSheet()` 保留为已废弃的空操作(弹层不再有第三方运行时需要预热)。
|
|
52
|
+
|
|
53
|
+
### 大图预览
|
|
54
|
+
|
|
55
|
+
`previewImages(images, index)` 首次调用时按需加载 Yet Another Lightbox(`./utils/image-preview-host`,单独 chunk)。缩放/双击/下拉关闭/点背景关闭由它提供,滚动锁、Escape 与层级走 kit overlay;样式由 `@tbox.cn/app-build` 的 `tboxVendorLayerPlugin` 收进 `@layer vendor`。
|
|
56
|
+
|
|
57
|
+
### 轻提示
|
|
58
|
+
|
|
59
|
+
`toast(content, tone)` 是纯 DOM 实现(不依赖 React 与组件库):单实例替换、2000ms、视口居中、不拦截点击、`role="status"` 可朗读;Node/SSR 直接空返回。可从 `@tbox.cn/app-agent-ui-kit/toast` 独立导入,避免引入完整聊天壳。
|
|
60
|
+
|
|
61
|
+
## SSR / Node 边界
|
|
62
|
+
|
|
63
|
+
弹层与提示都没有第三方 CSS 运行时依赖,`bottom-sheet` / `overlay` / `toast` 子路径在 Vite/Node SSR 里可直接 import。大图预览宿主只经 `import()` 到达(它静态引 Lightbox 与它的 `styles.css`),不会进入 Node 加载图——`scripts/tests/guard-kit-ssr.test.mjs` 按静态可达性守住这条。kit **根入口**仍依赖 SVG/PNG/CSS 资产,只承诺 bundler(Vite 等)消费,不承诺 raw-Node 直跑。
|
|
64
|
+
|
|
12
65
|
## 验证
|
|
13
66
|
|
|
14
67
|
```sh
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-agent-ui-kit",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.11",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Agent 对话壳 UI 组件包(源码发布,单一模板统一化 W2-1):ChatPage/壳组件族/主题 tokens。",
|
|
6
6
|
"exports": {
|
|
@@ -13,8 +13,24 @@
|
|
|
13
13
|
"import": "./src/theme/theme.ts"
|
|
14
14
|
},
|
|
15
15
|
"./package.json": "./package.json",
|
|
16
|
-
"./theme/
|
|
17
|
-
"./theme/contract": "./src/theme/contract.json"
|
|
16
|
+
"./theme/base.css": "./src/theme/base.css",
|
|
17
|
+
"./theme/contract": "./src/theme/contract.json",
|
|
18
|
+
"./icons": {
|
|
19
|
+
"types": "./src/icons.ts",
|
|
20
|
+
"import": "./src/icons.ts"
|
|
21
|
+
},
|
|
22
|
+
"./toast": {
|
|
23
|
+
"types": "./src/utils/toast.ts",
|
|
24
|
+
"import": "./src/utils/toast.ts"
|
|
25
|
+
},
|
|
26
|
+
"./overlay": {
|
|
27
|
+
"types": "./src/overlay/index.ts",
|
|
28
|
+
"import": "./src/overlay/index.ts"
|
|
29
|
+
},
|
|
30
|
+
"./bottom-sheet": {
|
|
31
|
+
"types": "./src/components/BottomSheet/index.tsx",
|
|
32
|
+
"import": "./src/components/BottomSheet/index.tsx"
|
|
33
|
+
}
|
|
18
34
|
},
|
|
19
35
|
"files": [
|
|
20
36
|
"src",
|
|
@@ -23,13 +39,13 @@
|
|
|
23
39
|
"LICENSE"
|
|
24
40
|
],
|
|
25
41
|
"dependencies": {
|
|
26
|
-
"@tbox.cn/app-contracts": "1.0.0-alpha.1",
|
|
27
42
|
"@phosphor-icons/react": "2.1.10",
|
|
28
|
-
"antd-mobile": "5.42.3",
|
|
29
43
|
"react-markdown": "9.1.0",
|
|
30
44
|
"remark-gfm": "4.0.1",
|
|
45
|
+
"yet-another-react-lightbox": "3.33.1",
|
|
31
46
|
"zustand": "4.5.7",
|
|
32
|
-
"@tbox.cn/app-agent-sdk-client": "1.0.0-alpha.
|
|
47
|
+
"@tbox.cn/app-agent-sdk-client": "1.0.0-alpha.11",
|
|
48
|
+
"@tbox.cn/app-contracts": "1.0.0-alpha.11"
|
|
33
49
|
},
|
|
34
50
|
"peerDependencies": {
|
|
35
51
|
"react": "18.3.1",
|
|
@@ -40,7 +56,6 @@
|
|
|
40
56
|
"@types/react": "18.3.31",
|
|
41
57
|
"@types/react-dom": "18.3.7",
|
|
42
58
|
"happy-dom": "20.11.2",
|
|
43
|
-
"less": "4.8.1",
|
|
44
59
|
"react": "18.3.1",
|
|
45
60
|
"react-dom": "18.3.1",
|
|
46
61
|
"typescript": "5.9.3",
|
package/src/ChatPage.tsx
CHANGED
|
@@ -7,8 +7,12 @@
|
|
|
7
7
|
* - 域自持段(首屏数据/门禁/详情弹层/活动预热)经槽渲染函数自带——本页零域语义。
|
|
8
8
|
*/
|
|
9
9
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
10
|
+
|
|
11
|
+
// SSR(预览/探针)没有 DOM 可量:useLayoutEffect 会打印不可消除的告警,服务端退化为 useEffect。
|
|
12
|
+
const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
10
13
|
import { consumeAutoSendQuery, runAutoSend } from '@tbox.cn/app-agent-sdk-client';
|
|
11
14
|
import { usePullDownHistory } from './hooks/use-pull-down-history';
|
|
15
|
+
import { registerScrollHost } from './overlay';
|
|
12
16
|
import type { TboxCardPayload } from '@tbox.cn/app-contracts';
|
|
13
17
|
import { CardRenderer } from './components/CardRenderer';
|
|
14
18
|
import { ChatInput } from './components/ChatInput';
|
|
@@ -17,6 +21,7 @@ import { MessageBubble } from './components/MessageBubble';
|
|
|
17
21
|
import { QuickCommands } from './components/QuickCommands';
|
|
18
22
|
import ScrollToBottomButton from './components/ScrollToBottomButton';
|
|
19
23
|
import { useChatConfigStore } from './chat-config';
|
|
24
|
+
import { resolveBrandName } from './shell';
|
|
20
25
|
import {
|
|
21
26
|
KitChatProvider,
|
|
22
27
|
useChatStore,
|
|
@@ -25,8 +30,7 @@ import {
|
|
|
25
30
|
type KitChatContextValue,
|
|
26
31
|
} from './store-context';
|
|
27
32
|
import type { ChatInputMessage } from './types';
|
|
28
|
-
import './
|
|
29
|
-
import './theme/tokens.less';
|
|
33
|
+
import './theme/tokens.css';
|
|
30
34
|
|
|
31
35
|
export interface ChatPageProps {
|
|
32
36
|
/** 应用 chat store(zustand 绑定 hook + API;如 `useChatStore`) */
|
|
@@ -87,10 +91,40 @@ function ChatPageInner() {
|
|
|
87
91
|
|
|
88
92
|
const slots = useClientSlots();
|
|
89
93
|
const homeSlot = slots?.resolve('home');
|
|
94
|
+
const conversationOverlay = slots?.resolve('conversationOverlay');
|
|
95
|
+
const identity = useChatConfigStore((s) => s.identity);
|
|
96
|
+
const welcome = useChatConfigStore((s) => s.welcome);
|
|
97
|
+
|
|
98
|
+
// 页面元信息跟身份与主题走:标题、主题色不再停在模板出厂值(B9)。
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
if (typeof document === 'undefined') return;
|
|
101
|
+
const name = identity?.name;
|
|
102
|
+
if (name) document.title = name;
|
|
103
|
+
const bg = getComputedStyle(document.documentElement).getPropertyValue('--tbox-bg-page').trim();
|
|
104
|
+
if (bg) {
|
|
105
|
+
let meta = document.querySelector('meta[name="theme-color"]');
|
|
106
|
+
if (!meta) {
|
|
107
|
+
meta = document.createElement('meta');
|
|
108
|
+
meta.setAttribute('name', 'theme-color');
|
|
109
|
+
document.head.appendChild(meta);
|
|
110
|
+
}
|
|
111
|
+
meta.setAttribute('content', bg);
|
|
112
|
+
}
|
|
113
|
+
}, [identity?.name]);
|
|
114
|
+
// 常驻品牌条:chat.json identity.name → HELLO_ACK 的 mallName → 不渲染(壳契约见 shell.ts)
|
|
115
|
+
const brandName = resolveBrandName(identity, mallName);
|
|
90
116
|
|
|
91
117
|
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
|
|
92
118
|
const [voiceActive, setVoiceActive] = useState(false);
|
|
93
119
|
const bodyRef = useRef<HTMLDivElement>(null);
|
|
120
|
+
|
|
121
|
+
// 弹层滚动锁要锁的是应用自己的滚动宿主(body 在本壳里并不滚动):登记给 kit overlay 内核,
|
|
122
|
+
// 业务弹层因此不再需要读 `.chat-page__body` 这类壳内部类名。
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
const el = bodyRef.current;
|
|
125
|
+
if (!el) return undefined;
|
|
126
|
+
return registerScrollHost(el);
|
|
127
|
+
}, []);
|
|
94
128
|
// 底部输入区(QuickCommands + ChatInput)高度测量:底部避让消费(如半屏 sheet)
|
|
95
129
|
const chatBottomRef = useRef<HTMLDivElement>(null);
|
|
96
130
|
// 历史前插视口锚:home 槽位占位(历史插入发生在其上,保持用户看到的位置)
|
|
@@ -163,7 +197,7 @@ function ChatPageInner() {
|
|
|
163
197
|
|
|
164
198
|
// 历史插入发生在当前内容上方:绘制前只补一次新增高度,保持用户原本看到的位置。
|
|
165
199
|
// 不设置定时器、不钉住 scrollTop;补偿后下一次滚动立即由用户接管。
|
|
166
|
-
|
|
200
|
+
useIsomorphicLayoutEffect(() => {
|
|
167
201
|
const pending = pendingHistoryViewportRef.current;
|
|
168
202
|
const anchor = historyAnchorRef.current;
|
|
169
203
|
if (!pending || !anchor) return;
|
|
@@ -196,6 +230,16 @@ function ChatPageInner() {
|
|
|
196
230
|
|
|
197
231
|
const showHistoryEntry = historyHasMore && historyMessages.length === 0;
|
|
198
232
|
|
|
233
|
+
// 空会话欢迎区:没有 home 槽时给首屏一个落点(标题来自 chat.json welcome,缺省「开始对话」)
|
|
234
|
+
const showWelcome = !homeSlot
|
|
235
|
+
&& historyMessages.length === 0
|
|
236
|
+
&& messages.length === 0
|
|
237
|
+
&& inlineOrphanCards.length === 0;
|
|
238
|
+
const welcomeTitle = welcome?.title ?? identity?.name ?? '开始对话';
|
|
239
|
+
const welcomeSubtitle = welcome?.subtitle ?? identity?.tagline ?? '可以直接提问,也可以从下面的问题开始';
|
|
240
|
+
// 空会话(有 home 槽或无 home 槽)共用快捷问题的显示条件。
|
|
241
|
+
const isEmptyState = showHome || showWelcome;
|
|
242
|
+
|
|
199
243
|
const pull = usePullDownHistory({
|
|
200
244
|
isAtTopRef,
|
|
201
245
|
isLoadingHistory,
|
|
@@ -228,23 +272,53 @@ function ChatPageInner() {
|
|
|
228
272
|
return () => ro.disconnect();
|
|
229
273
|
}, []);
|
|
230
274
|
|
|
231
|
-
//
|
|
232
|
-
//
|
|
275
|
+
// 回合锚定:新回合把本轮用户消息置顶,流式期间不追底。
|
|
276
|
+
// 贴底滚动会把问题顶出视口(实测卡片 648px 时问题上移 244px),用户看不到自己在问什么。
|
|
277
|
+
const turnAnchorRef = useRef<string | null>(null);
|
|
233
278
|
useEffect(() => {
|
|
234
|
-
const
|
|
235
|
-
if (
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
|
|
279
|
+
const lastUser = [...messages].reverse().find((message) => message.role === 'user');
|
|
280
|
+
if (!lastUser || turnAnchorRef.current === lastUser.id) return;
|
|
281
|
+
turnAnchorRef.current = lastUser.id;
|
|
282
|
+
const body = bodyRef.current;
|
|
283
|
+
const element = body?.querySelector(`[data-message-id="${lastUser.id}"]`);
|
|
284
|
+
if (!body || !element) return;
|
|
285
|
+
const delta = element.getBoundingClientRect().top - body.getBoundingClientRect().top;
|
|
286
|
+
body.scrollTop += delta - 8;
|
|
287
|
+
isNearBottomRef.current = false;
|
|
288
|
+
// 回合锚定后内容通常超出视口:必须让「回到底部」入口出现,否则用户只能靠手势找底。
|
|
289
|
+
if (body.scrollHeight > body.clientHeight + 8) setShowScrollToBottom(true);
|
|
290
|
+
}, [messages]);
|
|
291
|
+
|
|
292
|
+
// 已脱离底部时内容继续变高(流式回复、卡片后挂载):保持浮标可见,不追底。
|
|
293
|
+
useEffect(() => {
|
|
294
|
+
if (isNearBottomRef.current) return;
|
|
295
|
+
const el = bodyRef.current;
|
|
296
|
+
if (el && el.scrollHeight > el.clientHeight + 8) setShowScrollToBottom(true);
|
|
297
|
+
}, [messages, liveCardSignature, orphanCards]);
|
|
298
|
+
|
|
299
|
+
// 没有新回合、但卡片在文字之后挂载时,内容会继续变高:只在用户本来就在底部时补滚,
|
|
300
|
+
// 且不改变「本轮问题已在视口内」的锚点。
|
|
301
|
+
useEffect(() => {
|
|
302
|
+
if (!isNearBottomRef.current) return;
|
|
303
|
+
const el = bodyRef.current;
|
|
304
|
+
if (el && messages.length === 0 && orphanCards.length > 0) el.scrollTop = el.scrollHeight;
|
|
305
|
+
}, [orphanCards, liveCardSignature, messages.length]);
|
|
240
306
|
|
|
241
307
|
return (
|
|
242
|
-
<div className={`chat-page${showHome ? '' : ' chat-page--conversation'}`}>
|
|
243
|
-
|
|
308
|
+
<div className={`chat-page relative flex h-full w-full min-h-0 flex-col overflow-hidden bg-background ${showHome ? '' : ' chat-page--conversation'}`}>
|
|
309
|
+
{/* 整页背景独立于滚动内容,覆盖历史入口、首页与底部输入区;图片按视口居中铺满、不重复。 */}
|
|
310
|
+
<div className="chat-page__bg pointer-events-none absolute inset-0 z-0 bg-background bg-center bg-cover bg-no-repeat" />
|
|
244
311
|
|
|
245
|
-
|
|
312
|
+
{brandName ? (
|
|
313
|
+
<div className="chat-page__brand relative z-10 flex items-baseline justify-center gap-3 border-b border-border bg-background px-page pb-3 pt-[calc(16px+env(safe-area-inset-top))]" aria-label={brandName}>
|
|
314
|
+
<span className="chat-page__brand-name text-base font-bold tracking-wide text-foreground">{brandName}</span>
|
|
315
|
+
{identity?.tagline ? <span className="chat-page__brand-tagline text-xs text-muted-foreground">{identity.tagline}</span> : null}
|
|
316
|
+
</div>
|
|
317
|
+
) : null}
|
|
318
|
+
|
|
319
|
+
<div className="chat-page__body-wrapper relative z-10 flex min-h-0 flex-1 flex-col">
|
|
246
320
|
<div
|
|
247
|
-
className="chat-page__body"
|
|
321
|
+
className="chat-page__body mx-auto w-full max-w-content flex-1 min-h-0 overflow-y-auto overflow-x-hidden overscroll-contain pb-6 pt-[calc(12px+env(safe-area-inset-top))] [overflow-anchor:none] [scrollbar-width:none] [-webkit-overflow-scrolling:touch] [&::-webkit-scrollbar]:hidden"
|
|
248
322
|
ref={bodyRef}
|
|
249
323
|
onScroll={onScroll}
|
|
250
324
|
onWheel={pull.handleWheel}
|
|
@@ -267,14 +341,30 @@ function ChatPageInner() {
|
|
|
267
341
|
))}
|
|
268
342
|
</div>
|
|
269
343
|
|
|
270
|
-
{/*
|
|
271
|
-
|
|
344
|
+
{/* 空会话欢迎区:没有 home 槽时的首屏落点(文案来自 chat.json welcome / identity) */}
|
|
345
|
+
{showWelcome ? (
|
|
346
|
+
<div className="chat-page__welcome flex flex-col items-center justify-center gap-2 px-page pb-4 pt-6 text-center">
|
|
347
|
+
<h2 className="chat-page__welcome-title m-0 text-xl font-bold text-foreground">{welcomeTitle}</h2>
|
|
348
|
+
{welcomeSubtitle ? <p className="chat-page__welcome-subtitle m-0 text-sm leading-normal text-muted-foreground">{welcomeSubtitle}</p> : null}
|
|
349
|
+
</div>
|
|
350
|
+
) : null}
|
|
351
|
+
|
|
352
|
+
{/* 历史前插视口锚 + home 槽渲染位(域自持显隐;缺席则不渲染)。flow-root 让首页子元素
|
|
353
|
+
的外边距留在槽内,不折叠到历史入口与首页之间。 */}
|
|
354
|
+
<div className="chat-page__home-slot flow-root" ref={historyAnchorRef}>
|
|
272
355
|
{homeSlot?.({ onSend: send, ...(mallName ? { mallName } : {}) })}
|
|
273
356
|
</div>
|
|
274
357
|
|
|
358
|
+
{/* 空会话的服务入口:快捷问题与欢迎区成组居中,两列全可见(不再贴底单行溢出)。 */}
|
|
359
|
+
{!blocked && isEmptyState ? (
|
|
360
|
+
<div className="chat-page__home-actions mt-6 flex w-full flex-col items-center">
|
|
361
|
+
<QuickCommands variant="grid" hidden={voiceActive} onCommand={send} />
|
|
362
|
+
</div>
|
|
363
|
+
) : null}
|
|
364
|
+
|
|
275
365
|
{/* 业务旁路 emit 的卡片(如欢迎卡) */}
|
|
276
366
|
{inlineOrphanCards.map((card) => (
|
|
277
|
-
<div key={card.id} className="chat-page__orphan-card">
|
|
367
|
+
<div key={card.id} className="chat-page__orphan-card mx-page">
|
|
278
368
|
<CardRenderer card={card} />
|
|
279
369
|
</div>
|
|
280
370
|
))}
|
|
@@ -288,9 +378,9 @@ function ChatPageInner() {
|
|
|
288
378
|
<ScrollToBottomButton visible={showScrollToBottom} onClick={scrollToBottom} />
|
|
289
379
|
</div>
|
|
290
380
|
|
|
291
|
-
{/*
|
|
381
|
+
{/* 对话态保留底部快捷指令;空会话的快捷问题在正文里成组展示(不重复渲染)。 */}
|
|
292
382
|
<div ref={chatBottomRef}>
|
|
293
|
-
{!blocked && <QuickCommands hidden={voiceActive} onCommand={send} />}
|
|
383
|
+
{!blocked && !isEmptyState && <QuickCommands hidden={voiceActive} onCommand={send} />}
|
|
294
384
|
<ChatInput
|
|
295
385
|
generating={isGenerating}
|
|
296
386
|
blocked={blocked}
|
|
@@ -300,6 +390,11 @@ function ChatPageInner() {
|
|
|
300
390
|
onVoiceActivityChange={setVoiceActive}
|
|
301
391
|
/>
|
|
302
392
|
</div>
|
|
393
|
+
{conversationOverlay && (
|
|
394
|
+
<div className="chat-page__conversation-overlay contents">
|
|
395
|
+
{conversationOverlay({ onSend: send, ...(mallName ? { mallName } : {}) })}
|
|
396
|
+
</div>
|
|
397
|
+
)}
|
|
303
398
|
</div>
|
|
304
399
|
);
|
|
305
400
|
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { act } from 'react';
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
import { flushSync } from 'react-dom';
|
|
5
|
+
import { createRoot, type Root } from 'react-dom/client';
|
|
6
|
+
import { ActionList } from './primitives';
|
|
7
|
+
import type { CardActions } from './use-card-actions';
|
|
8
|
+
|
|
9
|
+
const roots: Root[] = [];
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
for (const root of roots) root.unmount();
|
|
13
|
+
roots.length = 0;
|
|
14
|
+
document.body.innerHTML = '';
|
|
15
|
+
vi.useRealTimers();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
function render(node: React.ReactElement): HTMLElement {
|
|
19
|
+
const el = document.createElement('div');
|
|
20
|
+
document.body.appendChild(el);
|
|
21
|
+
const root = createRoot(el);
|
|
22
|
+
roots.push(root);
|
|
23
|
+
flushSync(() => root.render(node));
|
|
24
|
+
return el;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function makeApi(calls: string[], generating = false): CardActions {
|
|
28
|
+
return {
|
|
29
|
+
send: (text: string) => calls.push('send:' + text),
|
|
30
|
+
invoke: (id: string) => calls.push('invoke:' + id),
|
|
31
|
+
disabled: generating,
|
|
32
|
+
isHistory: false,
|
|
33
|
+
isGenerating: generating,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const actions = [{ label: '领券', invoke: { id: 'claim' } }];
|
|
38
|
+
|
|
39
|
+
describe('ActionList pending(方案 A:不改协议)', () => {
|
|
40
|
+
it('点击后只执行一次、按钮禁用并带 aria-busy;20 秒后解除', () => {
|
|
41
|
+
vi.useFakeTimers();
|
|
42
|
+
const calls: string[] = [];
|
|
43
|
+
const el = render(<ActionList api={makeApi(calls)} actions={actions} />);
|
|
44
|
+
const button = el.querySelector('button')!;
|
|
45
|
+
flushSync(() => button.click());
|
|
46
|
+
expect(calls).toEqual(['invoke:claim']);
|
|
47
|
+
expect(button.disabled).toBe(true);
|
|
48
|
+
expect(button.getAttribute('aria-busy')).toBe('true');
|
|
49
|
+
flushSync(() => button.click());
|
|
50
|
+
expect(calls.length).toBe(1);
|
|
51
|
+
flushSync(() => {
|
|
52
|
+
vi.advanceTimersByTime(20000);
|
|
53
|
+
});
|
|
54
|
+
expect(el.querySelector('button')!.disabled).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('同一 tick 连点两次只执行一次(ref 同步判重)', () => {
|
|
58
|
+
const calls: string[] = [];
|
|
59
|
+
const el = render(<ActionList api={makeApi(calls)} actions={actions} />);
|
|
60
|
+
const button = el.querySelector('button')!;
|
|
61
|
+
act(() => {
|
|
62
|
+
button.click();
|
|
63
|
+
button.click();
|
|
64
|
+
});
|
|
65
|
+
expect(calls).toEqual(['invoke:claim']);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('生成一轮结束后第一次点击仍进入 pending', () => {
|
|
69
|
+
const calls: string[] = [];
|
|
70
|
+
const el = document.createElement('div');
|
|
71
|
+
document.body.appendChild(el);
|
|
72
|
+
const root = createRoot(el);
|
|
73
|
+
roots.push(root);
|
|
74
|
+
act(() => root.render(<ActionList api={makeApi(calls, false)} actions={actions} />));
|
|
75
|
+
act(() => root.render(<ActionList api={makeApi(calls, true)} actions={actions} />));
|
|
76
|
+
act(() => root.render(<ActionList api={makeApi(calls, false)} actions={actions} />));
|
|
77
|
+
const button = el.querySelector('button')!;
|
|
78
|
+
act(() => button.click());
|
|
79
|
+
expect(el.querySelector('button')!.getAttribute('aria-busy')).toBe('true');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('没有 send / invoke 的动作不进入 pending', () => {
|
|
83
|
+
const calls: string[] = [];
|
|
84
|
+
const el = render(<ActionList api={makeApi(calls)} actions={[{ label: '占位' }]} />);
|
|
85
|
+
const button = el.querySelector('button')!;
|
|
86
|
+
act(() => button.click());
|
|
87
|
+
expect(calls).toEqual([]);
|
|
88
|
+
expect(button.disabled).toBe(false);
|
|
89
|
+
expect(button.getAttribute('aria-busy')).toBeNull();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { defineCard } from './define-card';
|
|
3
|
+
|
|
4
|
+
describe('defineCard', () => {
|
|
5
|
+
const Component = () => null;
|
|
6
|
+
|
|
7
|
+
it('默认交回 SDK 默认按钮条(存量卡兼容)', () => {
|
|
8
|
+
const card = defineCard({ type: 'demo-card' }, Component);
|
|
9
|
+
expect(card.type).toBe('demo-card');
|
|
10
|
+
expect(card.selfRendersActions).toBe(false);
|
|
11
|
+
expect(card.actionsMode).toBe('default');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("actions: 'self' 表示卡片自己渲染动作", () => {
|
|
15
|
+
const card = defineCard({ type: 'view-card', actions: 'self' }, Component);
|
|
16
|
+
expect(card.selfRendersActions).toBe(true);
|
|
17
|
+
expect(card.actionsMode).toBe('self');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("actions: 'auto' 显式交回 SDK 默认条", () => {
|
|
21
|
+
const card = defineCard({ type: 'legacy-card', actions: 'auto' }, Component);
|
|
22
|
+
expect(card.selfRendersActions).toBe(false);
|
|
23
|
+
expect(card.actionsMode).toBe('auto');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 卡片工厂:把「带静态 type 的组件」这一形态固化。
|
|
3
|
+
*
|
|
4
|
+
* 应用级卡片有两条强制约束同时成立:
|
|
5
|
+
* - app-build 扫描器要求 `export const card = defineCard({ type: '…' }, …)` 的首参是对象字面量;
|
|
6
|
+
* - 生成 registry 把导出值直接当 `CardComponent` 注册,所以运行期必须是组件且带 `.type`。
|
|
7
|
+
* 手写 Object.assign 每张卡重复一遍形态,且容易漏掉 `.type`——本工厂是唯一写法。
|
|
8
|
+
*
|
|
9
|
+
* 动作所有权:默认 `auto`——SDK 用服务端下发的按钮条渲染动作,保持旧卡兼容;
|
|
10
|
+
* 卡片自己渲染动作(`ActionList` / `ViewCard`)时显式 `actions: 'self'`,SDK 不再叠加
|
|
11
|
+
*(确认弹层、入参、历史禁用都由卡内实现)。视图卡自动派生时由生成器写 `self`。
|
|
12
|
+
*/
|
|
13
|
+
import type { ComponentType } from 'react';
|
|
14
|
+
import type { CardComponent } from '@tbox.cn/app-contracts';
|
|
15
|
+
|
|
16
|
+
/** 应用级卡片的渲染入参(与 `CardComponent` 同形,由协议定义,此处仅命名)。 */
|
|
17
|
+
export interface CardRenderProps<T> {
|
|
18
|
+
data: T;
|
|
19
|
+
cardId: string;
|
|
20
|
+
isHistory: boolean;
|
|
21
|
+
actionTokens?: Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `defineCard` 的返回类型:既是可注册的卡片组件,也带静态 `type` 与动作所有权标记。 */
|
|
25
|
+
export type DefinedCard<T> = CardComponent<T> & {
|
|
26
|
+
type: string;
|
|
27
|
+
selfRendersActions: boolean;
|
|
28
|
+
/** 动作所有权来源:default = 未显式声明(SDK 默认条),auto / self = 显式 */
|
|
29
|
+
actionsMode: 'default' | 'auto' | 'self';
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 声明一张应用级卡片。
|
|
34
|
+
*
|
|
35
|
+
* @param meta 卡片类型声明;`type` 必须与同 base 的服务端 meta 的 `cardType` 一致。
|
|
36
|
+
* `actions` 缺省 `'auto'`(SDK 默认按钮条,兼容旧卡);自己渲染动作的卡写 `'self'`,
|
|
37
|
+
* 否则会出现两条按钮或动作被禁用。
|
|
38
|
+
* @param Component 渲染组件,入参为 `CardRenderProps`
|
|
39
|
+
*/
|
|
40
|
+
export function defineCard<T>(
|
|
41
|
+
meta: { type: string; actions?: 'self' | 'auto' },
|
|
42
|
+
Component: ComponentType<CardRenderProps<T>>,
|
|
43
|
+
): DefinedCard<T> {
|
|
44
|
+
const selfRendersActions = meta.actions === 'self';
|
|
45
|
+
const actionsMode = meta.actions ?? 'default';
|
|
46
|
+
return Object.assign(Component, { type: meta.type, selfRendersActions, actionsMode }) as unknown as DefinedCard<T>;
|
|
47
|
+
}
|