@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,207 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ import type { IASRAdapter } from '@tbox.cn/app-agent-sdk-client';
3
+ import { useRefreshSession } from '@tbox.cn/app-agent-sdk-client';
4
+ import { isSessionExpiredError } from '../../../utils/session-error';
5
+ import { toast } from '../../../utils/toast';
6
+ import {
7
+ getMoveInteractionState,
8
+ getReleaseAction,
9
+ type VoiceInteractionState,
10
+ } from './machine';
11
+
12
+ export interface VoicePressHandlers {
13
+ onTouchStart: (e: React.TouchEvent) => void;
14
+ onTouchMove: (e: React.TouchEvent) => void;
15
+ onTouchEnd: (e: React.TouchEvent) => void;
16
+ onTouchCancel: (e: React.TouchEvent) => void;
17
+ }
18
+
19
+ const MAX_DURATION = 60; // 录音上限(秒)
20
+ const COUNTDOWN_START = 10; // 剩余 10 秒起显示倒计时
21
+
22
+ // 录音权限被拒/不可用时给用户的提示(补回旧模板丢失的“被拒反馈”——旧版会弹 modal / 触发
23
+ // onPermissionDenied,新版此前是静默回 idle)。两端通用:toast 是 antd-mobile 的纯 H5 实现。
24
+ // 到这里的错误来自实际录音引擎(浏览器;或壳内桥失败降级后的 H5——见 FallbackASRAdapter),
25
+ // 故 NotFoundError 可精确判“无设备”,其余(NotAllowedError/未知)用“检查录音权限 + 重试”兜底。
26
+ const MIC_PERMISSION_MSG = '无法使用麦克风,请检查录音权限后重试';
27
+ function micErrorMessage(err: unknown): string {
28
+ const name = (err as { name?: string } | null)?.name;
29
+ if (name === 'NotFoundError' || name === 'DevicesNotFoundError') return '未检测到麦克风设备';
30
+ return MIC_PERMISSION_MSG;
31
+ }
32
+
33
+ /** 按住说话交互:按下录音、上滑取消、松手转文字、到 60s 自动停。识别结果通过 onResult 回传。 */
34
+ export function useVoicePress({
35
+ disabled,
36
+ asr,
37
+ onResult,
38
+ }: {
39
+ disabled?: boolean;
40
+ asr?: IASRAdapter;
41
+ onResult: (text: string) => void;
42
+ }) {
43
+ const refreshSession = useRefreshSession();
44
+ const [interactionState, setStateRaw] = useState<VoiceInteractionState>('idle');
45
+ const [duration, setDurationRaw] = useState(0);
46
+ const stateRef = useRef<VoiceInteractionState>('idle');
47
+ const durationRef = useRef(0);
48
+ const startYRef = useRef(0);
49
+ const recordingRef = useRef(false);
50
+ // 本次录音实际使用的 adapter:session 过期重试后会切到刷新出来的新 adapter
51
+ const recordingAsrRef = useRef<IASRAdapter | undefined>(asr);
52
+ const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
53
+ const finishRef = useRef<() => void>(() => {});
54
+
55
+ const setState = useCallback((s: VoiceInteractionState) => {
56
+ stateRef.current = s;
57
+ setStateRaw(s);
58
+ }, []);
59
+ const setDur = (n: number) => {
60
+ durationRef.current = n;
61
+ setDurationRaw(n);
62
+ };
63
+ const stopTimer = () => {
64
+ if (timerRef.current) {
65
+ clearInterval(timerRef.current);
66
+ timerRef.current = null;
67
+ }
68
+ };
69
+ const startTimer = () => {
70
+ stopTimer();
71
+ setDur(0);
72
+ timerRef.current = setInterval(() => {
73
+ setDur(durationRef.current + 1);
74
+ if (durationRef.current >= MAX_DURATION) {
75
+ stopTimer();
76
+ finishRef.current(); // 到上限按"松手"处理(转文字 / 若在取消区则取消)
77
+ }
78
+ }, 1000);
79
+ };
80
+
81
+ const onTouchStart = useCallback(
82
+ async (e: React.TouchEvent) => {
83
+ if (disabled || !asr || recordingRef.current) return;
84
+ // 长按录音由应用接管,避免移动端浏览器启动原生选择/拖拽手势。
85
+ e.preventDefault();
86
+ startYRef.current = e.touches[0]?.clientY ?? 0;
87
+ setState('pressing');
88
+
89
+ // 权限检查(对齐原模板 useVoiceRecorder):未授权/检查异常 → 提示并回 idle
90
+ if (asr.checkPermission) {
91
+ try {
92
+ if (!(await asr.checkPermission())) {
93
+ toast(MIC_PERMISSION_MSG, 'error');
94
+ setState('idle');
95
+ return;
96
+ }
97
+ } catch {
98
+ toast(MIC_PERMISSION_MSG, 'error');
99
+ setState('idle');
100
+ return;
101
+ }
102
+ }
103
+
104
+ let active = asr;
105
+ try {
106
+ try {
107
+ await asr.startRecording();
108
+ } catch (err) {
109
+ // session 过期 → 刷新并用新 adapter 重试一次(对齐原模板)
110
+ if (isSessionExpiredError(err) && refreshSession) {
111
+ const next = await refreshSession();
112
+ if (next.asr) {
113
+ active = next.asr;
114
+ await active.startRecording();
115
+ } else {
116
+ throw err;
117
+ }
118
+ } else {
119
+ throw err;
120
+ }
121
+ }
122
+ recordingAsrRef.current = active;
123
+ recordingRef.current = true;
124
+ startTimer();
125
+ if (stateRef.current === 'pressing') setState('recording-active');
126
+ } catch (err) {
127
+ recordingRef.current = false;
128
+ toast(isSessionExpiredError(err) ? '登录状态已失效,请稍后重试' : micErrorMessage(err), 'error');
129
+ setState('idle');
130
+ }
131
+ },
132
+ [disabled, asr, refreshSession, setState],
133
+ );
134
+
135
+ const onTouchMove = useCallback(
136
+ (e: React.TouchEvent) => {
137
+ if (!recordingRef.current) return;
138
+ e.preventDefault();
139
+ const y = e.touches[0]?.clientY ?? 0;
140
+ setState(getMoveInteractionState(startYRef.current, y));
141
+ },
142
+ [setState],
143
+ );
144
+
145
+ const finish = useCallback(async () => {
146
+ stopTimer();
147
+ if (!recordingRef.current) {
148
+ setState('idle');
149
+ return;
150
+ }
151
+ recordingRef.current = false;
152
+ // 用本次录音实际使用的 adapter(可能因 session 重试切换过)
153
+ const active = recordingAsrRef.current ?? asr;
154
+ const action = getReleaseAction(stateRef.current);
155
+ if (action === 'cancel') {
156
+ active?.cancelRecording();
157
+ setState('idle');
158
+ return;
159
+ }
160
+ setState('processing');
161
+ try {
162
+ const text = (await active?.stopRecording()) ?? '';
163
+ if (text) onResult(text);
164
+ } catch {
165
+ /* ignore */
166
+ }
167
+ setState('idle');
168
+ }, [asr, onResult, setState]);
169
+ finishRef.current = finish;
170
+
171
+ const handlers: VoicePressHandlers = {
172
+ onTouchStart,
173
+ onTouchMove,
174
+ onTouchEnd: () => finish(),
175
+ onTouchCancel: () => {
176
+ stopTimer();
177
+ if (recordingRef.current) {
178
+ recordingRef.current = false;
179
+ (recordingAsrRef.current ?? asr)?.cancelRecording();
180
+ }
181
+ setState('idle');
182
+ },
183
+ };
184
+
185
+ const counting =
186
+ recordingRef.current &&
187
+ (interactionState === 'recording-active' || interactionState === 'recording-cancel' || interactionState === 'pressing');
188
+ const countdown = counting && duration >= MAX_DURATION - COUNTDOWN_START ? MAX_DURATION - duration : null;
189
+
190
+ useEffect(() => {
191
+ const locked = interactionState !== 'idle';
192
+ document.body.classList.toggle('tbox-voice-page-lock', locked);
193
+ if (!locked) return undefined;
194
+ const preventNativeGesture = (event: Event) => event.preventDefault();
195
+ document.addEventListener('selectstart', preventNativeGesture);
196
+ document.addEventListener('dragstart', preventNativeGesture);
197
+ document.addEventListener('contextmenu', preventNativeGesture);
198
+ return () => {
199
+ document.body.classList.remove('tbox-voice-page-lock');
200
+ document.removeEventListener('selectstart', preventNativeGesture);
201
+ document.removeEventListener('dragstart', preventNativeGesture);
202
+ document.removeEventListener('contextmenu', preventNativeGesture);
203
+ };
204
+ }, [interactionState]);
205
+
206
+ return { interactionState, overlayVisible: interactionState !== 'idle', countdown, handlers };
207
+ }
@@ -0,0 +1,49 @@
1
+ /* 顶部历史入口:参考稿的浅灰胶囊,居中跟随内容滚动;历史拉出后由页面撤下。 */
2
+ .history-entry {
3
+ display: flex;
4
+ justify-content: center;
5
+ /* 自身不留边距;与欢迎区的 4PX 间距由页面 index.less 的 `.history-entry ~ .mall-home` 统一编排。 */
6
+ padding: 0;
7
+
8
+ &__button {
9
+ appearance: none;
10
+ padding: 3PX 12PX;
11
+ border: none;
12
+ border-radius: 25PX;
13
+ background: rgba(15, 64, 113, 0.06);
14
+ color: var(--tbox-text-muted);
15
+ font-size: 11.5PX;
16
+ font-weight: 400;
17
+ line-height: 16PX;
18
+ transition: background var(--tbox-transition-fast);
19
+
20
+ &:active {
21
+ background: rgba(15, 64, 113, 0.1);
22
+ }
23
+
24
+ /* 加载态小转子:与文案同处胶囊内,替代原独立指示器 */
25
+ display: inline-flex;
26
+ align-items: center;
27
+ gap: 5PX;
28
+
29
+ &:disabled {
30
+ /* 未就绪只是短暂不可点,保持视觉常态;加载态由文案表达。 */
31
+ cursor: default;
32
+ }
33
+ }
34
+
35
+ &__spinner {
36
+ width: 10PX;
37
+ height: 10PX;
38
+ border: 1.5PX solid rgba(15, 64, 113, 0.18);
39
+ border-top-color: var(--tbox-primary);
40
+ border-radius: 50%;
41
+ animation: history-entry-spin 0.6s linear infinite;
42
+ }
43
+ }
44
+
45
+ @keyframes history-entry-spin {
46
+ to {
47
+ transform: rotate(360deg);
48
+ }
49
+ }
@@ -0,0 +1,42 @@
1
+ // 顶部历史入口胶囊:同时承载点击入口和轻量下拉状态反馈。
2
+ // 常驻渲染是刻意的:运行时未就绪时只禁用,不隐藏——否则就绪后插入 DOM 会把下方欢迎区顶开。
3
+ import type { PullStatus } from '../../utils/pull-down-machine';
4
+ import './index.less';
5
+
6
+ function labelOf(status: PullStatus, loading: boolean): string {
7
+ if (loading || status === 'loading') return '加载中...';
8
+ if (status === 'pulling') return '下拉查看历史';
9
+ if (status === 'ready') return '松手查看历史';
10
+ if (status === 'noMore') return '没有更多历史记录';
11
+ return '历史记录';
12
+ }
13
+
14
+ export default function HistoryEntry({
15
+ visible,
16
+ loading,
17
+ disabled,
18
+ pullStatus,
19
+ onLoad,
20
+ }: {
21
+ visible: boolean;
22
+ loading: boolean;
23
+ disabled: boolean;
24
+ pullStatus: PullStatus;
25
+ onLoad: () => void;
26
+ }) {
27
+ if (!visible) return null;
28
+
29
+ return (
30
+ <div className="history-entry">
31
+ <button
32
+ type="button"
33
+ className="history-entry__button"
34
+ disabled={disabled}
35
+ onClick={onLoad}
36
+ >
37
+ {(loading || pullStatus === 'loading') && <span className="history-entry__spinner" />}
38
+ {labelOf(pullStatus, loading)}
39
+ </button>
40
+ </div>
41
+ );
42
+ }
@@ -0,0 +1,241 @@
1
+ .tbox-media-list {
2
+ display: flex;
3
+ align-items: flex-end;
4
+ gap: 16px;
5
+ padding: 0 0 16px;
6
+ overflow: visible;
7
+ }
8
+
9
+ .tbox-media-list__scroll {
10
+ display: flex;
11
+ flex-wrap: nowrap;
12
+ overflow-x: auto;
13
+ gap: 16px;
14
+ -webkit-overflow-scrolling: touch;
15
+ min-width: 0;
16
+
17
+ &::-webkit-scrollbar {
18
+ display: none;
19
+ }
20
+ }
21
+
22
+ .tbox-media-item {
23
+ width: var(--tbox-media-item-size);
24
+ min-width: var(--tbox-media-item-size);
25
+ height: var(--tbox-media-item-size);
26
+ border-radius: var(--tbox-media-item-radius);
27
+ overflow: visible;
28
+ position: relative;
29
+ background: #f5f5f5;
30
+
31
+ &--image {
32
+ overflow: hidden;
33
+ }
34
+
35
+ &__thumb {
36
+ width: 100%;
37
+ height: 100%;
38
+ display: block;
39
+ object-fit: cover;
40
+ }
41
+
42
+ &__overlay {
43
+ position: absolute;
44
+ inset: 0;
45
+ display: flex;
46
+ flex-direction: column;
47
+ align-items: center;
48
+ justify-content: center;
49
+ background: rgba(0, 0, 0, 0.5);
50
+ border-radius: var(--tbox-media-item-radius);
51
+ gap: 8px;
52
+ }
53
+
54
+ &__progress-ring {
55
+ width: 60px;
56
+ height: 60px;
57
+ border-radius: 50%;
58
+ display: flex;
59
+ align-items: center;
60
+ justify-content: center;
61
+ transition: background var(--tbox-motion-duration-base) ease;
62
+ }
63
+
64
+ &__progress-inner {
65
+ width: 48px;
66
+ height: 48px;
67
+ border-radius: 50%;
68
+ background: rgba(0, 0, 0, 0.7);
69
+ display: flex;
70
+ align-items: center;
71
+ justify-content: center;
72
+ }
73
+
74
+ &__progress-text {
75
+ color: #fff;
76
+ font-size: 20px;
77
+ font-weight: bold;
78
+ text-align: center;
79
+ }
80
+
81
+ &__status-icon {
82
+ display: flex;
83
+ align-items: center;
84
+ justify-content: center;
85
+ }
86
+
87
+ &__status-text {
88
+ color: rgba(255, 255, 255, 0.85);
89
+ font-size: 20px;
90
+ font-weight: 500;
91
+ text-align: center;
92
+ white-space: nowrap;
93
+ }
94
+
95
+ &__remove {
96
+ position: absolute;
97
+ top: 12px;
98
+ right: 12px;
99
+ width: 36px;
100
+ height: 36px;
101
+ border-radius: 18px;
102
+ background: rgba(0, 0, 0, 0.55);
103
+ display: flex;
104
+ align-items: center;
105
+ justify-content: center;
106
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
107
+ z-index: 2;
108
+ }
109
+ }
110
+
111
+ /* 非图片(视频/文件)卡片 */
112
+ .tbox-file-item {
113
+ display: flex;
114
+ flex-direction: row;
115
+ align-items: center;
116
+ gap: 12px;
117
+ min-width: 280px;
118
+ height: var(--tbox-media-item-size);
119
+ border-radius: var(--tbox-media-item-radius);
120
+ background: #fcfcfc;
121
+ padding: 16px;
122
+ box-sizing: border-box;
123
+ position: relative;
124
+ overflow: visible;
125
+
126
+ &__icon {
127
+ flex-shrink: 0;
128
+ width: 48px;
129
+ height: 48px;
130
+ display: flex;
131
+ align-items: center;
132
+ justify-content: center;
133
+ }
134
+
135
+ &__info {
136
+ display: flex;
137
+ flex-direction: column;
138
+ justify-content: center;
139
+ gap: 4px;
140
+ flex: 1;
141
+ min-width: 0;
142
+ }
143
+
144
+ &__name {
145
+ font-size: 24px;
146
+ color: #333;
147
+ font-weight: 500;
148
+ overflow: hidden;
149
+ text-overflow: ellipsis;
150
+ white-space: nowrap;
151
+ max-width: 100%;
152
+ }
153
+
154
+ &__meta {
155
+ font-size: 20px;
156
+ color: rgba(0, 0, 0, 0.25);
157
+ white-space: nowrap;
158
+
159
+ &--failed {
160
+ color: var(--tbox-error);
161
+ }
162
+ }
163
+
164
+ .tbox-media-item__remove {
165
+ position: absolute;
166
+ top: 12px;
167
+ right: 12px;
168
+ }
169
+ }
170
+
171
+ /* 末尾“加更多”按钮 */
172
+ .tbox-media-add {
173
+ width: var(--tbox-media-item-size);
174
+ min-width: var(--tbox-media-item-size);
175
+ height: var(--tbox-media-item-size);
176
+ border-radius: var(--tbox-media-item-radius);
177
+ background: rgba(255, 255, 255, 0.6);
178
+ display: flex;
179
+ align-items: center;
180
+ justify-content: center;
181
+ flex-shrink: 0;
182
+ position: relative;
183
+ }
184
+
185
+ /* 加更多的二级 popover */
186
+ .tbox-media-popover {
187
+ position: absolute;
188
+ bottom: calc(var(--tbox-media-item-size) + 8px);
189
+ right: 0;
190
+ width: var(--tbox-media-item-size);
191
+ background: #fff;
192
+ border-radius: 20px;
193
+ padding: 16px 0;
194
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
195
+ z-index: 10;
196
+ opacity: 0;
197
+ transform: translateY(8px);
198
+ transition: opacity var(--tbox-motion-duration-fast) ease-out, transform var(--tbox-motion-duration-fast) ease-out;
199
+ pointer-events: none;
200
+
201
+ &--visible {
202
+ opacity: 1;
203
+ transform: translateY(0);
204
+ pointer-events: auto;
205
+ }
206
+
207
+ &__item {
208
+ display: flex;
209
+ flex-direction: row;
210
+ align-items: center;
211
+ height: 60px;
212
+ gap: 8px;
213
+ padding: 0 16px;
214
+
215
+ & + & {
216
+ margin-top: 8px;
217
+ }
218
+ }
219
+
220
+ &__icon-wrap {
221
+ width: 40px;
222
+ height: 40px;
223
+ border-radius: 8px;
224
+ display: flex;
225
+ align-items: center;
226
+ justify-content: center;
227
+ flex-shrink: 0;
228
+ }
229
+
230
+ &__icon {
231
+ width: 32px;
232
+ height: 32px;
233
+ }
234
+
235
+ &__label {
236
+ font-size: 28px;
237
+ color: rgba(31, 59, 99, 0.88);
238
+ font-weight: 400;
239
+ line-height: 1.4;
240
+ }
241
+ }