@tbox.cn/app-agent-ui-kit 1.0.0-alpha.1 → 1.0.0-alpha.7
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 +1 -1
- package/package.json +9 -6
- package/src/ChatPage.tsx +34 -7
- 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 +35 -71
- package/src/chat-config.ts +18 -0
- package/src/components/ActionBar/DebugButton.tsx +3 -3
- package/src/components/ActionBar/index.tsx +13 -14
- package/src/components/ChatHeader/index.tsx +19 -17
- package/src/components/ChatInput/index.tsx +32 -26
- package/src/components/ChatInput/voice/VoicePressField.tsx +12 -3
- package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +9 -9
- package/src/components/HistoryEntry/index.tsx +3 -4
- package/src/components/MediaList/index.tsx +42 -28
- package/src/components/MessageBubble/index.tsx +52 -27
- package/src/components/PullDownIndicator/index.tsx +3 -4
- package/src/components/QuickCommands/index.tsx +6 -6
- package/src/components/ReasoningBlock/index.tsx +14 -9
- package/src/components/ScrollToBottomButton/index.tsx +2 -4
- package/src/components/ToolCallBlock/index.tsx +54 -36
- package/src/icons.ts +39 -0
- package/src/index.ts +54 -1
- package/src/shell.contract.json +59 -0
- package/src/shell.test.ts +61 -0
- package/src/shell.ts +45 -0
- package/src/theme/base.css +132 -0
- package/src/theme/contract.json +1 -1
- package/src/theme/theme-contract.test.ts +3 -3
- package/src/theme/{tokens.less → tokens.css} +14 -12
- 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,7 +3,7 @@
|
|
|
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
8
|
单点装配;域 UI(如商圈主屏)经 home 槽渲染函数自带。
|
|
9
9
|
- **主题**:kit 默认 tokens(`src/theme/tokens.less`);品牌资产包经 `html[data-theme]` 级联覆写 +
|
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.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Agent 对话壳 UI 组件包(源码发布,单一模板统一化 W2-1):ChatPage/壳组件族/主题 tokens。",
|
|
6
6
|
"exports": {
|
|
@@ -13,8 +13,12 @@
|
|
|
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
|
+
}
|
|
18
22
|
},
|
|
19
23
|
"files": [
|
|
20
24
|
"src",
|
|
@@ -23,13 +27,13 @@
|
|
|
23
27
|
"LICENSE"
|
|
24
28
|
],
|
|
25
29
|
"dependencies": {
|
|
26
|
-
"@tbox.cn/app-contracts": "1.0.0-alpha.1",
|
|
27
30
|
"@phosphor-icons/react": "2.1.10",
|
|
28
31
|
"antd-mobile": "5.42.3",
|
|
29
32
|
"react-markdown": "9.1.0",
|
|
30
33
|
"remark-gfm": "4.0.1",
|
|
31
34
|
"zustand": "4.5.7",
|
|
32
|
-
"@tbox.cn/app-
|
|
35
|
+
"@tbox.cn/app-contracts": "1.0.0-alpha.7",
|
|
36
|
+
"@tbox.cn/app-agent-sdk-client": "1.0.0-alpha.7"
|
|
33
37
|
},
|
|
34
38
|
"peerDependencies": {
|
|
35
39
|
"react": "18.3.1",
|
|
@@ -40,7 +44,6 @@
|
|
|
40
44
|
"@types/react": "18.3.31",
|
|
41
45
|
"@types/react-dom": "18.3.7",
|
|
42
46
|
"happy-dom": "20.11.2",
|
|
43
|
-
"less": "4.8.1",
|
|
44
47
|
"react": "18.3.1",
|
|
45
48
|
"react-dom": "18.3.1",
|
|
46
49
|
"typescript": "5.9.3",
|
package/src/ChatPage.tsx
CHANGED
|
@@ -17,6 +17,7 @@ import { MessageBubble } from './components/MessageBubble';
|
|
|
17
17
|
import { QuickCommands } from './components/QuickCommands';
|
|
18
18
|
import ScrollToBottomButton from './components/ScrollToBottomButton';
|
|
19
19
|
import { useChatConfigStore } from './chat-config';
|
|
20
|
+
import { resolveBrandName } from './shell';
|
|
20
21
|
import {
|
|
21
22
|
KitChatProvider,
|
|
22
23
|
useChatStore,
|
|
@@ -25,8 +26,7 @@ import {
|
|
|
25
26
|
type KitChatContextValue,
|
|
26
27
|
} from './store-context';
|
|
27
28
|
import type { ChatInputMessage } from './types';
|
|
28
|
-
import './
|
|
29
|
-
import './theme/tokens.less';
|
|
29
|
+
import './theme/tokens.css';
|
|
30
30
|
|
|
31
31
|
export interface ChatPageProps {
|
|
32
32
|
/** 应用 chat store(zustand 绑定 hook + API;如 `useChatStore`) */
|
|
@@ -87,6 +87,10 @@ function ChatPageInner() {
|
|
|
87
87
|
|
|
88
88
|
const slots = useClientSlots();
|
|
89
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);
|
|
90
94
|
|
|
91
95
|
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
|
|
92
96
|
const [voiceActive, setVoiceActive] = useState(false);
|
|
@@ -196,6 +200,14 @@ function ChatPageInner() {
|
|
|
196
200
|
|
|
197
201
|
const showHistoryEntry = historyHasMore && historyMessages.length === 0;
|
|
198
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
|
+
|
|
199
211
|
const pull = usePullDownHistory({
|
|
200
212
|
isAtTopRef,
|
|
201
213
|
isLoadingHistory,
|
|
@@ -239,12 +251,19 @@ function ChatPageInner() {
|
|
|
239
251
|
}, [messages, orphanCards, liveCardSignature]);
|
|
240
252
|
|
|
241
253
|
return (
|
|
242
|
-
<div className={`chat-page${showHome ? '' : ' chat-page--conversation'}`}>
|
|
243
|
-
<div className="chat-page__bg" />
|
|
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" />
|
|
244
256
|
|
|
245
|
-
|
|
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">
|
|
246
265
|
<div
|
|
247
|
-
className="chat-page__body"
|
|
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"
|
|
248
267
|
ref={bodyRef}
|
|
249
268
|
onScroll={onScroll}
|
|
250
269
|
onWheel={pull.handleWheel}
|
|
@@ -267,6 +286,14 @@ function ChatPageInner() {
|
|
|
267
286
|
))}
|
|
268
287
|
</div>
|
|
269
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
|
+
|
|
270
297
|
{/* 历史前插视口锚 + home 槽渲染位(域自持显隐;缺席则不渲染) */}
|
|
271
298
|
<div className="chat-page__home-slot" ref={historyAnchorRef}>
|
|
272
299
|
{homeSlot?.({ onSend: send, ...(mallName ? { mallName } : {}) })}
|
|
@@ -274,7 +301,7 @@ function ChatPageInner() {
|
|
|
274
301
|
|
|
275
302
|
{/* 业务旁路 emit 的卡片(如欢迎卡) */}
|
|
276
303
|
{inlineOrphanCards.map((card) => (
|
|
277
|
-
<div key={card.id} className="chat-page__orphan-card">
|
|
304
|
+
<div key={card.id} className="chat-page__orphan-card mx-page">
|
|
278
305
|
<CardRenderer card={card} />
|
|
279
306
|
</div>
|
|
280
307
|
))}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
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
|
+
expect(markup).toContain('tbox-stats__value');
|
|
93
|
+
expect(markup).toContain('tbox-timeline__item--gain');
|
|
94
|
+
expect(markup).toContain('+100');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('EntityRow 支持价格与说明,EmptyState 给下一步', () => {
|
|
98
|
+
const markup = html(
|
|
99
|
+
<>
|
|
100
|
+
<EntityRow title="江南小馆" meta="餐饮 · L1" price="¥80/人" tags={<TagList tags={['满100减15']} />} />
|
|
101
|
+
<EmptyState title="暂无停车券" description="缴费或参加活动可攒积分" />
|
|
102
|
+
</>,
|
|
103
|
+
);
|
|
104
|
+
expect(markup).toContain('tbox-entity__price');
|
|
105
|
+
expect(markup).toContain('¥80/人');
|
|
106
|
+
expect(markup).toContain('tbox-empty__desc');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('CheckRow 只读态不带交互语义', () => {
|
|
110
|
+
const readOnly = html(<CheckRow label="使用停车券" checked />);
|
|
111
|
+
expect(readOnly).toContain('tbox-check--on');
|
|
112
|
+
expect(readOnly).toContain('tbox-check--static');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('ActionList 点击前确认时先开弹层,确认后才调用动作', () => {
|
|
116
|
+
const calls: string[] = [];
|
|
117
|
+
const api = {
|
|
118
|
+
send: (text: string) => calls.push(`send:${text}`),
|
|
119
|
+
invoke: (id: string, input?: Record<string, unknown>) => calls.push(`invoke:${id}:${JSON.stringify(input ?? {})}`),
|
|
120
|
+
disabled: false,
|
|
121
|
+
isHistory: false,
|
|
122
|
+
isGenerating: false,
|
|
123
|
+
};
|
|
124
|
+
const markup = html(
|
|
125
|
+
<ActionList
|
|
126
|
+
api={api}
|
|
127
|
+
actions={[
|
|
128
|
+
{ label: '确认支付', invoke: { id: 'pay-parking', input: { plate: '浙A' } }, confirm: { title: '确认支付 ¥5?' } },
|
|
129
|
+
{ label: '再想想', send: { text: '先不付', displayText: '先不付' } },
|
|
130
|
+
]}
|
|
131
|
+
/>,
|
|
132
|
+
);
|
|
133
|
+
expect(markup).toContain('确认支付');
|
|
134
|
+
expect(markup).toContain('再想想');
|
|
135
|
+
expect(calls).toEqual([]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('ActionList 禁用时按钮带 disabled', () => {
|
|
139
|
+
const markup = html(
|
|
140
|
+
<ActionList
|
|
141
|
+
api={{ send: () => {}, invoke: () => {}, disabled: true, isHistory: false, isGenerating: false }}
|
|
142
|
+
actions={[{ label: '领券', invoke: { id: 'claim' } }]}
|
|
143
|
+
/>,
|
|
144
|
+
);
|
|
145
|
+
expect(markup).toContain('disabled');
|
|
146
|
+
});
|
|
147
|
+
});
|