# README
Overview This chat application is a command-line interface (CLI) tool that allows users to interact with a simulated AI chatbot. The bot can handle regular chat messages, provide predefined responses, and engage in specialized interactive sessions triggered by certain commands or keywords detected in the user's input. The interactive sessions currently implemented are /code and /add_test, designed to collect information from the user to generate code or test templates, respectively.
Features Basic Chat Functionality:
The application allows users to input text, which the bot responds to with a hardcoded message after a simulated processing delay. A simple prompt system (You:) is used for user input, and the bot responses are prefixed with AI:. Command Handling:
Users can type commands such as /help, /exit, /code, and /add_test. /help: Lists all available commands. /exit: Exits the application gracefully. /code: Starts an interactive session to collect coding requirements. /add_test: Initiates an interactive session to gather information for generating test code. Interactive Sessions:
Code Session (/code): Asks a series of predefined questions about the user's coding requirements. Generates a simple code snippet based on user inputs. Add Test Session (/add_test): Collects information about which function to test, the source file, and edge cases. Generates a basic test function template using the collected information. Dynamic Session Detection:
The application analyzes user input to detect keywords or phrases that suggest a relevant session. If a match is found, the bot suggests starting a session (e.g., /code or /add_test). The user can confirm or decline the suggestion. Architecture Main Components Message Type:
A struct representing a single message in the chat, containing: Sender: Indicates who sent the message (User or AI). Content: The text content of the message. Chat Struct:
The core structure managing the chat functionality, containing: history: A slice storing the chat history as Message objects. isInCodeSession: A boolean indicating if the user is currently in a /code session. codeSessionData: A map storing user responses during the /code session. codeQuestions: A predefined slice of questions for the /code session. codeQuestionIndex: An integer tracking the current question in the /code session. isInAddTestSession: A boolean indicating if the user is in an /add_test session. addTestSessionData: A map storing user responses during the /add_test session. addTestQuestions: A predefined slice of questions for the /add_test session. addTestQuestionIndex: An integer tracking the current question in the /add_test session. Session Keywords:
A map of session names (code, add_test) to a slice of keywords that might trigger those sessions. Methods:
AddMessage(sender, content string): Adds a message to the chat history. DetectSession(userInput string) (string, bool): Analyzes user input to detect if any session should be suggested. StartCodeSession(), AskNextCodeQuestion(), HandleCodeResponse(userInput string), GenerateCode(): Methods for managing the /code session. StartAddTestSession(), AskNextAddTestQuestion(), HandleAddTestResponse(userInput string), GenerateTestCode(): Methods for managing the /add_test session. GetAIResponse(userInput string): Generates a simulated AI response for regular chat messages. Main Loop Signal Handling:
Captures OS signals to exit the application gracefully when the user presses Ctrl+C. Input Handling:
The application reads user input and determines how to process it based on the current context (e.g., in a session or regular chat). If in a session, the input is routed to the respective session handler. If not in a session, it checks for session triggers, commands, or processes the input as a regular chat message. Session Flow Code Session:
Triggered by the /code command or detected keywords. Asks a series of predefined questions and stores user responses. Generates a basic code snippet based on the collected responses. Add Test Session:
Triggered by the /add_test command or detected keywords. Asks a series of questions to gather information for a test function. Generates a test function template based on the user inputs. Dynamic Session Detection:
Detects potential session triggers in user input using the DetectSession() method. If a match is found, it suggests starting the session and waits for user confirmation. How to Extend the Application Adding New Sessions:
Create a new session type with its own set of questions and response handling methods. Update the sessionKeywords map to include keywords for the new session. Implement session methods similar to the existing ones (e.g., StartNewSession(), AskNextNewQuestion(), HandleNewResponse(), GenerateNewOutput()). Update the main loop to handle the new session. Improving Session Detection:
Use more advanced natural language processing techniques or integrate external libraries for better keyword detection. Allow dynamic configuration of session keywords through a configuration file or database. Enhanced User Interaction:
Implement a more interactive confirmation process for session suggestions, such as a timeout for user responses. Add the ability to save and resume sessions. Persistent Storage:
Store chat history and session data in a database or file system for future reference. Implement a way to export the generated code or test templates. Error Handling and Validation:
Add validation for user inputs to ensure they meet expected formats. Implement error handling for unexpected inputs or situations. Conclusion This application is a flexible CLI chatbot designed to assist with generating code and test templates based on user input. It features dynamic session detection, interactive sessions, and a modular architecture that makes it easy to extend. Future improvements could include better natural language understanding, persistent storage, and more sophisticated error handling.
By following this detailed description, another developer should be able to replicate the functionality, understand the architecture, and add new features or sessions with minimal friction.
Example of how to create a mode that leverages the chat.Chat method to interact with AI.
import (
"github.com/sashabaranov/go-openai"
)
var EXAMPLE TMode = "example"
func init() {
RegisterMode(EXAMPLE, NewExampleMode)
}
type ExampleMode struct {
chat *Chat
}
func NewExampleMode(chat *Chat) *ExampleMode {
return &ExampleMode{
chat: chat,
}
}
func (em *ExampleMode) Start() (Message, Command, error) {
return Message{Text: "let's geek out"}, MODE_START, nil
}
func (em *ExampleMode) HandleResponse(msg Message) (Message, Command, error) {
type AiOutput struct {
Response string `json:"response" jsonschema:"title=response,description=the assistant's response to the user."`
}
var aiOut AiOutput
err := em.chat.Chat(&aiOut, []openai.ChatCompletionMessage{
{
Role: "user",
Content: msg.Text,
},
})
if err != nil {
return TextMessage("chat error: " + err.Error()), MODE_QUIT, nil
}
return TextMessage(aiOut.Response), NOOP, nil
}
func (em *ExampleMode) HandleIntent(msg Message, intent Intent) (Message, Command, error) {
return em.HandleResponse(msg)
}
func (em *ExampleMode) Stop() error {
// do nothing
return nil
}