The renderChatUI() function initializes and renders the Chat UI using Ink. It returns a controller object that allows you to interact with the chat interface programmatically.
Through this controller, you can:
- Register input listeners using
onInput(action). When a user submits a message in the UI, the provided action callback is executed with the message string. - Send assistant messages using
sendMessage(message). This pushes a message with the assistant role into the chat history, allowing the application to simulate or display AI responses.
Note: You must call onInput or sendMessage after the UI has been rendered. If you attempt to call these methods before the controller is ready, the function will throw an error: "Chat UI is not ready to register input handlers." or "Chat UI is not ready to send messages."
import { renderChatUI } from './src/cli-chat/index';
// Initialize the UI and get the controller
const chat = renderChatUI();
// Register a listener to handle user input
chat.onInput((message: string) => {
console.log(`User said: ${message}`);
});
// Later, you can push an assistant message to the UI
// chat.sendMessage("Hello! How can I help you today?");