From eddce1277af68fb111bb9c891a03797a0c91c17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20M=C3=BCller?= Date: Sun, 2 Jun 2024 16:29:42 +0200 Subject: [PATCH] feat: Add new conversation command --- .../github/fmueller/jarvis/commands/HelpCommand.kt | 10 ++++++++-- .../jarvis/commands/NewConversationCommand.kt | 13 +++++++++++++ .../fmueller/jarvis/commands/SlashCommandParser.kt | 4 ++++ .../fmueller/jarvis/conversation/Conversation.kt | 7 +++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/com/github/fmueller/jarvis/commands/NewConversationCommand.kt diff --git a/src/main/kotlin/com/github/fmueller/jarvis/commands/HelpCommand.kt b/src/main/kotlin/com/github/fmueller/jarvis/commands/HelpCommand.kt index 4332832..1260ffc 100644 --- a/src/main/kotlin/com/github/fmueller/jarvis/commands/HelpCommand.kt +++ b/src/main/kotlin/com/github/fmueller/jarvis/commands/HelpCommand.kt @@ -10,8 +10,14 @@ class HelpCommand : SlashCommand { conversation.addMessage( Message( Role.ASSISTANT, - "I'm Jarvis, your personal coding assistant. You can ask me anything. To make me work properly," + - " please install and run Ollama locally." + """ + I'm Jarvis, your personal coding assistant. You can ask me anything. To make me work properly, please install and run Ollama locally. + + Available commands: + + - ```/help``` or ```/?``` - Shows this help message + - ```/new``` - Starts a new conversation + """.trimIndent() ) ) return conversation diff --git a/src/main/kotlin/com/github/fmueller/jarvis/commands/NewConversationCommand.kt b/src/main/kotlin/com/github/fmueller/jarvis/commands/NewConversationCommand.kt new file mode 100644 index 0000000..443bc49 --- /dev/null +++ b/src/main/kotlin/com/github/fmueller/jarvis/commands/NewConversationCommand.kt @@ -0,0 +1,13 @@ +package com.github.fmueller.jarvis.commands + +import com.github.fmueller.jarvis.conversation.Conversation + +class NewConversationCommand : SlashCommand { + + // as long as we don't have multiple conversations + // we can just clear the messages + override suspend fun run(conversation: Conversation): Conversation { + conversation.clearMessages() + return conversation + } +} diff --git a/src/main/kotlin/com/github/fmueller/jarvis/commands/SlashCommandParser.kt b/src/main/kotlin/com/github/fmueller/jarvis/commands/SlashCommandParser.kt index 197ab1c..d494131 100644 --- a/src/main/kotlin/com/github/fmueller/jarvis/commands/SlashCommandParser.kt +++ b/src/main/kotlin/com/github/fmueller/jarvis/commands/SlashCommandParser.kt @@ -10,6 +10,10 @@ class SlashCommandParser(private val ollamaService: OllamaService) { return HelpCommand() } + if (trimmedMessage == "/new") { + return NewConversationCommand() + } + return ChatCommand(ollamaService) } } diff --git a/src/main/kotlin/com/github/fmueller/jarvis/conversation/Conversation.kt b/src/main/kotlin/com/github/fmueller/jarvis/conversation/Conversation.kt index 5d00472..94f1759 100644 --- a/src/main/kotlin/com/github/fmueller/jarvis/conversation/Conversation.kt +++ b/src/main/kotlin/com/github/fmueller/jarvis/conversation/Conversation.kt @@ -37,6 +37,13 @@ class Conversation(ollamaService: OllamaService) { propertyChangeSupport.firePropertyChange("messages", oldMessages, ArrayList(_messages)) } + fun clearMessages() { + val oldMessages = ArrayList(_messages) + _messages.clear() + _messages.add(Message(Role.ASSISTANT, "Hello! How can I help you?")) + propertyChangeSupport.firePropertyChange("messages", oldMessages, ArrayList(_messages)) + } + fun addPropertyChangeListener(listener: PropertyChangeListener) { propertyChangeSupport.addPropertyChangeListener(listener) }