class

LLM::OpenAI::History

Inherits JSON::Serializable::Unmapped < JSON::Serializable < Reference < Object

The serializable chat history keeps all wrapped messages to save the additional information when saving the session.

Constructors

new(*, __pull_for_json_serializable pull : JSON::PullParser)
Source

Instance methods

append_last_conversation(to : History, outer = false)

Append last whole conversation, starting with most recent user message,

  • either all messages, or
  • user and assistant only
Source
append_message(msg : Message, usage : Usage | Nil = nil)
Source
append_session_conversation(to : History, outer = false)

Append conversation from the branch-point of this session, starting with most recent user message,

  • either all messages, or
  • user and assistant only
Source
append_transformed_last_response?(to : History, & : String -> NamedTuple(prompt: String, response: String)) : Bool

Extract last response from assistant and yield it to caller to transform into a named tuple with a prompt and response, which in turn is converted into two messages that are appended to the given history. Returns true on success, false if unable to extract.

Source
branch(from : History, exclude_last_turn = false)

Copy the messages from another session's history, optionally excluding the last "turn".

Source
each_message

This yields each Message in the history

Source
extract_last_conversation(outer = false)

Extract last whole conversation, starting with most recent user message,

  • either all messages, or
  • user and assistant only
Source
extract_session_conversation(outer = false)

Extract conversation from the branch-point of this session, starting with most recent user message,

  • either all messages, or
  • user and assistant only
Source
format
Source
import(prompt : MCP::PromptResult, emit = false, & : ChatEvent -> ) : Nil
Source
last_usage

Returns the most recent usage information

Source
tail_chats(num = -1, & : ChatEvent -> )

Emit the last N request responses. Always tries to emit the query before the response. May include tool calls as a result. Use N=-1 to list all

Source
transfer_tail_chats(to : History, num = 1, filter_by_role : String | Nil = nil)
Source
transform_last_response?

Extract last response from assistant and yield it to caller to transform into a named tuple with a prompt and response, which in turn is used to create a message array that caller can inject into a session. Returns nil if no response in the current history.

Source

Nested types