@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.
Files changed (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +16 -2
  3. package/assets/keyboard-circle.svg +12 -0
  4. package/assets/more-circle.svg +3 -0
  5. package/assets/new-chat.svg +3 -0
  6. package/assets/quick-command-star.svg +3 -0
  7. package/assets/send-gradient.svg +14 -0
  8. package/assets/stop@2x.png +0 -0
  9. package/assets/voice-circle.svg +3 -0
  10. package/package.json +58 -3
  11. package/src/ChatPage.tsx +332 -0
  12. package/src/card/define-card.ts +34 -0
  13. package/src/card/primitives.test.tsx +147 -0
  14. package/src/card/primitives.tsx +627 -0
  15. package/src/card/use-card-actions.ts +86 -0
  16. package/src/chat-config.test.ts +46 -0
  17. package/src/chat-config.ts +80 -0
  18. package/src/components/ActionBar/DebugButton.test.tsx +84 -0
  19. package/src/components/ActionBar/DebugButton.tsx +53 -0
  20. package/src/components/ActionBar/index.test.tsx +83 -0
  21. package/src/components/ActionBar/index.tsx +158 -0
  22. package/src/components/CardRenderer.tsx +36 -0
  23. package/src/components/ChatHeader/index.test.tsx +74 -0
  24. package/src/components/ChatHeader/index.tsx +77 -0
  25. package/src/components/ChatInput/index.test.tsx +156 -0
  26. package/src/components/ChatInput/index.tsx +290 -0
  27. package/src/components/ChatInput/voice/VoicePressField.tsx +43 -0
  28. package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
  29. package/src/components/ChatInput/voice/machine.ts +27 -0
  30. package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
  31. package/src/components/HistoryEntry/index.tsx +41 -0
  32. package/src/components/MediaList/index.tsx +270 -0
  33. package/src/components/MessageBubble/index.test.tsx +134 -0
  34. package/src/components/MessageBubble/index.tsx +202 -0
  35. package/src/components/PullDownIndicator/index.tsx +31 -0
  36. package/src/components/QuickCommands/index.test.tsx +99 -0
  37. package/src/components/QuickCommands/index.tsx +52 -0
  38. package/src/components/ReasoningBlock/index.tsx +61 -0
  39. package/src/components/ScrollToBottomButton/index.tsx +8 -0
  40. package/src/components/ToolCallBlock/detail.test.ts +346 -0
  41. package/src/components/ToolCallBlock/detail.ts +204 -0
  42. package/src/components/ToolCallBlock/index.test.tsx +261 -0
  43. package/src/components/ToolCallBlock/index.tsx +230 -0
  44. package/src/components/ToolCallBlock/titles.ts +102 -0
  45. package/src/hooks/use-drag-gesture.test.ts +66 -0
  46. package/src/hooks/use-drag-gesture.ts +129 -0
  47. package/src/hooks/use-pull-down-history.ts +241 -0
  48. package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
  49. package/src/hooks/useMediaUpload.ts +242 -0
  50. package/src/hooks/useTTSAutoPlay.ts +82 -0
  51. package/src/icons.ts +39 -0
  52. package/src/index.ts +122 -0
  53. package/src/shell.contract.json +59 -0
  54. package/src/shell.test.ts +61 -0
  55. package/src/shell.ts +45 -0
  56. package/src/store-context.tsx +92 -0
  57. package/src/test/setup.ts +7 -0
  58. package/src/theme/base.css +132 -0
  59. package/src/theme/contract.json +72 -0
  60. package/src/theme/motion.css +19 -0
  61. package/src/theme/theme-contract.test.ts +69 -0
  62. package/src/theme/theme.ts +17 -0
  63. package/src/theme/tokens.css +81 -0
  64. package/src/types.ts +29 -0
  65. package/src/utils/copyText.ts +11 -0
  66. package/src/utils/haptics.test.ts +51 -0
  67. package/src/utils/haptics.ts +44 -0
  68. package/src/utils/imagePreview.ts +10 -0
  69. package/src/utils/pull-down-machine.test.ts +188 -0
  70. package/src/utils/pull-down-machine.ts +157 -0
  71. package/src/utils/session-error.ts +5 -0
  72. package/src/utils/toast.ts +15 -0
  73. package/src/utils/tool-call-display.test.ts +240 -0
  74. package/src/utils/tool-call-display.ts +146 -0
  75. package/src/vite-env.d.ts +1 -0
@@ -0,0 +1,82 @@
1
+ import { useEffect } from 'react';
2
+ import type { CoreChatState } from '@tbox.cn/app-agent-sdk-client';
3
+ import { useChatStoreApi } from '../store-context';
4
+ import { useTTSAdapter } from '@tbox.cn/app-agent-sdk-client';
5
+
6
+ const FLUSH_MS = 300; // 累积 delta 后批量送,避免过多 WS 消息
7
+
8
+ /**
9
+ * TTS 自动播放:开启后订阅流式消息,**边生成边送** TTS 流式播放(对齐原模板的流式自动朗读)。
10
+ * 关闭 / 卸载时停止。
11
+ */
12
+ export function useTTSAutoPlay(enabled: boolean) {
13
+ const tts = useTTSAdapter();
14
+ const store = useChatStoreApi();
15
+
16
+ useEffect(() => {
17
+ if (!enabled || !tts || !tts.startStreaming) return;
18
+
19
+ let activeId: string | null = null;
20
+ let prevContent = '';
21
+ let pending = '';
22
+ let timer: ReturnType<typeof setTimeout> | null = null;
23
+
24
+ const clearTimer = () => {
25
+ if (timer) {
26
+ clearTimeout(timer);
27
+ timer = null;
28
+ }
29
+ };
30
+ const flush = () => {
31
+ clearTimer();
32
+ if (pending) {
33
+ tts.sendText?.(pending);
34
+ pending = '';
35
+ }
36
+ };
37
+ const enqueue = (delta: string) => {
38
+ pending += delta;
39
+ if (!timer) timer = setTimeout(flush, FLUSH_MS);
40
+ };
41
+
42
+ const handle = (state: CoreChatState) => {
43
+ const { messages, streamingMsgId } = state;
44
+ const msg = messages.find((m) => m.id === streamingMsgId);
45
+
46
+ // 流结束 / 无流式消息 → 结束这轮
47
+ if (!msg || msg.status === 'done') {
48
+ if (activeId) {
49
+ flush();
50
+ tts.finishStreaming?.();
51
+ activeId = null;
52
+ prevContent = '';
53
+ }
54
+ return;
55
+ }
56
+ // 新流式消息 → 开一轮
57
+ if (msg.status === 'streaming' && msg.id !== activeId) {
58
+ tts.stop();
59
+ clearTimer();
60
+ pending = '';
61
+ activeId = msg.id;
62
+ prevContent = '';
63
+ tts.startStreaming?.();
64
+ return;
65
+ }
66
+ // 内容增量 → 入队
67
+ if (activeId === msg.id && msg.content.length > prevContent.length) {
68
+ enqueue(msg.content.slice(prevContent.length));
69
+ prevContent = msg.content;
70
+ }
71
+ };
72
+
73
+ const unsub = store.subscribe(handle);
74
+ handle(store.getState());
75
+
76
+ return () => {
77
+ unsub();
78
+ clearTimer();
79
+ if (activeId) tts.stop();
80
+ };
81
+ }, [enabled, tts, store]);
82
+ }
package/src/icons.ts ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * 图标入口:应用侧不再直接依赖 `@phosphor-icons/react`。
3
+ *
4
+ * 只再导出卡片与空态常用的一组,含义稳定、体积可控;需要新图标时在此增补,
5
+ * 应用侧 `import { IconName } from '@tbox.cn/app-agent-ui-kit/icons'`。
6
+ */
7
+ export {
8
+ ArrowRight,
9
+ ArrowsClockwise,
10
+ Calendar,
11
+ CaretDown,
12
+ CaretRight,
13
+ Check,
14
+ CheckCircle,
15
+ Clock,
16
+ Coins,
17
+ Copy,
18
+ CreditCard,
19
+ CurrencyCny,
20
+ Gift,
21
+ Info,
22
+ MapPin,
23
+ MapTrifold,
24
+ Plus,
25
+ QrCode,
26
+ Receipt,
27
+ SealCheck,
28
+ ShareNetwork,
29
+ Sparkle,
30
+ Star,
31
+ Storefront,
32
+ Ticket,
33
+ Trash,
34
+ User,
35
+ UserCircle,
36
+ Warning,
37
+ WarningCircle,
38
+ X,
39
+ } from '@phosphor-icons/react';
package/src/index.ts ADDED
@@ -0,0 +1,122 @@
1
+ /**
2
+ * @tbox.cn/app-agent-ui-kit:Agent 对话壳 UI 组件包(源码发布,单一模板统一化 W2-1)。
3
+ *
4
+ * 消费面:应用经 `<ChatPage store cards slots .../>` 单点装配(或自组 KitChatProvider 树);
5
+ * 域模块(如 mall-core)经 home 槽渲染函数自持域 UI——kit 组件零域语义。
6
+ * 主题:`@tbox.cn/app-agent-ui-kit/theme`(applyTheme)+ `theme/tokens.css` + `theme/base.css`(配合纯 CSS 主题包级联)。
7
+ */
8
+
9
+ // ── 首屏页面与注入位 ──
10
+ export { ChatPage } from './ChatPage';
11
+ export type { ChatPageProps } from './ChatPage';
12
+ // ── 行为面(W2-3 收口:SDK client 导出退役后宿主——下拉加载历史手势族)──
13
+ export { usePullDownHistory } from './hooks/use-pull-down-history';
14
+ export type { UsePullDownHistoryOptions } from './hooks/use-pull-down-history';
15
+ export {
16
+ KitChatProvider,
17
+ useChatStore,
18
+ useChatStoreApi,
19
+ useChatCards,
20
+ useChatRawCards,
21
+ useClientSlots,
22
+ } from './store-context';
23
+ export type { KitChatContextValue } from './store-context';
24
+
25
+ // ── chat 设置(构建期快照 + HELLO_ACK 水合) ──
26
+ export { configureChatConfigDefaults, useChatConfigStore } from './chat-config';
27
+ export type { QuickCommand } from './chat-config';
28
+
29
+ // ── 主题 ──
30
+ export { applyTheme, currentTheme } from './theme/theme';
31
+
32
+ // ── 壳契约(区域、可覆写选择器、品牌名来源) ──
33
+ export { SHELL_CONTRACT, resolveBrandName } from './shell';
34
+ export type { ShellArea, ShellContract, ShellRegion } from './shell';
35
+
36
+ // ── 卡片渲染(context 注册表注入) ──
37
+ export { CardRenderer, RawCardRenderer } from './components/CardRenderer';
38
+
39
+ // ── 应用级卡片工厂与卡片内交互 ──
40
+ export { defineCard } from './card/define-card';
41
+ export type { CardRenderProps, DefinedCard } from './card/define-card';
42
+ export { useCardActions } from './card/use-card-actions';
43
+ export type { CardActions, UseCardActionsOptions } from './card/use-card-actions';
44
+
45
+ // ── 卡片基元(信息类卡片直接拼,零样式文件) ──
46
+ export {
47
+ ActionList,
48
+ ActionRow,
49
+ Bill,
50
+ Card,
51
+ CardButton,
52
+ CardHeader,
53
+ CheckRow,
54
+ ConfirmSheet,
55
+ EmptyState,
56
+ EntityRow,
57
+ Hero,
58
+ KeyValueList,
59
+ MediaRow,
60
+ Notice,
61
+ StatGrid,
62
+ StatusTag,
63
+ Steps,
64
+ TagList,
65
+ Ticket,
66
+ Timeline,
67
+ } from './card/primitives';
68
+ export type {
69
+ ActionRowProps,
70
+ BillLine,
71
+ CardActionSpec,
72
+ CardHeaderProps,
73
+ CardProps,
74
+ CheckRowProps,
75
+ ConfirmSheetProps,
76
+ EntityRowProps,
77
+ HeroProps,
78
+ KeyValueItem,
79
+ MediaRowProps,
80
+ NoticeProps,
81
+ StatItem,
82
+ StepItem,
83
+ TicketProps,
84
+ TimelineItem,
85
+ Tone,
86
+ } from './card/primitives';
87
+
88
+ // ── 壳组件族 ──
89
+ export { MessageBubble } from './components/MessageBubble';
90
+ export { ChatInput } from './components/ChatInput';
91
+ export { QuickCommands } from './components/QuickCommands';
92
+ export { MediaList } from './components/MediaList';
93
+ export { ActionBar, CopyButton, getMessageCopyText } from './components/ActionBar';
94
+ export { DebugButton } from './components/ActionBar/DebugButton';
95
+ export { default as ChatHeader } from './components/ChatHeader';
96
+ export { default as HistoryEntry } from './components/HistoryEntry';
97
+ export { default as PullDownIndicator } from './components/PullDownIndicator';
98
+ export { default as ReasoningBlock } from './components/ReasoningBlock';
99
+ export { default as ScrollToBottomButton } from './components/ScrollToBottomButton';
100
+ export { default as ToolCallBlock } from './components/ToolCallBlock';
101
+
102
+ // ── hooks / utils ──
103
+ export { useTTSAutoPlay } from './hooks/useTTSAutoPlay';
104
+ export { useMediaUpload } from './hooks/useMediaUpload';
105
+ export { toast } from './utils/toast';
106
+ export type { ToastTone } from './utils/toast';
107
+ export { copyText } from './utils/copyText';
108
+ export { previewImages } from './utils/imagePreview';
109
+
110
+ // ── 类型(协议类型 re-export 单一来源 + UI 层约定) ──
111
+ export type {
112
+ ChatInputMessage,
113
+ ChatMessage,
114
+ FileTypeConfig,
115
+ InputMethod,
116
+ MediaConfig,
117
+ MediaItem,
118
+ MediaType,
119
+ MessageRole,
120
+ MessageStatus,
121
+ ToolCallInfo,
122
+ } from './types';
@@ -0,0 +1,59 @@
1
+ {
2
+ "$comment": "对话壳契约数据(docs/agent/kit.md 与 shell.test.ts 的共同来源;改这里 = 改壳的对外承诺)。regions = ChatPage 渲染的区域;internal = 不承诺稳定的内部结构。",
3
+ "regions": [
4
+ {
5
+ "id": "brand",
6
+ "area": "brand",
7
+ "selector": ".chat-page__brand",
8
+ "drivenBy": ["chat.json identity.name", "chat.json identity.tagline", "HELLO_ACK mallScope.mallName(缺省回落)"],
9
+ "brandable": true
10
+ },
11
+ {
12
+ "id": "welcome",
13
+ "area": "welcome",
14
+ "selector": ".chat-page__welcome",
15
+ "drivenBy": ["chat.json welcome.title", "chat.json welcome.subtitle", "chat.json identity(缺省回落)"],
16
+ "brandable": true
17
+ },
18
+ {
19
+ "id": "messages",
20
+ "area": "messages",
21
+ "selector": ".chat-page__body",
22
+ "drivenBy": ["chat store messages / historyMessages"],
23
+ "brandable": false
24
+ },
25
+ {
26
+ "id": "history-entry",
27
+ "area": "history",
28
+ "selector": ".history-entry",
29
+ "drivenBy": ["chat store historyHasMore / isLoadingHistory"],
30
+ "brandable": false
31
+ },
32
+ {
33
+ "id": "home-slot",
34
+ "area": "home",
35
+ "selector": ".chat-page__home-slot",
36
+ "drivenBy": ["客户端槽 home"],
37
+ "brandable": false
38
+ },
39
+ {
40
+ "id": "quick-commands",
41
+ "area": "quick-commands",
42
+ "selector": ".quick-commands",
43
+ "drivenBy": ["chat.json quickCommands"],
44
+ "brandable": true
45
+ },
46
+ {
47
+ "id": "input",
48
+ "area": "input",
49
+ "selector": ".tbox-chat-input",
50
+ "drivenBy": ["chat.json(enableTTS 等)", "chat store runtimeStatus / isGenerating"],
51
+ "brandable": false
52
+ }
53
+ ],
54
+ "internal": [
55
+ "MessageBubble 内部结构(.msg-row / .msg-main / .msg-thinking)",
56
+ "ChatInput 内部结构(.tbox-media-picker、发送按钮)",
57
+ "卡片渲染器内部结构(.tbox-card-*)"
58
+ ]
59
+ }
@@ -0,0 +1,61 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { describe, expect, it } from 'vitest';
5
+ import { SHELL_CONTRACT, resolveBrandName } from './shell';
6
+
7
+ const here = path.dirname(fileURLToPath(import.meta.url));
8
+ const read = (rel: string) => readFileSync(path.join(here, rel), 'utf8');
9
+
10
+ /** 契约声明的选择器必须真的出现在源码里——「文档说有、代码没接」在这里失败。 */
11
+ const SELECTOR_SOURCES: Record<string, string[]> = {
12
+ '.chat-page__brand': ['ChatPage.tsx'],
13
+ '.chat-page__welcome': ['ChatPage.tsx'],
14
+ '.chat-page__body': ['ChatPage.tsx'],
15
+ '.history-entry': ['components/HistoryEntry/index.tsx'],
16
+ '.chat-page__home-slot': ['ChatPage.tsx'],
17
+ '.quick-commands': ['components/QuickCommands/index.tsx'],
18
+ '.tbox-chat-input': ['components/ChatInput/index.tsx'],
19
+ };
20
+
21
+ describe('壳契约', () => {
22
+ it('区域 id 唯一且每个选择器都有源码出处', () => {
23
+ const ids = SHELL_CONTRACT.regions.map((region) => region.id);
24
+ expect(new Set(ids).size).toBe(ids.length);
25
+ for (const region of SHELL_CONTRACT.regions) {
26
+ const sources = SELECTOR_SOURCES[region.selector];
27
+ expect(sources, `壳契约新增选择器 ${region.selector} 时同步补源码映射`).toBeDefined();
28
+ const className = region.selector.replace(/^\./, '');
29
+ const found = sources.some((file) => read(file).includes(className));
30
+ expect(found, `${region.id}: ${region.selector} 在源码里不存在`).toBe(true);
31
+ }
32
+ });
33
+
34
+ it('可覆写区域只限壳层类名,内部结构排除在外', () => {
35
+ for (const region of SHELL_CONTRACT.regions.filter((item) => item.brandable)) {
36
+ expect(region.selector.startsWith('.chat-page__') || region.selector === '.quick-commands').toBe(true);
37
+ }
38
+ expect(SHELL_CONTRACT.internal.length).toBeGreaterThan(0);
39
+ });
40
+
41
+ it('每个区域都写明驱动来源', () => {
42
+ for (const region of SHELL_CONTRACT.regions) {
43
+ expect(region.drivenBy.length, `${region.id} 缺 drivenBy`).toBeGreaterThan(0);
44
+ }
45
+ });
46
+ });
47
+
48
+ describe('品牌名三级来源', () => {
49
+ it('identity.name 优先', () => {
50
+ expect(resolveBrandName({ name: '悦享里', tagline: '商圈助手' }, '某商场')).toBe('悦享里');
51
+ });
52
+
53
+ it('缺 identity 时回落 HELLO_ACK 的 mallName', () => {
54
+ expect(resolveBrandName(undefined, '某商场')).toBe('某商场');
55
+ });
56
+
57
+ it('都没有则不渲染', () => {
58
+ expect(resolveBrandName(undefined, undefined)).toBeUndefined();
59
+ expect(resolveBrandName({ name: '' }, undefined)).toBeUndefined();
60
+ });
61
+ });
package/src/shell.ts ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * 对话壳契约:哪些区域由什么驱动、哪些选择器可以覆写。
3
+ *
4
+ * 数据在 `shell.contract.json`(`docs/agent/kit.md` 与 `shell.test.ts` 的共同来源);
5
+ * 本文件只做类型收口与品牌名解析。
6
+ *
7
+ * - `docs/agent/shell.md` 的「能改什么」表由契约数据生成,杜绝「文档说有、代码没接」;
8
+ * - `shell.test.ts` 断言每条声明在源码里真的成立,声明与实现漂移即失败。
9
+ *
10
+ * 只描述壳层(ChatPage 渲染的区域)。kit 内部组件(MessageBubble、ChatInput 的内部结构)
11
+ * 不承诺稳定;改样式请用契约里 `brandable: true` 的选择器。
12
+ */
13
+ import type { ChatIdentity } from './chat-config';
14
+ import contract from './shell.contract.json';
15
+
16
+ export type ShellArea = 'brand' | 'welcome' | 'messages' | 'history' | 'home' | 'quick-commands' | 'input';
17
+
18
+ export interface ShellRegion {
19
+ /** 稳定 id(文档与测试引用) */
20
+ id: string;
21
+ /** 作用区域 */
22
+ area: ShellArea;
23
+ /** 根类名(应用可据此覆写) */
24
+ selector: string;
25
+ /** 由什么驱动:配置键、槽或 store 字段 */
26
+ drivenBy: string[];
27
+ /** 可覆写(`brand.css` 里改它算受支持用法) */
28
+ brandable: boolean;
29
+ }
30
+
31
+ export interface ShellContract {
32
+ regions: ShellRegion[];
33
+ /** 不承诺稳定、不要覆写的内部结构 */
34
+ internal: string[];
35
+ }
36
+
37
+ export const SHELL_CONTRACT: ShellContract = contract as ShellContract;
38
+
39
+ /**
40
+ * 品牌名三级来源:`chat.json identity.name` → `HELLO_ACK mallScope.mallName` → 无(不渲染品牌条)。
41
+ * 智能体身份是配置项,不是代码:应用只改 `config/chat.json`。
42
+ */
43
+ export function resolveBrandName(identity: ChatIdentity | undefined, mallName: string | undefined): string | undefined {
44
+ return identity?.name || mallName || undefined;
45
+ }
@@ -0,0 +1,92 @@
1
+ /**
2
+ * KitChatProvider:壳组件的 store 注入位(单一模板统一化 W2-1)。
3
+ *
4
+ * 壳组件不 import 应用 store——经 React context 读取注入的 store 与卡片注册表;
5
+ * 应用在 `<ChatPage store={useChatStore} cards={CHAT_REGISTRY} rawCards={CARD_REGISTRY}/>`
6
+ * 一处装配(或自组 KitChatProvider 树)。
7
+ *
8
+ * useChatStore 调用签名与 zustand 绑定 hook 逐字兼容(`useChatStore((s)=>s.x)`)——
9
+ * 组件代码从应用 store 迁移零改写(仅 import 来源变化)。
10
+ */
11
+ import { createContext, useContext, useMemo, type ReactNode } from 'react';
12
+ import type { CardComponent, TboxCardPayload } from '@tbox.cn/app-contracts';
13
+ import type { ClientSlotRegistry, CoreChatState, CoreChatStore } from '@tbox.cn/app-agent-sdk-client';
14
+
15
+ export interface KitChatContextValue {
16
+ /** 应用注入的 chat store(zustand 绑定 hook + API) */
17
+ readonly store: CoreChatStore;
18
+ /** 聊天入口卡片注册表(模块注册 + 组合层覆盖) */
19
+ readonly cards: Record<string, CardComponent>;
20
+ /** 原始形态卡片注册表(无覆盖;详情弹层等防嵌套消费) */
21
+ readonly rawCards: Record<string, CardComponent>;
22
+ /** 客户端槽注册表(home 首期唯一槽;缺席 = chat 流形态) */
23
+ readonly slots?: ClientSlotRegistry;
24
+ /**
25
+ * 旁路/消息卡内联流过滤(域注入;缺省恒等):域声明哪些卡不进内联流
26
+ * (由域浮层自持,如 mall 会员门禁卡系)——同时参与 home-first 形态判定。
27
+ */
28
+ readonly filterInlineCards?: (cards: readonly TboxCardPayload[]) => TboxCardPayload[];
29
+ }
30
+
31
+ const IDENTITY_FILTER = (cards: readonly TboxCardPayload[]): TboxCardPayload[] => [...cards];
32
+
33
+ const KitChatContext = createContext<KitChatContextValue | null>(null);
34
+
35
+ export function KitChatProvider({
36
+ store,
37
+ cards,
38
+ rawCards,
39
+ slots,
40
+ filterInlineCards,
41
+ children,
42
+ }: KitChatContextValue & { children: ReactNode }) {
43
+ const value = useMemo<KitChatContextValue>(
44
+ () => ({
45
+ store,
46
+ cards,
47
+ rawCards,
48
+ ...(slots ? { slots } : {}),
49
+ ...(filterInlineCards ? { filterInlineCards } : {}),
50
+ }),
51
+ [store, cards, rawCards, slots, filterInlineCards],
52
+ );
53
+ return <KitChatContext.Provider value={value}>{children}</KitChatContext.Provider>;
54
+ }
55
+
56
+ function useKitChatContext(): KitChatContextValue {
57
+ const value = useContext(KitChatContext);
58
+ if (!value) {
59
+ throw new Error('KitChatProvider 缺席:kit 组件须在 <ChatPage>(或自组 Provider)内使用');
60
+ }
61
+ return value;
62
+ }
63
+
64
+ /** 选择器读取(镜像 zustand 绑定 hook 调用签名;组件从 context 取 store)。 */
65
+ export function useChatStore<T>(selector: (state: CoreChatState) => T): T {
66
+ return useKitChatContext().store(selector);
67
+ }
68
+
69
+ /** store API(getState/subscribe;effect/事件回调场景)。 */
70
+ export function useChatStoreApi(): CoreChatStore {
71
+ return useKitChatContext().store;
72
+ }
73
+
74
+ /** 聊天入口卡片注册表(MessageBubble 等卡渲染消费)。 */
75
+ export function useChatCards(): Record<string, CardComponent> {
76
+ return useKitChatContext().cards;
77
+ }
78
+
79
+ /** 原始形态卡片注册表(RawCardRenderer 消费)。 */
80
+ export function useChatRawCards(): Record<string, CardComponent> {
81
+ return useKitChatContext().rawCards;
82
+ }
83
+
84
+ /** 客户端槽注册表(home 槽;缺席 = chat 流形态)。 */
85
+ export function useClientSlots(): ClientSlotRegistry | undefined {
86
+ return useKitChatContext().slots;
87
+ }
88
+
89
+ /** 旁路/消息卡内联流过滤器(域注入;缺省恒等)。 */
90
+ export function useInlineCardFilter(): (cards: readonly TboxCardPayload[]) => TboxCardPayload[] {
91
+ return useKitChatContext().filterInlineCards ?? IDENTITY_FILTER;
92
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * vitest 全局 setup:React act() 环境声明单一真源。
3
+ * 此前散落各用例内联声明、多数漏声明(act 告警刷屏 CI 日志);
4
+ * 统一收敛后新用例自动继承,无需逐文件记忆。node 环境纯逻辑用例设此全局无副作用。
5
+ */
6
+ (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean })
7
+ .IS_REACT_ACT_ENVIRONMENT = true;
@@ -0,0 +1,132 @@
1
+ /**
2
+ * 基座重置(进 `@layer base`):盒模型、文档级字体、卡框与重置规则都在这一层,
3
+ * 组件上的工具类可以覆写它们;层外的跨组件规则见文件末尾(有意留层外)。
4
+ * token 来自 tokens.css / motion.css。
5
+ */
6
+ @import './tokens.css';
7
+ @import './motion.css';
8
+
9
+ @layer base {
10
+ * {
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ html,
15
+ body,
16
+ #root {
17
+ margin: 0;
18
+ padding: 0;
19
+ height: 100%;
20
+ }
21
+
22
+ body {
23
+ /* mall-vis 字栈:demo 拉丁栈 + 显式 CJK(demo 依赖容器隐式 CJK,模板面向开放浏览器) */
24
+ font-family:
25
+ -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'PingFang SC',
26
+ 'Microsoft YaHei', sans-serif;
27
+ font-size: var(--tbox-fs-body);
28
+ color: var(--tbox-text-primary);
29
+ background: var(--tbox-bg-page);
30
+ -webkit-font-smoothing: antialiased;
31
+ }
32
+
33
+ /* 顶层容器全宽(渐变底铺满),内容区由各处 max-width:800px 居中(对齐原模板 #app)。 */
34
+ #root {
35
+ width: 100%;
36
+ position: relative;
37
+ }
38
+
39
+ /* 去掉移动端点击高亮 */
40
+ * {
41
+ -webkit-tap-highlight-color: transparent;
42
+ }
43
+
44
+ /* 卡片统一外层 = 卡片「卡框」。所有卡片共有的框在这里,单卡组件别重复写:
45
+ - 间距:卡片与上下元素(工具调用 / 气泡)的间距,与 .tool-calls 一致;
46
+ - 外观:默认背景(白) / 1px 边框 / 圆角 / 阴影;`overflow:hidden` 把内容裁进圆角;
47
+ - 入场动效:mount 即播(0.36s 上浮淡入,对齐 mall-vis 消息卡节奏)。
48
+ 值为物理 px(大写 PX,不再经 pxtorem 折半):圆角 var(--tbox-radius-lg)=24、边框 1、阴影 var(--tbox-shadow-sm)=0 4 13。
49
+ ⚠️ **内边距不在卡框里**——由卡片自己写。卡框给 padding 会让「自带背景色的卡」在外面露一圈白边(卡里套卡)。
50
+ 宽度也不在卡框里:默认占满整列(≈移动端 350~400px 设计宽),紧凑卡自己写 max-width。
51
+ 单卡组件(.tbox-<type>)只画内容;**要整卡换底色 / 出血图,直接在自己 root 上 `background:<色>` 或铺满图**——
52
+ 会铺满卡框、裁进圆角、不出白边(别再自己设圆角,卡框已裁)。不写背景则露卡框默认白底,不透明。 */
53
+ .tbox-card {
54
+ margin-bottom: 6PX;
55
+ background: var(--tbox-bg-card);
56
+ border: 1PX solid var(--tbox-border);
57
+ border-radius: var(--tbox-radius-lg);
58
+ box-shadow: var(--tbox-shadow-sm);
59
+ overflow: hidden;
60
+ animation: tbox-card-enter var(--tbox-motion-duration-slow) ease;
61
+ }
62
+
63
+ @keyframes tbox-card-enter {
64
+ from {
65
+ opacity: 0;
66
+ transform: translateY(8PX);
67
+ }
68
+
69
+ to {
70
+ opacity: 1;
71
+ transform: translateY(0);
72
+ }
73
+ }
74
+
75
+ @media (prefers-reduced-motion: reduce) {
76
+ .tbox-card {
77
+ animation: none;
78
+ }
79
+ }
80
+
81
+ /* 多卡纵向容器(SDK CardCarousel 多卡分支):容器内卡片关闭逐卡入场动画——
82
+ 同屏多卡逐张挂载时 0.36s 上浮淡入会交叠抖动;单卡直出与其他场景的入场动画保留
83
+ (命中卡片本体 .tbox-card:卡框动画在自身规则上,item 中间层无动画规则) */
84
+ .tbox-card-stack .tbox-card {
85
+ animation: none;
86
+ }
87
+
88
+ /* 兜底态(未知 cardType / 渲染出错,由 CardRenderer 给外层加此修饰类):
89
+ 复用卡框,弱化成无阴影的虚线提示框(内边距自带,因为卡框不再给 padding) */
90
+ .tbox-card--fallback {
91
+ max-width: 80%;
92
+ padding: 16PX 12PX;
93
+ background: var(--tbox-bg-subtle);
94
+ border: 0.5PX dashed var(--tbox-border);
95
+ box-shadow: none;
96
+ color: var(--tbox-text-muted);
97
+ font-size: var(--tbox-fs-caption);
98
+ }
99
+
100
+ /* ===== antd-mobile 全局弹层层级(2026-09-04):业务半屏浮层(门店详情/门店导览)z-index 1100,
101
+ * Toast/Dialog/Popup 等瞬态反馈弹层默认 1000 会被盖住(门店券行领取的弹出提醒不可见实证)——
102
+ * 统一提至 1200 置于一切业务浮层之上(antd-mobile 官方 :root:root 双写覆盖约定)===== */
103
+ :root:root {
104
+ --adm-z-index-mask: 1200;
105
+ }
106
+
107
+ /* 基元/模块迁移后不再各自写 margin reset:标题、段落、列表统一归零(在 base 层,工具类可覆写)。 */
108
+ h1, h2, h3, h4, h5, h6, p, dl, dd, dt, ul, ol {
109
+ margin: 0;
110
+ }
111
+ }
112
+
113
+ /* ── 层外:壳的跨组件覆写(必须压过工具类,故不进 @layer) ── */
114
+ /* 录音期间锁页面(由 useVoicePress 在 document.body 上 toggle,不是 TSX 元素):
115
+ 禁选中/拖拽/长按菜单,必须压过浏览器默认与页面内联样式。 */
116
+ .tbox-voice-page-lock,
117
+ .tbox-voice-page-lock * {
118
+ -webkit-user-select: none !important;
119
+ user-select: none !important;
120
+ -webkit-touch-callout: none !important;
121
+ -webkit-user-drag: none !important;
122
+ }
123
+
124
+ /* 顶部历史入口在场时欢迎区不再叠加自身顶部留白(壳的跨组件规则)。 */
125
+ .history-entry ~ .mall-home {
126
+ padding-top: 0;
127
+ }
128
+
129
+ /* 顶部历史入口在场时商圈主屏不再叠加自身顶部留白(ChatPage 槽位包裹层)。 */
130
+ .history-entry ~ .chat-page__home-slot .mall-home {
131
+ padding-top: 0;
132
+ }