feat: esc to interrupt
This commit is contained in:
parent
350cc09597
commit
5643a53b5a
3 changed files with 33 additions and 2 deletions
|
|
@ -62,7 +62,8 @@ export async function streamChatResponse(
|
|||
chatHistory: ChatCompletionMessageParam[],
|
||||
assistant: ContinueClient["assistant"],
|
||||
client: ContinueClient["client"],
|
||||
callbacks?: StreamCallbacks
|
||||
callbacks?: StreamCallbacks,
|
||||
abortController?: AbortController
|
||||
) {
|
||||
// Prepare tools for the API call
|
||||
const toolsForRequest = getAllTools();
|
||||
|
|
@ -84,6 +85,8 @@ export async function streamChatResponse(
|
|||
messages: chatHistory,
|
||||
stream: true,
|
||||
tools: toolsForRequest,
|
||||
}, {
|
||||
signal: abortController?.signal,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error(
|
||||
|
|
@ -99,6 +102,11 @@ export async function streamChatResponse(
|
|||
let toolArguments = "";
|
||||
|
||||
for await (const chunk of stream) {
|
||||
// Check if we should abort
|
||||
if (abortController?.signal.aborted) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Handle regular content
|
||||
if (chunk.choices[0].delta.content) {
|
||||
const content = chunk.choices[0].delta.content;
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
const [isWaitingForResponse, setIsWaitingForResponse] = useState(false);
|
||||
const [messages, setMessages] = useState<DisplayMessage[]>([]);
|
||||
const [inputMode, setInputMode] = useState(true);
|
||||
const [abortController, setAbortController] = useState<AbortController | null>(null);
|
||||
const { exit } = useApp();
|
||||
|
||||
// Handle initial prompt
|
||||
|
|
@ -79,6 +80,8 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
setMessages((prev) => [...prev, { role: "user", content: message }]);
|
||||
|
||||
// Start streaming response
|
||||
const controller = new AbortController();
|
||||
setAbortController(controller);
|
||||
setIsWaitingForResponse(true);
|
||||
setInputMode(false);
|
||||
|
||||
|
|
@ -122,7 +125,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
},
|
||||
};
|
||||
|
||||
await streamChatResponse(newHistory, assistant, client, streamCallbacks);
|
||||
await streamChatResponse(newHistory, assistant, client, streamCallbacks, controller);
|
||||
|
||||
// Finalize the assistant message
|
||||
const finalAssistantMessage: ChatCompletionMessageParam = {
|
||||
|
|
@ -147,11 +150,22 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
{ role: "system", content: errorMessage },
|
||||
]);
|
||||
} finally {
|
||||
setAbortController(null);
|
||||
setIsWaitingForResponse(false);
|
||||
setInputMode(true);
|
||||
}
|
||||
};
|
||||
|
||||
const handleInterrupt = () => {
|
||||
if (abortController && isWaitingForResponse) {
|
||||
abortController.abort();
|
||||
setMessages((prev) => [
|
||||
...prev,
|
||||
{ role: "system", content: "[Interrupted by user]" },
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
const renderMessage = (message: DisplayMessage, index: number) => {
|
||||
const isUser = message.role === "user";
|
||||
const isSystem = message.role === "system";
|
||||
|
|
@ -198,6 +212,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
onSubmit={handleUserMessage}
|
||||
isWaitingForResponse={isWaitingForResponse}
|
||||
inputMode={inputMode}
|
||||
onInterrupt={handleInterrupt}
|
||||
/>
|
||||
</Box>
|
||||
</Box>
|
||||
|
|
|
|||
|
|
@ -6,12 +6,14 @@ interface UserInputProps {
|
|||
onSubmit: (message: string) => void;
|
||||
isWaitingForResponse: boolean;
|
||||
inputMode: boolean;
|
||||
onInterrupt?: () => void;
|
||||
}
|
||||
|
||||
const UserInput: React.FC<UserInputProps> = ({
|
||||
onSubmit,
|
||||
isWaitingForResponse,
|
||||
inputMode,
|
||||
onInterrupt,
|
||||
}) => {
|
||||
const [textBuffer] = useState(() => new TextBuffer());
|
||||
const [inputText, setInputText] = useState("");
|
||||
|
|
@ -24,6 +26,12 @@ const UserInput: React.FC<UserInputProps> = ({
|
|||
return;
|
||||
}
|
||||
|
||||
// Handle escape key to interrupt streaming
|
||||
if (key.escape && isWaitingForResponse && onInterrupt) {
|
||||
onInterrupt();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!inputMode) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue