Record Class EventQuery

java.lang.Object
java.lang.Record
org.springframework.ai.session.EventQuery
Record Components:
keywords - the keywords to look for, at least one, none blank
matchMode - how several keywords combine
messageTypes - the message types to search, or null for every type
since - only events at or after this instant, to the microsecond, or null for no lower bound
page - the zero-based page of matches to return, within the first MAX_MATCHES matches
pageSize - the number of matches on a page

public record EventQuery(List<String> keywords, EventQuery.MatchMode matchMode, @Nullable Set<MessageType> messageTypes, @Nullable Instant since, int page, int pageSize) extends Record
A keyword search over the messages of a session: what SessionService.searchEvents(java.lang.String, org.springframework.ai.session.EventQuery) looks for. It is the read for recall, finding something that was said, where EventFilter is the read for the prompt.

The search covers the whole log, the events a compaction covered included: they are no longer sent to the model, and a search is how it finds them again. Only EventType.MESSAGE events are searched, by the text of their message, and synthetic events never: a summary repeats what the log still holds. A tool result carries its data outside the message text, so it is not found.

A keyword matches when the text contains it, ignoring case. With several keywords, EventQuery.MatchMode.ANY matches a text that contains one of them and EventQuery.MatchMode.ALL a text that contains every one. The matches come oldest first, a page at a time.

Letters are compared the same way everywhere for ASCII text. For other text the store decides: a database may ignore accents or fold case differently, by its collation.

A query is bounded, so that one search costs a bounded read: at most MAX_KEYWORDS keywords of at most MAX_KEYWORD_LENGTH characters, and pages only within the first MAX_MATCHES matches.

apply(List) is the reference implementation for a log held in memory; a store that can push the search down to a query does so and agrees with it (see SessionRepository.searchEvents(java.lang.String, org.springframework.ai.session.EventQuery)).


 EventQuery first = EventQuery.keyword("invoice");
 EventQuery both = EventQuery.builder().keywords("invoice", "refund").matchMode(MatchMode.ALL).build();
 EventQuery recent = EventQuery.builder().keywords("invoice").since(lastWeek).page(1).build();
 
Since:
2.1.0
Author:
Christian Tzolov
  • Field Details

    • DEFAULT_PAGE_SIZE

      public static final int DEFAULT_PAGE_SIZE
      The page size of a query that sets none.
      See Also:
    • MAX_PAGE_SIZE

      public static final int MAX_PAGE_SIZE
      The largest page size, so that one search cannot return a whole log.
      See Also:
    • MAX_MATCHES

      public static final int MAX_MATCHES
      How far a search pages: the page and the pages before it hold at most this many matches. A store reads the matches from the first one on, so this bounds what one search reads.
      See Also:
    • MAX_KEYWORDS

      public static final int MAX_KEYWORDS
      The most keywords a query takes; each becomes a predicate of the store's query.
      See Also:
    • MAX_KEYWORD_LENGTH

      public static final int MAX_KEYWORD_LENGTH
      The longest keyword, in characters.
      See Also:
  • Constructor Details

    • EventQuery

      public EventQuery(List<String> keywords, EventQuery.MatchMode matchMode, @Nullable Set<MessageType> messageTypes, @Nullable Instant since, int page, int pageSize)
      Creates an instance of a EventQuery record class.
      Parameters:
      keywords - the value for the keywords record component
      matchMode - the value for the matchMode record component
      messageTypes - the value for the messageTypes record component
      since - the value for the since record component
      page - the value for the page record component
      pageSize - the value for the pageSize record component
  • Method Details

    • keyword

      public static EventQuery keyword(String keyword)
      Returns a query for the first page of the events that contain the keyword.
    • builder

      public static EventQuery.Builder builder()
      Returns a new EventQuery.Builder.
    • limit

      public int limit()
      How many matches a store reads to return this page: the page and every page before it. A store that cannot skip rows in its query reads this many and keeps the page.
    • matches

      public boolean matches(SessionEvent event)
      Whether the event is one this query finds: a message event that is not synthetic, of a searched type, not older than since(), whose text contains the keywords.
    • indexOfIgnoreCase

      public static int indexOfIgnoreCase(String text, String keyword)
      The index of the first place the text holds the keyword, ignoring case, or -1. The characters are compared one by one, so the index is one of the text as given, and a letter matches its other case wherever it stands in a word.
    • apply

      public List<SessionEvent> apply(List<SessionEvent> events)
      Runs the query over the events of a log, given oldest first: the matching events, oldest first, cut to the page.
    • pageOf

      public List<SessionEvent> pageOf(List<SessionEvent> matches)
      Keeps this query's page of the matches a store read from the first one on, up to limit() of them.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • keywords

      public List<String> keywords()
      Returns the value of the keywords record component.
      Returns:
      the value of the keywords record component
    • matchMode

      public EventQuery.MatchMode matchMode()
      Returns the value of the matchMode record component.
      Returns:
      the value of the matchMode record component
    • messageTypes

      public @Nullable Set<MessageType> messageTypes()
      Returns the value of the messageTypes record component.
      Returns:
      the value of the messageTypes record component
    • since

      public @Nullable Instant since()
      Returns the value of the since record component.
      Returns:
      the value of the since record component
    • page

      public int page()
      Returns the value of the page record component.
      Returns:
      the value of the page record component
    • pageSize

      public int pageSize()
      Returns the value of the pageSize record component.
      Returns:
      the value of the pageSize record component