mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-17 06:58:09 +01:00
fixed scroll bug
This commit is contained in:
@@ -35,11 +35,8 @@ export default function LLMResponse({ data, onConfirm, isLastMessage }) {
|
||||
{!requiresConfirm && data.tool && data.next === "confirm" && (
|
||||
<div className="text-sm text-center text-green-600 dark:text-green-400">
|
||||
<div>
|
||||
Agent ran tool: <strong>{data.tool ?? "Unknown"}</strong>
|
||||
Agent chose tool: <strong>{data.tool ?? "Unknown"}</strong>
|
||||
</div>
|
||||
{/* <div>
|
||||
{JSON.stringify(data, null, 2)}
|
||||
</div> */}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -8,9 +8,10 @@ export default function App() {
|
||||
const containerRef = useRef(null);
|
||||
const inputRef = useRef(null);
|
||||
const [conversation, setConversation] = useState([]);
|
||||
const [lastMessage, setLastMessage] = useState(null); // New state for tracking the last message
|
||||
const [userInput, setUserInput] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [done, setDone] = useState(true); // New `done` state
|
||||
const [done, setDone] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
// Poll /get-conversation-history every 0.5 seconds
|
||||
@@ -19,16 +20,22 @@ export default function App() {
|
||||
const res = await fetch("http://127.0.0.1:8000/get-conversation-history");
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
// Update conversation
|
||||
setConversation(data.messages || []);
|
||||
const newConversation = data.messages || [];
|
||||
setConversation(newConversation);
|
||||
|
||||
if (data.messages && data.messages.length > 0) {
|
||||
const lastMessage = data.messages[data.messages.length - 1];
|
||||
setLoading(lastMessage.actor !== "agent");
|
||||
setDone(lastMessage.response.next === "done");
|
||||
if (newConversation.length > 0) {
|
||||
const lastMsg = newConversation[newConversation.length - 1];
|
||||
setLoading(lastMsg.actor !== "agent");
|
||||
setDone(lastMsg.response.next === "done");
|
||||
|
||||
// Only scroll if the last message changes
|
||||
if (!lastMessage || lastMsg.response.response !== lastMessage.response.response) {
|
||||
setLastMessage(lastMsg); // Update the last message
|
||||
}
|
||||
} else {
|
||||
setLoading(false);
|
||||
setDone(true); // Default to `done` if no messages
|
||||
setDone(true);
|
||||
setLastMessage(null); // Clear last message if no messages
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -37,12 +44,24 @@ export default function App() {
|
||||
}, POLL_INTERVAL);
|
||||
|
||||
return () => clearInterval(intervalId);
|
||||
}, []);
|
||||
}, [lastMessage]);
|
||||
|
||||
useEffect(() => {
|
||||
if (containerRef.current && lastMessage) {
|
||||
containerRef.current.scrollTop = containerRef.current.scrollHeight;
|
||||
}
|
||||
}, [lastMessage]); // Scroll only when the last message changes
|
||||
|
||||
useEffect(() => {
|
||||
if (inputRef.current) {
|
||||
inputRef.current.focus(); // Ensure the input box retains focus
|
||||
}
|
||||
}, [userInput, loading, done]);
|
||||
|
||||
const handleSendMessage = async () => {
|
||||
if (!userInput.trim()) return;
|
||||
try {
|
||||
setLoading(true); // Mark as loading
|
||||
setLoading(true);
|
||||
await fetch(
|
||||
`http://127.0.0.1:8000/send-prompt?prompt=${encodeURIComponent(userInput)}`,
|
||||
{ method: "POST" }
|
||||
@@ -71,6 +90,7 @@ export default function App() {
|
||||
{ method: "POST" }
|
||||
);
|
||||
setConversation([]); // Clear local state
|
||||
setLastMessage(null); // Reset last message
|
||||
} catch (err) {
|
||||
console.error("Error ending chat:", err);
|
||||
}
|
||||
@@ -82,19 +102,6 @@ export default function App() {
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (containerRef.current) {
|
||||
containerRef.current.scrollTop = containerRef.current.scrollHeight;
|
||||
}
|
||||
}, [conversation, loading, done]);
|
||||
|
||||
useEffect(() => {
|
||||
if (inputRef.current) {
|
||||
inputRef.current.focus(); // Ensure the input box retains focus
|
||||
}
|
||||
}, [userInput, loading, done]); // Add other dependencies if necessary
|
||||
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-screen">
|
||||
<NavBar title="Temporal AI Agent" />
|
||||
@@ -119,7 +126,6 @@ export default function App() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Floating Input Section */}
|
||||
{/* Fixed bottom input */}
|
||||
<div
|
||||
className="fixed bottom-0 left-1/2 transform -translate-x-1/2
|
||||
|
||||
Reference in New Issue
Block a user