@tbox.cn/app-agent-ui-kit 1.0.0-alpha.7 → 1.0.0-alpha.9

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 (35) hide show
  1. package/README.md +21 -1
  2. package/package.json +11 -3
  3. package/src/ChatPage.tsx +70 -17
  4. package/src/card/action-list-pending.test.tsx +91 -0
  5. package/src/card/define-card.test.ts +25 -0
  6. package/src/card/define-card.ts +18 -5
  7. package/src/card/primitives.test.tsx +10 -0
  8. package/src/card/primitives.tsx +136 -37
  9. package/src/card/view-card-inline.test.tsx +42 -0
  10. package/src/card/view-card.test.tsx +79 -0
  11. package/src/card/view-card.tsx +250 -0
  12. package/src/chat-config.ts +11 -7
  13. package/src/components/ActionBar/DebugButton.test.tsx +3 -0
  14. package/src/components/ActionBar/DebugButton.tsx +2 -7
  15. package/src/components/BottomSheet/index.test.tsx +90 -0
  16. package/src/components/BottomSheet/index.tsx +129 -0
  17. package/src/components/ChatInput/index.test.tsx +2 -2
  18. package/src/components/ChatInput/index.tsx +22 -17
  19. package/src/components/HistoryEntry/index.tsx +3 -2
  20. package/src/components/MessageBubble/index.tsx +11 -5
  21. package/src/components/QuickCommands/index.test.tsx +3 -1
  22. package/src/components/QuickCommands/index.tsx +31 -8
  23. package/src/components/ScrollToBottomButton/index.tsx +7 -3
  24. package/src/components/ToolCallBlock/index.tsx +4 -2
  25. package/src/index.ts +8 -0
  26. package/src/preview-provider.tsx +47 -0
  27. package/src/shell.contract.json +9 -0
  28. package/src/shell.test.ts +10 -0
  29. package/src/shell.ts +15 -0
  30. package/src/surface.test.tsx +35 -0
  31. package/src/surface.tsx +44 -0
  32. package/src/theme/base.css +9 -11
  33. package/src/theme/contract.json +123 -2
  34. package/src/theme/theme-contract.test.ts +16 -0
  35. package/src/theme/tokens.css +9 -12
package/src/shell.test.ts CHANGED
@@ -43,6 +43,16 @@ describe('壳契约', () => {
43
43
  expect(region.drivenBy.length, `${region.id} 缺 drivenBy`).toBeGreaterThan(0);
44
44
  }
45
45
  });
46
+
47
+ it('控件契约的 data-tbox-control 都在源码里', () => {
48
+ const sources = ['components/ChatInput/index.tsx', 'components/HistoryEntry/index.tsx', 'components/ScrollToBottomButton/index.tsx'];
49
+ for (const control of SHELL_CONTRACT.controls ?? []) {
50
+ const attr = /data-tbox-control=['"]([^'"]+)['"]/.exec(control.selector)?.[1];
51
+ expect(attr, `控件 ${control.id} 的选择器必须用 data-tbox-control`).toBeTruthy();
52
+ expect(sources.some((file) => read(file).includes('data-tbox-control="' + attr + '"')), `控件 ${control.id} 在源码里不存在`).toBe(true);
53
+ expect(control.minHit).toBeGreaterThanOrEqual(44);
54
+ }
55
+ });
46
56
  });
47
57
 
