@tbox.cn/app-template-agent 1.0.0-alpha.7 → 1.0.0-alpha.8
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/.tbox/app.json +1 -1
- package/.tbox/default-modules.json +1 -1
- package/.tbox/miniapp-containers.json +1 -1
- package/AGENTS.md +8 -7
- package/apps/client/index.html +0 -43
- package/apps/client/scripts/check-evidence.mjs +327 -77
- package/apps/client/scripts/evidence-gate.mjs +135 -0
- package/apps/client/scripts/evidence-gate.test.mjs +77 -0
- package/apps/client/scripts/render-card.mjs +20 -67
- package/apps/client/scripts/surface-css.mjs +51 -0
- package/apps/client/src/app.config.ts +4 -1
- package/apps/client/src/styles/brand.css +2 -3
- package/apps/client/src/styles/tailwind.css +17 -17
- package/apps/server/scripts/scenario-lib.mjs +2 -1
- package/docs/agent/agent-prompt.md +1 -1
- package/docs/agent/card-action.md +5 -0
- package/docs/agent/card.md +44 -2
- package/docs/agent/kit.md +2 -2
- package/docs/agent/style.md +8 -2
- package/docs/agent/tool-emits-card.md +7 -2
- package/docs/agent/tool.md +2 -0
- package/docs/agent/verify.md +23 -9
- package/package.json +16 -5
- package/packages/agent-ui-kit/AGENTS.md +1 -1
- package/packages/agent-ui-kit/catalog.json +7 -3
- package/packages/agent-ui-kit/package.json +1 -1
- package/packages/agent-ui-kit/src/ChatPage.tsx +40 -16
- package/packages/agent-ui-kit/src/card/action-list-pending.test.tsx +91 -0
- package/packages/agent-ui-kit/src/card/define-card.test.ts +25 -0
- package/packages/agent-ui-kit/src/card/define-card.ts +18 -5
- package/packages/agent-ui-kit/src/card/primitives.tsx +122 -27
- package/packages/agent-ui-kit/src/card/view-card-inline.test.tsx +42 -0
- package/packages/agent-ui-kit/src/card/view-card.test.tsx +79 -0
- package/packages/agent-ui-kit/src/card/view-card.tsx +245 -0
- package/packages/agent-ui-kit/src/components/ChatInput/index.tsx +1 -1
- package/packages/agent-ui-kit/src/components/HistoryEntry/index.tsx +2 -2
- package/packages/agent-ui-kit/src/components/MessageBubble/index.tsx +11 -5
- package/packages/agent-ui-kit/src/components/QuickCommands/index.test.tsx +3 -1
- package/packages/agent-ui-kit/src/components/QuickCommands/index.tsx +31 -8
- package/packages/agent-ui-kit/src/index.ts +4 -0
- package/packages/agent-ui-kit/src/preview-provider.tsx +47 -0
- package/packages/agent-ui-kit/src/surface.test.tsx +35 -0
- package/packages/agent-ui-kit/src/surface.tsx +44 -0
- package/packages/agent-ui-kit/src/theme/base.css +9 -11
- package/packages/agent-ui-kit/src/theme/contract.json +45 -1
- package/packages/agent-ui-kit/src/theme/tokens.css +8 -11
- package/pnpm-lock.template.yaml +91 -91
- package/pnpm-workspace.template.yaml +10 -10
- package/scripts/assert-pack.mjs +48 -0
- package/scripts/check-css-target.mjs +6 -3
- package/scripts/checks.json +123 -26
- package/.tbox/card-render/baseline/demo.png +0 -0
- package/.tbox/card-render/demo.html +0 -342
- package/.tbox/card-render/demo.png +0 -0
- package/.tbox/card-render/scale-demo.audit.html +0 -984
- package/.tbox/card-render/scale-demo.html +0 -939
- package/.tbox/evidence/cards.txt +0 -1
- package/.tbox/evidence/gate.json +0 -1
- package/.tbox/evidence/last-gate.json +0 -1
- package/.tbox/evidence/scenarios.json +0 -1
- package/apps/client/.tbox-gen/._manifest.json +0 -0
- package/apps/client/.tbox-gen/._registry.client.ts +0 -0
- package/apps/client/.tbox-gen/._registry.server.ts +0 -0
- package/apps/client/.tbox-gen/app-registry.ts +0 -3
- package/apps/client/.tbox-gen/documents/._index.html +0 -0
- package/apps/client/.tbox-gen/documents/_tbox/._card-preview.html +0 -0
- package/apps/client/.tbox-gen/documents/_tbox/card-preview.html +0 -62
- package/apps/client/.tbox-gen/documents/index.html +0 -63
- package/apps/client/.tbox-gen/entries/._card-preview.tsx +0 -0
- package/apps/client/.tbox-gen/entries/._index.tsx +0 -0
- package/apps/client/.tbox-gen/entries/card-preview.tsx +0 -6
- package/apps/client/.tbox-gen/entries/index.tsx +0 -11
- package/apps/client/.tbox-gen/manifest.json +0 -17
- package/apps/client/.tbox-gen/registry.client.ts +0 -6
- package/apps/client/.tbox-gen/registry.server.ts +0 -15
- package/apps/client/STYLE_GUIDE.md +0 -48
- package/apps/client/VISUAL_RESTORE.md +0 -90
- package/scripts/._compat-probe.mjs +0 -0
- package/scripts/compat-probe.mjs +0 -108
- /package/{.tbox/card-render/._demo.html → apps/client/scripts/._evidence-gate.mjs} +0 -0
- /package/{.tbox/card-render/._demo.png → apps/client/scripts/._evidence-gate.test.mjs} +0 -0
- /package/{.tbox/card-render/._scale-demo.audit.html → apps/client/scripts/._surface-css.mjs} +0 -0
- /package/{.tbox/card-render/._scale-demo.html → packages/agent-ui-kit/src/._preview-provider.tsx} +0 -0
- /package/{.tbox/card-render/baseline/._demo.png → packages/agent-ui-kit/src/._surface.test.tsx} +0 -0
- /package/{.tbox/evidence/._cards.txt → packages/agent-ui-kit/src/._surface.tsx} +0 -0
- /package/{.tbox/evidence/._gate.json → packages/agent-ui-kit/src/card/._action-list-pending.test.tsx} +0 -0
- /package/{.tbox/evidence/._last-gate.json → packages/agent-ui-kit/src/card/._define-card.test.ts} +0 -0
- /package/{.tbox/evidence/._scenarios.json → packages/agent-ui-kit/src/card/._view-card-inline.test.tsx} +0 -0
- /package/{apps/client/._STYLE_GUIDE.md → packages/agent-ui-kit/src/card/._view-card.test.tsx} +0 -0
- /package/{apps/client/._VISUAL_RESTORE.md → packages/agent-ui-kit/src/card/._view-card.tsx} +0 -0
- /package/{apps/client/.tbox-gen/._app-registry.ts → scripts/._assert-pack.mjs} +0 -0
|
@@ -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
|
|
|
@@ -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 = {
|
|
@@ -575,11 +597,47 @@ export interface CardActionSpec {
|
|
|
575
597
|
*
|
|
576
598
|
* 用法:`const api = useCardActions({ cardId, isHistory, actionTokens }); <ActionList api={api} actions={[...]} />`
|
|
577
599
|
*
|
|
578
|
-
* @behavior
|
|
600
|
+
* @behavior 首个动作为整行主按钮,其余并排一行;confirm 走挂到 body 的 ConfirmSheet;disabled 自动接 api.disabled。
|
|
579
601
|
*/
|
|
602
|
+
const PENDING_TIMEOUT_MS = 20000;
|
|
603
|
+
|
|
580
604
|
export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: CardActions }) {
|
|
581
|
-
const [
|
|
582
|
-
const
|
|
605
|
+
const [confirmIndex, setConfirmIndex] = useState<number | null>(null);
|
|
606
|
+
const [running, setRunning] = useState<number | null>(null);
|
|
607
|
+
// 单飞用 ref 同步判重:state 要等重渲染,连点两下会都通过。
|
|
608
|
+
const runningRef = useRef<number | null>(null);
|
|
609
|
+
const wasGenerating = useRef(false);
|
|
610
|
+
|
|
611
|
+
// 兜底解除:没有回执(方案 A 不改协议)时,按钮不能永远转圈。
|
|
612
|
+
useEffect(() => {
|
|
613
|
+
if (running === null) return undefined;
|
|
614
|
+
const timer = setTimeout(() => {
|
|
615
|
+
runningRef.current = null;
|
|
616
|
+
setRunning(null);
|
|
617
|
+
}, PENDING_TIMEOUT_MS);
|
|
618
|
+
return () => clearTimeout(timer);
|
|
619
|
+
}, [running]);
|
|
620
|
+
|
|
621
|
+
// 生成态从 true 回到 false = 这一轮有结果,解除 pending。
|
|
622
|
+
// 无论点击发生在生成前还是生成后,标记都要在当前这一轮结束时复位。
|
|
623
|
+
useEffect(() => {
|
|
624
|
+
if (api.isGenerating) {
|
|
625
|
+
wasGenerating.current = true;
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
if (!wasGenerating.current) return;
|
|
629
|
+
wasGenerating.current = false;
|
|
630
|
+
if (runningRef.current !== null) {
|
|
631
|
+
runningRef.current = null;
|
|
632
|
+
setRunning(null);
|
|
633
|
+
}
|
|
634
|
+
}, [api.isGenerating]);
|
|
635
|
+
|
|
636
|
+
const run = (spec: CardActionSpec, index: number) => {
|
|
637
|
+
// 没有 send / invoke 的动作不进入 pending,否则只会空转。
|
|
638
|
+
if (!spec.invoke && !spec.send) return;
|
|
639
|
+
runningRef.current = index;
|
|
640
|
+
setRunning(index);
|
|
583
641
|
if (spec.invoke) {
|
|
584
642
|
api.invoke(spec.invoke.id, spec.invoke.input);
|
|
585
643
|
return;
|
|
@@ -590,22 +648,58 @@ export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: C
|
|
|
590
648
|
}
|
|
591
649
|
};
|
|
592
650
|
const trigger = (spec: CardActionSpec, index: number) => {
|
|
593
|
-
if (api.disabled) return;
|
|
651
|
+
if (api.disabled || runningRef.current !== null || confirmIndex !== null) return;
|
|
594
652
|
if (spec.confirm) {
|
|
595
|
-
|
|
653
|
+
setConfirmIndex(index);
|
|
596
654
|
return;
|
|
597
655
|
}
|
|
598
|
-
run(spec);
|
|
656
|
+
run(spec, index);
|
|
599
657
|
};
|
|
600
|
-
const pendingSpec =
|
|
658
|
+
const pendingSpec = confirmIndex === null ? undefined : actions[confirmIndex];
|
|
659
|
+
const busy = api.disabled || running !== null || confirmIndex !== null;
|
|
660
|
+
const label = (text: string, index: number) => (
|
|
661
|
+
<span className="inline-flex items-center gap-2">
|
|
662
|
+
{running === index ? (
|
|
663
|
+
<span
|
|
664
|
+
className="tbox-btn__spinner inline-block size-4 animate-spin rounded-full border-2 border-current border-t-transparent"
|
|
665
|
+
aria-hidden="true"
|
|
666
|
+
/>
|
|
667
|
+
) : null}
|
|
668
|
+
{text}
|
|
669
|
+
</span>
|
|
670
|
+
);
|
|
601
671
|
return (
|
|
602
672
|
<>
|
|
603
673
|
<div className="tbox-action-list mt-2.5 flex flex-col gap-2">
|
|
604
|
-
{actions
|
|
605
|
-
<CardButton
|
|
606
|
-
{
|
|
674
|
+
{actions[0] ? (
|
|
675
|
+
<CardButton
|
|
676
|
+
variant={actions[0].style ?? 'primary'}
|
|
677
|
+
disabled={busy}
|
|
678
|
+
busy={running === 0}
|
|
679
|
+
onClick={() => trigger(actions[0], 0)}
|
|
680
|
+
>
|
|
681
|
+
{label(actions[0].label, 0)}
|
|
607
682
|
</CardButton>
|
|
608
|
-
)
|
|
683
|
+
) : null}
|
|
684
|
+
{actions.length > 1 ? (
|
|
685
|
+
<div className="tbox-action-list__row flex gap-2">
|
|
686
|
+
{actions.slice(1).map((spec, index) => {
|
|
687
|
+
const actionIndex = index + 1;
|
|
688
|
+
return (
|
|
689
|
+
<div key={spec.label + '-' + actionIndex} className="min-w-0 flex-1">
|
|
690
|
+
<CardButton
|
|
691
|
+
variant={spec.style ?? 'secondary'}
|
|
692
|
+
disabled={busy}
|
|
693
|
+
busy={running === actionIndex}
|
|
694
|
+
onClick={() => trigger(spec, actionIndex)}
|
|
695
|
+
>
|
|
696
|
+
{label(spec.label, actionIndex)}
|
|
697
|
+
</CardButton>
|
|
698
|
+
</div>
|
|
699
|
+
);
|
|
700
|
+
})}
|
|
701
|
+
</div>
|
|
702
|
+
) : null}
|
|
609
703
|
</div>
|
|
610
704
|
{pendingSpec?.confirm ? (
|
|
611
705
|
<ConfirmSheet
|
|
@@ -616,10 +710,11 @@ export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: C
|
|
|
616
710
|
{...(pendingSpec.confirm.cancelText ? { cancelText: pendingSpec.confirm.cancelText } : {})}
|
|
617
711
|
onConfirm={() => {
|
|
618
712
|
const spec = pendingSpec;
|
|
619
|
-
|
|
620
|
-
|
|
713
|
+
const index = confirmIndex ?? 0;
|
|
714
|
+
setConfirmIndex(null);
|
|
715
|
+
run(spec, index);
|
|
621
716
|
}}
|
|
622
|
-
onCancel={() =>
|
|
717
|
+
onCancel={() => setConfirmIndex(null)}
|
|
623
718
|
/>
|
|
624
719
|
) : null}
|
|
625
720
|
</>
|
|
@@ -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,245 @@
|
|
|
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 api={api} actions={safeView.actions.map(toSpec)} />
|
|
241
|
+
) : null}
|
|
242
|
+
{inline.sheet}
|
|
243
|
+
</Card>
|
|
244
|
+
);
|
|
245
|
+
}
|
|
@@ -208,7 +208,7 @@ export function ChatInput({
|
|
|
208
208
|
/>
|
|
209
209
|
) : (
|
|
210
210
|
<textarea
|
|
211
|
-
className="tbox-chat-input__field min-w-0 flex-1 resize-none border-0 bg-transparent p-0 text-base leading-[24px] text-foreground outline-none max-h-[100px] [font-family:inherit] placeholder:text-muted-foreground disabled:opacity-[0.45]"
|
|
211
|
+
className="tbox-chat-input__field min-h-11 min-w-0 flex-1 resize-none border-0 bg-transparent p-0 text-base leading-[24px] text-foreground outline-none max-h-[100px] [font-family:inherit] placeholder:text-muted-foreground disabled:opacity-[0.45]"
|
|
212
212
|
value={text}
|
|
213
213
|
placeholder={blocked?.message ?? '有什么需要问我的吗'}
|
|
214
214
|
rows={1}
|
|
@@ -29,11 +29,11 @@ export default function HistoryEntry({
|
|
|
29
29
|
<div className="history-entry flex justify-center p-0">
|
|
30
30
|
<button
|
|
31
31
|
type="button"
|
|
32
|
-
className="history-entry__button inline-flex appearance-none items-center gap-[5px] rounded-[25px] border-0 bg-
|
|
32
|
+
className="history-entry__button inline-flex min-h-11 appearance-none items-center gap-[5px] rounded-[25px] border-0 bg-muted px-4 py-1 text-sm font-normal leading-[18px] text-muted-foreground transition-colors duration-[var(--tbox-motion-duration-fast)] ease-[var(--tbox-motion-easing-standard)] [font-family:inherit] active:bg-border disabled:cursor-default"
|
|
33
33
|
disabled={disabled}
|
|
34
34
|
onClick={onLoad}
|
|
35
35
|
>
|
|
36
|
-
{(loading || pullStatus === 'loading') && <span className="history-entry__spinner size-[10px] rounded-full border-[1.5px] border-
|
|
36
|
+
{(loading || pullStatus === 'loading') && <span className="history-entry__spinner size-[10px] rounded-full border-[1.5px] border-border border-t-primary animate-[history-entry-spin_0.6s_linear_infinite]" />}
|
|
37
37
|
{labelOf(pullStatus, loading)}
|
|
38
38
|
</button>
|
|
39
39
|
</div>
|