Skip to content

Commit

Permalink
feat: Add help command
Browse files Browse the repository at this point in the history
  • Loading branch information
fmueller committed Jun 1, 2024
1 parent 173fb05 commit ee05a12
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/com/github/fmueller/jarvis/commands/ChatCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.fmueller.jarvis.commands

import com.github.fmueller.jarvis.ai.OllamaService
import com.github.fmueller.jarvis.conversation.Conversation
import com.github.fmueller.jarvis.conversation.Message
import com.github.fmueller.jarvis.conversation.Role

class ChatCommand(private val ollamaService: OllamaService) : SlashCommand {

override suspend fun run(conversation: Conversation): Conversation {
val response = ollamaService.chat(conversation.messages).trim()
conversation.addMessage(Message(Role.ASSISTANT, response))
return conversation
}
}
19 changes: 19 additions & 0 deletions src/main/kotlin/com/github/fmueller/jarvis/commands/HelpCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.fmueller.jarvis.commands

import com.github.fmueller.jarvis.conversation.Conversation
import com.github.fmueller.jarvis.conversation.Message
import com.github.fmueller.jarvis.conversation.Role

class HelpCommand : SlashCommand {

override suspend fun run(conversation: Conversation): Conversation {
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."
)
)
return conversation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.fmueller.jarvis.commands

import com.github.fmueller.jarvis.conversation.Conversation

interface SlashCommand {

suspend fun run(conversation: Conversation): Conversation
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.fmueller.jarvis.commands

import com.github.fmueller.jarvis.ai.OllamaService

class SlashCommandParser(private val ollamaService: OllamaService) {

fun parse(message: String): SlashCommand {
val trimmedMessage = message.trim().lowercase()
if (trimmedMessage == "/help" || trimmedMessage == "/?") {
return HelpCommand()
}

return ChatCommand(ollamaService)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.fmueller.jarvis.conversation

import com.github.fmueller.jarvis.ai.OllamaService
import com.github.fmueller.jarvis.commands.SlashCommandParser
import java.beans.PropertyChangeListener
import java.beans.PropertyChangeSupport
import java.time.LocalDateTime
Expand All @@ -15,24 +16,25 @@ enum class Role {

data class Message(val role: Role, val content: String, val createdAt: LocalDateTime = LocalDateTime.now())

class Conversation(private val ollamaService: OllamaService) {
class Conversation(ollamaService: OllamaService) {

private val messages = mutableListOf<Message>()
private var _messages = mutableListOf<Message>()
val messages get() = _messages.toList()

private val commandParser = SlashCommandParser(ollamaService)
private val propertyChangeSupport = PropertyChangeSupport(this)

suspend fun chat(message: String): Conversation {
addMessage(Message(Role.USER, message.trim()))

// TODO implement simple pipeline for slash command detection
val response = ollamaService.chat(messages).trim()
addMessage(Message(Role.ASSISTANT, response))
return this
val command = commandParser.parse(message)
return command.run(this)
}

fun addMessage(message: Message) {
val oldMessages = ArrayList(messages)
messages.add(message)
propertyChangeSupport.firePropertyChange("messages", oldMessages, ArrayList(messages))
val oldMessages = ArrayList(_messages)
_messages.add(message)
propertyChangeSupport.firePropertyChange("messages", oldMessages, ArrayList(_messages))
}

fun addPropertyChangeListener(listener: PropertyChangeListener) {
Expand Down

0 comments on commit ee05a12

Please sign in to comment.