48
58
  describe('品牌名三级来源', () => {
package/src/shell.ts CHANGED
@@ -28,8 +28,23 @@ export interface ShellRegion {
28
28
  brandable: boolean;
29
29
  }
30
30
 
31
+ export interface ShellControl {
32
+ /** 稳定 id(探针与文档引用) */
33
+ id: string;
34
+ /** 可定位的选择器(data-tbox-control) */
35
+ selector: string;
36
+ /** 可访问名 */
37
+ name: string;
38
+ /** 最小命中区(px) */
39
+ minHit: number;
40
+ /** 能力开关(关闭时控件可缺席) */
41
+ capability?: string | string[];
42
+ }
43
+
31
44
  export interface ShellContract {
32
45
  regions: ShellRegion[];
46
+ /** 交互控件契约:探针按它查,不按内部类名猜 */
47
+ controls?: ShellControl[];
33
48
  /** 不承诺稳定、不要覆写的内部结构 */
34
49
  internal: string[];
35
50
  }
@@ -0,0 +1,35 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { createElement } from 'react';
3
+ import { renderToStaticMarkup } from 'react-dom/server';
4
+ import { Card, CardButton, CardHeader, defineCard, useCardActions } from './index';
5
+ import { createSurfaceHarness } from './surface';
6
+
7
+ /** 一张用 kit 动作的卡:没有 provider 会抛「KitChatProvider 缺席」。 */
8
+ const DemoCard = defineCard({ type: 'demo-card' }, ({ data, cardId, isHistory }) => {
9
+ const api = useCardActions({ cardId, isHistory });
10
+ return (
11
+ <Card>
12
+ <CardHeader title={String((data as { title: string }).title)} />
13
+ <CardButton onClick={() => api.send('继续')}>继续</CardButton>
14
+ </Card>
15
+ );
16
+ });
17
+
18
+ describe('createSurfaceHarness', () => {
19
+ it('给出与运行时同形的 provider,kit 卡片可直接渲染', () => {
20
+ const harness = createSurfaceHarness({ cards: { 'demo-card': DemoCard } });
21
+ const html = renderToStaticMarkup(
22
+ harness.wrap(createElement(DemoCard, { data: { title: '停车订单' }, cardId: 'c1', isHistory: false })),
23
+ );
24
+ expect(html).toContain('停车订单');
25
+ expect(html).toContain('继续');
26
+ });
27
+
28
+ it('历史态可渲染且动作禁用', () => {
29
+ const harness = createSurfaceHarness({ cards: { 'demo-card': DemoCard } });
30
+ const html = renderToStaticMarkup(
31
+ harness.wrap(createElement(DemoCard, { data: { title: '停车订单' }, cardId: 'c1', isHistory: true })),
32
+ );
33
+ expect(html).toContain('disabled');
34
+ });
35
+ });
@@ -0,0 +1,44 @@
1
+ /**
2
+ * SurfaceHarness:卡片/聊天表面的唯一装配入口。
3
+ *
4
+ * 证据(check:fast)、截图(card render)、预览页都要渲染真实组件——但每个入口都手搭
5
+ * provider 栈,就会出现「预览崩、证据过、运行时坏」的分裂。这里把 store + provider 收成
6
+ * 一个工厂:看到的就是运行时看到的。
7
+ *
8
+ * 用法:
9
+ * const harness = createSurfaceHarness({ cards, state });
10
+ * renderToStaticMarkup(harness.wrap(<MyCard ... />));
11
+ */
12
+ import type { ReactNode } from 'react';
13
+ import type { CardComponent } from '@tbox.cn/app-contracts';
14
+ import type { ClientSlotRegistry, CoreChatState } from '@tbox.cn/app-agent-sdk-client';
15
+ import { KitChatProvider } from './store-context';
16
+ import { createPreviewStore } from './preview-provider';
17
+
18
+ export interface SurfaceHarnessOptions {
19
+ /** cards = 组合层注册表(含覆盖);rawCards 缺省同 cards。 */
20
+ cards?: Record<string, CardComponent>;
21
+ rawCards?: Record<string, CardComponent>;
22
+ /** 客户端槽(home);缺省无。 */
23
+ slots?: ClientSlotRegistry;
24
+ /** store 初值覆盖(如 isGenerating / 历史态)。 */
25
+ state?: Partial<CoreChatState>;
26
+ }
27
+
28
+ export interface SurfaceHarness {
29
+ store: ReturnType<typeof createPreviewStore>;
30
+ /** 把一个元素包进与运行时同形的 provider 树。 */
31
+ wrap: (node: ReactNode) => ReactNode;
32
+ }
33
+
34
+ export function createSurfaceHarness(options: SurfaceHarnessOptions = {}): SurfaceHarness {
35
+ const cards = options.cards ?? {};
36
+ const rawCards = options.rawCards ?? cards;
37
+ const store = createPreviewStore(options.state);
38
+ const wrap = (node: ReactNode): ReactNode => (
39
+ <KitChatProvider store={store} cards={cards} rawCards={rawCards} {...(options.slots ? { slots: options.slots } : {})}>
40
+ {node}
41
+ </KitChatProvider>
42
+ );
43
+ return { store, wrap };
44
+ }
@@ -45,7 +45,7 @@
45
45
  - 间距:卡片与上下元素(工具调用 / 气泡)的间距,与 .tool-calls 一致;
46
46
  - 外观:默认背景(白) / 1px 边框 / 圆角 / 阴影;`overflow:hidden` 把内容裁进圆角;
47
47
  - 入场动效:mount 即播(0.36s 上浮淡入,对齐 mall-vis 消息卡节奏)。
48
- 值为物理 px(大写 PX,不再经 pxtorem 折半):圆角 var(--tbox-radius-lg)=24、边框 1、阴影 var(--tbox-shadow-sm)=0 4 13。
48
+ 值为物理 px(大写 PX):圆角 var(--tbox-radius-lg)=24、边框 1、阴影 var(--tbox-shadow-sm)=0 4 13。
49
49
  ⚠️ **内边距不在卡框里**——由卡片自己写。卡框给 padding 会让「自带背景色的卡」在外面露一圈白边(卡里套卡)。
50
50
  宽度也不在卡框里:默认占满整列(≈移动端 350~400px 设计宽),紧凑卡自己写 max-width。
51
51
  单卡组件(.tbox-<type>)只画内容;**要整卡换底色 / 出血图,直接在自己 root 上 `background:<色>` 或铺满图**——
@@ -104,6 +104,14 @@
104
104
  --adm-z-index-mask: 1200;
105
105
  }
106
106
 
107
+ /* 表单控件继承排版:模板不引 preflight,原生 button/input 否则落到浏览器默认 13.33px(越档字号)。
108
+ 作用域限定在壳与卡片内,工具类仍可覆写。 */
109
+ :where(.tbox-app, .chat-page, .tbox-card) :where(button, input, textarea, select) {
110
+ font: inherit;
111
+ color: inherit;
112
+ letter-spacing: inherit;
113
+ }
114
+
107
115
  /* 基元/模块迁移后不再各自写 margin reset:标题、段落、列表统一归零(在 base 层,工具类可覆写)。 */
108
116
  h1, h2, h3, h4, h5, h6, p, dl, dd, dt, ul, ol {
109
117
  margin: 0;
@@ -120,13 +128,3 @@
120
128
  -webkit-touch-callout: none !important;
121
129
  -webkit-user-drag: none !important;
122
130
  }
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
- }
@@ -1,5 +1,5 @@
1
1
  {
2
- "$comment": "主题可覆写变量封闭清单(主题轨 W-T2;doctor theme-package-structure 消费——超集即 error;theme-contract.test.ts 锁 tokens.css+motion.css 的平台键 ⊆ platform 段)。domains 段 = 域色两段式通道(identity/surface/border 三角色,v1 封闭;promotion 按域色矩阵无描边列)。assets 段 = 素材变量链语义位(W-T3 铺开,铺开后逐一核对)。",
2
+ "$comment": "主题可覆写变量封闭清单(主题轨 W-T2;doctor theme-package-structure 消费——超集即 error;theme-contract.test.ts 锁 tokens.css+motion.css 的平台键 ⊆ platform 段)。domains 段 = 域色两段式通道(identity/surface/border 三角色,v1 封闭;promotion 按域色矩阵无描边列)。assets 段 = 素材变量链语义位(W-T3 铺开,铺开后逐一核对)。 pairs 段 = 对比度对账清单(fg/bg 都是 platform 键;min 为 WCAG 阈值;color-mix 派生的由浏览器车道补验)。",
3
3
  "platform": [
4
4
  "--tbox-accent",
5
5
  "--tbox-accent-light",
@@ -68,5 +68,126 @@
68
68
  "--tbox-asset-promotion-activity-header",
69
69
  "--tbox-asset-promotion-coupon-header",
70
70
  "--tbox-asset-sg-header"
71
- ]
71
+ ],
72
+ "pairs": [
73
+ {
74
+ "fg": "--tbox-text-primary",
75
+ "bg": "--tbox-bg-card",
76
+ "min": 4.5,
77
+ "usage": "正文"
78
+ },
79
+ {
80
+ "fg": "--tbox-text-primary",
81
+ "bg": "--tbox-bg-page",
82
+ "min": 4.5,
83
+ "usage": "正文"
84
+ },
85
+ {
86
+ "fg": "--tbox-text-secondary",
87
+ "bg": "--tbox-bg-card",
88
+ "min": 4.5,
89
+ "usage": "次要文字"
90
+ },
91
+ {
92
+ "fg": "--tbox-text-secondary",
93
+ "bg": "--tbox-bg-page",
94
+ "min": 4.5,
95
+ "usage": "次要文字"
96
+ },
97
+ {
98
+ "fg": "--tbox-text-muted",
99
+ "bg": "--tbox-bg-card",
100
+ "min": 4.5,
101
+ "usage": "说明文字"
102
+ },
103
+ {
104
+ "fg": "--tbox-text-muted",
105
+ "bg": "--tbox-bg-page",
106
+ "min": 4.5,
107
+ "usage": "说明文字"
108
+ },
109
+ {
110
+ "fg": "--tbox-primary",
111
+ "bg": "--tbox-bg-card",
112
+ "min": 4.5,
113
+ "usage": "链接与主色文字"
114
+ },
115
+ {
116
+ "fg": "--tbox-primary-light",
117
+ "bg": "--tbox-primary-dark",
118
+ "min": 4.5,
119
+ "usage": "深底主视觉文字(hero tone=inverse)"
120
+ }
121
+ ],
122
+ "derive": {
123
+ "$comment": "浅色模式派生规则(pnpm exec tbox-app theme derive 消费):from = seed | accent;l 为相对种子的 L 增量,L/C 为绝对目标;派生结果须 pairs 全部闭合。键必须 ⊆ platform。",
124
+ "--tbox-primary": {
125
+ "from": "seed"
126
+ },
127
+ "--tbox-primary-dark": {
128
+ "from": "seed",
129
+ "l": -0.12
130
+ },
131
+ "--tbox-primary-light": {
132
+ "from": "seed",
133
+ "L": 0.96,
134
+ "C": 0.03
135
+ },
136
+ "--tbox-accent": {
137
+ "from": "accent"
138
+ },
139
+ "--tbox-accent-light": {
140
+ "from": "accent",
141
+ "L": 0.96,
142
+ "C": 0.03
143
+ },
144
+ "--tbox-bg-page": {
145
+ "from": "seed",
146
+ "L": 0.975,
147
+ "C": 0.008,
148
+ "mood": {
149
+ "warm": {
150
+ "H": 80
151
+ },
152
+ "cool": {
153
+ "H": 240
154
+ }
155
+ }
156
+ },
157
+ "--tbox-bg-card": {
158
+ "from": "seed",
159
+ "L": 0.99,
160
+ "C": 0.004
161
+ },
162
+ "--tbox-bg-subtle": {
163
+ "from": "seed",
164
+ "L": 0.95,
165
+ "C": 0.01
166
+ },
167
+ "--tbox-text-primary": {
168
+ "from": "seed",
169
+ "L": 0.25,
170
+ "C": 0.02
171
+ },
172
+ "--tbox-text-secondary": {
173
+ "from": "seed",
174
+ "L": 0.42,
175
+ "C": 0.02
176
+ },
177
+ "--tbox-text-muted": {
178
+ "from": "seed",
179
+ "L": 0.52,
180
+ "C": 0.015
181
+ },
182
+ "--tbox-border": {
183
+ "from": "seed",
184
+ "L": 0.9,
185
+ "C": 0.01
186
+ },
187
+ "--tbox-border-strong": {
188
+ "from": "seed",
189
+ "L": 0.82,
190
+ "C": 0.015
191
+ }
192
+ }
72
193
  }
