@tbox.cn/app-agent-ui-kit 0.0.0-stage → 1.0.0-alpha.10

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 (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -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 +70 -3
  11. package/src/ChatPage.tsx +400 -0
  12. package/src/card/action-list-pending.test.tsx +91 -0
  13. package/src/card/define-card.test.ts +25 -0
  14. package/src/card/define-card.ts +47 -0
  15. package/src/card/primitives.test.tsx +157 -0
  16. package/src/card/primitives.tsx +717 -0
  17. package/src/card/use-card-actions.ts +86 -0
  18. package/src/card/view-card-inline.test.tsx +42 -0
  19. package/src/card/view-card.test.tsx +79 -0
  20. package/src/card/view-card.tsx +250 -0
  21. package/src/chat-config.test.ts +46 -0
  22. package/src/chat-config.ts +84 -0
  23. package/src/components/ActionBar/DebugButton.test.tsx +87 -0
  24. package/src/components/ActionBar/DebugButton.tsx +48 -0
  25. package/src/components/ActionBar/index.test.tsx +83 -0
  26. package/src/components/ActionBar/index.tsx +158 -0
  27. package/src/components/BottomSheet/index.test.tsx +97 -0
  28. package/src/components/BottomSheet/index.tsx +49 -0
  29. package/src/components/CardRenderer.tsx +36 -0
  30. package/src/components/ChatHeader/index.test.tsx +74 -0
  31. package/src/components/ChatHeader/index.tsx +77 -0
  32. package/src/components/ChatInput/index.test.tsx +156 -0
  33. package/src/components/ChatInput/index.tsx +295 -0
  34. package/src/components/ChatInput/voice/VoicePressField.tsx +43 -0
  35. package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
  36. package/src/components/ChatInput/voice/machine.ts +27 -0
  37. package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
  38. package/src/components/HistoryEntry/index.tsx +42 -0
  39. package/src/components/MediaList/index.tsx +270 -0
  40. package/src/components/MessageBubble/index.test.tsx +134 -0
  41. package/src/components/MessageBubble/index.tsx +208 -0
  42. package/src/components/PullDownIndicator/index.tsx +31 -0
  43. package/src/components/QuickCommands/index.test.tsx +101 -0
  44. package/src/components/QuickCommands/index.tsx +75 -0
  45. package/src/components/ReasoningBlock/index.tsx +61 -0
  46. package/src/components/ScrollToBottomButton/index.tsx +12 -0
  47. package/src/components/ToolCallBlock/detail.test.ts +346 -0
  48. package/src/components/ToolCallBlock/detail.ts +204 -0
  49. package/src/components/ToolCallBlock/index.test.tsx +261 -0
  50. package/src/components/ToolCallBlock/index.tsx +232 -0
  51. package/src/components/ToolCallBlock/titles.ts +102 -0
  52. package/src/hooks/use-drag-gesture.test.ts +66 -0
  53. package/src/hooks/use-drag-gesture.ts +129 -0
  54. package/src/hooks/use-pull-down-history.ts +241 -0
  55. package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
  56. package/src/hooks/useMediaUpload.ts +242 -0
  57. package/src/hooks/useTTSAutoPlay.ts +82 -0
  58. package/src/icons.ts +39 -0
  59. package/src/index.ts +143 -0
  60. package/src/overlay/Overlay.tsx +154 -0
  61. package/src/overlay/focus.ts +53 -0
  62. package/src/overlay/index.ts +16 -0
  63. package/src/overlay/inert.ts +27 -0
  64. package/src/overlay/layers.ts +21 -0
  65. package/src/overlay/overlay.ssr.test.tsx +27 -0
  66. package/src/overlay/overlay.test.tsx +209 -0
  67. package/src/overlay/presence.ts +86 -0
  68. package/src/overlay/scroll-lock.ts +62 -0
  69. package/src/overlay/stack.ts +111 -0
  70. package/src/overlay/use-overlay.ts +80 -0
  71. package/src/preview-provider.tsx +47 -0
  72. package/src/shell.contract.json +75 -0
  73. package/src/shell.test.ts +72 -0
  74. package/src/shell.ts +60 -0
  75. package/src/store-context.tsx +92 -0
  76. package/src/surface.test.tsx +35 -0
  77. package/src/surface.tsx +44 -0
  78. package/src/test/setup.ts +7 -0
  79. package/src/theme/base.css +167 -0
  80. package/src/theme/contract.json +193 -0
  81. package/src/theme/motion.css +19 -0
  82. package/src/theme/theme-contract.test.ts +85 -0
  83. package/src/theme/theme.ts +17 -0
  84. package/src/theme/tokens.css +78 -0
  85. package/src/types.ts +29 -0
  86. package/src/utils/copyText.ts +11 -0
  87. package/src/utils/haptics.test.ts +51 -0
  88. package/src/utils/haptics.ts +44 -0
  89. package/src/utils/image-preview-host.tsx +95 -0
  90. package/src/utils/imagePreview.test.ts +32 -0
  91. package/src/utils/imagePreview.ts +19 -0
  92. package/src/utils/pull-down-machine.test.ts +188 -0
  93. package/src/utils/pull-down-machine.ts +157 -0
  94. package/src/utils/session-error.ts +5 -0
  95. package/src/utils/toast.test.ts +64 -0
  96. package/src/utils/toast.ts +108 -0
  97. package/src/utils/tool-call-display.test.ts +240 -0
  98. package/src/utils/tool-call-display.ts +146 -0
  99. package/src/vite-env.d.ts +1 -0
@@ -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
+ }
@@ -0,0 +1,157 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { renderToStaticMarkup } from 'react-dom/server';
3
+ import {
4
+ ActionList,
5
+ ActionRow,
6
+ Bill,
7
+ Card,
8
+ CardButton,
9
+ CardHeader,
10
+ CheckRow,
11
+ ConfirmSheet,
12
+ EmptyState,
13
+ EntityRow,
14
+ Hero,
15
+ KeyValueList,
16
+ MediaRow,
17
+ Notice,
18
+ StatGrid,
19
+ StatusTag,
20
+ Steps,
21
+ TagList,
22
+ Ticket,
23
+ Timeline,
24
+ } from './primitives';
25
+
26
+ const html = (node: React.ReactElement) => renderToStaticMarkup(node);
27
+
28
+ describe('卡片基元', () => {
29
+ it('容器 + 标题 + 属性列表成套渲染', () => {
30
+ const markup = html(
31
+ <Card footer="数据更新于刚刚">
32
+ <CardHeader eyebrow="订单" title="待支付" status={<StatusTag tone="warning">待支付</StatusTag>} />
33
+ <KeyValueList items={[{ label: '车牌', value: '浙A·12345' }, { label: '入场', value: '12:01' }]} />
34
+ </Card>,
35
+ );
36
+ expect(markup).toContain('tbox-card-primitive');
37
+ expect(markup).toContain('待支付');
38
+ expect(markup).toContain('浙A·12345');
39
+ expect(markup).toContain('数据更新于刚刚');
40
+ });
41
+
42
+ it('明细带合计', () => {
43
+ const markup = html(
44
+ <Bill lines={[{ label: '停车费', value: '15.00 元' }, { label: '券抵扣', value: '-10.00 元', muted: true }]} total={{ label: '应付', value: '5.00 元' }} />,
45
+ );
46
+ expect(markup).toContain('应付');
47
+ expect(markup).toContain('tbox-bill__row--muted');
48
+ });
49
+
50
+ it('提示条按 tone 变体', () => {
51
+ expect(html(<Notice tone="warning">额度不足</Notice>)).toContain('tbox-notice--warning');
52
+ });
53
+
54
+ it('按钮行在禁用态加类名', () => {
55
+ const markup = html(
56
+ <ActionRow disabled primary={<CardButton>立即支付</CardButton>} secondary={<CardButton variant="secondary">取消</CardButton>} />,
57
+ );
58
+ expect(markup).toContain('tbox-actions--disabled');
59
+ expect(markup).toContain('立即支付');
60
+ });
61
+
62
+ it('确认弹层关闭时渲染为空', () => {
63
+ expect(
64
+ html(
65
+ <ConfirmSheet open={false} title="确认" onConfirm={() => {}} onCancel={() => {}} />,
66
+ ),
67
+ ).toBe('');
68
+ expect(
69
+ html(<ConfirmSheet open title="确认入会" description="将获得 100 积分" onConfirm={() => {}} onCancel={() => {}} />),
70
+ ).toContain('确认入会');
71
+ });
72
+
73
+ it('票券、步骤、图文条目可渲染', () => {
74
+ expect(html(<Ticket amount="20 元" condition="满 100 可用" meta="有效期至 12-31" />)).toContain('tbox-ticket__amount');
75
+ expect(html(<Steps steps={[{ title: 'B2 层', detail: '靠近 3 号电梯' }, { title: '取车' }]} />)).toContain('tbox-steps__index');
76
+ expect(html(<MediaRow title="江南小馆" meta="B1 · 人均 60" action={<CardButton variant="link">去看看</CardButton>} />)).toContain('江南小馆');
77
+ });
78
+ });
79
+
80
+
81
+ describe('第二版基元', () => {
82
+ it('Hero / StatGrid / Timeline 渲染品牌块与数值', () => {
83
+ const markup = html(
84
+ <>
85
+ <Hero eyebrow="MEMBER" title="我的积分" value={100} unit="积分" footnote="可兑停车券" />
86
+ <StatGrid items={[{ label: '积分', value: 100 }, { label: '券', value: 2, hint: '本月' }]} />
87
+ <Timeline items={[{ label: '入会礼', amount: '+100', tone: 'gain', time: '11/18' }]} />
88
+ </>,
89
+ );
90
+ expect(markup).toContain('tbox-hero');
91
+ expect(markup).toContain('我的积分');
92
+ // 深底主视觉:底用 primary-dark、字用 primary-foreground,全部走令牌(无字面量色值)。
93
+ const inverse = html(<Hero eyebrow="ROUTE" title="导航" value={8} unit="分钟" tone="inverse" />);
94
+ expect(inverse).toContain('tbox-hero--inverse');
95
+ expect(inverse).toContain('bg-primary-dark');
96
+ expect(inverse).toContain('text-primary-foreground');
97
+ expect(inverse).not.toMatch(/#[0-9a-fA-F]{3,8}\b|rgba?\(/);
98
+ // 未知 tone(旧数据 / 新 kit 错配)回落 primary,不拼空类名。
99
+ const unknown = html(<Hero title="兜底" tone={'legacy' as never} />);
100
+ expect(unknown).toContain('tbox-hero--legacy');
101
+ expect(unknown).toContain('from-primary-foreground');
102
+ expect(markup).toContain('tbox-stats__value');
103
+ expect(markup).toContain('tbox-timeline__item--gain');
104
+ expect(markup).toContain('+100');
105
+ });
106
+
107
+ it('EntityRow 支持价格与说明,EmptyState 给下一步', () => {
108
+ const markup = html(
109
+ <>
110
+ <EntityRow title="江南小馆" meta="餐饮 · L1" price="¥80/人" tags={<TagList tags={['满100减15']} />} />
111
+ <EmptyState title="暂无停车券" description="缴费或参加活动可攒积分" />
112
+ </>,
113
+ );
114
+ expect(markup).toContain('tbox-entity__price');
115
+ expect(markup).toContain('¥80/人');
116
+ expect(markup).toContain('tbox-empty__desc');
117
+ });
118
+
119
+ it('CheckRow 只读态不带交互语义', () => {
120
+ const readOnly = html(<CheckRow label="使用停车券" checked />);
121
+ expect(readOnly).toContain('tbox-check--on');
122
+ expect(readOnly).toContain('tbox-check--static');
123
+ });
124
+
125
+ it('ActionList 点击前确认时先开弹层,确认后才调用动作', () => {
126
+ const calls: string[] = [];
127
+ const api = {
128
+ send: (text: string) => calls.push(`send:${text}`),
129
+ invoke: (id: string, input?: Record<string, unknown>) => calls.push(`invoke:${id}:${JSON.stringify(input ?? {})}`),
130
+ disabled: false,
131
+ isHistory: false,
132
+ isGenerating: false,
133
+ };
134
+ const markup = html(
135
+ <ActionList
136
+ api={api}
137
+ actions={[
138
+ { label: '确认支付', invoke: { id: 'pay-parking', input: { plate: '浙A' } }, confirm: { title: '确认支付 ¥5?' } },
139
+ { label: '再想想', send: { text: '先不付', displayText: '先不付' } },
140
+ ]}
141
+ />,
142
+ );
143
+ expect(markup).toContain('确认支付');
144
+ expect(markup).toContain('再想想');
145
+ expect(calls).toEqual([]);
146
+ });
147
+
148
+ it('ActionList 禁用时按钮带 disabled', () => {
149
+ const markup = html(
150
+ <ActionList
151
+ api={{ send: () => {}, invoke: () => {}, disabled: true, isHistory: false, isGenerating: false }}
152
+ actions={[{ label: '领券', invoke: { id: 'claim' } }]}
153
+ />,
154
+ );
155
+ expect(markup).toContain('disabled');
156
+ });
157
+ });