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