@tbox.cn/app-agent-ui-kit 0.0.0-stage → 1.0.0-alpha.6
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/LICENSE +21 -0
- package/README.md +16 -2
- package/assets/keyboard-circle.svg +12 -0
- package/assets/more-circle.svg +3 -0
- package/assets/new-chat.svg +3 -0
- package/assets/quick-command-star.svg +3 -0
- package/assets/send-gradient.svg +14 -0
- package/assets/stop@2x.png +0 -0
- package/assets/voice-circle.svg +3 -0
- package/package.json +58 -3
- package/src/ChatPage.tsx +332 -0
- package/src/card/define-card.ts +34 -0
- package/src/card/primitives.test.tsx +147 -0
- package/src/card/primitives.tsx +627 -0
- package/src/card/use-card-actions.ts +86 -0
- package/src/chat-config.test.ts +46 -0
- package/src/chat-config.ts +80 -0
- package/src/components/ActionBar/DebugButton.test.tsx +84 -0
- package/src/components/ActionBar/DebugButton.tsx +53 -0
- package/src/components/ActionBar/index.test.tsx +83 -0
- package/src/components/ActionBar/index.tsx +158 -0
- package/src/components/CardRenderer.tsx +36 -0
- package/src/components/ChatHeader/index.test.tsx +74 -0
- package/src/components/ChatHeader/index.tsx +77 -0
- package/src/components/ChatInput/index.test.tsx +156 -0
- package/src/components/ChatInput/index.tsx +290 -0
- package/src/components/ChatInput/voice/VoicePressField.tsx +43 -0
- package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
- package/src/components/ChatInput/voice/machine.ts +27 -0
- package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
- package/src/components/HistoryEntry/index.tsx +41 -0
- package/src/components/MediaList/index.tsx +270 -0
- package/src/components/MessageBubble/index.test.tsx +134 -0
- package/src/components/MessageBubble/index.tsx +202 -0
- package/src/components/PullDownIndicator/index.tsx +31 -0
- package/src/components/QuickCommands/index.test.tsx +99 -0
- package/src/components/QuickCommands/index.tsx +52 -0
- package/src/components/ReasoningBlock/index.tsx +61 -0
- package/src/components/ScrollToBottomButton/index.tsx +8 -0
- package/src/components/ToolCallBlock/detail.test.ts +346 -0
- package/src/components/ToolCallBlock/detail.ts +204 -0
- package/src/components/ToolCallBlock/index.test.tsx +261 -0
- package/src/components/ToolCallBlock/index.tsx +230 -0
- package/src/components/ToolCallBlock/titles.ts +102 -0
- package/src/hooks/use-drag-gesture.test.ts +66 -0
- package/src/hooks/use-drag-gesture.ts +129 -0
- package/src/hooks/use-pull-down-history.ts +241 -0
- package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
- package/src/hooks/useMediaUpload.ts +242 -0
- package/src/hooks/useTTSAutoPlay.ts +82 -0
- package/src/icons.ts +39 -0
- package/src/index.ts +122 -0
- package/src/shell.contract.json +59 -0
- package/src/shell.test.ts +61 -0
- package/src/shell.ts +45 -0
- package/src/store-context.tsx +92 -0
- package/src/test/setup.ts +7 -0
- package/src/theme/base.css +132 -0
- package/src/theme/contract.json +72 -0
- package/src/theme/motion.css +19 -0
- package/src/theme/theme-contract.test.ts +69 -0
- package/src/theme/theme.ts +17 -0
- package/src/theme/tokens.css +81 -0
- package/src/types.ts +29 -0
- package/src/utils/copyText.ts +11 -0
- package/src/utils/haptics.test.ts +51 -0
- package/src/utils/haptics.ts +44 -0
- package/src/utils/imagePreview.ts +10 -0
- package/src/utils/pull-down-machine.test.ts +188 -0
- package/src/utils/pull-down-machine.ts +157 -0
- package/src/utils/session-error.ts +5 -0
- package/src/utils/toast.ts +15 -0
- package/src/utils/tool-call-display.test.ts +240 -0
- package/src/utils/tool-call-display.ts +146 -0
- package/src/vite-env.d.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tbox.cn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tbox.cn/app-agent-ui-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Agent 对话壳 UI 组件包(源码发布)——单一模板统一化 W2-1 自 mall 模板抽取。
|
|
4
|
+
|
|
5
|
+
- **真源**:本包(`packages/agent-ui-kit`)。模板出厂副本经 `scripts/sync-agent-ui-kit.mjs` 派生
|
|
6
|
+
(name 改写 `@tbox.cn/app-agent-ui-kit` → `@tbox.cn/app-agent-ui-kit`),guard identity 锁定一致。
|
|
7
|
+
- **消费面**:应用 `<ChatPage store={useChatStore} cards={CHAT_REGISTRY} slots={clientCtx.slots}/>`
|
|
8
|
+
单点装配;域 UI(如商圈主屏)经 home 槽渲染函数自带。
|
|
9
|
+
- **主题**:kit 默认 tokens(`src/theme/tokens.less`);品牌资产包经 `html[data-theme]` 级联覆写 +
|
|
10
|
+
接线三行(`import '<pkg>/index.css'` + `import '<pkg>/brand.js'`[可选] + `applyTheme(id)`;见 docs/reference/templates.md)。
|
|
11
|
+
|
|
12
|
+
## 验证
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm --filter @tbox.cn/app-agent-ui-kit typecheck
|
|
16
|
+
pnpm --filter @tbox.cn/app-agent-ui-kit test
|
|
17
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<svg width="44px" height="44px" viewBox="0 0 44 44" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<g fill="currentColor" fill-rule="evenodd">
|
|
3
|
+
<path d="M22,0 C34.1502645,0 44,9.8497355 44,22 C44,34.1502645 34.1502645,44 22,44 C9.8497355,44 0,34.1502645 0,22 C0,9.8497355 9.8497355,0 22,0 Z M22,3 C11.5065898,3 3,11.5065898 3,22 C3,32.4934102 11.5065898,41 22,41 C32.4934102,41 41,32.4934102 41,22 C41,11.5065898 32.4934102,3 22,3 Z"/>
|
|
4
|
+
<circle cx="14" cy="15" r="2"/>
|
|
5
|
+
<circle cx="22" cy="15" r="2"/>
|
|
6
|
+
<circle cx="30" cy="15" r="2"/>
|
|
7
|
+
<circle cx="14" cy="22" r="2"/>
|
|
8
|
+
<circle cx="22" cy="22" r="2"/>
|
|
9
|
+
<circle cx="30" cy="22" r="2"/>
|
|
10
|
+
<rect x="12" y="28" width="20" height="4" rx="2"/>
|
|
11
|
+
</g>
|
|
12
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg width="44" height="44" viewBox="0 0 44 44" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<path d="M22,0 C34.15,0 44,9.85 44,22 C44,34.15 34.15,44 22,44 C9.85,44 0,34.15 0,22 C0,9.85 9.85,0 22,0 Z M22,3 C11.507,3 3,11.507 3,22 C3,32.493 11.507,41 22,41 C32.493,41 41,32.493 41,22 C41,11.507 32.493,3 22,3 Z M23.5,11.7 L23.499,20.5 L32.3,20.5 L32.3,23.5 L23.499,23.5 L23.5,32.3 L20.5,32.3 L20.499,23.5 L11.7,23.5 L11.7,20.5 L20.499,20.5 L20.5,11.7 L23.5,11.7 Z" fill="currentColor"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg width="44" height="44" viewBox="0 0 44 44" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<path d="M22,2.75 C26.737,2.74 31.309,4.484 34.837,7.645 L35.002,7.803 C35.259,8.103 35.42,8.488 35.42,8.914 C35.42,9.578 35.019,10.176 34.405,10.428 C33.79,10.68 33.084,10.535 32.619,10.061 L32.674,10.094 C29.981,7.669 26.539,6.239 22.92,6.043 L21.93,6.017 C17.686,6.018 13.619,7.719 10.637,10.74 C5.72,15.726 4.605,23.325 7.883,29.513 L8.078,29.872 L8.078,35.922 L14.128,35.922 L14.491,36.117 C16.493,37.175 18.695,37.799 20.955,37.95 L22,37.987 C25.817,37.987 29.429,36.663 32.303,34.236 L32.736,33.851 C33.335,33.311 34.233,33.271 34.877,33.755 C35.512,34.232 35.699,35.105 35.31,35.798 L35.152,36.08 C31.868,39.164 27.599,40.985 23.1,41.221 L21.912,41.25 C18.928,41.232 15.988,40.527 13.321,39.189 L6.791,39.189 C5.697,39.189 4.811,38.303 4.811,37.209 L4.811,30.683 C1.093,23.283 2.502,14.338 8.316,8.441 C11.638,5.079 16.086,3.065 20.805,2.787 L22,2.75 Z M33.755,13.845 C34.657,13.845 35.387,14.579 35.387,15.477 L35.387,20.368 L40.007,20.368 C40.908,20.368 41.639,21.099 41.639,22 C41.639,22.901 40.908,23.632 40.007,23.632 L35.383,23.632 L35.383,28.424 C35.383,29.325 34.653,30.056 33.752,30.056 C32.851,30.056 32.12,29.325 32.12,28.424 L32.12,23.632 L27.588,23.632 C27.005,23.632 26.466,23.321 26.175,22.816 C25.883,22.311 25.883,21.689 26.175,21.184 C26.466,20.679 27.005,20.368 27.588,20.368 L32.124,20.368 L32.124,15.477 C32.124,14.575 32.857,13.845 33.755,13.845 Z" fill="#4E6688"/>
|
|
3
|
+
</svg>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg width="56" height="56" viewBox="0 0 56 56" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient x1="-22%" y1="14.7%" x2="90.1%" y2="75.2%" id="sendGrad">
|
|
4
|
+
<stop stop-color="#AE94FF" offset="0%"/>
|
|
5
|
+
<stop stop-color="#2388FF" offset="100%"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<circle fill="url(#sendGrad)" cx="28" cy="28" r="28"/>
|
|
9
|
+
<g transform="translate(28,28) rotate(-90) translate(-28,-28) translate(10,10)" fill="#FFF">
|
|
10
|
+
<g transform="translate(16.68,17.1) scale(-1,1) translate(-16.68,-17.1) translate(1.86,4.5)">
|
|
11
|
+
<path d="M27.914,10.908 L5.868,10.908 C5.865,10.908 5.862,10.9 5.865,10.9 L14.18,2.908 C14.615,2.49 14.794,1.871 14.649,1.284 C14.504,0.698 14.057,0.234 13.477,0.066 C12.896,-0.101 12.271,0.054 11.836,0.472 L0.518,11.349 C0.187,11.668 0,12.108 0,12.567 C0,13.027 0.187,13.467 0.518,13.785 L11.898,24.725 C12.22,25.037 12.654,25.208 13.103,25.2 C13.551,25.191 13.978,25.004 14.288,24.679 C14.934,24.005 14.913,22.936 14.241,22.289 L5.927,14.297 C5.923,14.294 5.927,14.289 5.931,14.289 L27.95,14.289 C28.849,14.289 29.583,13.585 29.637,12.702 C29.695,11.723 28.891,10.908 27.913,10.908 L27.914,10.908 Z"/>
|
|
12
|
+
</g>
|
|
13
|
+
</g>
|
|
14
|
+
</svg>
|
|
Binary file
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
<svg width="44" height="44" viewBox="0 0 44 44" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<path d="M22,0 C34.15,0 44,9.85 44,22 C44,34.15 34.15,44 22,44 C9.85,44 0,34.15 0,22 C0,9.85 9.85,0 22,0 Z M22,3 C11.507,3 3,11.507 3,22 C3,32.493 11.507,41 22,41 C32.493,41 41,32.493 41,22 C41,11.507 32.493,3 22,3 Z M24.654,8.334 L25.784,9.32 C29.433,12.502 31.568,17.097 31.568,22.034 C31.568,26.932 29.467,31.493 25.87,34.674 L24.746,35.667 L22.759,33.42 L23.883,32.426 C26.843,29.809 28.568,26.063 28.568,22.034 C28.568,17.973 26.814,14.199 23.812,11.58 L22.682,10.594 L24.654,8.334 Z M19.79,13.769 L20.92,14.755 C23.004,16.572 24.224,19.198 24.224,22.018 C24.224,24.815 23.024,27.422 20.969,29.238 L19.845,30.232 L17.858,27.985 L18.982,26.991 C20.399,25.738 21.224,23.946 21.224,22.018 C21.224,20.074 20.385,18.269 18.948,17.016 L17.818,16.03 L19.79,13.769 Z M16.289,19.962 C16.841,19.962 17.289,20.409 17.289,20.962 L17.289,23.038 C17.289,23.591 16.841,24.038 16.289,24.038 L14.212,24.038 C13.66,24.038 13.212,23.591 13.212,23.038 L13.212,20.962 C13.212,20.409 13.66,19.962 14.212,19.962 L16.289,19.962 Z" fill="currentColor"/>
|
|
3
|
+
</svg>
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-agent-ui-kit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "1.0.0-alpha.6",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Agent 对话壳 UI 组件包(源码发布,单一模板统一化 W2-1):ChatPage/壳组件族/主题 tokens。",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"import": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"./theme": {
|
|
12
|
+
"types": "./src/theme/theme.ts",
|
|
13
|
+
"import": "./src/theme/theme.ts"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.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
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"assets",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@phosphor-icons/react": "2.1.10",
|
|
31
|
+
"antd-mobile": "5.42.3",
|
|
32
|
+
"react-markdown": "9.1.0",
|
|
33
|
+
"remark-gfm": "4.0.1",
|
|
34
|
+
"zustand": "4.5.7",
|
|
35
|
+
"@tbox.cn/app-contracts": "1.0.0-alpha.6",
|
|
36
|
+
"@tbox.cn/app-agent-sdk-client": "1.0.0-alpha.6"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": "18.3.1",
|
|
40
|
+
"react-dom": "18.3.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "24.13.3",
|
|
44
|
+
"@types/react": "18.3.31",
|
|
45
|
+
"@types/react-dom": "18.3.7",
|
|
46
|
+
"happy-dom": "20.11.2",
|
|
47
|
+
"react": "18.3.1",
|
|
48
|
+
"react-dom": "18.3.1",
|
|
49
|
+
"typescript": "5.9.3",
|
|
50
|
+
"vite": "6.4.3",
|
|
51
|
+
"vitest": "4.1.10"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=20.0.0"
|
|
55
|
+
},
|
|
56
|
+
"license": "MIT",
|
|
57
|
+
"scripts": {
|
|
58
|
+
"typecheck": "tsc --noEmit",
|
|
59
|
+
"test": "vitest run"
|
|
60
|
+
}
|
|
6
61
|
}
|
package/src/ChatPage.tsx
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChatPage(单一模板统一化 W2-1:自 mall 模板 pages/index/index.tsx 抽取通用页)。
|
|
3
|
+
*
|
|
4
|
+
* - 自组 KitChatProvider(store/cards/rawCards/slots 注入位;应用单点装配);
|
|
5
|
+
* - 应用形态由 home 槽存在性派生(D7):注册 home → 空态渲染槽(home-first,会话态与
|
|
6
|
+
* 空态恒挂载——域自持其显隐);缺席 → chat 流(空态为空体,landing 槽已退役);
|
|
7
|
+
* - 域自持段(首屏数据/门禁/详情弹层/活动预热)经槽渲染函数自带——本页零域语义。
|
|
8
|
+
*/
|
|
9
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
10
|
+
import { consumeAutoSendQuery, runAutoSend } from '@tbox.cn/app-agent-sdk-client';
|
|
11
|
+
import { usePullDownHistory } from './hooks/use-pull-down-history';
|
|
12
|
+
import type { TboxCardPayload } from '@tbox.cn/app-contracts';
|
|
13
|
+
import { CardRenderer } from './components/CardRenderer';
|
|
14
|
+
import { ChatInput } from './components/ChatInput';
|
|
15
|
+
import HistoryEntry from './components/HistoryEntry';
|
|
16
|
+
import { MessageBubble } from './components/MessageBubble';
|
|
17
|
+
import { QuickCommands } from './components/QuickCommands';
|
|
18
|
+
import ScrollToBottomButton from './components/ScrollToBottomButton';
|
|
19
|
+
import { useChatConfigStore } from './chat-config';
|
|
20
|
+
import { resolveBrandName } from './shell';
|
|
21
|
+
import {
|
|
22
|
+
KitChatProvider,
|
|
23
|
+
useChatStore,
|
|
24
|
+
useClientSlots,
|
|
25
|
+
useInlineCardFilter,
|
|
26
|
+
type KitChatContextValue,
|
|
27
|
+
} from './store-context';
|
|
28
|
+
import type { ChatInputMessage } from './types';
|
|
29
|
+
import './theme/tokens.css';
|
|
30
|
+
|
|
31
|
+
export interface ChatPageProps {
|
|
32
|
+
/** 应用 chat store(zustand 绑定 hook + API;如 `useChatStore`) */
|
|
33
|
+
store: KitChatContextValue['store'];
|
|
34
|
+
/** 聊天入口卡片注册表(模块注册 + 组合层覆盖) */
|
|
35
|
+
cards: KitChatContextValue['cards'];
|
|
36
|
+
/** 原始形态卡片注册表(缺省 = cards——无覆盖应用零配置) */
|
|
37
|
+
rawCards?: KitChatContextValue['rawCards'];
|
|
38
|
+
/** 客户端槽注册表(home 首期唯一槽;缺席 = chat 流形态) */
|
|
39
|
+
slots?: KitChatContextValue['slots'];
|
|
40
|
+
/**
|
|
41
|
+
* 旁路卡内联流过滤(域注入;缺省恒等):域可声明哪些 orphan 卡不进内联流
|
|
42
|
+
* (由域浮层自持,如 mall 会员门禁卡系)——过滤结果同时参与 home-first 形态判定。
|
|
43
|
+
*/
|
|
44
|
+
filterInlineCards?: (cards: readonly TboxCardPayload[]) => TboxCardPayload[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function ChatPage({ store, cards, rawCards, slots, filterInlineCards }: ChatPageProps) {
|
|
48
|
+
return (
|
|
49
|
+
<KitChatProvider
|
|
50
|
+
store={store}
|
|
51
|
+
cards={cards}
|
|
52
|
+
rawCards={rawCards ?? cards}
|
|
53
|
+
{...(slots ? { slots } : {})}
|
|
54
|
+
{...(filterInlineCards ? { filterInlineCards } : {})}
|
|
55
|
+
>
|
|
56
|
+
<ChatPageInner />
|
|
57
|
+
</KitChatProvider>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function ChatPageInner() {
|
|
62
|
+
const messages = useChatStore((s) => s.messages);
|
|
63
|
+
const historyMessages = useChatStore((s) => s.historyMessages);
|
|
64
|
+
const historyHasMore = useChatStore((s) => s.historyHasMore);
|
|
65
|
+
const isLoadingHistory = useChatStore((s) => s.isLoadingHistory);
|
|
66
|
+
const loadMoreHistory = useChatStore((s) => s.loadMoreHistory);
|
|
67
|
+
const isGenerating = useChatStore((s) => s.isGenerating);
|
|
68
|
+
const runtimeStatus = useChatStore((s) => s.runtimeStatus);
|
|
69
|
+
const authBlocked = useChatStore((s) => s.authBlocked);
|
|
70
|
+
const orphanCards = useChatStore((s) => s.orphanCards);
|
|
71
|
+
const cardsByMessageId = useChatStore((s) => s.cardsByMessageId);
|
|
72
|
+
const initConversation = useChatStore((s) => s.initConversation);
|
|
73
|
+
const destroy = useChatStore((s) => s.destroy);
|
|
74
|
+
const sendMessage = useChatStore((s) => s.sendMessage);
|
|
75
|
+
const cancelGeneration = useChatStore((s) => s.cancelGeneration);
|
|
76
|
+
const newConversation = useChatStore((s) => s.newConversation);
|
|
77
|
+
const retryBootstrap = useChatStore((s) => s.retryBootstrap);
|
|
78
|
+
// 品牌名 = HELLO_ACK.mallScope.mallName(服务端 buildScope 回显;存在即可渲染的 kit 同源约定)
|
|
79
|
+
const mallName = useChatStore((s) => s.helloAck?.mallScope?.mallName);
|
|
80
|
+
// chat settings 水合:HELLO_ACK 服务端真源 → 静态 store(ack 未达/键缺席保留构建期快照;
|
|
81
|
+
// 重连/新会话产生新 helloAck 再水合——applyServerSettings 幂等)
|
|
82
|
+
const serverSettings = useChatStore((s) => s.helloAck?.chatConfig?.settings);
|
|
83
|
+
const applyServerSettings = useChatConfigStore((s) => s.applyServerSettings);
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
applyServerSettings(serverSettings);
|
|
86
|
+
}, [serverSettings, applyServerSettings]);
|
|
87
|
+
|
|
88
|
+
const slots = useClientSlots();
|
|
89
|
+
const homeSlot = slots?.resolve('home');
|
|
90
|
+
const identity = useChatConfigStore((s) => s.identity);
|
|
91
|
+
const welcome = useChatConfigStore((s) => s.welcome);
|
|
92
|
+
// 常驻品牌条:chat.json identity.name → HELLO_ACK 的 mallName → 不渲染(壳契约见 shell.ts)
|
|
93
|
+
const brandName = resolveBrandName(identity, mallName);
|
|
94
|
+
|
|
95
|
+
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
|
|
96
|
+
const [voiceActive, setVoiceActive] = useState(false);
|
|
97
|
+
const bodyRef = useRef<HTMLDivElement>(null);
|
|
98
|
+
// 底部输入区(QuickCommands + ChatInput)高度测量:底部避让消费(如半屏 sheet)
|
|
99
|
+
const chatBottomRef = useRef<HTMLDivElement>(null);
|
|
100
|
+
// 历史前插视口锚:home 槽位占位(历史插入发生在其上,保持用户看到的位置)
|
|
101
|
+
const historyAnchorRef = useRef<HTMLDivElement>(null);
|
|
102
|
+
const historyLoadInFlightRef = useRef<Promise<void> | null>(null);
|
|
103
|
+
const pendingHistoryViewportRef = useRef<{ anchorTop: number } | null>(null);
|
|
104
|
+
const isAtTopRef = useRef(true);
|
|
105
|
+
const isNearBottomRef = useRef(true);
|
|
106
|
+
|
|
107
|
+
const startNewConversation = useCallback(() => {
|
|
108
|
+
void newConversation();
|
|
109
|
+
const el = bodyRef.current;
|
|
110
|
+
if (el) el.scrollTop = 0;
|
|
111
|
+
isAtTopRef.current = true;
|
|
112
|
+
isNearBottomRef.current = false;
|
|
113
|
+
setShowScrollToBottom(false);
|
|
114
|
+
}, [newConversation]);
|
|
115
|
+
|
|
116
|
+
const send = useCallback((input: string | ChatInputMessage) => {
|
|
117
|
+
// 主动发送代表用户要回到最新对话;待消息 DOM 提交后由下方 effect 落底。
|
|
118
|
+
// 生成中 store 会拒绝新消息,此时不改变用户正在查看的滚动位置。
|
|
119
|
+
if (isGenerating) return;
|
|
120
|
+
isNearBottomRef.current = true;
|
|
121
|
+
setShowScrollToBottom(false);
|
|
122
|
+
sendMessage(input);
|
|
123
|
+
}, [isGenerating, sendMessage]);
|
|
124
|
+
|
|
125
|
+
// 连接终态阻断(登录失败/scope 拒绝):禁用输入 + 提示条 + 重试。
|
|
126
|
+
// 门控仅 runtimeStatus=failed 或 authBlocked——bootstrapping 等过渡态不闪现横幅
|
|
127
|
+
const blocked =
|
|
128
|
+
runtimeStatus === 'failed' || authBlocked
|
|
129
|
+
? {
|
|
130
|
+
message: authBlocked ?? '登录失败,暂时无法对话',
|
|
131
|
+
onRetry: () => void retryBootstrap(true),
|
|
132
|
+
}
|
|
133
|
+
: undefined;
|
|
134
|
+
|
|
135
|
+
const scrollToBottom = useCallback(() => {
|
|
136
|
+
const el = bodyRef.current;
|
|
137
|
+
if (el) el.scrollTop = el.scrollHeight;
|
|
138
|
+
isNearBottomRef.current = true;
|
|
139
|
+
setShowScrollToBottom(false);
|
|
140
|
+
}, []);
|
|
141
|
+
|
|
142
|
+
const NEAR_BOTTOM = 150;
|
|
143
|
+
const onScroll = useCallback(() => {
|
|
144
|
+
const el = bodyRef.current;
|
|
145
|
+
if (!el) return;
|
|
146
|
+
isAtTopRef.current = el.scrollTop <= 5;
|
|
147
|
+
const distanceToBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
|
|
148
|
+
isNearBottomRef.current = distanceToBottom < NEAR_BOTTOM;
|
|
149
|
+
setShowScrollToBottom(!isNearBottomRef.current);
|
|
150
|
+
}, []);
|
|
151
|
+
|
|
152
|
+
// 顶部入口与轻量下拉共用同一加载任务;同一周期复用 Promise,避免点击/手势重复请求。
|
|
153
|
+
const onLoadHistory = useCallback(() => {
|
|
154
|
+
if (historyLoadInFlightRef.current) return historyLoadInFlightRef.current;
|
|
155
|
+
const anchor = historyAnchorRef.current;
|
|
156
|
+
if (anchor) {
|
|
157
|
+
pendingHistoryViewportRef.current = {
|
|
158
|
+
anchorTop: anchor.getBoundingClientRect().top,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
const task = loadMoreHistory().finally(() => {
|
|
162
|
+
if (historyLoadInFlightRef.current === task) historyLoadInFlightRef.current = null;
|
|
163
|
+
});
|
|
164
|
+
historyLoadInFlightRef.current = task;
|
|
165
|
+
return task;
|
|
166
|
+
}, [loadMoreHistory]);
|
|
167
|
+
|
|
168
|
+
// 历史插入发生在当前内容上方:绘制前只补一次新增高度,保持用户原本看到的位置。
|
|
169
|
+
// 不设置定时器、不钉住 scrollTop;补偿后下一次滚动立即由用户接管。
|
|
170
|
+
useLayoutEffect(() => {
|
|
171
|
+
const pending = pendingHistoryViewportRef.current;
|
|
172
|
+
const anchor = historyAnchorRef.current;
|
|
173
|
+
if (!pending || !anchor) return;
|
|
174
|
+
pendingHistoryViewportRef.current = null;
|
|
175
|
+
const anchorDelta = anchor.getBoundingClientRect().top - pending.anchorTop;
|
|
176
|
+
if (Math.abs(anchorDelta) > 0.5) {
|
|
177
|
+
const el = bodyRef.current;
|
|
178
|
+
if (el) el.scrollTop += anchorDelta;
|
|
179
|
+
}
|
|
180
|
+
}, [historyMessages]);
|
|
181
|
+
|
|
182
|
+
const filterInlineCards = useInlineCardFilter();
|
|
183
|
+
const inlineOrphanCards = useMemo(
|
|
184
|
+
() => filterInlineCards(orphanCards),
|
|
185
|
+
[orphanCards, filterInlineCards],
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
// 是否仍为空会话(home-first 形态判定);home 槽缺席时始终为对话形态
|
|
189
|
+
const showHome = !!homeSlot
|
|
190
|
+
&& historyMessages.length === 0
|
|
191
|
+
&& messages.length === 0
|
|
192
|
+
&& inlineOrphanCards.length === 0;
|
|
193
|
+
|
|
194
|
+
const liveCardSignature = useMemo(
|
|
195
|
+
() => messages
|
|
196
|
+
.map((message) => `${message.id}:${(cardsByMessageId[message.id] ?? []).map((card) => card.id).join(',')}`)
|
|
197
|
+
.join('|'),
|
|
198
|
+
[messages, cardsByMessageId],
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
const showHistoryEntry = historyHasMore && historyMessages.length === 0;
|
|
202
|
+
|
|
203
|
+
// 空会话欢迎区:没有 home 槽时给首屏一个落点(标题来自 chat.json welcome,缺省「开始对话」)
|
|
204
|
+
const showWelcome = !homeSlot
|
|
205
|
+
&& historyMessages.length === 0
|
|
206
|
+
&& messages.length === 0
|
|
207
|
+
&& inlineOrphanCards.length === 0;
|
|
208
|
+
const welcomeTitle = welcome?.title ?? identity?.name ?? '开始对话';
|
|
209
|
+
const welcomeSubtitle = welcome?.subtitle ?? identity?.tagline;
|
|
210
|
+
|
|
211
|
+
const pull = usePullDownHistory({
|
|
212
|
+
isAtTopRef,
|
|
213
|
+
isLoadingHistory,
|
|
214
|
+
hasMore: historyHasMore,
|
|
215
|
+
onLoad: onLoadHistory,
|
|
216
|
+
disabled: runtimeStatus !== 'ready',
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
useEffect(() => {
|
|
220
|
+
// 深链自动发送:同步消费(StrictMode 双挂载/刷新安全),引导完成后等连接就绪发送
|
|
221
|
+
const autoQuery = consumeAutoSendQuery();
|
|
222
|
+
void initConversation().then(async () => {
|
|
223
|
+
if (autoQuery) await runAutoSend(autoQuery, sendMessage);
|
|
224
|
+
});
|
|
225
|
+
return () => {
|
|
226
|
+
destroy();
|
|
227
|
+
};
|
|
228
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
229
|
+
}, []);
|
|
230
|
+
|
|
231
|
+
// 底部输入区高度 → CSS 变量 --tbox-chat-bottom-height,供半屏 sheet(portal 到 body)避让消费。
|
|
232
|
+
useEffect(() => {
|
|
233
|
+
const el = chatBottomRef.current;
|
|
234
|
+
if (!el) return;
|
|
235
|
+
const update = () =>
|
|
236
|
+
document.documentElement.style.setProperty('--tbox-chat-bottom-height', `${el.offsetHeight}px`);
|
|
237
|
+
update();
|
|
238
|
+
const ro = new ResizeObserver(update);
|
|
239
|
+
ro.observe(el);
|
|
240
|
+
return () => ro.disconnect();
|
|
241
|
+
}, []);
|
|
242
|
+
|
|
243
|
+
// 仅在已有对话内容且贴近底部时自动滚动,避免打断用户上翻查看历史。
|
|
244
|
+
// 依赖含 cardsByMessageId:主卡在文字完成后挂载、消息行增高,需补滚避免卡片截出视口
|
|
245
|
+
useEffect(() => {
|
|
246
|
+
const hasLiveContent = messages.length > 0 || orphanCards.length > 0;
|
|
247
|
+
if (hasLiveContent && isNearBottomRef.current) {
|
|
248
|
+
const el = bodyRef.current;
|
|
249
|
+
if (el) el.scrollTop = el.scrollHeight;
|
|
250
|
+
}
|
|
251
|
+
}, [messages, orphanCards, liveCardSignature]);
|
|
252
|
+
|
|
253
|
+
return (
|
|
254
|
+
<div className={`chat-page relative flex h-full w-full min-h-0 flex-col overflow-hidden bg-background ${showHome ? '' : ' chat-page--conversation'}`}>
|
|
255
|
+
<div className="chat-page__bg pointer-events-none absolute inset-0 z-0 bg-background" />
|
|
256
|
+
|
|
257
|
+
{brandName ? (
|
|
258
|
+
<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}>
|
|
259
|
+
<span className="chat-page__brand-name text-base font-bold tracking-wide text-foreground">{brandName}</span>
|
|
260
|
+
{identity?.tagline ? <span className="chat-page__brand-tagline text-xs text-muted-foreground">{identity.tagline}</span> : null}
|
|
261
|
+
</div>
|
|
262
|
+
) : null}
|
|
263
|
+
|
|
264
|
+
<div className="chat-page__body-wrapper relative z-10 flex min-h-0 flex-1 flex-col">
|
|
265
|
+
<div
|
|
266
|
+
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"
|
|
267
|
+
ref={bodyRef}
|
|
268
|
+
onScroll={onScroll}
|
|
269
|
+
onWheel={pull.handleWheel}
|
|
270
|
+
onTouchStart={pull.handleTouchStart}
|
|
271
|
+
onTouchMove={pull.handleTouchMove}
|
|
272
|
+
onTouchEnd={pull.handleTouchEnd}
|
|
273
|
+
onTouchCancel={pull.handleTouchCancel}
|
|
274
|
+
>
|
|
275
|
+
<HistoryEntry
|
|
276
|
+
visible={showHistoryEntry}
|
|
277
|
+
loading={isLoadingHistory}
|
|
278
|
+
disabled={runtimeStatus !== 'ready' || isLoadingHistory}
|
|
279
|
+
pullStatus={pull.pullStatus}
|
|
280
|
+
onLoad={() => void onLoadHistory()}
|
|
281
|
+
/>
|
|
282
|
+
|
|
283
|
+
<div className="chat-page__history">
|
|
284
|
+
{historyMessages.map((msg) => (
|
|
285
|
+
<MessageBubble key={msg.id} message={msg} />
|
|
286
|
+
))}
|
|
287
|
+
</div>
|
|
288
|
+
|
|
289
|
+
{/* 空会话欢迎区:没有 home 槽时的首屏落点(文案来自 chat.json welcome / identity) */}
|
|
290
|
+
{showWelcome ? (
|
|
291
|
+
<div className="chat-page__welcome flex flex-col items-center justify-center gap-2 px-page pb-5 pt-15 text-center">
|
|
292
|
+
<h2 className="chat-page__welcome-title m-0 text-xl font-bold text-foreground">{welcomeTitle}</h2>
|
|
293
|
+
{welcomeSubtitle ? <p className="chat-page__welcome-subtitle m-0 text-sm leading-normal text-muted-foreground">{welcomeSubtitle}</p> : null}
|
|
294
|
+
</div>
|
|
295
|
+
) : null}
|
|
296
|
+
|
|
297
|
+
{/* 历史前插视口锚 + home 槽渲染位(域自持显隐;缺席则不渲染) */}
|
|
298
|
+
<div className="chat-page__home-slot" ref={historyAnchorRef}>
|
|
299
|
+
{homeSlot?.({ onSend: send, ...(mallName ? { mallName } : {}) })}
|
|
300
|
+
</div>
|
|
301
|
+
|
|
302
|
+
{/* 业务旁路 emit 的卡片(如欢迎卡) */}
|
|
303
|
+
{inlineOrphanCards.map((card) => (
|
|
304
|
+
<div key={card.id} className="chat-page__orphan-card mx-page">
|
|
305
|
+
<CardRenderer card={card} />
|
|
306
|
+
</div>
|
|
307
|
+
))}
|
|
308
|
+
|
|
309
|
+
{/* 消息(每条消息下的卡片由 MessageBubble 内部渲染) */}
|
|
310
|
+
{messages.map((msg) => (
|
|
311
|
+
<MessageBubble key={msg.id} message={msg} />
|
|
312
|
+
))}
|
|
313
|
+
</div>
|
|
314
|
+
|
|
315
|
+
<ScrollToBottomButton visible={showScrollToBottom} onClick={scrollToBottom} />
|
|
316
|
+
</div>
|
|
317
|
+
|
|
318
|
+
{/* 首页与对话态都保留快捷指令;语音活跃时只隐去胶囊并保留灰底占位,阻断时才卸载。 */}
|
|
319
|
+
<div ref={chatBottomRef}>
|
|
320
|
+
{!blocked && <QuickCommands hidden={voiceActive} onCommand={send} />}
|
|
321
|
+
<ChatInput
|
|
322
|
+
generating={isGenerating}
|
|
323
|
+
blocked={blocked}
|
|
324
|
+
onSend={send}
|
|
325
|
+
onCancel={cancelGeneration}
|
|
326
|
+
onNewChat={startNewConversation}
|
|
327
|
+
onVoiceActivityChange={setVoiceActive}
|
|
328
|
+
/>
|
|
329
|
+
</div>
|
|
330
|
+
</div>
|
|
331
|
+
);
|
|
332
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 卡片工厂:把「带静态 type 的组件」这一形态固化。
|
|
3
|
+
*
|
|
4
|
+
* 应用级卡片有两条强制约束同时成立:
|
|
5
|
+
* - app-build 扫描器要求 `export const card = defineCard({ type: '…' }, …)` 的首参是对象字面量;
|
|
6
|
+
* - 生成 registry 把导出值直接当 `CardComponent` 注册,所以运行期必须是组件且带 `.type`。
|
|
7
|
+
* 手写 Object.assign 每张卡重复一遍形态,且容易漏掉 `.type`——本工厂是唯一写法。
|
|
8
|
+
*/
|
|
9
|
+
import type { ComponentType } from 'react';
|
|
10
|
+
import type { CardComponent } from '@tbox.cn/app-contracts';
|
|
11
|
+
|
|
12
|
+
/** 应用级卡片的渲染入参(与 `CardComponent` 同形,由协议定义,此处仅命名)。 */
|
|
13
|
+
export interface CardRenderProps<T> {
|
|
14
|
+
data: T;
|
|
15
|
+
cardId: string;
|
|
16
|
+
isHistory: boolean;
|
|
17
|
+
actionTokens?: Record<string, string>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** `defineCard` 的返回类型:既是可注册的卡片组件,也带静态 `type`。 */
|
|
21
|
+
export type DefinedCard<T> = CardComponent<T> & { type: string };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 声明一张应用级卡片。
|
|
25
|
+
*
|
|
26
|
+
* @param meta 卡片类型声明;`type` 必须与同 base 的服务端 meta 的 `cardType` 一致
|
|
27
|
+
* @param Component 渲染组件,入参为 `CardRenderProps`
|
|
28
|
+
*/
|
|
29
|
+
export function defineCard<T>(
|
|
30
|
+
meta: { type: string },
|
|
31
|
+
Component: ComponentType<CardRenderProps<T>>,
|
|
32
|
+
): DefinedCard<T> {
|
|
33
|
+
return Object.assign(Component, { type: meta.type }) as unknown as DefinedCard<T>;
|
|
34
|
+
}
|