@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.
- package/LICENSE +21 -0
- package/README.md +16 -2
- package/assets/keyboard-circle.svg +12 -0
- package/assets/more-circle.svg +3 -0
- package/assets/new-chat.svg +3 -0
- package/assets/quick-command-star.svg +3 -0
- package/assets/send-gradient.svg +14 -0
- package/assets/stop@2x.png +0 -0
- package/assets/voice-circle.svg +3 -0
- package/package.json +55 -3
- package/src/ChatPage.less +62 -0
- package/src/ChatPage.tsx +305 -0
- package/src/chat-config.test.ts +82 -0
- package/src/chat-config.ts +62 -0
- package/src/components/ActionBar/DebugButton.test.tsx +84 -0
- package/src/components/ActionBar/DebugButton.tsx +53 -0
- package/src/components/ActionBar/index.less +123 -0
- package/src/components/ActionBar/index.test.tsx +83 -0
- package/src/components/ActionBar/index.tsx +159 -0
- package/src/components/CardRenderer.tsx +36 -0
- package/src/components/ChatHeader/index.less +148 -0
- package/src/components/ChatHeader/index.test.tsx +74 -0
- package/src/components/ChatHeader/index.tsx +75 -0
- package/src/components/ChatInput/index.less +394 -0
- package/src/components/ChatInput/index.test.tsx +156 -0
- package/src/components/ChatInput/index.tsx +284 -0
- package/src/components/ChatInput/voice/VoicePressField.tsx +34 -0
- package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
- package/src/components/ChatInput/voice/machine.ts +27 -0
- package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
- package/src/components/HistoryEntry/index.less +49 -0
- package/src/components/HistoryEntry/index.tsx +42 -0
- package/src/components/MediaList/index.less +241 -0
- package/src/components/MediaList/index.tsx +256 -0
- package/src/components/MessageBubble/index.less +325 -0
- package/src/components/MessageBubble/index.test.tsx +134 -0
- package/src/components/MessageBubble/index.tsx +177 -0
- package/src/components/PullDownIndicator/index.less +32 -0
- package/src/components/PullDownIndicator/index.tsx +32 -0
- package/src/components/QuickCommands/index.less +63 -0
- package/src/components/QuickCommands/index.test.tsx +99 -0
- package/src/components/QuickCommands/index.tsx +52 -0
- package/src/components/ReasoningBlock/index.less +90 -0
- package/src/components/ReasoningBlock/index.tsx +56 -0
- package/src/components/ScrollToBottomButton/index.less +32 -0
- package/src/components/ScrollToBottomButton/index.tsx +10 -0
- package/src/components/ToolCallBlock/detail.test.ts +346 -0
- package/src/components/ToolCallBlock/detail.ts +204 -0
- package/src/components/ToolCallBlock/index.less +312 -0
- package/src/components/ToolCallBlock/index.test.tsx +261 -0
- package/src/components/ToolCallBlock/index.tsx +212 -0
- package/src/components/ToolCallBlock/titles.ts +102 -0
- package/src/hooks/use-drag-gesture.test.ts +66 -0
- package/src/hooks/use-drag-gesture.ts +129 -0
- package/src/hooks/use-pull-down-history.ts +241 -0
- package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
- package/src/hooks/useMediaUpload.ts +242 -0
- package/src/hooks/useTTSAutoPlay.ts +82 -0
- package/src/index.ts +69 -0
- package/src/store-context.tsx +92 -0
- package/src/test/setup.ts +7 -0
- package/src/theme/contract.json +72 -0
- package/src/theme/global.less +99 -0
- package/src/theme/motion.less +19 -0
- package/src/theme/theme-contract.test.ts +69 -0
- package/src/theme/theme.ts +17 -0
- package/src/theme/tokens.less +79 -0
- package/src/types.ts +29 -0
- package/src/utils/copyText.ts +11 -0
- package/src/utils/haptics.test.ts +51 -0
- package/src/utils/haptics.ts +44 -0
- package/src/utils/imagePreview.ts +10 -0
- package/src/utils/pull-down-machine.test.ts +188 -0
- package/src/utils/pull-down-machine.ts +157 -0
- package/src/utils/session-error.ts +5 -0
- package/src/utils/toast.ts +15 -0
- package/src/utils/tool-call-display.test.ts +240 -0
- package/src/utils/tool-call-display.ts +146 -0
- package/src/vite-env.d.ts +1 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { act } from 'react';
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
import { createRoot } from 'react-dom/client';
|
|
5
|
+
import type { VoiceInteractionState } from './voice/machine';
|
|
6
|
+
|
|
7
|
+
const mocks = vi.hoisted(() => ({
|
|
8
|
+
interactionState: 'idle' as VoiceInteractionState,
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
vi.mock('@tbox.cn/app-agent-sdk-client', () => ({
|
|
12
|
+
isInAlipayShell: () => false,
|
|
13
|
+
useASRAdapter: () => undefined,
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
vi.mock('../../hooks/useMediaUpload', () => ({
|
|
17
|
+
useMediaUpload: () => ({
|
|
18
|
+
items: [],
|
|
19
|
+
addFiles: vi.fn(),
|
|
20
|
+
addFromBridge: vi.fn(),
|
|
21
|
+
removeItem: vi.fn(),
|
|
22
|
+
retry: vi.fn(),
|
|
23
|
+
clear: vi.fn(),
|
|
24
|
+
uploading: false,
|
|
25
|
+
}),
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
vi.mock('../../components/MediaList', () => ({ MediaList: () => null }));
|
|
29
|
+
|
|
30
|
+
vi.mock('./voice/useVoicePress', () => ({
|
|
31
|
+
useVoicePress: () => ({
|
|
32
|
+
interactionState: mocks.interactionState,
|
|
33
|
+
overlayVisible: mocks.interactionState !== 'idle',
|
|
34
|
+
countdown: null,
|
|
35
|
+
handlers: {
|
|
36
|
+
onTouchStart: vi.fn(),
|
|
37
|
+
onTouchMove: vi.fn(),
|
|
38
|
+
onTouchEnd: vi.fn(),
|
|
39
|
+
onTouchCancel: vi.fn(),
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
}));
|
|
43
|
+
|
|
44
|
+
import { ChatInput } from './index';
|
|
45
|
+
import { useChatConfigStore } from '../../chat-config';
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
mocks.interactionState = 'idle';
|
|
49
|
+
// 重置回构建期快照(对齐模板 config/chat.json 出厂值;派生矩阵用例经 setState 驱动水合模拟)
|
|
50
|
+
useChatConfigStore.setState({
|
|
51
|
+
enableImageUpload: false,
|
|
52
|
+
enableVideoUpload: false,
|
|
53
|
+
enableTTS: false,
|
|
54
|
+
enableVoiceInput: true,
|
|
55
|
+
defaultInputMode: 'TEXT',
|
|
56
|
+
});
|
|
57
|
+
document.body.innerHTML = '';
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('ChatInput 语音活动状态', () => {
|
|
61
|
+
it('idle、识别中和结束时依次向页面投影 false、true、false', async () => {
|
|
62
|
+
const onVoiceActivityChange = vi.fn();
|
|
63
|
+
const container = document.createElement('div');
|
|
64
|
+
document.body.appendChild(container);
|
|
65
|
+
const root = createRoot(container);
|
|
66
|
+
const render = () => root.render(
|
|
67
|
+
<ChatInput onSend={vi.fn()} onVoiceActivityChange={onVoiceActivityChange} />,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
await act(async () => render());
|
|
71
|
+
expect(onVoiceActivityChange).toHaveBeenLastCalledWith(false);
|
|
72
|
+
|
|
73
|
+
mocks.interactionState = 'processing';
|
|
74
|
+
await act(async () => render());
|
|
75
|
+
expect(onVoiceActivityChange).toHaveBeenLastCalledWith(true);
|
|
76
|
+
|
|
77
|
+
mocks.interactionState = 'idle';
|
|
78
|
+
await act(async () => render());
|
|
79
|
+
expect(onVoiceActivityChange).toHaveBeenLastCalledWith(false);
|
|
80
|
+
|
|
81
|
+
await act(async () => root.unmount());
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('ChatInput 派生模式(服务端 defaultInputMode 水合)', () => {
|
|
86
|
+
/** voice 态投影:录音模式显示"键盘"切换图标;text 态显示"语音"切换图标 */
|
|
87
|
+
const inVoiceMode = (container: HTMLElement) =>
|
|
88
|
+
container.querySelector('img[alt="键盘"]') !== null;
|
|
89
|
+
|
|
90
|
+
const renderInput = async (root: ReturnType<typeof createRoot>) => {
|
|
91
|
+
await act(async () => root.render(<ChatInput onSend={vi.fn()} />));
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
it('① voice 关 + defaultInputMode=VOICE → 强制 text(语音按钮隐藏)', async () => {
|
|
95
|
+
useChatConfigStore.setState({ enableVoiceInput: false, defaultInputMode: 'VOICE' });
|
|
96
|
+
const container = document.createElement('div');
|
|
97
|
+
document.body.appendChild(container);
|
|
98
|
+
const root = createRoot(container);
|
|
99
|
+
await renderInput(root);
|
|
100
|
+
expect(inVoiceMode(container)).toBe(false);
|
|
101
|
+
await act(async () => root.unmount());
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('② voice 开 + defaultInputMode=VOICE → 初始即 voice', async () => {
|
|
105
|
+
useChatConfigStore.setState({ enableVoiceInput: true, defaultInputMode: 'VOICE' });
|
|
106
|
+
const container = document.createElement('div');
|
|
107
|
+
document.body.appendChild(container);
|
|
108
|
+
const root = createRoot(container);
|
|
109
|
+
await renderInput(root);
|
|
110
|
+
expect(inVoiceMode(container)).toBe(true);
|
|
111
|
+
await act(async () => root.unmount());
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('③ 初值 TEXT 渲染后水合 VOICE → 派生自动转 voice(useState 捕获竞态修复)', async () => {
|
|
115
|
+
useChatConfigStore.setState({ enableVoiceInput: true, defaultInputMode: 'TEXT' });
|
|
116
|
+
const container = document.createElement('div');
|
|
117
|
+
document.body.appendChild(container);
|
|
118
|
+
const root = createRoot(container);
|
|
119
|
+
await renderInput(root);
|
|
120
|
+
expect(inVoiceMode(container)).toBe(false);
|
|
121
|
+
// 模拟 HELLO_ACK 晚到水合(服务端 defaultInputMode='VOICE')
|
|
122
|
+
useChatConfigStore.setState({ defaultInputMode: 'VOICE' });
|
|
123
|
+
await renderInput(root);
|
|
124
|
+
expect(inVoiceMode(container)).toBe(true);
|
|
125
|
+
await act(async () => root.unmount());
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('④ 用户手动切 text 后水合 VOICE 到达 → 显式选择优先,保持 text', async () => {
|
|
129
|
+
useChatConfigStore.setState({ enableVoiceInput: true, defaultInputMode: 'VOICE' });
|
|
130
|
+
const container = document.createElement('div');
|
|
131
|
+
document.body.appendChild(container);
|
|
132
|
+
const root = createRoot(container);
|
|
133
|
+
await renderInput(root);
|
|
134
|
+
expect(inVoiceMode(container)).toBe(true);
|
|
135
|
+
// 用户手动切回 text(voice 态的"键盘"切换钮——div 容器 onClick)
|
|
136
|
+
const keyboardToggle = container.querySelector<HTMLElement>('.tbox-voice-toggle')!;
|
|
137
|
+
await act(async () => keyboardToggle.click());
|
|
138
|
+
await renderInput(root);
|
|
139
|
+
expect(inVoiceMode(container)).toBe(false);
|
|
140
|
+
// 水合再次下发 VOICE(如重连回显)——用户显式选择仍优先
|
|
141
|
+
useChatConfigStore.setState({ defaultInputMode: 'VOICE' });
|
|
142
|
+
await renderInput(root);
|
|
143
|
+
expect(inVoiceMode(container)).toBe(false);
|
|
144
|
+
await act(async () => root.unmount());
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('⑤ voice 开 + defaultInputMode=TEXT → text', async () => {
|
|
148
|
+
useChatConfigStore.setState({ enableVoiceInput: true, defaultInputMode: 'TEXT' });
|
|
149
|
+
const container = document.createElement('div');
|
|
150
|
+
document.body.appendChild(container);
|
|
151
|
+
const root = createRoot(container);
|
|
152
|
+
await renderInput(root);
|
|
153
|
+
expect(inVoiceMode(container)).toBe(false);
|
|
154
|
+
await act(async () => root.unmount());
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { useMediaUpload } from '../../hooks/useMediaUpload';
|
|
3
|
+
import { useASRAdapter, isInAlipayShell } from '@tbox.cn/app-agent-sdk-client';
|
|
4
|
+
import { useChatConfigStore } from '../../chat-config';
|
|
5
|
+
|
|
6
|
+
import type { ChatInputMessage, InputMethod } from '../../types';
|
|
7
|
+
import { MediaList } from '../../components/MediaList';
|
|
8
|
+
import { useVoicePress } from './voice/useVoicePress';
|
|
9
|
+
import { VoicePressField } from './voice/VoicePressField';
|
|
10
|
+
import { VoiceRecordingOverlay } from './voice/VoiceRecordingOverlay';
|
|
11
|
+
import newChatIcon from '../../../assets/new-chat.svg';
|
|
12
|
+
import voiceIcon from '../../../assets/voice-circle.svg';
|
|
13
|
+
import keyboardIcon from '../../../assets/keyboard-circle.svg';
|
|
14
|
+
import sendIcon from '../../../assets/send-gradient.svg';
|
|
15
|
+
import stopIcon from '../../../assets/stop@2x.png';
|
|
16
|
+
import moreIcon from '../../../assets/more-circle.svg';
|
|
17
|
+
import './index.less';
|
|
18
|
+
|
|
19
|
+
type PickKind = 'image' | 'camera' | 'video';
|
|
20
|
+
|
|
21
|
+
/** 媒体选取菜单图标(远程素材,对齐原模板)。 */
|
|
22
|
+
const OPTION_ICONS: Record<PickKind, { icon: string; label: string }> = {
|
|
23
|
+
image: { icon: 'https://mdn.alipayobjects.com/huamei_nzq4hr/afts/img/A*DfDhSYAMzoQAAAAAHiAAAAgAel-hAQ/original', label: '相册' },
|
|
24
|
+
camera: { icon: 'https://mdn.alipayobjects.com/huamei_nzq4hr/afts/img/A*xdKLSabxqmQAAAAAOnAAAAgAel-hAQ/original', label: '拍摄' },
|
|
25
|
+
video: { icon: 'https://mdn.alipayobjects.com/huamei_nzq4hr/afts/img/A*xdKLSabxqmQAAAAAOnAAAAgAel-hAQ/original', label: '视频' },
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const MENU_ANIMATION_MS = 250;
|
|
29
|
+
|
|
30
|
+
/** 连接终态阻断(登录失败/scope 拒绝):提示条 + 输入禁用 + 可选重试入口 */
|
|
31
|
+
export interface ChatBlocked {
|
|
32
|
+
message: string;
|
|
33
|
+
onRetry?: () => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 媒体上传默认约束(分类型数量 + 单文件大小)。如需放开/收紧按业务改这里。 */
|
|
37
|
+
const DEFAULT_MEDIA_CONFIG = {
|
|
38
|
+
image: { maxCount: 9, maxSize: 10 * 1024 * 1024 },
|
|
39
|
+
video: { maxCount: 1, maxSize: 50 * 1024 * 1024 },
|
|
40
|
+
file: { maxCount: 5, maxSize: 20 * 1024 * 1024 },
|
|
41
|
+
} as const;
|
|
42
|
+
const MEDIA_MAX_TOTAL = 9;
|
|
43
|
+
|
|
44
|
+
/** 对话输入框(对齐原模板):媒体预览 + 新对话 + 药丸[输入框/按住说话 + 语音切换 + 发送/“+”] + 动画选取菜单 + 免责声明。 */
|
|
45
|
+
export function ChatInput({
|
|
46
|
+
generating,
|
|
47
|
+
blocked,
|
|
48
|
+
onSend,
|
|
49
|
+
onCancel,
|
|
50
|
+
onNewChat,
|
|
51
|
+
onVoiceActivityChange,
|
|
52
|
+
disclaimer = '以上内容由AI生成,请谨慎甄别。',
|
|
53
|
+
}: {
|
|
54
|
+
generating?: boolean;
|
|
55
|
+
blocked?: ChatBlocked;
|
|
56
|
+
onSend: (msg: ChatInputMessage) => void;
|
|
57
|
+
onCancel?: () => void;
|
|
58
|
+
onNewChat?: () => void;
|
|
59
|
+
onVoiceActivityChange?: (active: boolean) => void;
|
|
60
|
+
disclaimer?: string;
|
|
61
|
+
}) {
|
|
62
|
+
const [text, setText] = useState('');
|
|
63
|
+
const imageRef = useRef<HTMLInputElement>(null);
|
|
64
|
+
const cameraRef = useRef<HTMLInputElement>(null);
|
|
65
|
+
const videoRef = useRef<HTMLInputElement>(null);
|
|
66
|
+
const closeTimerRef = useRef<ReturnType<typeof setTimeout>>();
|
|
67
|
+
const { items, addFiles, addFromBridge, removeItem, retry, clear, uploading } = useMediaUpload(
|
|
68
|
+
MEDIA_MAX_TOTAL,
|
|
69
|
+
DEFAULT_MEDIA_CONFIG,
|
|
70
|
+
);
|
|
71
|
+
const asr = useASRAdapter();
|
|
72
|
+
const inShell = isInAlipayShell();
|
|
73
|
+
const enableImage = useChatConfigStore((s) => s.enableImageUpload);
|
|
74
|
+
const enableVideo = useChatConfigStore((s) => s.enableVideoUpload);
|
|
75
|
+
const enableVoice = useChatConfigStore((s) => s.enableVoiceInput);
|
|
76
|
+
const defaultInputMode = useChatConfigStore((s) => s.defaultInputMode);
|
|
77
|
+
// 派生模式(禁 useState 捕获初值——水合晚到时服务端 defaultInputMode 进不了已捕获 state):
|
|
78
|
+
// userMode=null 跟随默认(随水合重渲染更新);显式值 = 用户手动切换优先
|
|
79
|
+
const [userMode, setUserMode] = useState<'text' | 'voice' | null>(null);
|
|
80
|
+
const inputMode: 'text' | 'voice' =
|
|
81
|
+
!enableVoice ? 'text' : userMode ?? (defaultInputMode === 'VOICE' ? 'voice' : 'text');
|
|
82
|
+
const [menuVisible, setMenuVisible] = useState(false);
|
|
83
|
+
const [menuAnimating, setMenuAnimating] = useState(false);
|
|
84
|
+
|
|
85
|
+
const voice = useVoicePress({
|
|
86
|
+
disabled: generating || !!blocked,
|
|
87
|
+
asr,
|
|
88
|
+
onResult: (t) => onSend({ text: t, mediaItems: [], inputMethod: 'voice' }),
|
|
89
|
+
});
|
|
90
|
+
const voiceActive = voice.interactionState !== 'idle';
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
onVoiceActivityChange?.(voiceActive);
|
|
94
|
+
}, [onVoiceActivityChange, voiceActive]);
|
|
95
|
+
|
|
96
|
+
useEffect(() => () => onVoiceActivityChange?.(false), [onVoiceActivityChange]);
|
|
97
|
+
|
|
98
|
+
const isWebPc = !inShell && typeof window !== 'undefined' && window.innerWidth > 450;
|
|
99
|
+
|
|
100
|
+
const pick = (kind: PickKind) => {
|
|
101
|
+
if (inShell) {
|
|
102
|
+
addFromBridge(kind);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
(kind === 'image' ? imageRef : kind === 'camera' ? cameraRef : videoRef).current?.click();
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// 媒体选项:相册 / 拍摄(移动端)/ 视频,按 config 开关
|
|
109
|
+
const menuOptions = [
|
|
110
|
+
enableImage && { type: 'image' as const, ...OPTION_ICONS.image },
|
|
111
|
+
enableImage && !isWebPc && { type: 'camera' as const, ...OPTION_ICONS.camera },
|
|
112
|
+
enableVideo && { type: 'video' as const, ...OPTION_ICONS.video },
|
|
113
|
+
].filter(Boolean) as { type: PickKind; icon: string; label: string }[];
|
|
114
|
+
|
|
115
|
+
const openMenu = () => {
|
|
116
|
+
setMenuVisible(true);
|
|
117
|
+
requestAnimationFrame(() => requestAnimationFrame(() => setMenuAnimating(true)));
|
|
118
|
+
};
|
|
119
|
+
const closeMenu = () => {
|
|
120
|
+
if (closeTimerRef.current) clearTimeout(closeTimerRef.current);
|
|
121
|
+
setMenuAnimating(false);
|
|
122
|
+
closeTimerRef.current = setTimeout(() => setMenuVisible(false), MENU_ANIMATION_MS);
|
|
123
|
+
};
|
|
124
|
+
const onPlus = () => {
|
|
125
|
+
if (menuVisible) return closeMenu();
|
|
126
|
+
if (menuOptions.length <= 1) return menuOptions[0] && pick(menuOptions[0].type);
|
|
127
|
+
openMenu();
|
|
128
|
+
};
|
|
129
|
+
const onMenuSelect = (kind: PickKind) => {
|
|
130
|
+
closeMenu();
|
|
131
|
+
pick(kind);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
if (!menuVisible) return;
|
|
136
|
+
const onDown = (e: MouseEvent | TouchEvent) => {
|
|
137
|
+
const t = e.target as HTMLElement;
|
|
138
|
+
if (t && !t.closest('.tbox-media-picker') && !t.closest('.tbox-media-picker-menu')) closeMenu();
|
|
139
|
+
};
|
|
140
|
+
document.addEventListener('mousedown', onDown);
|
|
141
|
+
document.addEventListener('touchstart', onDown, { passive: true });
|
|
142
|
+
return () => {
|
|
143
|
+
document.removeEventListener('mousedown', onDown);
|
|
144
|
+
document.removeEventListener('touchstart', onDown);
|
|
145
|
+
};
|
|
146
|
+
}, [menuVisible]);
|
|
147
|
+
|
|
148
|
+
useEffect(() => () => closeTimerRef.current && clearTimeout(closeTimerRef.current), []);
|
|
149
|
+
|
|
150
|
+
const canSend = !!(text.trim() || items.length > 0) && !uploading && inputMode === 'text' && !blocked;
|
|
151
|
+
|
|
152
|
+
const submit = () => {
|
|
153
|
+
if (!canSend) return;
|
|
154
|
+
const media = items.filter((it) => it.fileId && !it.illegal);
|
|
155
|
+
const inputMethod: InputMethod = media.length ? media[0].type : 'text';
|
|
156
|
+
onSend({ text: text.trim(), mediaItems: media, inputMethod });
|
|
157
|
+
setText('');
|
|
158
|
+
clear();
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const onPickFiles = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
162
|
+
addFiles(e.target.files ? Array.from(e.target.files) : []);
|
|
163
|
+
e.target.value = '';
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const toggleMode = () => {
|
|
167
|
+
if (blocked) return;
|
|
168
|
+
setUserMode(inputMode === 'text' ? 'voice' : 'text');
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<div className="tbox-chat-input">
|
|
173
|
+
<MediaList
|
|
174
|
+
items={items}
|
|
175
|
+
onRemove={removeItem}
|
|
176
|
+
onRetry={retry}
|
|
177
|
+
onPickImage={() => pick('image')}
|
|
178
|
+
onPickCamera={() => pick('camera')}
|
|
179
|
+
onPickVideo={() => pick('video')}
|
|
180
|
+
enableImage={enableImage}
|
|
181
|
+
enableVideo={enableVideo}
|
|
182
|
+
isWebPc={isWebPc}
|
|
183
|
+
/>
|
|
184
|
+
|
|
185
|
+
<div className="tbox-chat-input__row">
|
|
186
|
+
{onNewChat && (
|
|
187
|
+
<div className="tbox-new-chat-btn" onClick={onNewChat}>
|
|
188
|
+
<img className="tbox-new-chat-btn__icon" src={newChatIcon} alt="新对话" />
|
|
189
|
+
</div>
|
|
190
|
+
)}
|
|
191
|
+
|
|
192
|
+
<div className="tbox-chat-input__wrapper">
|
|
193
|
+
<input ref={imageRef} type="file" accept="image/*" multiple hidden onChange={onPickFiles} />
|
|
194
|
+
<input ref={cameraRef} type="file" accept="image/*" capture="environment" hidden onChange={onPickFiles} />
|
|
195
|
+
<input ref={videoRef} type="file" accept="video/*" hidden onChange={onPickFiles} />
|
|
196
|
+
|
|
197
|
+
{inputMode === 'voice' ? (
|
|
198
|
+
<VoicePressField
|
|
199
|
+
disabled={generating || !!blocked}
|
|
200
|
+
interactionState={voice.interactionState}
|
|
201
|
+
handlers={voice.handlers}
|
|
202
|
+
/>
|
|
203
|
+
) : (
|
|
204
|
+
<textarea
|
|
205
|
+
className="tbox-chat-input__field"
|
|
206
|
+
value={text}
|
|
207
|
+
placeholder={blocked?.message ?? '有什么需要问我的吗'}
|
|
208
|
+
rows={1}
|
|
209
|
+
disabled={!!blocked}
|
|
210
|
+
onChange={(e) => setText(e.target.value)}
|
|
211
|
+
onKeyDown={(e) => {
|
|
212
|
+
if (e.key === 'Enter' && !e.shiftKey) {
|
|
213
|
+
e.preventDefault();
|
|
214
|
+
submit();
|
|
215
|
+
}
|
|
216
|
+
}}
|
|
217
|
+
/>
|
|
218
|
+
)}
|
|
219
|
+
|
|
220
|
+
{enableVoice && (
|
|
221
|
+
<div className="tbox-voice-toggle" onClick={toggleMode}>
|
|
222
|
+
<img
|
|
223
|
+
className="tbox-voice-toggle__icon"
|
|
224
|
+
src={inputMode === 'voice' ? keyboardIcon : voiceIcon}
|
|
225
|
+
alt={inputMode === 'voice' ? '键盘' : '语音'}
|
|
226
|
+
/>
|
|
227
|
+
</div>
|
|
228
|
+
)}
|
|
229
|
+
|
|
230
|
+
{blocked ? null : generating ? (
|
|
231
|
+
<div className="tbox-chat-input__send tbox-chat-input__send--active" onClick={onCancel}>
|
|
232
|
+
<img className="tbox-chat-input__send-img" src={stopIcon} alt="停止" />
|
|
233
|
+
</div>
|
|
234
|
+
) : canSend ? (
|
|
235
|
+
<div className="tbox-chat-input__send tbox-chat-input__send--active" onClick={submit}>
|
|
236
|
+
<img className="tbox-chat-input__send-img" src={sendIcon} alt="发送" />
|
|
237
|
+
</div>
|
|
238
|
+
) : menuOptions.length > 0 ? (
|
|
239
|
+
<div className="tbox-media-picker">
|
|
240
|
+
<div
|
|
241
|
+
className={`tbox-media-picker__trigger${menuVisible ? ' tbox-media-picker__trigger--open' : ''}`}
|
|
242
|
+
onClick={onPlus}
|
|
243
|
+
>
|
|
244
|
+
<img className="tbox-media-picker__trigger-icon" src={moreIcon} alt="更多" />
|
|
245
|
+
</div>
|
|
246
|
+
</div>
|
|
247
|
+
) : null}
|
|
248
|
+
</div>
|
|
249
|
+
{blocked && (
|
|
250
|
+
<div className="tbox-chat-input__blocked">
|
|
251
|
+
<span className="tbox-chat-input__blocked-msg">{blocked.message}</span>
|
|
252
|
+
{blocked.onRetry && (
|
|
253
|
+
<button type="button" className="tbox-chat-input__blocked-retry" onClick={blocked.onRetry}>
|
|
254
|
+
重试
|
|
255
|
+
</button>
|
|
256
|
+
)}
|
|
257
|
+
</div>
|
|
258
|
+
)}
|
|
259
|
+
</div>
|
|
260
|
+
|
|
261
|
+
{menuVisible && (
|
|
262
|
+
<div className={`tbox-media-picker-menu${menuAnimating ? ' tbox-media-picker-menu--visible' : ''}`}>
|
|
263
|
+
{menuOptions.map((o) => (
|
|
264
|
+
<div key={o.type} className="tbox-media-picker-menu__option" onClick={() => onMenuSelect(o.type)}>
|
|
265
|
+
<div className="tbox-media-picker-menu__option-content">
|
|
266
|
+
<img className="tbox-media-picker-menu__option-icon" src={o.icon} alt="" />
|
|
267
|
+
<span className="tbox-media-picker-menu__option-label">{o.label}</span>
|
|
268
|
+
</div>
|
|
269
|
+
</div>
|
|
270
|
+
))}
|
|
271
|
+
</div>
|
|
272
|
+
)}
|
|
273
|
+
|
|
274
|
+
{disclaimer && <div className="tbox-chat-input__disclaimer">{disclaimer}</div>}
|
|
275
|
+
|
|
276
|
+
<VoiceRecordingOverlay
|
|
277
|
+
visible={voice.overlayVisible}
|
|
278
|
+
interactionState={voice.interactionState}
|
|
279
|
+
countdown={voice.countdown}
|
|
280
|
+
handlers={voice.handlers}
|
|
281
|
+
/>
|
|
282
|
+
</div>
|
|
283
|
+
);
|
|
284
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { VoiceInteractionState } from './machine';
|
|
2
|
+
import type { VoicePressHandlers } from './useVoicePress';
|
|
3
|
+
|
|
4
|
+
function fieldText(s: VoiceInteractionState): string {
|
|
5
|
+
if (s === 'recording-cancel') return '松开取消';
|
|
6
|
+
if (s === 'processing') return '识别中...';
|
|
7
|
+
if (s === 'pressing' || s === 'recording-active') return '松开转文字';
|
|
8
|
+
return '按住说话';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** “按住说话”输入条(替代文本框,语音模式下显示)。 */
|
|
12
|
+
export function VoicePressField({
|
|
13
|
+
disabled,
|
|
14
|
+
interactionState,
|
|
15
|
+
handlers,
|
|
16
|
+
}: {
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
interactionState: VoiceInteractionState;
|
|
19
|
+
handlers: VoicePressHandlers;
|
|
20
|
+
}) {
|
|
21
|
+
return (
|
|
22
|
+
<div
|
|
23
|
+
className={`tbox-voice-press-field tbox-voice-press-field--${interactionState}${disabled ? ' tbox-voice-press-field--disabled' : ''}`}
|
|
24
|
+
onTouchStart={disabled ? undefined : handlers.onTouchStart}
|
|
25
|
+
onTouchMove={disabled ? undefined : handlers.onTouchMove}
|
|
26
|
+
onTouchEnd={disabled ? undefined : handlers.onTouchEnd}
|
|
27
|
+
onTouchCancel={disabled ? undefined : handlers.onTouchCancel}
|
|
28
|
+
>
|
|
29
|
+
<div className="tbox-voice-press-field__content">
|
|
30
|
+
<span className="tbox-voice-press-field__text">{fieldText(interactionState)}</span>
|
|
31
|
+
</div>
|
|
32
|
+
</div>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { VoiceInteractionState } from './machine';
|
|
2
|
+
import type { VoicePressHandlers } from './useVoicePress';
|
|
3
|
+
|
|
4
|
+
function overlayTip(s: VoiceInteractionState): string {
|
|
5
|
+
if (s === 'recording-cancel') return '松开取消';
|
|
6
|
+
if (s === 'processing') return '识别中...';
|
|
7
|
+
return '松手发送,上移取消';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** 录音全屏浮层(按压期间显示)。 */
|
|
11
|
+
export function VoiceRecordingOverlay({
|
|
12
|
+
visible,
|
|
13
|
+
interactionState,
|
|
14
|
+
countdown,
|
|
15
|
+
handlers,
|
|
16
|
+
}: {
|
|
17
|
+
visible: boolean;
|
|
18
|
+
interactionState: VoiceInteractionState;
|
|
19
|
+
countdown?: number | null;
|
|
20
|
+
handlers: VoicePressHandlers;
|
|
21
|
+
}) {
|
|
22
|
+
if (!visible) return null;
|
|
23
|
+
const isCancel = interactionState === 'recording-cancel';
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div className="tbox-voice-overlay">
|
|
27
|
+
<div className="tbox-voice-overlay__mask" />
|
|
28
|
+
<div
|
|
29
|
+
className="tbox-voice-overlay__capture"
|
|
30
|
+
onTouchStart={handlers.onTouchStart}
|
|
31
|
+
onTouchMove={handlers.onTouchMove}
|
|
32
|
+
onTouchEnd={handlers.onTouchEnd}
|
|
33
|
+
onTouchCancel={handlers.onTouchCancel}
|
|
34
|
+
>
|
|
35
|
+
<div className="tbox-voice-overlay__panel">
|
|
36
|
+
{countdown != null && interactionState === 'recording-active' && (
|
|
37
|
+
<div className="tbox-voice-overlay__bubble">
|
|
38
|
+
<span className="tbox-voice-overlay__bubble-text">{countdown}秒后自动停止录音</span>
|
|
39
|
+
</div>
|
|
40
|
+
)}
|
|
41
|
+
<span className={`tbox-voice-overlay__tip${isCancel ? ' tbox-voice-overlay__tip--cancel' : ''}`}>
|
|
42
|
+
{overlayTip(interactionState)}
|
|
43
|
+
</span>
|
|
44
|
+
<div className={`tbox-voice-overlay__button${isCancel ? ' tbox-voice-overlay__button--cancel' : ''}`}>
|
|
45
|
+
<div className="tbox-voice-overlay__wave" />
|
|
46
|
+
</div>
|
|
47
|
+
</div>
|
|
48
|
+
</div>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** 语音按压交互状态机(纯逻辑,移植自原模板 voice-interaction-machine)。 */
|
|
2
|
+
export type VoiceInteractionState =
|
|
3
|
+
| 'idle'
|
|
4
|
+
| 'pressing'
|
|
5
|
+
| 'recording-active'
|
|
6
|
+
| 'recording-cancel'
|
|
7
|
+
| 'processing';
|
|
8
|
+
|
|
9
|
+
export type VoiceReleaseAction = 'stop' | 'cancel' | 'noop';
|
|
10
|
+
|
|
11
|
+
export const CANCEL_GESTURE_THRESHOLD = 25;
|
|
12
|
+
|
|
13
|
+
/** 按压中上滑超过阈值 → 取消态,否则录音态 */
|
|
14
|
+
export function getMoveInteractionState(
|
|
15
|
+
startClientY: number,
|
|
16
|
+
currentClientY: number,
|
|
17
|
+
threshold = CANCEL_GESTURE_THRESHOLD,
|
|
18
|
+
): VoiceInteractionState {
|
|
19
|
+
return startClientY - currentClientY > threshold ? 'recording-cancel' : 'recording-active';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 松手动作:取消 / 停止(转文字) / 无操作 */
|
|
23
|
+
export function getReleaseAction(state: VoiceInteractionState): VoiceReleaseAction {
|
|
24
|
+
if (state === 'recording-cancel') return 'cancel';
|
|
25
|
+
if (state === 'pressing' || state === 'recording-active') return 'stop';
|
|
26
|
+
return 'noop';
|
|
27
|
+
}
|