fix: tui no longer completely broken

This commit is contained in:
Nate 2025-07-13 14:16:42 -07:00
parent 4af31b5f71
commit 8c0fd5e87e

View file

@ -35,8 +35,8 @@ const TUIChat: React.FC<TUIChatProps> = ({
const [userInput, setUserInput] = useState(""); const [userInput, setUserInput] = useState("");
const [isWaitingForResponse, setIsWaitingForResponse] = useState(false); const [isWaitingForResponse, setIsWaitingForResponse] = useState(false);
const [currentAssistantMessage, setCurrentAssistantMessage] = useState("");
const [messages, setMessages] = useState<DisplayMessage[]>([]); const [messages, setMessages] = useState<DisplayMessage[]>([]);
const [inputMode, setInputMode] = useState(true);
const { exit } = useApp(); const { exit } = useApp();
// Handle initial prompt // Handle initial prompt
@ -80,7 +80,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
// Start streaming response // Start streaming response
setIsWaitingForResponse(true); setIsWaitingForResponse(true);
setCurrentAssistantMessage(""); setInputMode(false);
// Add placeholder for streaming message // Add placeholder for streaming message
setMessages((prev) => [ setMessages((prev) => [
@ -90,43 +90,20 @@ const TUIChat: React.FC<TUIChatProps> = ({
try { try {
const newHistory = [...chatHistory, newUserMessage]; const newHistory = [...chatHistory, newUserMessage];
// Create a custom stream handler that updates our UI
const originalWrite = process.stdout.write;
let assistantResponse = ""; let assistantResponse = "";
process.stdout.write = function ( // Mock the streaming for now - you'll need to modify streamChatResponse
chunk: any, // to accept a callback or use a different approach
encoding?: any,
callback?: any
) {
const text = chunk.toString();
assistantResponse += text;
setCurrentAssistantMessage(assistantResponse);
// Update the streaming message
setMessages((prev) => {
const newMessages = [...prev];
const lastMessage = newMessages[newMessages.length - 1];
if (lastMessage.isStreaming) {
lastMessage.content = assistantResponse;
}
return newMessages;
});
// Call original write for logging purposes
return originalWrite.call(this, chunk, encoding, callback);
};
await streamChatResponse(newHistory, assistant, client); await streamChatResponse(newHistory, assistant, client);
// Restore original write // For now, we'll just wait and then get the response
process.stdout.write = originalWrite; // In a real implementation, you'd need to modify streamChatResponse
// to provide a way to capture the streamed content
// Finalize the assistant message // Finalize the assistant message
const finalAssistantMessage: ChatCompletionMessageParam = { const finalAssistantMessage: ChatCompletionMessageParam = {
role: "assistant", role: "assistant",
content: assistantResponse, content: assistantResponse || "Response received", // Fallback
}; };
setChatHistory((prev) => [...prev, finalAssistantMessage]); setChatHistory((prev) => [...prev, finalAssistantMessage]);
@ -136,6 +113,9 @@ const TUIChat: React.FC<TUIChatProps> = ({
const lastMessage = newMessages[newMessages.length - 1]; const lastMessage = newMessages[newMessages.length - 1];
if (lastMessage.isStreaming) { if (lastMessage.isStreaming) {
lastMessage.isStreaming = false; lastMessage.isStreaming = false;
if (!lastMessage.content) {
lastMessage.content = "Response received";
}
} }
return newMessages; return newMessages;
}); });
@ -147,7 +127,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
]); ]);
} finally { } finally {
setIsWaitingForResponse(false); setIsWaitingForResponse(false);
setCurrentAssistantMessage(""); setInputMode(true);
} }
}; };
@ -157,6 +137,10 @@ const TUIChat: React.FC<TUIChatProps> = ({
return; return;
} }
if (!inputMode) {
return;
}
if (key.return) { if (key.return) {
if (userInput.trim() && !isWaitingForResponse) { if (userInput.trim() && !isWaitingForResponse) {
handleUserMessage(userInput.trim()); handleUserMessage(userInput.trim());
@ -165,12 +149,13 @@ const TUIChat: React.FC<TUIChatProps> = ({
return; return;
} }
if (key.backspace) { if (key.backspace || key.delete) {
setUserInput((prev) => prev.slice(0, -1)); setUserInput((prev) => prev.slice(0, -1));
return; return;
} }
if (!key.ctrl && !key.meta && input) { // Only add printable characters
if (input && input.length === 1 && !key.ctrl && !key.meta) {
setUserInput((prev) => prev + input); setUserInput((prev) => prev + input);
} }
}); });
@ -203,7 +188,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
return ( return (
<Box flexDirection="column" height="100%"> <Box flexDirection="column" height="100%">
{/* Chat history */} {/* Chat history */}
<Box flexDirection="column" flexGrow={1}> <Box flexDirection="column" flexGrow={1} paddingX={1}>
{messages.map(renderMessage)} {messages.map(renderMessage)}
</Box> </Box>
@ -213,12 +198,12 @@ const TUIChat: React.FC<TUIChatProps> = ({
You:{" "} You:{" "}
</Text> </Text>
<Text>{userInput}</Text> <Text>{userInput}</Text>
{!isWaitingForResponse && <Text color="gray">▋</Text>} {inputMode && !isWaitingForResponse && <Text color="gray">▋</Text>}
</Box> </Box>
{/* Status */} {/* Status */}
{isWaitingForResponse && ( {isWaitingForResponse && (
<Box> <Box paddingX={1}>
<Text color="yellow">Waiting for response...</Text> <Text color="yellow">Waiting for response...</Text>
</Box> </Box>
)} )}