fix:修正对话附加bug,删除无用id
This commit is contained in:
16
src/App.js
16
src/App.js
@@ -15,7 +15,6 @@ import { requestBotReply } from "./dsAPI";
|
|||||||
export default function App() {
|
export default function App() {
|
||||||
const [messages, setMessages] = useState([
|
const [messages, setMessages] = useState([
|
||||||
{
|
{
|
||||||
id: 0,
|
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
content: "你好,有什么可以帮你?",
|
content: "你好,有什么可以帮你?",
|
||||||
},
|
},
|
||||||
@@ -26,7 +25,6 @@ export default function App() {
|
|||||||
const [isSending, setIsSending] = useState(false);
|
const [isSending, setIsSending] = useState(false);
|
||||||
const [isBotTyping, setIsBotTyping] = useState(false);
|
const [isBotTyping, setIsBotTyping] = useState(false);
|
||||||
|
|
||||||
const msgIdRef = useRef(1);
|
|
||||||
const bottomRef = useRef(null);
|
const bottomRef = useRef(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -38,12 +36,12 @@ export default function App() {
|
|||||||
if (!text || isSending) return;
|
if (!text || isSending) return;
|
||||||
|
|
||||||
const userMessage = {
|
const userMessage = {
|
||||||
id: msgIdRef.current++,
|
|
||||||
role: "user",
|
role: "user",
|
||||||
content: text,
|
content: text,
|
||||||
};
|
};
|
||||||
|
|
||||||
setMessages((prev) => [...prev, userMessage]);
|
const nextMessages = [...messages, userMessage];
|
||||||
|
setMessages(nextMessages);
|
||||||
setInputText("");
|
setInputText("");
|
||||||
setIsSending(true);
|
setIsSending(true);
|
||||||
setIsBotTyping(true);
|
setIsBotTyping(true);
|
||||||
@@ -51,8 +49,7 @@ export default function App() {
|
|||||||
// 给模型的上下文
|
// 给模型的上下文
|
||||||
const historyForModel = [
|
const historyForModel = [
|
||||||
{ role: "system", content: "你是一个简洁的助手,用中文回答。" },
|
{ role: "system", content: "你是一个简洁的助手,用中文回答。" },
|
||||||
...messages.map(({ role, content }) => ({ role, content })),
|
...nextMessages.map(({ role, content }) => ({ role, content })),
|
||||||
{ role: "user", content: text },
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let reply = "";
|
let reply = "";
|
||||||
@@ -63,7 +60,6 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const botMessage = {
|
const botMessage = {
|
||||||
id: msgIdRef.current++,
|
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
content: reply,
|
content: reply,
|
||||||
};
|
};
|
||||||
@@ -81,11 +77,6 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
// min-h-screen 最小高度 = 整个屏幕高度
|
|
||||||
// bg-gradient-to-b 背景是 从上到下的渐变
|
|
||||||
// from-background 渐变起点颜色
|
|
||||||
// to-content2/50 渐变终点颜色 /50 = 50% 透明度
|
|
||||||
// p-4 控制四周内距
|
|
||||||
<div className="min-h-screen bg-gradient-to-b from-background to-content2/50 p-4">
|
<div className="min-h-screen bg-gradient-to-b from-background to-content2/50 p-4">
|
||||||
<div className="mx-auto max-w-3xl">
|
<div className="mx-auto max-w-3xl">
|
||||||
<Card className="shadow-xl">
|
<Card className="shadow-xl">
|
||||||
@@ -109,7 +100,6 @@ export default function App() {
|
|||||||
const isUser = m.role === "user";
|
const isUser = m.role === "user";
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={m.id}
|
|
||||||
className={`flex ${isUser ? "justify-end" : "justify-start"}`}
|
className={`flex ${isUser ? "justify-end" : "justify-start"}`}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
|
|||||||
Reference in New Issue
Block a user