diff --git a/src/streamChatResponse.ts b/src/streamChatResponse.ts index 4755404c7..ad1b73b90 100644 --- a/src/streamChatResponse.ts +++ b/src/streamChatResponse.ts @@ -50,11 +50,19 @@ export function getAllTools() { type TODO = any; +export interface StreamCallbacks { + onContent?: (content: string) => void; + onToolStart?: (toolName: string) => void; + onToolResult?: (result: string) => void; + onToolError?: (error: string) => void; +} + // Define a function to handle streaming responses with tool calling export async function streamChatResponse( chatHistory: ChatCompletionMessageParam[], assistant: ContinueClient["assistant"], - client: ContinueClient["client"] + client: ContinueClient["client"], + callbacks?: StreamCallbacks ) { // Prepare tools for the API call const toolsForRequest = getAllTools(); @@ -94,7 +102,11 @@ export async function streamChatResponse( // Handle regular content if (chunk.choices[0].delta.content) { const content = chunk.choices[0].delta.content; - process.stdout.write(chalk.white(content)); + if (callbacks?.onContent) { + callbacks.onContent(content); + } else { + process.stdout.write(chalk.white(content)); + } aiResponse += content; } @@ -121,11 +133,15 @@ export async function streamChatResponse( if (toolCall) { if (!toolCall.name) { toolCall.name = toolCallDelta.function.name; - process.stdout.write( - `\n${chalk.yellow("[Using tool:")} ${chalk.yellow.bold( - toolCall.name - )}${chalk.yellow("]")}` - ); + if (callbacks?.onToolStart) { + callbacks.onToolStart(toolCall.name); + } else { + process.stdout.write( + `\n${chalk.yellow("[Using tool:")} ${chalk.yellow.bold( + toolCall.name + )}${chalk.yellow("]")}` + ); + } } } } @@ -152,7 +168,9 @@ export async function streamChatResponse( } } - console.info(); // Add a newline after the response + if (!callbacks?.onContent) { + console.info(); // Add a newline after the response + } // Add the assistant's response to chat history if there's content or tool calls if (currentToolCalls.length > 0) { @@ -193,7 +211,11 @@ export async function streamChatResponse( content: toolResult, }); - console.info(chalk.green(toolResult) + "\n"); + if (callbacks?.onToolResult) { + callbacks.onToolResult(toolResult); + } else { + console.info(chalk.green(toolResult) + "\n"); + } } catch (error) { const errorMessage = `Error executing tool ${toolCall.name}: ${ error instanceof Error ? error.message : String(error) @@ -203,11 +225,15 @@ export async function streamChatResponse( tool_call_id: toolCall.id, content: errorMessage, }); - console.info( - `${chalk.red("[Tool error:")} ${chalk.red(errorMessage)}${chalk.red( - ")" - )}` - ); + if (callbacks?.onToolError) { + callbacks.onToolError(errorMessage); + } else { + console.info( + `${chalk.red("[Tool error:")} ${chalk.red(errorMessage)}${chalk.red( + ")" + )}` + ); + } } } diff --git a/src/ui/TUIChat.tsx b/src/ui/TUIChat.tsx index f505d747e..145da891b 100644 --- a/src/ui/TUIChat.tsx +++ b/src/ui/TUIChat.tsx @@ -3,7 +3,7 @@ import { Box, Text, useApp } from "ink"; import { ChatCompletionMessageParam } from "openai/resources.mjs"; import React, { useEffect, useState } from "react"; import { handleSlashCommands } from "../slashCommands.js"; -import { streamChatResponse } from "../streamChatResponse.js"; +import { streamChatResponse, StreamCallbacks } from "../streamChatResponse.js"; import UserInput from "./UserInput.js"; interface TUIChatProps { @@ -92,18 +92,45 @@ const TUIChat: React.FC = ({ const newHistory = [...chatHistory, newUserMessage]; let assistantResponse = ""; - // 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); + // Create callbacks to handle streaming updates + const streamCallbacks: StreamCallbacks = { + onContent: (content: string) => { + assistantResponse += content; + setMessages((prev) => { + const newMessages = [...prev]; + const lastMessage = newMessages[newMessages.length - 1]; + if (lastMessage.isStreaming) { + lastMessage.content += content; + } + return newMessages; + }); + }, + onToolStart: (toolName: string) => { + setMessages((prev) => [ + ...prev, + { role: "system", content: `[Using tool: ${toolName}]` }, + ]); + }, + onToolResult: (result: string) => { + setMessages((prev) => [ + ...prev, + { role: "system", content: result }, + ]); + }, + onToolError: (error: string) => { + setMessages((prev) => [ + ...prev, + { role: "system", content: `[Tool error: ${error}]` }, + ]); + }, + }; - // 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 + await streamChatResponse(newHistory, assistant, client, streamCallbacks); // Finalize the assistant message const finalAssistantMessage: ChatCompletionMessageParam = { role: "assistant", - content: assistantResponse || "Response received", // Fallback + content: assistantResponse, }; setChatHistory((prev) => [...prev, finalAssistantMessage]); @@ -113,9 +140,6 @@ const TUIChat: React.FC = ({ const lastMessage = newMessages[newMessages.length - 1]; if (lastMessage.isStreaming) { lastMessage.isStreaming = false; - if (!lastMessage.content) { - lastMessage.content = "Response received"; - } } return newMessages; }); @@ -159,24 +183,27 @@ const TUIChat: React.FC = ({ return ( - {/* Chat history */} - + {/* Chat history - takes up all available space above input */} + {messages.map(renderMessage)} - {/* Input area */} - - - {/* Status */} - {isWaitingForResponse && ( - - Waiting for response... - - )} + {/* Fixed bottom section */} + + {/* Status */} + {isWaitingForResponse && ( + + Waiting for response... + + )} + + {/* Input area - always at bottom */} + + ); };