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