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

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 (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +16 -2
  3. package/assets/keyboard-circle.svg +12 -0
  4. package/assets/more-circle.svg +3 -0
  5. package/assets/new-chat.svg +3 -0
  6. package/assets/quick-command-star.svg +3 -0
  7. package/assets/send-gradient.svg +14 -0
  8. package/assets/stop@2x.png +0 -0
  9. package/assets/voice-circle.svg +3 -0
  10. package/package.json +55 -3
  11. package/src/ChatPage.less +62 -0
  12. package/src/ChatPage.tsx +305 -0
  13. package/src/chat-config.test.ts +82 -0
  14. package/src/chat-config.ts +62 -0
  15. package/src/components/ActionBar/DebugButton.test.tsx +84 -0
  16. package/src/components/ActionBar/DebugButton.tsx +53 -0
  17. package/src/components/ActionBar/index.less +123 -0
  18. package/src/components/ActionBar/index.test.tsx +83 -0
  19. package/src/components/ActionBar/index.tsx +159 -0
  20. package/src/components/CardRenderer.tsx +36 -0
  21. package/src/components/ChatHeader/index.less +148 -0
  22. package/src/components/ChatHeader/index.test.tsx +74 -0
  23. package/src/components/ChatHeader/index.tsx +75 -0
  24. package/src/components/ChatInput/index.less +394 -0
  25. package/src/components/ChatInput/index.test.tsx +156 -0
  26. package/src/components/ChatInput/index.tsx +284 -0
  27. package/src/components/ChatInput/voice/VoicePressField.tsx +34 -0
  28. package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
  29. package/src/components/ChatInput/voice/machine.ts +27 -0
  30. package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
  31. package/src/components/HistoryEntry/index.less +49 -0
  32. package/src/components/HistoryEntry/index.tsx +42 -0
  33. package/src/components/MediaList/index.less +241 -0
  34. package/src/components/MediaList/index.tsx +256 -0
  35. package/src/components/MessageBubble/index.less +325 -0
  36. package/src/components/MessageBubble/index.test.tsx +134 -0
  37. package/src/components/MessageBubble/index.tsx +177 -0
  38. package/src/components/PullDownIndicator/index.less +32 -0
  39. package/src/components/PullDownIndicator/index.tsx +32 -0
  40. package/src/components/QuickCommands/index.less +63 -0
  41. package/src/components/QuickCommands/index.test.tsx +99 -0
  42. package/src/components/QuickCommands/index.tsx +52 -0
  43. package/src/components/ReasoningBlock/index.less +90 -0
  44. package/src/components/ReasoningBlock/index.tsx +56 -0
  45. package/src/components/ScrollToBottomButton/index.less +32 -0
  46. package/src/components/ScrollToBottomButton/index.tsx +10 -0
  47. package/src/components/ToolCallBlock/detail.test.ts +346 -0
  48. package/src/components/ToolCallBlock/detail.ts +204 -0
  49. package/src/components/ToolCallBlock/index.less +312 -0
  50. package/src/components/ToolCallBlock/index.test.tsx +261 -0
  51. package/src/components/ToolCallBlock/index.tsx +212 -0
  52. package/src/components/ToolCallBlock/titles.ts +102 -0
  53. package/src/hooks/use-drag-gesture.test.ts +66 -0
  54. package/src/hooks/use-drag-gesture.ts +129 -0
  55. package/src/hooks/use-pull-down-history.ts +241 -0
  56. package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
  57. package/src/hooks/useMediaUpload.ts +242 -0
  58. package/src/hooks/useTTSAutoPlay.ts +82 -0
  59. package/src/index.ts +69 -0
  60. package/src/store-context.tsx +92 -0
  61. package/src/test/setup.ts +7 -0
  62. package/src/theme/contract.json +72 -0
  63. package/src/theme/global.less +99 -0
  64. package/src/theme/motion.less +19 -0
  65. package/src/theme/theme-contract.test.ts +69 -0
  66. package/src/theme/theme.ts +17 -0
  67. package/src/theme/tokens.less +79 -0
  68. package/src/types.ts +29 -0
  69. package/src/utils/copyText.ts +11 -0
  70. package/src/utils/haptics.test.ts +51 -0
  71. package/src/utils/haptics.ts +44 -0
  72. package/src/utils/imagePreview.ts +10 -0
  73. package/src/utils/pull-down-machine.test.ts +188 -0
  74. package/src/utils/pull-down-machine.ts +157 -0
  75. package/src/utils/session-error.ts +5 -0
  76. package/src/utils/toast.ts +15 -0
  77. package/src/utils/tool-call-display.test.ts +240 -0
  78. package/src/utils/tool-call-display.ts +146 -0
  79. package/src/vite-env.d.ts +1 -0
@@ -0,0 +1,188 @@
1
+ /**
2
+ * createPullDownMachine 全转移表回归(纯逻辑,node 环境):
3
+ * - 实时门控取代 touchStart 锁定(未到顶 move 重锚 → 到顶后从最近锚点测距);
4
+ * - 顶部门槛跨越(pulling→ready→touchEnd shouldLoad)/ 不足回弹 / noMore 封顶 + holdNoMore;
5
+ * - isLoading 全忽略;wheel 累积语义(顶部向上滚累积越阈即发,发后/离顶/向下滚归零);reset 归零。
6
+ */
7
+ import { describe, it, expect } from 'vitest';
8
+ import {
9
+ createPullDownMachine,
10
+ DAMPING,
11
+ PULL_DOWN_THRESHOLD,
12
+ PULL_TRIGGER_OFFSET,
13
+ WHEEL_DISTANCE_SCALE,
14
+ WHEEL_TRIGGER_ACCUM,
15
+ } from './pull-down-machine';
16
+
17
+ /** 越过触发线所需的手指行程(阈值按内容位移定义,手指行程 = 位移 / 阻尼)。 */
18
+ const FINGER_OVER = PULL_TRIGGER_OFFSET / DAMPING + 10;
19
+
20
+ describe('createPullDownMachine(touch 路径)', () => {
21
+ it('M1 二段手势:未到顶 move×3 重锚 → 到顶后从最近锚点测距(实时门控)', () => {
22
+ const m = createPullDownMachine();
23
+ m.touchStart(300);
24
+ // 未到顶:重锚 + 状态不动(无 preventDefault)
25
+ expect(m.touchMove({ clientY: 250, isAtTop: false, hasMore: true, isLoading: false })).toEqual({ preventDefault: false });
26
+ expect(m.touchMove({ clientY: 200, isAtTop: false, hasMore: true, isLoading: false })).toEqual({ preventDefault: false });
27
+ m.touchMove({ clientY: 100, isAtTop: false, hasMore: true, isLoading: false });
28
+ // 到顶后:从 100 测距(若未重锚会从 300 测出巨值)
29
+ const r = m.touchMove({ clientY: 130, isAtTop: true, hasMore: true, isLoading: false });
30
+ expect(r.preventDefault).toBe(true);
31
+ expect(m.getState().distance).toBe(30 * DAMPING);
32
+ expect(m.getState().status).toBe('pulling');
33
+ });
34
+
35
+ it('M2 门槛跨越:distance > PULL_TRIGGER_OFFSET → ready;touchEnd 触发 shouldLoad 并进入 loading', () => {
36
+ const m = createPullDownMachine();
37
+ m.touchStart(100);
38
+ m.touchMove({ clientY: 100 + FINGER_OVER, isAtTop: true, hasMore: true, isLoading: false });
39
+ expect(m.getState().status).toBe('ready');
40
+ const end = m.touchEnd({ hasMore: true });
41
+ expect(end).toEqual({ shouldLoad: true, holdNoMore: false });
42
+ expect(m.getState()).toEqual({ status: 'loading', distance: PULL_DOWN_THRESHOLD });
43
+ });
44
+
45
+ it('M2b 触发线以下:保持 pulling,松手不加载(位移≠意图)', () => {
46
+ const m = createPullDownMachine();
47
+ m.touchStart(100);
48
+ // 差一点越线:阈值按内容位移定义,故手指行程取 (触发线 - 1) / 阻尼
49
+ m.touchMove({ clientY: 100 + (PULL_TRIGGER_OFFSET - 1) / DAMPING, isAtTop: true, hasMore: true, isLoading: false });
50
+ expect(m.getState().distance).toBeLessThan(PULL_TRIGGER_OFFSET);
51
+ expect(m.getState().status).toBe('pulling');
52
+ expect(m.touchEnd({ hasMore: true })).toEqual({ shouldLoad: false, holdNoMore: false });
53
+ });
54
+
55
+ it('M3 不足阈值 touchEnd:回弹 idle(shouldLoad=false)', () => {
56
+ const m = createPullDownMachine();
57
+ m.touchStart(100);
58
+ m.touchMove({ clientY: 120, isAtTop: true, hasMore: true, isLoading: false });
59
+ expect(m.getState().status).toBe('pulling');
60
+ expect(m.touchEnd({ hasMore: true })).toEqual({ shouldLoad: false, holdNoMore: false });
61
+ expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
62
+ });
63
+
64
+ it('M4 deltaY<=0(上滑):状态回 idle、distance 归零', () => {
65
+ const m = createPullDownMachine();
66
+ m.touchStart(100);
67
+ m.touchMove({ clientY: 140, isAtTop: true, hasMore: true, isLoading: false });
68
+ expect(m.getState().status).toBe('pulling');
69
+ m.touchMove({ clientY: 90, isAtTop: true, hasMore: true, isLoading: false });
70
+ expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
71
+ });
72
+
73
+ it('M5 hasMore=false:noMore 态 + 距离封顶 + touchEnd holdNoMore', () => {
74
+ const m = createPullDownMachine();
75
+ m.touchStart(100);
76
+ m.touchMove({ clientY: 400, isAtTop: true, hasMore: false, isLoading: false });
77
+ expect(m.getState().status).toBe('noMore');
78
+ expect(m.getState().distance).toBe(PULL_DOWN_THRESHOLD); // 封顶
79
+ expect(m.touchEnd({ hasMore: false })).toEqual({ shouldLoad: false, holdNoMore: true });
80
+ expect(m.getState().status).toBe('noMore'); // 保留(wrapper 定时 reset)
81
+ });
82
+
83
+ it('M6 isLoading=true:touchMove 全忽略', () => {
84
+ const m = createPullDownMachine();
85
+ m.touchStart(100);
86
+ const r = m.touchMove({ clientY: 400, isAtTop: true, hasMore: true, isLoading: true });
87
+ expect(r.preventDefault).toBe(false);
88
+ expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
89
+ });
90
+ });
91
+
92
+ describe('createPullDownMachine(wheel 路径)', () => {
93
+ it('W1 顶部向上滚累积:分次未达阈不触发,越阈触发且归零', () => {
94
+ const m = createPullDownMachine();
95
+ const step = WHEEL_TRIGGER_ACCUM / 2; // 两步刚好到线(不越),第三步越线
96
+ expect(m.wheel({ deltaY: -step, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
97
+ expect(m.wheel({ deltaY: -step, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
98
+ expect(m.wheel({ deltaY: -10, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: true }); // 越线
99
+ expect(m.wheel({ deltaY: -step, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false }); // 触发后归零重计
100
+ });
101
+
102
+ it('W1b 单次大 deltaY 不足以触发:滚轮需连续意图(回归「一滚即加载」)', () => {
103
+ const m = createPullDownMachine();
104
+ expect(m.wheel({ deltaY: -(WHEEL_TRIGGER_ACCUM - 1), isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
105
+ });
106
+
107
+ it('W2 离顶 / 向下滚:累积清零', () => {
108
+ const m = createPullDownMachine();
109
+ const near = WHEEL_TRIGGER_ACCUM - 10; // 差一点越线
110
+ m.wheel({ deltaY: -near, isAtTop: true, hasMore: true });
111
+ m.wheel({ deltaY: 10, isAtTop: true, hasMore: true }); // 向下滚清零
112
+ expect(m.wheel({ deltaY: -near, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
113
+ m.wheel({ deltaY: -near, isAtTop: false, hasMore: true }); // 离顶清零
114
+ expect(m.wheel({ deltaY: -near, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
115
+ });
116
+
117
+ it('W3 wheel 驱动与拖拽同尺度的视觉:累积中 pulling,越阈直接进 loading', () => {
118
+ const m = createPullDownMachine();
119
+ const half = WHEEL_TRIGGER_ACCUM / 2;
120
+ m.wheel({ deltaY: -half, isAtTop: true, hasMore: true });
121
+ expect(m.getState().status).toBe('pulling');
122
+ // 累积按触发线之比换算到拖拽尺度,两条路径的文案切换点因此重合
123
+ expect(m.getState().distance).toBeCloseTo(half * WHEEL_DISTANCE_SCALE);
124
+
125
+ m.wheel({ deltaY: -WHEEL_TRIGGER_ACCUM, isAtTop: true, hasMore: true }); // 越线
126
+ // 没有「松手」这一刻,越阈即发:直接进 loading,不经 ready
127
+ expect(m.getState().status).toBe('loading');
128
+ });
129
+
130
+ it('W4 wheelIdle:停止滚动后清掉累积与视觉,文案不卡在拉动态', () => {
131
+ const m = createPullDownMachine();
132
+ const near = WHEEL_TRIGGER_ACCUM - 10;
133
+ m.wheel({ deltaY: -near, isAtTop: true, hasMore: true });
134
+ expect(m.getState().status).toBe('pulling');
135
+ m.wheelIdle();
136
+ expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
137
+ // 累积也已清零:复位后需重新攒够才触发
138
+ expect(m.wheel({ deltaY: -near, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
139
+ });
140
+
141
+ it('W4b 加载中 wheel 全忽略:惯性滚动不得把 loading 打回 pulling(动画弹跳根因)', () => {
142
+ const m = createPullDownMachine();
143
+ m.wheel({ deltaY: -(WHEEL_TRIGGER_ACCUM + 10), isAtTop: true, hasMore: true });
144
+ expect(m.getState().status).toBe('loading');
145
+ const during = m.getState();
146
+
147
+ // 触控板惯性在加载期间继续送 wheel
148
+ expect(m.wheel({ deltaY: -200, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
149
+ expect(m.getState()).toEqual(during);
150
+ // 显式 isLoading 同样短路
151
+ expect(m.wheel({ deltaY: -200, isAtTop: true, hasMore: true, isLoading: true })).toEqual({ shouldLoad: false });
152
+ expect(m.getState()).toEqual(during);
153
+ });
154
+
155
+ it('W4c wheelIdle 不打断加载:loading 期间只清累积,视觉留给加载完成时的 reset', () => {
156
+ const m = createPullDownMachine();
157
+ m.wheel({ deltaY: -(WHEEL_TRIGGER_ACCUM + 10), isAtTop: true, hasMore: true });
158
+ expect(m.getState().status).toBe('loading');
159
+ m.wheelIdle();
160
+ expect(m.getState().status).toBe('loading'); // 文案不该被打回「历史记录」
161
+ });
162
+
163
+ it('W5 hasMore=false:wheel 同样进 noMore 且距离封顶,不发加载', () => {
164
+ const m = createPullDownMachine();
165
+ expect(m.wheel({ deltaY: -500, isAtTop: true, hasMore: false })).toEqual({ shouldLoad: false });
166
+ expect(m.getState().status).toBe('noMore');
167
+ expect(m.getState().distance).toBe(PULL_DOWN_THRESHOLD);
168
+ });
169
+ });
170
+
171
+ describe('createPullDownMachine(reset)', () => {
172
+ it('R1 reset:status/distance/wheelAccum 全归零(G9 noMore 复活)', () => {
173
+ const m = createPullDownMachine();
174
+ m.touchStart(100);
175
+ m.touchMove({ clientY: 100 + FINGER_OVER, isAtTop: true, hasMore: false, isLoading: false });
176
+ expect(m.getState().status).toBe('noMore');
177
+ m.wheel({ deltaY: -50, isAtTop: true, hasMore: true });
178
+ m.reset();
179
+ expect(m.getState()).toEqual({ status: 'idle', distance: 0 });
180
+ // noMore 复活:reset 后 hasMore=true 的正常下拉可用
181
+ m.touchStart(100);
182
+ m.touchMove({ clientY: 100 + FINGER_OVER, isAtTop: true, hasMore: true, isLoading: false });
183
+ expect(m.getState().status).toBe('ready');
184
+ // wheelAccum 已归零
185
+ expect(m.wheel({ deltaY: -30, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
186
+ expect(m.wheel({ deltaY: -30, isAtTop: true, hasMore: true })).toEqual({ shouldLoad: false });
187
+ });
188
+ });
@@ -0,0 +1,157 @@
1
+ /**
2
+ * 下拉加载历史手势状态机(纯逻辑,零 React 依赖,node 可测)。
3
+ * 核心修正:touchStart 不再锁定 isAtTop(旧实现在未滚动容器上初始 false → 全新手势死区),
4
+ * 门控移到 touchMove 实时判定——未到顶时每次重锚,到顶后从最近锚点测距;
5
+ * wheel 为桌面回退(顶部向上滚累积,越阈即发,发后归零)。
6
+ * 阈值三分:PULL_DOWN_THRESHOLD 只管指示器视觉,PULL_TRIGGER_OFFSET 管触摸触发,
7
+ * WHEEL_TRIGGER_ACCUM 管滚轮触发——共用一个常量会让「看得见」等同于「要加载」。
8
+ * wrapper(use-pull-down-history)职责:状态镜像 / 定时器 / disabled 短路;
9
+ * 业务防抖权威在 store(isLoadingHistory),机器内 isLoading 忽略为双保险。
10
+ */
11
+
12
+ /** 指示器完全露出所需的阻尼后位移——纯视觉基准(PullDownIndicator 的 marginTop 同源)。 */
13
+ export const PULL_DOWN_THRESHOLD = 60;
14
+ /**
15
+ * 触发加载所需的**内容位移**,对齐平台惯例:iOS UIRefreshControl 实际在 60~80pt 触发
16
+ * (控件自身高约 60pt),Android SwipeRefreshLayout 默认 64dp(DEFAULT_CIRCLE_TARGET)。
17
+ * 取 64 落在两者交集;阻尼 0.5 下对应手指行程 128px。
18
+ * 注意这些惯例量的是内容位移而非手指行程——按手指行程设阈值会得到四五倍的错值。
19
+ */
20
+ export const PULL_TRIGGER_OFFSET = 64;
21
+ /**
22
+ * 桌面滚轮累积触发阈值(单独于触摸):一次滚轮事件 deltaY 常见 100+,
23
+ * 与触摸共用 60 会「一滚即加载」;这里要求连续向上滚约三次。
24
+ */
25
+ export const WHEEL_TRIGGER_ACCUM = 128;
26
+ /** wheel 累积 → 拖拽尺度的换算比,使两条路径的位移与文案切换点重合。 */
27
+ export const WHEEL_DISTANCE_SCALE = PULL_TRIGGER_OFFSET / WHEEL_TRIGGER_ACCUM;
28
+ /** wheel 停止多久后复位视觉(ms)——触控板惯性滚动的间隔通常远小于此。 */
29
+ export const WHEEL_IDLE_RESET_MS = 220;
30
+ /**
31
+ * 拖拽阻尼:原生下拉在小位移区间接近 1:1 跟手,0.4 会显得「沉」(手指 100px 内容才动 40px)。
32
+ * 0.5 兼顾跟手感与「越拉越沉」的物理暗示。
33
+ */
34
+ export const DAMPING = 0.5;
35
+ /** noMore 展示保持时长(wrapper 定时后 reset;机器内部无定时器保 node 可测) */
36
+ export const NO_MORE_HOLD_MS = 1500;
37
+
38
+ export type PullStatus = 'idle' | 'pulling' | 'ready' | 'loading' | 'noMore';
39
+
40
+ export interface PullDownState {
41
+ status: PullStatus;
42
+ distance: number;
43
+ }
44
+
45
+ export function createPullDownMachine() {
46
+ let status: PullStatus = 'idle';
47
+ let distance = 0;
48
+ let startY = 0;
49
+ let wheelAccum = 0;
50
+
51
+ /** 同步置 loading(touchEnd 越阈触发;离开由 wrapper 在 onLoad 完成后 reset()) */
52
+ const enterLoading = () => {
53
+ status = 'loading';
54
+ distance = PULL_DOWN_THRESHOLD;
55
+ };
56
+
57
+ const resetDistance = () => {
58
+ if (status !== 'idle' && status !== 'noMore') {
59
+ status = 'idle';
60
+ }
61
+ if (status !== 'noMore') distance = 0;
62
+ };
63
+
64
+ return {
65
+ getState(): PullDownState {
66
+ return { status, distance };
67
+ },
68
+
69
+ touchStart(clientY: number): void {
70
+ startY = clientY;
71
+ },
72
+
73
+ touchMove(i: { clientY: number; isAtTop: boolean; hasMore: boolean; isLoading: boolean }): { preventDefault: boolean } {
74
+ if (i.isLoading) return { preventDefault: false };
75
+ // 未到顶:重锚(原生滚动照常),状态回 idle(noMore 保留——顶部已无更多的展示态)
76
+ if (!i.isAtTop) {
77
+ startY = i.clientY;
78
+ resetDistance();
79
+ return { preventDefault: false };
80
+ }
81
+ const deltaY = i.clientY - startY;
82
+ if (deltaY <= 0) {
83
+ resetDistance();
84
+ return { preventDefault: false };
85
+ }
86
+ if (!i.hasMore) {
87
+ status = 'noMore';
88
+ distance = Math.min(deltaY * DAMPING, PULL_DOWN_THRESHOLD);
89
+ return { preventDefault: true };
90
+ }
91
+ distance = deltaY * DAMPING;
92
+ status = distance > PULL_TRIGGER_OFFSET ? 'ready' : 'pulling';
93
+ return { preventDefault: true };
94
+ },
95
+
96
+ touchEnd(i: { hasMore: boolean }): { shouldLoad: boolean; holdNoMore: boolean } {
97
+ if (status === 'ready' && i.hasMore) {
98
+ enterLoading();
99
+ return { shouldLoad: true, holdNoMore: false };
100
+ }
101
+ if (status === 'noMore') {
102
+ return { shouldLoad: false, holdNoMore: true }; // wrapper 定时后 reset
103
+ }
104
+ status = 'idle';
105
+ distance = 0;
106
+ return { shouldLoad: false, holdNoMore: false };
107
+ },
108
+
109
+ /**
110
+ * 触控板双指滑动 / 滚轮。与拖拽的差别是没有「松手」这一刻,故越阈即发、不经 ready 等释放。
111
+ * 但累积过程同样驱动 status/distance——否则双指滑动全程无反馈、加载突然发生,
112
+ * 与拖拽的观感对不上。distance 按触发线之比换算到与拖拽同一尺度,胶囊位移与文案才同步。
113
+ */
114
+ wheel(i: { deltaY: number; isAtTop: boolean; hasMore: boolean; isLoading?: boolean }): { shouldLoad: boolean } {
115
+ // 加载中全忽略:触控板惯性会在加载期间继续送 wheel,若放行会把 status 从 loading
116
+ // 改回 pulling、distance 反复增减——表现为动画弹来弹去(touchMove 同守卫)。
117
+ if (i.isLoading || status === 'loading') return { shouldLoad: false };
118
+ // 离顶 / 向下滚:累积与视觉一并复位
119
+ if (!i.isAtTop || i.deltaY >= 0) {
120
+ wheelAccum = 0;
121
+ resetDistance();
122
+ return { shouldLoad: false };
123
+ }
124
+ wheelAccum -= i.deltaY;
125
+ if (!i.hasMore) {
126
+ status = 'noMore';
127
+ distance = Math.min(wheelAccum * WHEEL_DISTANCE_SCALE, PULL_DOWN_THRESHOLD);
128
+ return { shouldLoad: false };
129
+ }
130
+ if (wheelAccum > WHEEL_TRIGGER_ACCUM) {
131
+ wheelAccum = 0;
132
+ enterLoading();
133
+ return { shouldLoad: true };
134
+ }
135
+ distance = wheelAccum * WHEEL_DISTANCE_SCALE;
136
+ status = 'pulling';
137
+ return { shouldLoad: false };
138
+ },
139
+
140
+ /**
141
+ * wheel 停止后由 wrapper 定时调用:清掉累积与视觉,避免文案卡在拉动态。
142
+ * loading 期间只清累积不动视觉——此刻的 loading 由加载完成时的 reset 收尾,
143
+ * 在这里复位会把「加载中」文案打回「历史记录」。
144
+ */
145
+ wheelIdle(): void {
146
+ wheelAccum = 0;
147
+ if (status !== 'loading') resetDistance();
148
+ },
149
+
150
+ reset(): void {
151
+ status = 'idle';
152
+ distance = 0;
153
+ startY = 0;
154
+ wheelAccum = 0;
155
+ },
156
+ };
157
+ }
@@ -0,0 +1,5 @@
1
+ /** 判断错误是否为 session 过期(保守策略:宁可多重试一次)。 */
2
+ export function isSessionExpiredError(err: unknown): boolean {
3
+ const msg = String((err as Error)?.message || err || '');
4
+ return /过期|expired|PARAM_INVALID|unauthorized|token/i.test(msg);
5
+ }
@@ -0,0 +1,15 @@
1
+ /** 提示语气(对齐原模板 TboxCard.toast 的 tone):info 无图标、success/error 带图标 */
2
+ export type ToastTone = 'info' | 'success' | 'error';
3
+
4
+ /**
5
+ * 轻提示(懒加载 antd-mobile 的 Toast)。
6
+ * 与 imagePreview 共用同一个 antd-mobile 动态 chunk——首次弹提示/看大图时才加载,
7
+ * 不占首屏体积。错误反馈等延迟一帧无感。
8
+ *
9
+ * @param tone 语气:success → 成功图标,error → 失败图标,info/省略 → 无图标(对齐旧模板映射)
10
+ */
11
+ export async function toast(content: string, tone: ToastTone = 'info'): Promise<void> {
12
+ const { Toast } = await import('antd-mobile');
13
+ const icon = tone === 'success' ? 'success' : tone === 'error' ? 'fail' : undefined;
14
+ Toast.show(icon ? { content, icon } : { content });
15
+ }
@@ -0,0 +1,240 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { describeToolCall, formatCompactToolIo } from './tool-call-display';
3
+ import type { ToolCallInfo } from '@tbox.cn/app-agent-sdk-client';
4
+ import { buildPreToolArgsPayload, buildPreToolResultPayload } from '@tbox.cn/app-contracts';
5
+ import {
6
+ buildMemoryToolCallPayload,
7
+ buildSkillToolCallPayload,
8
+ MEMORY_TOOL_CALL_NAME,
9
+ } from '@tbox.cn/app-contracts';
10
+
11
+ const normal = (over: Partial<ToolCallInfo> = {}): ToolCallInfo => ({
12
+ id: 't1',
13
+ name: 'queryPoints',
14
+ status: 'done',
15
+ result: JSON.stringify({ temp: 22 }),
16
+ ...over,
17
+ });
18
+
19
+ describe('describeToolCall(010 下沉 S5:展示语义三分支)', () => {
20
+ it('skill:label 取 skillName,无详情', () => {
21
+ const { args } = buildSkillToolCallPayload({ name: 'demo-guide', description: '演示' });
22
+ const d = describeToolCall({
23
+ id: 's1',
24
+ name: '__tbox_skill',
25
+ args,
26
+ result: '{}',
27
+ status: 'done',
28
+ });
29
+ expect(d).toEqual({ kind: 'skill', label: 'demo-guide', hasDetail: false });
30
+ });
31
+
32
+ it('preTool done:label 取 description、详情 = 结果文本', () => {
33
+ const args = buildPreToolArgsPayload({
34
+ toolName: 'queryPoints',
35
+ description: '积分查询',
36
+ toolArgs: { accountId: 'M0001' },
37
+ });
38
+ const result = buildPreToolResultPayload({
39
+ toolName: 'queryPoints',
40
+ status: 'done',
41
+ result: { temp: 22 },
42
+ });
43
+ const d = describeToolCall({
44
+ id: 'p1',
45
+ name: '__tbox_pretool',
46
+ args,
47
+ result,
48
+ status: 'done',
49
+ });
50
+ expect(d.kind).toBe('preTool');
51
+ if (d.kind === 'preTool') {
52
+ expect(d.label).toBe('积分查询');
53
+ expect(d.status).toBe('done');
54
+ expect(d.hasDetail).toBe(true);
55
+ expect(d.resultText).toContain('"temp": 22');
56
+ }
57
+ });
58
+
59
+ it('preTool calling:label 可用(ARGS 先行)、无详情', () => {
60
+ const args = buildPreToolArgsPayload({ toolName: 'queryPoints', description: '积分查询' });
61
+ const d = describeToolCall({
62
+ id: 'p2',
63
+ name: '__tbox_pretool',
64
+ args,
65
+ status: 'calling',
66
+ });
67
+ expect(d.kind).toBe('preTool');
68
+ if (d.kind === 'preTool') {
69
+ expect(d.status).toBe('calling');
70
+ expect(d.hasDetail).toBe(false);
71
+ expect(d.label).toBe('积分查询');
72
+ }
73
+ });
74
+
75
+ it('preTool error:经 result payload status 判定(ToolCallInfo.status 恒 done,R 语义)', () => {
76
+ const args = buildPreToolArgsPayload({ toolName: 'queryPoints', description: '积分查询' });
77
+ const result = buildPreToolResultPayload({
78
+ toolName: 'queryPoints',
79
+ status: 'error',
80
+ error: '超时',
81
+ });
82
+ const d = describeToolCall({
83
+ id: 'p3',
84
+ name: '__tbox_pretool',
85
+ args,
86
+ result,
87
+ status: 'done',
88
+ });
89
+ expect(d.kind).toBe('preTool');
90
+ if (d.kind === 'preTool') {
91
+ expect(d.status).toBe('error');
92
+ expect(d.errorText).toBe('超时');
93
+ }
94
+ });
95
+
96
+ it('preTool 容错:ARGS 非法 → label 降级空串、状态不崩', () => {
97
+ const d = describeToolCall({
98
+ id: 'p4',
99
+ name: '__tbox_pretool',
100
+ args: 'not-json',
101
+ result: 'not-json',
102
+ status: 'done',
103
+ });
104
+ expect(d.kind).toBe('preTool');
105
+ if (d.kind === 'preTool') {
106
+ expect(d.label).toBe('');
107
+ expect(d.status).toBe('done');
108
+ }
109
+ });
110
+
111
+ it('normal:label = 工具名,详情 = 有 result/error', () => {
112
+ expect(describeToolCall(normal())).toEqual({
113
+ kind: 'normal',
114
+ label: 'queryPoints',
115
+ hasDetail: true,
116
+ });
117
+ expect(describeToolCall(normal({ status: 'calling', result: undefined }))).toEqual({
118
+ kind: 'normal',
119
+ label: 'queryPoints',
120
+ hasDetail: false,
121
+ });
122
+ });
123
+ });
124
+
125
+ describe('describeToolCall memory 分支(018 tool-debug)', () => {
126
+ const memoryTc = (over: Partial<ToolCallInfo> = {}): ToolCallInfo => {
127
+ const payload = buildMemoryToolCallPayload({
128
+ query: '推荐美食',
129
+ status: 'hit',
130
+ records: [
131
+ { id: 'm1', kind: 'preference', content: '用户爱吃辣' },
132
+ { id: 'm2', kind: 'fact', content: '用户是会员' },
133
+ ],
134
+ durMs: 120,
135
+ });
136
+ return {
137
+ id: 'mem1',
138
+ name: MEMORY_TOOL_CALL_NAME,
139
+ args: payload.args,
140
+ result: payload.result,
141
+ status: 'done',
142
+ ...over,
143
+ };
144
+ };
145
+
146
+ it('hit:label 带命中数,详情 = records 预览', () => {
147
+ const d = describeToolCall(memoryTc());
148
+ expect(d.kind).toBe('memory');
149
+ if (d.kind === 'memory') {
150
+ expect(d.status).toBe('hit');
151
+ expect(d.hits).toBe(2);
152
+ expect(d.label).toBe('记忆召回 · 2 条命中');
153
+ expect(d.hasDetail).toBe(true);
154
+ expect(d.detailText).toContain('[preference] 用户爱吃辣');
155
+ expect(d.detailText).toContain('[fact] 用户是会员');
156
+ }
157
+ });
158
+
159
+ it('miss:无详情(I3/S1——miss 仅 label 表达)', () => {
160
+ const payload = buildMemoryToolCallPayload({ query: 'x', status: 'miss' });
161
+ const d = describeToolCall(
162
+ memoryTc({ args: payload.args, result: payload.result }),
163
+ );
164
+ expect(d.kind).toBe('memory');
165
+ if (d.kind === 'memory') {
166
+ expect(d.status).toBe('miss');
167
+ expect(d.hits).toBe(0);
168
+ expect(d.hasDetail).toBe(false);
169
+ expect(d.label).toBe('记忆召回');
170
+ }
171
+ });
172
+
173
+ it('degraded:详情 = 错误信息(调试载荷即错误本身)', () => {
174
+ const payload = buildMemoryToolCallPayload({
175
+ query: 'x',
176
+ status: 'degraded',
177
+ error: '记忆召回超时(600ms deadline)',
178
+ });
179
+ const d = describeToolCall(
180
+ memoryTc({ args: payload.args, result: payload.result }),
181
+ );
182
+ expect(d.kind).toBe('memory');
183
+ if (d.kind === 'memory') {
184
+ expect(d.status).toBe('degraded');
185
+ expect(d.hasDetail).toBe(true);
186
+ expect(d.detailText).toBe('记忆召回超时(600ms deadline)');
187
+ }
188
+ });
189
+
190
+ it('skipped:reason 意图命中进 label、无详情', () => {
191
+ const payload = buildMemoryToolCallPayload({ query: 'x', status: 'skipped', reason: 'intent' });
192
+ const d = describeToolCall(
193
+ memoryTc({ args: payload.args, result: payload.result }),
194
+ );
195
+ expect(d.kind).toBe('memory');
196
+ if (d.kind === 'memory') {
197
+ expect(d.status).toBe('skipped');
198
+ expect(d.label).toBe('记忆召回 · 跳过(意图命中)');
199
+ expect(d.hasDetail).toBe(false);
200
+ }
201
+ });
202
+
203
+ it('容错:result 非法 JSON → miss 降级不崩', () => {
204
+ const d = describeToolCall(memoryTc({ result: 'not-json' }));
205
+ expect(d.kind).toBe('memory');
206
+ if (d.kind === 'memory') {
207
+ expect(d.status).toBe('miss');
208
+ expect(d.hasDetail).toBe(false);
209
+ }
210
+ });
211
+ });
212
+
213
+ describe('formatCompactToolIo(018 dev 紧凑格式)', () => {
214
+ it('合法 JSON → 单行化', () => {
215
+ expect(formatCompactToolIo('{"a": 1, "b": [2, 3]}')).toBe('{"a":1,"b":[2,3]}');
216
+ });
217
+
218
+ it('短文本原样返回', () => {
219
+ expect(formatCompactToolIo('plain text')).toBe('plain text');
220
+ expect(formatCompactToolIo('')).toBe('');
221
+ });
222
+
223
+ it('超长 → 头尾保留 + 中段省略标记(tail 300 保尾部 cards/错误)', () => {
224
+ const head = 'h'.repeat(900);
225
+ const middle = 'm'.repeat(500);
226
+ const tail = 't'.repeat(300);
227
+ const out = formatCompactToolIo(head + middle + tail);
228
+ expect(out).toContain(head.slice(0, 100));
229
+ expect(out).toContain(tail.slice(-100));
230
+ expect(out).toContain('已省略 500 字符');
231
+ expect(out).not.toContain(middle.slice(0, 50));
232
+ });
233
+
234
+ it('自定义截断参数', () => {
235
+ const out = formatCompactToolIo('a'.repeat(100), { head: 20, tail: 10 });
236
+ expect(out).toContain('已省略 70 字符');
237
+ expect(out.startsWith('a'.repeat(20))).toBe(true);
238
+ expect(out.endsWith('a'.repeat(10))).toBe(true);
239
+ });
240
+ });