@@ -18,6 +18,7 @@ const contract = JSON.parse(readFileSync(path.join(themeDir, 'contract.json'), '
18
18
  platform: string[];
19
19
  domains: string[];
20
20
  assets: string[];
21
+ derive?: Record<string, { from: 'seed' | 'accent'; L?: number; C?: number; l?: number; mood?: Record<string, { H: number }> }>;
21
22
  };
22
23
 
23
24
  function extractRootVars(file: string): string[] {
@@ -55,6 +56,21 @@ describe('主题契约(contract.json)', () => {
55
56
  for (const key of contract.assets) expect(key.startsWith('--tbox-asset-')).toBe(true);
56
57
  });
57
58
 
59
+ it('derive 段的键 ⊆ platform,且带 from 来源', () => {
60
+ const derive = contract.derive ?? {};
61
+ const allowed = new Set(contract.platform);
62
+ const drift = Object.entries(derive)
63
+ .filter(([key]) => !key.startsWith('$'))
64
+ .filter(([key]) => !allowed.has(key))
65
+ .map(([key]) => key);
66
+ expect(drift, 'derive 声明了契约外的令牌:' + drift.join(', ')).toEqual([]);
67
+ for (const [key, rule] of Object.entries(derive)) {
68
+ if (key.startsWith('$')) continue;
69
+ expect(['seed', 'accent']).toContain(rule.from);
70
+ }
71
+ expect(Object.keys(derive).length).toBeGreaterThan(8);
72
+ });
73
+
58
74
  it('motion 族 5 键在清单内(主题轨 T-2.1 契约)', () => {
59
75
  for (const key of [
60
76
  '--tbox-motion-duration-fast',
@@ -7,13 +7,13 @@
7
7
  *
8
8
  * 单位约定(2026-11 统一样式层):全部长/字号/圆角/阴影写**物理 px**(大写 `PX`)。
9
9
  * 组件样式一律走 Tailwind 工具类(4px 标度 + 12/14/16/18/20/24/30/36 字号档),
10
- * 这些变量只服务主题投影(`rounded-lg` / `shadow-sm` 等);模板里的 pxtorem 已无小写 px 可转。
10
+ * 这些变量只服务主题投影(`rounded-lg` / `shadow-sm` 等)。模板没有 rem 换算,也没有动态根字号。
11
11
  * 视觉源对照与域色矩阵见 docs/reference/templates.md(视觉体系与域色矩阵)。
12
12
  */
13
13
  :root {
14
14
  /* 主色(mall-vis 蓝系) */
15
15
  --tbox-primary: #096bfa;
16
- --tbox-primary-dark: #2368ec;
16
+ --tbox-primary-dark: #1c61e5;
17
17
  --tbox-primary-light: #eaf3ff;
18
18
  --tbox-accent: #096bfa;
19
19
  --tbox-accent-light: #eaf3ff;
@@ -44,21 +44,18 @@
44
44
  --tbox-warning: #df8b16;
45
45
  --tbox-error: #e42f46;
46
46
 
47
- /* 字号——按 750/2x 设计稿基线,对标消息气泡(MessageBubble):
48
- 正文 .msg-markdown / __text = 25px,次要信息(时间/角色) = 18~19px,标题 h1~h4 = 36/32/30/28px。
49
- 注意:rootValue 100 + 根字号脚本会把 px 折半渲染(750 稿 → ≈375 手机列),
50
- 所以这里写的是 2x 值,别按普通 web 的 12/14/16 写(那样渲染出来只有一半、发虚)。 */
51
- --tbox-fs-heading: 15PX; /* ≈气泡 h3:商品名 / 店名 */
52
- --tbox-fs-body: 12.5PX; /* =气泡正文 */
53
- --tbox-fs-caption: 10PX; /* ≈气泡次要信息(时间/角色) */
47
+ /* 字号角色(物理 px):聊天正文 16、卡片正文 14、说明 12,都在 Tailwind 档位内。
48
+ 组件按角色取值,主题才改得动字号;不要再写设计稿 2x 值。 */
49
+ --tbox-fs-heading: 18PX; /* 卡片/分区标题 */
50
+ --tbox-fs-body: 16PX; /* 聊天正文 */
51
+ --tbox-fs-caption: 12PX; /* 说明与次要信息 */
54
52
 
55
53
  /* 圆角 */
56
54
  --tbox-radius-sm: 6PX;
57
55
  --tbox-radius-md: 9PX;
58
56
  --tbox-radius-lg: 24PX;
59
57
 
60
- /* 内容最大宽度(对齐参考 H5):全宽渐变底 + 内容区 720px 居中。
61
- 大写 PX 不被 postcss-pxtorem 转换(正则大小写敏感),保持固定像素。 */
58
+ /* 内容最大宽度(对齐参考 H5):全宽渐变底 + 内容区 720px 居中。 */
62
59
  --tbox-content-max-width: 720PX;
63
60
  /* 页面水平栅格:欢迎区、主卡、快捷入口、输入区、消息气泡和消息卡片共用。 */
64
61
  --tbox-page-gutter: 16PX;
@@ -69,7 +66,7 @@
69
66
 
70
67
  /* 输入框 */
71
68
  --tbox-input-bg: rgba(255, 255, 255, 0.7);
72
- /* 输入条跨过 450PX 根字号阈值时保持固定高度,仅横向伸缩。 */
69
+ /* 输入条高度固定,仅横向伸缩。 */
73
70
  --tbox-input-radius: 25PX;
74
71
  --tbox-input-padding: 10PX 12PX;
75
72
  --tbox-send-btn-size: 22PX;