@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.
- package/README.md +21 -1
- package/package.json +11 -3
- package/src/ChatPage.tsx +70 -17
- package/src/card/action-list-pending.test.tsx +91 -0
- package/src/card/define-card.test.ts +25 -0
- package/src/card/define-card.ts +18 -5
- package/src/card/primitives.test.tsx +10 -0
- package/src/card/primitives.tsx +136 -37
- package/src/card/view-card-inline.test.tsx +42 -0
- package/src/card/view-card.test.tsx +79 -0
- package/src/card/view-card.tsx +250 -0
- package/src/chat-config.ts +11 -7
- package/src/components/ActionBar/DebugButton.test.tsx +3 -0
- package/src/components/ActionBar/DebugButton.tsx +2 -7
- package/src/components/BottomSheet/index.test.tsx +90 -0
- package/src/components/BottomSheet/index.tsx +129 -0
- package/src/components/ChatInput/index.test.tsx +2 -2
- package/src/components/ChatInput/index.tsx +22 -17
- package/src/components/HistoryEntry/index.tsx +3 -2
- package/src/components/MessageBubble/index.tsx +11 -5
- package/src/components/QuickCommands/index.test.tsx +3 -1
- package/src/components/QuickCommands/index.tsx +31 -8
- package/src/components/ScrollToBottomButton/index.tsx +7 -3
- package/src/components/ToolCallBlock/index.tsx +4 -2
- package/src/index.ts +8 -0
- package/src/preview-provider.tsx +47 -0
- package/src/shell.contract.json +9 -0
- package/src/shell.test.ts +10 -0
- package/src/shell.ts +15 -0
- package/src/surface.test.tsx +35 -0
- package/src/surface.tsx +44 -0
- package/src/theme/base.css +9 -11
- package/src/theme/contract.json +123 -2
- package/src/theme/theme-contract.test.ts +16 -0
- package/src/theme/tokens.css +9 -12
package/src/card/primitives.tsx
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 设计约束:
|
|
5
5
|
* - 全部走主题 token(`--tbox-*`),换主题自动跟随;不写死品牌色;
|
|
6
|
-
* -
|
|
6
|
+
* - 尺寸与字号一律物理 px(没有 rem 换算,也没有动态根字号);
|
|
7
7
|
* - 不引入新的交互语义:按钮一律由调用方决定 `onClick`,禁用由 `useCardActions().disabled` 统一控制。
|
|
8
8
|
*
|
|
9
9
|
* 信息类卡片(明细、票据、状态、路线)用这些积木拼即可,不必自写结构与样式;
|
|
10
10
|
* 需要完全自定义布局时再写组件,规则见 `docs/agent/style.md`。
|
|
11
11
|
*/
|
|
12
|
-
import { useState } from 'react';
|
|
12
|
+
import { useEffect, useRef, useState } from 'react';
|
|
13
13
|
import type { ReactNode } from 'react';
|
|
14
14
|
import type { CardActions } from './use-card-actions';
|
|
15
15
|
|
|
@@ -18,18 +18,18 @@ export type Tone = 'neutral' | 'info' | 'success' | 'warning' | 'error';
|
|
|
18
18
|
/** 状态色:中性走契约语义色,其余是旧 less 里的固定色值(透明度底 + 深色字) */
|
|
19
19
|
const TAG_TONE: Record<Tone, string> = {
|
|
20
20
|
neutral: 'bg-muted text-secondary-foreground',
|
|
21
|
-
info: 'bg-
|
|
22
|
-
success: 'bg-
|
|
23
|
-
warning: 'bg-
|
|
24
|
-
error: 'bg-
|
|
21
|
+
info: 'bg-primary-soft text-primary',
|
|
22
|
+
success: 'bg-success-soft text-success',
|
|
23
|
+
warning: 'bg-warning-soft text-warn-ink',
|
|
24
|
+
error: 'bg-danger-soft text-destructive',
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
const NOTICE_TONE: Record<Tone, string> = {
|
|
28
28
|
neutral: 'bg-muted',
|
|
29
|
-
info: 'bg-
|
|
30
|
-
success: 'bg-
|
|
31
|
-
warning: 'bg-
|
|
32
|
-
error: 'bg-
|
|
29
|
+
info: 'bg-primary-soft',
|
|
30
|
+
success: 'bg-success-soft',
|
|
31
|
+
warning: 'bg-warning-soft',
|
|
32
|
+
error: 'bg-danger-soft',
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
export interface CardProps {
|
|
@@ -191,9 +191,9 @@ export function ActionRow({ primary, secondary, disabled }: ActionRowProps) {
|
|
|
191
191
|
* @behavior primary 撑满整行;secondary 与 link 宽度随内容;禁用态不触发点击。
|
|
192
192
|
*/
|
|
193
193
|
const BUTTON_VARIANT = {
|
|
194
|
-
primary: 'w-full bg-primary px-
|
|
195
|
-
secondary: 'w-
|
|
196
|
-
link: 'w-auto bg-transparent px-
|
|
194
|
+
primary: 'w-full bg-primary px-4 py-2.5 text-sm text-white',
|
|
195
|
+
secondary: 'w-full border border-border bg-transparent px-4 py-2.5 text-sm text-primary',
|
|
196
|
+
link: 'w-auto bg-transparent px-1 py-1 text-sm text-primary',
|
|
197
197
|
} as const;
|
|
198
198
|
|
|
199
199
|
export function CardButton({
|
|
@@ -201,18 +201,22 @@ export function CardButton({
|
|
|
201
201
|
onClick,
|
|
202
202
|
variant = 'primary',
|
|
203
203
|
disabled,
|
|
204
|
+
busy,
|
|
204
205
|
}: {
|
|
205
206
|
children: ReactNode;
|
|
206
207
|
onClick?: () => void;
|
|
207
208
|
variant?: 'primary' | 'secondary' | 'link';
|
|
208
209
|
disabled?: boolean;
|
|
210
|
+
/** 动作进行中:只影响可访问性语义,视觉由调用方给 spinner */
|
|
211
|
+
busy?: boolean;
|
|
209
212
|
}) {
|
|
210
213
|
return (
|
|
211
214
|
<button
|
|
212
|
-
className={`tbox-btn tbox-btn--${variant} inline-flex cursor-pointer items-center justify-center rounded-full border-0 leading-tight disabled:cursor-not-allowed ${BUTTON_VARIANT[variant]}`}
|
|
215
|
+
className={`tbox-btn tbox-btn--${variant} inline-flex min-h-11 cursor-pointer items-center justify-center rounded-full border-0 leading-tight outline-offset-2 focus-visible:outline-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-55 ${BUTTON_VARIANT[variant]}`}
|
|
213
216
|
type="button"
|
|
214
217
|
onClick={onClick}
|
|
215
218
|
disabled={disabled}
|
|
219
|
+
aria-busy={busy || undefined}
|
|
216
220
|
>
|
|
217
221
|
{children}
|
|
218
222
|
</button>
|
|
@@ -239,22 +243,40 @@ export function ConfirmSheet({
|
|
|
239
243
|
onConfirm,
|
|
240
244
|
onCancel,
|
|
241
245
|
}: ConfirmSheetProps) {
|
|
246
|
+
useEffect(() => {
|
|
247
|
+
if (!open) return undefined;
|
|
248
|
+
const onKey = (event: KeyboardEvent) => {
|
|
249
|
+
if (event.key === 'Escape') onCancel();
|
|
250
|
+
};
|
|
251
|
+
document.addEventListener('keydown', onKey);
|
|
252
|
+
return () => document.removeEventListener('keydown', onKey);
|
|
253
|
+
}, [open, onCancel]);
|
|
242
254
|
if (!open) return null;
|
|
243
|
-
|
|
244
|
-
|
|
255
|
+
// 就地渲染 fixed 弹层:卡框已不再用 filter(filter 会给 fixed 子元素造新的定位基准),
|
|
256
|
+
// 因此弹层相对视口定位;保留 SSR 可渲染(证据包用 renderToStaticMarkup 断言内容)。
|
|
257
|
+
const sheet = (
|
|
258
|
+
<div
|
|
259
|
+
className="tbox-sheet fixed inset-0 z-50"
|
|
260
|
+
role="dialog"
|
|
261
|
+
aria-modal="true"
|
|
262
|
+
aria-label={typeof title === 'string' ? title : undefined}
|
|
263
|
+
>
|
|
245
264
|
<div className="tbox-sheet__mask absolute inset-0 bg-scrim" onClick={onCancel} />
|
|
246
|
-
<div className="tbox-sheet__panel absolute inset-x-0 bottom-0 flex flex-col gap-2.5 rounded-t-xl bg-card px-4 pb-
|
|
265
|
+
<div className="tbox-sheet__panel absolute inset-x-0 bottom-0 flex flex-col gap-2.5 rounded-t-xl bg-card px-4 pb-[calc(20px+env(safe-area-inset-bottom))] pt-4">
|
|
247
266
|
<h5 className="tbox-sheet__title m-0 text-base">{title}</h5>
|
|
248
|
-
{description ? <p className="tbox-sheet__desc m-0 text-
|
|
249
|
-
<div className="tbox-sheet__actions
|
|
267
|
+
{description ? <p className="tbox-sheet__desc m-0 text-sm text-secondary-foreground">{description}</p> : null}
|
|
268
|
+
<div className="tbox-sheet__actions mt-1 grid grid-cols-2 gap-2">
|
|
250
269
|
<CardButton variant="secondary" onClick={onCancel}>
|
|
251
|
-
{cancelText}
|
|
270
|
+
<span className="whitespace-nowrap">{cancelText}</span>
|
|
271
|
+
</CardButton>
|
|
272
|
+
<CardButton onClick={onConfirm}>
|
|
273
|
+
<span className="whitespace-nowrap">{confirmText}</span>
|
|
252
274
|
</CardButton>
|
|
253
|
-
<CardButton onClick={onConfirm}>{confirmText}</CardButton>
|
|
254
275
|
</div>
|
|
255
276
|
</div>
|
|
256
277
|
</div>
|
|
257
278
|
);
|
|
279
|
+
return sheet;
|
|
258
280
|
}
|
|
259
281
|
|
|
260
282
|
const TICKET_TONE = {
|
|
@@ -266,6 +288,8 @@ const HERO_TONE = {
|
|
|
266
288
|
primary: 'bg-linear-to-br from-primary-foreground to-card',
|
|
267
289
|
warm: 'bg-linear-to-br from-warm to-card',
|
|
268
290
|
jade: 'bg-linear-to-br from-jade to-card',
|
|
291
|
+
// 深色底:只用契约里的令牌(primary-dark / primary-light),不引入字面量色值。
|
|
292
|
+
inverse: 'bg-primary-dark text-primary-foreground [&_.tbox-hero__eyebrow]:text-primary-foreground [&_.tbox-hero__footnote]:text-primary-foreground [&_.tbox-hero__value_small]:text-primary-foreground',
|
|
269
293
|
neutral: 'bg-muted',
|
|
270
294
|
} as const;
|
|
271
295
|
|
|
@@ -360,13 +384,15 @@ export interface HeroProps {
|
|
|
360
384
|
/** 数值下方一句说明 */
|
|
361
385
|
footnote?: ReactNode;
|
|
362
386
|
icon?: ReactNode;
|
|
363
|
-
tone?: 'primary' | 'warm' | 'jade' | 'neutral';
|
|
387
|
+
tone?: 'primary' | 'warm' | 'jade' | 'inverse' | 'neutral';
|
|
364
388
|
}
|
|
365
389
|
|
|
366
390
|
/** 品牌色块:主数值 + 说明,用于会员、积分、钱包类卡片的头部。 */
|
|
367
391
|
export function Hero({ eyebrow, title, value, unit, footnote, icon, tone = 'primary' }: HeroProps) {
|
|
392
|
+
// 旧 kit 与新数据错配时(未知 tone)回落 primary,不拼出空类名。
|
|
393
|
+
const toneClass = HERO_TONE[tone] ?? HERO_TONE.primary;
|
|
368
394
|
return (
|
|
369
|
-
<div className={`tbox-hero tbox-hero--${tone} flex items-center gap-2 rounded-md p-3.5 pb-3.25 ${
|
|
395
|
+
<div className={`tbox-hero tbox-hero--${tone} flex items-center gap-2 rounded-md p-3.5 pb-3.25 ${toneClass}`}>
|
|
370
396
|
<div className="tbox-hero__text flex min-w-0 flex-1 flex-col gap-1.5">
|
|
371
397
|
{eyebrow ? <span className="tbox-hero__eyebrow text-xs tracking-wide text-muted-foreground">{eyebrow}</span> : null}
|
|
372
398
|
<span className="tbox-hero__title text-sm font-semibold">{title}</span>
|
|
@@ -575,11 +601,47 @@ export interface CardActionSpec {
|
|
|
575
601
|
*
|
|
576
602
|
* 用法:`const api = useCardActions({ cardId, isHistory, actionTokens }); <ActionList api={api} actions={[...]} />`
|
|
577
603
|
*
|
|
578
|
-
* @behavior
|
|
604
|
+
* @behavior 首个动作为整行主按钮,其余并排一行;confirm 走挂到 body 的 ConfirmSheet;disabled 自动接 api.disabled。
|
|
579
605
|
*/
|
|
606
|
+
const PENDING_TIMEOUT_MS = 20000;
|
|
607
|
+
|
|
580
608
|
export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: CardActions }) {
|
|
581
|
-
const [
|
|
582
|
-
const
|
|
609
|
+
const [confirmIndex, setConfirmIndex] = useState<number | null>(null);
|
|
610
|
+
const [running, setRunning] = useState<number | null>(null);
|
|
611
|
+
// 单飞用 ref 同步判重:state 要等重渲染,连点两下会都通过。
|
|
612
|
+
const runningRef = useRef<number | null>(null);
|
|
613
|
+
const wasGenerating = useRef(false);
|
|
614
|
+
|
|
615
|
+
// 兜底解除:没有回执(方案 A 不改协议)时,按钮不能永远转圈。
|
|
616
|
+
useEffect(() => {
|
|
617
|
+
if (running === null) return undefined;
|
|
618
|
+
const timer = setTimeout(() => {
|
|
619
|
+
runningRef.current = null;
|
|
620
|
+
setRunning(null);
|
|
621
|
+
}, PENDING_TIMEOUT_MS);
|
|
622
|
+
return () => clearTimeout(timer);
|
|
623
|
+
}, [running]);
|
|
624
|
+
|
|
625
|
+
// 生成态从 true 回到 false = 这一轮有结果,解除 pending。
|
|
626
|
+
// 无论点击发生在生成前还是生成后,标记都要在当前这一轮结束时复位。
|
|
627
|
+
useEffect(() => {
|
|
628
|
+
if (api.isGenerating) {
|
|
629
|
+
wasGenerating.current = true;
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
if (!wasGenerating.current) return;
|
|
633
|
+
wasGenerating.current = false;
|
|
634
|
+
if (runningRef.current !== null) {
|
|
635
|
+
runningRef.current = null;
|
|
636
|
+
setRunning(null);
|
|
637
|
+
}
|
|
638
|
+
}, [api.isGenerating]);
|
|
639
|
+
|
|
640
|
+
const run = (spec: CardActionSpec, index: number) => {
|
|
641
|
+
// 没有 send / invoke 的动作不进入 pending,否则只会空转。
|
|
642
|
+
if (!spec.invoke && !spec.send) return;
|
|
643
|
+
runningRef.current = index;
|
|
644
|
+
setRunning(index);
|
|
583
645
|
if (spec.invoke) {
|
|
584
646
|
api.invoke(spec.invoke.id, spec.invoke.input);
|
|
585
647
|
return;
|
|
@@ -590,22 +652,58 @@ export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: C
|
|
|
590
652
|
}
|
|
591
653
|
};
|
|
592
654
|
const trigger = (spec: CardActionSpec, index: number) => {
|
|
593
|
-
if (api.disabled) return;
|
|
655
|
+
if (api.disabled || runningRef.current !== null || confirmIndex !== null) return;
|
|
594
656
|
if (spec.confirm) {
|
|
595
|
-
|
|
657
|
+
setConfirmIndex(index);
|
|
596
658
|
return;
|
|
597
659
|
}
|
|
598
|
-
run(spec);
|
|
660
|
+
run(spec, index);
|
|
599
661
|
};
|
|
600
|
-
const pendingSpec =
|
|
662
|
+
const pendingSpec = confirmIndex === null ? undefined : actions[confirmIndex];
|
|
663
|
+
const busy = api.disabled || running !== null || confirmIndex !== null;
|
|
664
|
+
const label = (text: string, index: number) => (
|
|
665
|
+
<span className="inline-flex items-center gap-2">
|
|
666
|
+
{running === index ? (
|
|
667
|
+
<span
|
|
668
|
+
className="tbox-btn__spinner inline-block size-4 animate-spin rounded-full border-2 border-current border-t-transparent"
|
|
669
|
+
aria-hidden="true"
|
|
670
|
+
/>
|
|
671
|
+
) : null}
|
|
672
|
+
{text}
|
|
673
|
+
</span>
|
|
674
|
+
);
|
|
601
675
|
return (
|
|
602
676
|
<>
|
|
603
677
|
<div className="tbox-action-list mt-2.5 flex flex-col gap-2">
|
|
604
|
-
{actions
|
|
605
|
-
<CardButton
|
|
606
|
-
{
|
|
678
|
+
{actions[0] ? (
|
|
679
|
+
<CardButton
|
|
680
|
+
variant={actions[0].style ?? 'primary'}
|
|
681
|
+
disabled={busy}
|
|
682
|
+
busy={running === 0}
|
|
683
|
+
onClick={() => trigger(actions[0], 0)}
|
|
684
|
+
>
|
|
685
|
+
{label(actions[0].label, 0)}
|
|
607
686
|
</CardButton>
|
|
608
|
-
)
|
|
687
|
+
) : null}
|
|
688
|
+
{actions.length > 1 ? (
|
|
689
|
+
<div className="tbox-action-list__row flex gap-2">
|
|
690
|
+
{actions.slice(1).map((spec, index) => {
|
|
691
|
+
const actionIndex = index + 1;
|
|
692
|
+
return (
|
|
693
|
+
<div key={spec.label + '-' + actionIndex} className="min-w-0 flex-1">
|
|
694
|
+
<CardButton
|
|
695
|
+
variant={spec.style ?? 'secondary'}
|
|
696
|
+
disabled={busy}
|
|
697
|
+
busy={running === actionIndex}
|
|
698
|
+
onClick={() => trigger(spec, actionIndex)}
|
|
699
|
+
>
|
|
700
|
+
{label(spec.label, actionIndex)}
|
|
701
|
+
</CardButton>
|
|
702
|
+
</div>
|
|
703
|
+
);
|
|
704
|
+
})}
|
|
705
|
+
</div>
|
|
706
|
+
) : null}
|
|
609
707
|
</div>
|
|
610
708
|
{pendingSpec?.confirm ? (
|
|
611
709
|
<ConfirmSheet
|
|
@@ -616,10 +714,11 @@ export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: C
|
|
|
616
714
|
{...(pendingSpec.confirm.cancelText ? { cancelText: pendingSpec.confirm.cancelText } : {})}
|
|
617
715
|
onConfirm={() => {
|
|
618
716
|
const spec = pendingSpec;
|
|
619
|
-
|
|
620
|
-
|
|
717
|
+
const index = confirmIndex ?? 0;
|
|
718
|
+
setConfirmIndex(null);
|
|
719
|
+
run(spec, index);
|
|
621
720
|
}}
|
|
622
|
-
onCancel={() =>
|
|
721
|
+
onCancel={() => setConfirmIndex(null)}
|
|
623
722
|
/>
|
|
624
723
|
) : null}
|
|
625
724
|
</>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { act } from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
5
|
+
import type { CardView } from '@tbox.cn/app-contracts';
|
|
6
|
+
import { createSurfaceHarness } from '../surface';
|
|
7
|
+
import { ViewCard } from './view-card';
|
|
8
|
+
|
|
9
|
+
const view: CardView = {
|
|
10
|
+
header: { title: '活动' },
|
|
11
|
+
blocks: [
|
|
12
|
+
{
|
|
13
|
+
kind: 'list',
|
|
14
|
+
items: [{ title: '亲子手作', action: { label: '报名', invoke: { id: 'register' } } }],
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const roots: ReturnType<typeof createRoot>[] = [];
|
|
20
|
+
afterEach(async () => {
|
|
21
|
+
await act(async () => {
|
|
22
|
+
for (const root of roots) root.unmount();
|
|
23
|
+
});
|
|
24
|
+
roots.length = 0;
|
|
25
|
+
document.body.innerHTML = '';
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('ViewCard 列表内联动作', () => {
|
|
29
|
+
it('点击后进入单飞状态,按钮禁用', async () => {
|
|
30
|
+
const harness = createSurfaceHarness({});
|
|
31
|
+
const el = document.createElement('div');
|
|
32
|
+
document.body.appendChild(el);
|
|
33
|
+
const root = createRoot(el);
|
|
34
|
+
roots.push(root);
|
|
35
|
+
act(() => root.render(harness.wrap(<ViewCard view={view} cardId="c1" actionTokens={{ register: 'token' }} />)));
|
|
36
|
+
const button = [...el.querySelectorAll('button')].find((item) => item.textContent?.includes('报名'))!;
|
|
37
|
+
await act(async () => {
|
|
38
|
+
button.click();
|
|
39
|
+
});
|
|
40
|
+
expect(el.querySelector('button')!.disabled).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createElement } from 'react';
|
|
3
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
4
|
+
import type { CardView } from '@tbox.cn/app-contracts';
|
|
5
|
+
import { createSurfaceHarness } from '../surface';
|
|
6
|
+
import { ViewCard } from './view-card';
|
|
7
|
+
|
|
8
|
+
const parking: CardView = {
|
|
9
|
+
header: { title: '停车订单', eyebrow: 'PARKING', status: { text: '待支付', tone: 'warning' } },
|
|
10
|
+
blocks: [
|
|
11
|
+
{ kind: 'facts', items: [['车牌', '浙A·12345'], ['停车时长', '2 小时 20 分钟']] },
|
|
12
|
+
{ kind: 'bill', lines: [['停车费', '¥15'], ['停车券', '-¥10', true]], total: ['应付', '¥5'] },
|
|
13
|
+
{ kind: 'notice', text: '已为你选好停车券', tone: 'info' },
|
|
14
|
+
],
|
|
15
|
+
actions: [
|
|
16
|
+
{ label: '确认支付 ¥5', invoke: { id: 'pay-parking', input: { plate: '浙A·12345' } }, confirm: { title: '确认支付?' } },
|
|
17
|
+
{ label: '用积分换停车券', send: '用积分兑换停车券' },
|
|
18
|
+
],
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const activity: CardView = {
|
|
22
|
+
header: { title: '近期活动', status: { text: '2 个推荐', tone: 'info' } },
|
|
23
|
+
blocks: [
|
|
24
|
+
{
|
|
25
|
+
kind: 'list',
|
|
26
|
+
items: [
|
|
27
|
+
{ title: '亲子手作', meta: '今天 15:00 · L2 连廊', tags: ['亲子'], description: '剩 8 组名额', action: { label: '报名', invoke: { id: 'register' } } },
|
|
28
|
+
{ title: '积分加倍', meta: '今天 10:00 · L1 中庭' },
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
actions: [{ label: '看第一个详情', send: '第一个活动的详情', style: 'secondary' }],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const harness = createSurfaceHarness({});
|
|
36
|
+
const render = (view: CardView, isHistory = false) =>
|
|
37
|
+
renderToStaticMarkup(harness.wrap(createElement(ViewCard, { view, cardId: 'c1', isHistory })));
|
|
38
|
+
|
|
39
|
+
describe('ViewCard', () => {
|
|
40
|
+
it('积木与动作渲染一次,标题/明细/提示都在', () => {
|
|
41
|
+
const html = render(parking);
|
|
42
|
+
expect(html).toContain('停车订单');
|
|
43
|
+
expect(html).toContain('浙A·12345');
|
|
44
|
+
expect(html).toContain('应付');
|
|
45
|
+
expect(html).toContain('已为你选好停车券');
|
|
46
|
+
expect(html.match(/确认支付 ¥5/g)?.length).toBe(1);
|
|
47
|
+
expect(html.match(/用积分换停车券/g)?.length).toBe(1);
|
|
48
|
+
expect(html).toContain('tbox-btn');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('列表积木渲染实体与内联动作', () => {
|
|
52
|
+
const html = render(activity);
|
|
53
|
+
expect(html).toContain('亲子手作');
|
|
54
|
+
expect(html).toContain('报名');
|
|
55
|
+
expect(html).toContain('2 个推荐');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('渲染前归一化:超预算积木只渲染前 4 个', () => {
|
|
59
|
+
const over = {
|
|
60
|
+
header: { title: '超预算' },
|
|
61
|
+
blocks: Array.from({ length: 5 }, (_, index) => ({ kind: 'tags' as const, tags: ['tag-' + index] })),
|
|
62
|
+
} as unknown as CardView;
|
|
63
|
+
const html = render(over);
|
|
64
|
+
expect(html).toContain('tag-0');
|
|
65
|
+
expect(html).toContain('tag-3');
|
|
66
|
+
expect(html).not.toContain('tag-4');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('坏数据回退为可读提示,不抛错', () => {
|
|
70
|
+
const broken = { header: { title: '坏卡' }, blocks: null } as unknown as CardView;
|
|
71
|
+
const html = render(broken);
|
|
72
|
+
expect(html).toContain('卡片数据不可用');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('历史态禁用动作', () => {
|
|
76
|
+
const html = render(parking, true);
|
|
77
|
+
expect(html).toContain('disabled');
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ViewCard:视图卡的通用渲染器(L1)。
|
|
3
|
+
*
|
|
4
|
+
* 一张卡 = 一份 CardView JSON;版式、动作路径、确认弹层、历史禁用都由平台实现一次。
|
|
5
|
+
* 应用只写 view(data) 函数(服务端),客户端不再有手写 tsx。
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect, useRef, useState, type ReactNode } from 'react';
|
|
8
|
+
import { normalizeCardView } from '@tbox.cn/app-contracts';
|
|
9
|
+
import type { CardView, CardViewAction, CardViewBlock } from '@tbox.cn/app-contracts';
|
|
10
|
+
import {
|
|
11
|
+
ActionList,
|
|
12
|
+
Bill,
|
|
13
|
+
Card,
|
|
14
|
+
CardButton,
|
|
15
|
+
CardHeader,
|
|
16
|
+
ConfirmSheet,
|
|
17
|
+
EmptyState,
|
|
18
|
+
EntityRow,
|
|
19
|
+
Hero,
|
|
20
|
+
KeyValueList,
|
|
21
|
+
Notice,
|
|
22
|
+
StatusTag,
|
|
23
|
+
Steps,
|
|
24
|
+
TagList,
|
|
25
|
+
Ticket,
|
|
26
|
+
Timeline,
|
|
27
|
+
} from './primitives';
|
|
28
|
+
import type { CardActionSpec } from './primitives';
|
|
29
|
+
import { useCardActions } from './use-card-actions';
|
|
30
|
+
|
|
31
|
+
type Api = ReturnType<typeof useCardActions>;
|
|
32
|
+
|
|
33
|
+
function toSpec(action: CardViewAction): CardActionSpec {
|
|
34
|
+
return {
|
|
35
|
+
label: action.label,
|
|
36
|
+
...(action.send !== undefined ? { send: action.send } : {}),
|
|
37
|
+
...(action.invoke ? { invoke: action.invoke } : {}),
|
|
38
|
+
...(action.confirm ? { confirm: action.confirm } : {}),
|
|
39
|
+
...(action.style ? { style: action.style } : {}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 列表项内联动作:与卡级动作共用确认弹层语义,但不占整行。 */
|
|
44
|
+
function useInlineRunner(api: Api) {
|
|
45
|
+
const [pending, setPending] = useState<CardViewAction | null>(null);
|
|
46
|
+
const [running, setRunning] = useState(false);
|
|
47
|
+
const runningRef = useRef(false);
|
|
48
|
+
const wasGenerating = useRef(false);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!running) return undefined;
|
|
51
|
+
const timer = setTimeout(() => {
|
|
52
|
+
runningRef.current = false;
|
|
53
|
+
setRunning(false);
|
|
54
|
+
}, 20000);
|
|
55
|
+
return () => clearTimeout(timer);
|
|
56
|
+
}, [running]);
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (api.isGenerating) {
|
|
59
|
+
wasGenerating.current = true;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (!wasGenerating.current) return;
|
|
63
|
+
wasGenerating.current = false;
|
|
64
|
+
if (runningRef.current) {
|
|
65
|
+
runningRef.current = false;
|
|
66
|
+
setRunning(false);
|
|
67
|
+
}
|
|
68
|
+
}, [api.isGenerating]);
|
|
69
|
+
const run = (action: CardViewAction) => {
|
|
70
|
+
if (!action.invoke && !action.send) return;
|
|
71
|
+
runningRef.current = true;
|
|
72
|
+
setRunning(true);
|
|
73
|
+
if (action.invoke) {
|
|
74
|
+
api.invoke(action.invoke.id, action.invoke.input);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (action.send) {
|
|
78
|
+
if (typeof action.send === 'string') api.send(action.send);
|
|
79
|
+
else api.send(action.send.text, { displayText: action.send.displayText });
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const trigger = (action: CardViewAction) => {
|
|
83
|
+
if (api.disabled || runningRef.current || pending !== null) return;
|
|
84
|
+
if (action.confirm) {
|
|
85
|
+
setPending(action);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
run(action);
|
|
89
|
+
};
|
|
90
|
+
const sheet =
|
|
91
|
+
pending && pending.confirm ? (
|
|
92
|
+
<ConfirmSheet
|
|
93
|
+
open
|
|
94
|
+
title={pending.confirm.title}
|
|
95
|
+
{...(pending.confirm.description ? { description: pending.confirm.description } : {})}
|
|
96
|
+
{...(pending.confirm.confirmText ? { confirmText: pending.confirm.confirmText } : {})}
|
|
97
|
+
{...(pending.confirm.cancelText ? { cancelText: pending.confirm.cancelText } : {})}
|
|
98
|
+
onConfirm={() => {
|
|
99
|
+
const action = pending;
|
|
100
|
+
setPending(null);
|
|
101
|
+
run(action);
|
|
102
|
+
}}
|
|
103
|
+
onCancel={() => setPending(null)}
|
|
104
|
+
/>
|
|
105
|
+
) : null;
|
|
106
|
+
return { trigger, sheet, busy: api.disabled || running || pending !== null };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function renderBlock(
|
|
110
|
+
block: CardViewBlock,
|
|
111
|
+
trigger: (action: CardViewAction) => void,
|
|
112
|
+
disabled: boolean,
|
|
113
|
+
): ReactNode {
|
|
114
|
+
switch (block.kind) {
|
|
115
|
+
case 'hero':
|
|
116
|
+
return (
|
|
117
|
+
<Hero
|
|
118
|
+
{...(block.eyebrow ? { eyebrow: block.eyebrow } : {})}
|
|
119
|
+
title={block.title}
|
|
120
|
+
{...(block.value ? { value: block.value } : {})}
|
|
121
|
+
{...(block.unit ? { unit: block.unit } : {})}
|
|
122
|
+
{...(block.footnote ? { footnote: block.footnote } : {})}
|
|
123
|
+
{...(block.tone ? { tone: block.tone } : {})}
|
|
124
|
+
/>
|
|
125
|
+
);
|
|
126
|
+
case 'facts':
|
|
127
|
+
return <KeyValueList items={block.items.map(([label, value]) => ({ label, value }))} />;
|
|
128
|
+
case 'bill':
|
|
129
|
+
return (
|
|
130
|
+
<Bill
|
|
131
|
+
lines={block.lines.map(([label, value, muted]) => ({ label, value, ...(muted ? { muted: true } : {}) }))}
|
|
132
|
+
{...(block.total ? { total: { label: block.total[0], value: block.total[1] } } : {})}
|
|
133
|
+
/>
|
|
134
|
+
);
|
|
135
|
+
case 'notice':
|
|
136
|
+
return <Notice {...(block.tone ? { tone: block.tone } : {})}>{block.text}</Notice>;
|
|
137
|
+
case 'tags':
|
|
138
|
+
return <TagList tags={block.tags} {...(block.tone ? { tone: block.tone } : {})} />;
|
|
139
|
+
case 'steps':
|
|
140
|
+
return <Steps steps={block.steps} />;
|
|
141
|
+
case 'ticket':
|
|
142
|
+
return (
|
|
143
|
+
<Ticket
|
|
144
|
+
amount={block.amount}
|
|
145
|
+
{...(block.condition ? { condition: block.condition } : {})}
|
|
146
|
+
{...(block.meta ? { meta: block.meta } : {})}
|
|
147
|
+
{...(block.status ? { status: block.status } : {})}
|
|
148
|
+
{...(block.tone ? { tone: block.tone } : {})}
|
|
149
|
+
/>
|
|
150
|
+
);
|
|
151
|
+
case 'timeline':
|
|
152
|
+
return (
|
|
153
|
+
<Timeline
|
|
154
|
+
items={block.items.map((item) => ({
|
|
155
|
+
label: item.label,
|
|
156
|
+
...(item.amount ? { amount: item.amount } : {}),
|
|
157
|
+
...(item.time ? { time: item.time } : {}),
|
|
158
|
+
...(item.tone ? { tone: item.tone } : {}),
|
|
159
|
+
}))}
|
|
160
|
+
/>
|
|
161
|
+
);
|
|
162
|
+
case 'empty':
|
|
163
|
+
return <EmptyState title={block.title} {...(block.description ? { description: block.description } : {})} />;
|
|
164
|
+
case 'list':
|
|
165
|
+
return (
|
|
166
|
+
<div className="tbox-view-list flex flex-col gap-2.5">
|
|
167
|
+
{block.items.map((item, index) => (
|
|
168
|
+
<EntityRow
|
|
169
|
+
key={item.title + '-' + index}
|
|
170
|
+
title={item.title}
|
|
171
|
+
{...(item.meta ? { meta: item.meta } : {})}
|
|
172
|
+
{...(item.price ? { price: item.price } : {})}
|
|
173
|
+
{...(item.description ? { description: item.description } : {})}
|
|
174
|
+
{...(item.tags && item.tags.length > 0 ? { tags: <TagList tags={item.tags} /> } : {})}
|
|
175
|
+
{...(item.action
|
|
176
|
+
? {
|
|
177
|
+
action: (
|
|
178
|
+
<CardButton
|
|
179
|
+
variant={item.action.style === 'primary' ? 'primary' : 'secondary'}
|
|
180
|
+
disabled={disabled}
|
|
181
|
+
onClick={() => trigger(item.action as CardViewAction)}
|
|
182
|
+
>
|
|
183
|
+
{item.action.label}
|
|
184
|
+
</CardButton>
|
|
185
|
+
),
|
|
186
|
+
}
|
|
187
|
+
: {})}
|
|
188
|
+
/>
|
|
189
|
+
))}
|
|
190
|
+
</div>
|
|
191
|
+
);
|
|
192
|
+
default: {
|
|
193
|
+
const unknown = block as { kind?: string };
|
|
194
|
+
console.warn('[ViewCard] 未知积木,已跳过:' + String(unknown.kind));
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function ViewCard({
|
|
201
|
+
view,
|
|
202
|
+
cardId,
|
|
203
|
+
isHistory,
|
|
204
|
+
actionTokens,
|
|
205
|
+
}: {
|
|
206
|
+
view: CardView;
|
|
207
|
+
cardId: string;
|
|
208
|
+
isHistory?: boolean;
|
|
209
|
+
actionTokens?: Record<string, string>;
|
|
210
|
+
}) {
|
|
211
|
+
let safeView: CardView;
|
|
212
|
+
try {
|
|
213
|
+
safeView = normalizeCardView(view);
|
|
214
|
+
} catch {
|
|
215
|
+
return (
|
|
216
|
+
<Card>
|
|
217
|
+
<Notice tone="error">卡片数据不可用,请重新触发一次</Notice>
|
|
218
|
+
</Card>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
const api = useCardActions({
|
|
222
|
+
cardId,
|
|
223
|
+
isHistory: isHistory === true,
|
|
224
|
+
...(actionTokens ? { actionTokens } : {}),
|
|
225
|
+
});
|
|
226
|
+
const inline = useInlineRunner(api);
|
|
227
|
+
return (
|
|
228
|
+
<Card>
|
|
229
|
+
<CardHeader
|
|
230
|
+
title={safeView.header.title}
|
|
231
|
+
{...(safeView.header.eyebrow ? { eyebrow: safeView.header.eyebrow } : {})}
|
|
232
|
+
{...(safeView.header.status
|
|
233
|
+
? { status: <StatusTag tone={safeView.header.status.tone ?? 'neutral'}>{safeView.header.status.text}</StatusTag> }
|
|
234
|
+
: {})}
|
|
235
|
+
/>
|
|
236
|
+
{safeView.blocks.map((block, index) => (
|
|
237
|
+
<div key={block.kind + '-' + index}>{renderBlock(block, inline.trigger, inline.busy)}</div>
|
|
238
|
+
))}
|
|
239
|
+
{safeView.actions && safeView.actions.length > 0 ? (
|
|
240
|
+
<ActionList
|
|
241
|
+
api={api}
|
|
242
|
+
actions={safeView.actions.map((action, index) =>
|
|
243
|
+
toSpec(index === 0 ? { ...action, style: action.style ?? 'primary' } : { ...action, style: 'secondary' }),
|
|
244
|
+
)}
|
|
245
|
+
/>
|
|
246
|
+
) : null}
|
|
247
|
+
{inline.sheet}
|
|
248
|
+
</Card>
|
|
249
|
+
);
|
|
250
|
+
}
|
package/src/chat-config.ts
CHANGED
|
@@ -40,14 +40,18 @@ interface ChatConfigStore {
|
|
|
40
40
|
applyServerSettings: (settings: ChatUiSettings | undefined) => void;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
// SSR / 探针注入点:浏览器生产环境不设置,零影响(探针看不到配置会误判)。
|
|
44
|
+
const ssrSnapshot = ((globalThis as { __TBOX_CHAT_CONFIG__?: Record<string, unknown> }).__TBOX_CHAT_CONFIG__ ?? {});
|
|
43
45
|
export const useChatConfigStore = create<ChatConfigStore>()((set) => ({
|
|
44
|
-
// kit 内建缺省(应用快照经 configureChatConfigDefaults
|
|
45
|
-
enableImageUpload: true,
|
|
46
|
-
enableVideoUpload: true,
|
|
47
|
-
enableTTS: true,
|
|
48
|
-
enableVoiceInput: true,
|
|
49
|
-
defaultInputMode: 'TEXT',
|
|
50
|
-
quickCommands: [],
|
|
46
|
+
// kit 内建缺省(应用快照经 configureChatConfigDefaults 覆盖;SSR 探针经 globalThis 注入)
|
|
47
|
+
enableImageUpload: (ssrSnapshot['enableImageUpload'] as boolean | undefined) ?? true,
|
|
48
|
+
enableVideoUpload: (ssrSnapshot['enableVideoUpload'] as boolean | undefined) ?? true,
|
|
49
|
+
enableTTS: (ssrSnapshot['enableTTS'] as boolean | undefined) ?? true,
|
|
50
|
+
enableVoiceInput: (ssrSnapshot['enableVoiceInput'] as boolean | undefined) ?? true,
|
|
51
|
+
defaultInputMode: (ssrSnapshot['defaultInputMode'] as 'TEXT' | 'VOICE' | undefined) ?? 'TEXT',
|
|
52
|
+
quickCommands: (ssrSnapshot['quickCommands'] as QuickCommand[] | undefined) ?? [],
|
|
53
|
+
identity: ssrSnapshot['identity'] as ChatIdentity | undefined,
|
|
54
|
+
welcome: ssrSnapshot['welcome'] as ChatWelcome | undefined,
|
|
51
55
|
retryOnError: true,
|
|
52
56
|
applyServerSettings: (settings) =>
|
|
53
57
|
set((cur) => ({
|