限制 AI 输入长度不超过 1000 字符,超出时提示用户
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -202,6 +202,8 @@ function makeId() {
|
||||
return `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
const MAX_INPUT_LENGTH = 1000;
|
||||
|
||||
export type AIPageVariant = "page" | "split";
|
||||
|
||||
export interface AIPageProps {
|
||||
@@ -422,6 +424,10 @@ export default function AIPage({ variant = "page", onClose }: AIPageProps = {})
|
||||
(question: string) => {
|
||||
const trimmed = question.trim();
|
||||
if (!trimmed || sending) return;
|
||||
if (trimmed.length > MAX_INPUT_LENGTH) {
|
||||
toast.error(formatText(aiText.inputTooLong, { max: MAX_INPUT_LENGTH }));
|
||||
return;
|
||||
}
|
||||
if (!mac || !deviceState || !api) {
|
||||
toast.error(aiText.deviceRequired);
|
||||
return;
|
||||
@@ -595,12 +601,27 @@ export default function AIPage({ variant = "page", onClose }: AIPageProps = {})
|
||||
[input, send],
|
||||
);
|
||||
|
||||
const onInputChange = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
setInput(e.target.value);
|
||||
const el = e.target;
|
||||
el.style.height = "auto";
|
||||
el.style.height = `${Math.min(el.scrollHeight, 160)}px`;
|
||||
}, []);
|
||||
const overLimitToastShown = useRef(false);
|
||||
|
||||
const onInputChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
const raw = e.target.value;
|
||||
if (raw.length > MAX_INPUT_LENGTH) {
|
||||
if (!overLimitToastShown.current) {
|
||||
toast.error(formatText(aiText.inputTooLong, { max: MAX_INPUT_LENGTH }));
|
||||
overLimitToastShown.current = true;
|
||||
}
|
||||
setInput(raw.slice(0, MAX_INPUT_LENGTH));
|
||||
} else {
|
||||
overLimitToastShown.current = false;
|
||||
setInput(raw);
|
||||
}
|
||||
const el = e.target;
|
||||
el.style.height = "auto";
|
||||
el.style.height = `${Math.min(el.scrollHeight, 160)}px`;
|
||||
},
|
||||
[aiText],
|
||||
);
|
||||
|
||||
// ── 优化记录:对比对话框 & 应用/回滚 ──
|
||||
const [compareMsg, setCompareMsg] = useState<OptimizeUIMessage | null>(null);
|
||||
@@ -880,6 +901,7 @@ export default function AIPage({ variant = "page", onClose }: AIPageProps = {})
|
||||
onChange={onInputChange}
|
||||
onKeyDown={onKeyDown}
|
||||
rows={1}
|
||||
maxLength={MAX_INPUT_LENGTH}
|
||||
placeholder={aiText.inputPlaceholder}
|
||||
className="flex-1 bg-transparent outline-none resize-none text-[15px] text-white placeholder:text-white/35 py-1.5 px-1 max-h-40 leading-[1.4]"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user