FindMessage Method  
 

Searches the conversation history for a message or response that matches the specified text.

Syntax

object.FindMessage( Pattern, [StartMessageId], [Options] )

Parameters

Pattern
A string which specifies the text that should be searched for. This may be a literal string or it may specify a simplified regular expression. The Options parameter can be used to specify if the search is case-sensitive. This parameter must be provided and it cannot be an empty string or contain only whitespace characters.
StartMessageId
An optional Integer parameter which specifies the message ID that the search should begin with. If this parameter is omitted or the value is zero, the search begins with the oldest message in the client history.
Options
An optional Integer value that specifies one or more search options. This parameter is constructed by using a bitwise operator with any of the following values:
Value Description
llmSearchDefault Search the entire message history for matching text.
llmSearchPrompt Search the history of message prompts for matching text.
llmSearchResponse Search the history of responses for matching text.
llmSearchCaseSensitive The pattern match will be case-sensitive. If this option is not specified, matches are not case-sensitive.
llmSearchLiteralMatch Perform a literal string search instead of using a regular expression.

Return Value

If a matching message is found, the method returns the unique message identifier for the message. If no message prompts and/or responses match the search pattern, the method returns zero and the LastError property will contain the error code.

Remarks

The FindMessage method searches the message history for text that matches a specific pattern. The search can be limited to prompts, responses, or both, depending on the options specified by the Options parameter. The pattern you are matching against must be a null-terminated string and it cannot consist of only whitespace characters (spaces, tabs, carriage returns and linefeeds).

If the StartMessageId parameter is non-zero, the method will begin searching with the first message whose identifier is equal to or greater than the specified value. Message identifiers are unique within a client session and are assigned incrementally, so newer messages will always have higher identifier values than older messages.

If the Pattern parameter does not contain any regular expression constructs (such as ^, $, [ ], or quantifiers like * and +), the search is performed using a simple substring comparison. Regular expression matching is automatically enabled when the pattern contains recognized regular expression constructs, unless the llmSearchLiteralMatch option is specified.

If a regular expression pattern is specified, the method compiles the pattern and searches the message content for a match. The regular expression matching uses a simplified version which only supports anchors, wildcards, match classes (enclosed in brackets) and a limited number of escape sequences such as \d to match any digit or \s to match any space character. It does not support grouping or replacement.

Supported regular expression constructs include:

  • The caret ^ and dollar symbol $ for the beginning and end of the content.
  • The period . to match any character.
  • Character classes such as [abc] which would match any of the characters enclosed in brackets.
  • Negated classes such as [^abc] which would match if none of those characters are found.
  • Escaped matches such as \d for digits, \w for word characters and \s for whitespace characters
  • The + quantifier can be used to match one or more occurrences.
  • The * quantifier will match zero or more occurrences.

This method uses a minimal regular expression parser and does not include support for complex constructs such as grouping, backreferences or alternation using the pipe | character. If you want to search for text which may contain characters typically used in regular expressions such as [ or $, you can either escape them using a backslash, or you can use the llmSearchLiteralMatch option which forces the method to only perform a substring search.

Applications searching for arbitrary user-entered text should usually specify the llmSearchLiteralMatch option to avoid unintended interpretation of regular expression characters. This will avoid situations where a user may unexpectedly enter search text which appears to be a regular expression pattern.

Regular expression matches are performed against the entire prompt or response text and are not implicitly anchored. It is also important to keep in mind that matches can span multiple lines (they do not end at line breaks) and will attempt to match as much text as possible (this is sometimes referred to as a "greedy match"). This can yield unexpected results if you use wildcards in the pattern which are overly broad, matching large sections of the prompt or response text. For example, a pattern like ".*abc" will match everything until the last instance of "abc" is found.

If neither the llmSearchPrompt nor llmSearchResponse options are explicitly specified, the search will match against both message prompts and responses.

The GetMessage method can be used to obtain the contents of the prompt and/or response for a matching message.

See Also

HistorySize Property, MessageCount Property, GetFirstMessage Method, GetMessage Method, GetNextMessage Method