Class Conversation

java.lang.Object
org.openhab.core.voice.text.conversation.Conversation

@NonNullByDefault public class Conversation extends Object
The Conversation class contains a list of messages in between the user and a LanguageInterpreter.
Author:
Miguel Álvarez Díez - Initial contribution
  • Field Details

  • Constructor Details

  • Method Details

    • addListener

      public void addListener(ConversationListener listener)
      Parameters:
      listener - the listener
    • removeListener

      public void removeListener(ConversationListener listener)
      Parameters:
      listener - the listener
    • setMaxMessages

      public void setMaxMessages(int maxMessages)
      Set the maximum number of messages to keep in history. At least 5 messages must be kept in the history.
      Parameters:
      maxMessages - the maximum number of messages
    • getId

      public String getId()
    • getCreated

      public Instant getCreated()
    • getLastUpdated

      public Instant getLastUpdated()
    • getMessages

      public List<Conversation.Message> getMessages()
      Get a copy of the list of messages.
      Returns:
      list of messages
    • getLastMessage

      public @Nullable Conversation.Message getLastMessage()
      Get the last message (if any).
      Returns:
      the last message or null
    • addMessage

      public int addMessage(ConversationRole role, String content) throws ConversationException
      Adds a new message to the end of the conversation.
      Parameters:
      role - the role that authored this message
      content - the content of the message
      Returns:
      the UID of the newly added message
      Throws:
      ConversationException - see addMessage(ConversationRole, String, Integer)
    • addMessage

      public int addMessage(ConversationRole role, String content, @Nullable Integer prevMessageID) throws ConversationException
      Adds a new message to the end of the conversation.

      The following conditions are enforced:

      • If prevMessageUID is provided: The last message has the expected UID, so the conversation hasn't changed unexpectedly.
      • A tool return message must come after a tool call message.
      • The first message is expected to be a user message.
      Parameters:
      role - the role that authored this message
      content - the content of the message
      prevMessageID - the ID of the (assumed) previous message
      Returns:
      the UID of the newly added message
      Throws:
      ConversationException - when any of the above conditions is violated
    • removeSinceMessage

      public boolean removeSinceMessage(int id)
      Removes a specific messages and all subsequent messages from the conversation.
      Parameters:
      id - the ID of the message and its successors to remove
      Returns:
      whether an actual removal happened
    • removeMessages

      public void removeMessages()
      Remove all messages from the conversation.