Class: SimpleAcp::Models::Session
- Defined in:
- lib/simple_acp/models/session.rb
Overview
Maintains state and conversation history across interactions.
Sessions allow agents to maintain context between requests, storing both conversation history and arbitrary state data.
Instance Attribute Summary collapse
-
#history ⇒ Array<Message>
readonly
Conversation history.
-
#id ⇒ String
readonly
Unique session ID.
-
#state ⇒ Object?
readonly
Arbitrary state data.
Class Method Summary collapse
-
.create ⇒ Session
Create a new session with a generated ID.
-
.from_hash(hash) ⇒ Session?
Create from a hash (JSON deserialization).
Instance Method Summary collapse
-
#add_to_history(message) ⇒ self
Add a message to the conversation history.
-
#clear_history! ⇒ self
Clear all conversation history.
-
#clear_state! ⇒ self
Clear the session state.
-
#empty? ⇒ Boolean
Check if the session has no history or state.
-
#initialize(**kwargs) ⇒ Session
constructor
A new instance of Session.
-
#message_count ⇒ Integer
Get the number of messages in history.
-
#set_state(state_data) ⇒ self
Set the session state.
-
#valid? ⇒ Boolean
Validate the session.
Methods inherited from Base
#==, attribute, attributes, #hash, own_attributes, #to_h, #to_json
Constructor Details
#initialize(**kwargs) ⇒ Session
Returns a new instance of Session.
27 28 29 30 31 |
# File 'lib/simple_acp/models/session.rb', line 27 def initialize(**kwargs) super @id ||= Types.generate_uuid @history ||= [] end |
Instance Attribute Details
#history ⇒ Array<Message> (readonly)
Returns conversation history.
21 |
# File 'lib/simple_acp/models/session.rb', line 21 attribute :history, default: -> { [] } |
#id ⇒ String (readonly)
Returns unique session ID.
17 |
# File 'lib/simple_acp/models/session.rb', line 17 attribute :id, required: true |
#state ⇒ Object? (readonly)
Returns arbitrary state data.
25 |
# File 'lib/simple_acp/models/session.rb', line 25 attribute :state |
Class Method Details
.create ⇒ Session
Create a new session with a generated ID.
48 49 50 |
# File 'lib/simple_acp/models/session.rb', line 48 def self.create new(id: Types.generate_uuid) end |
.from_hash(hash) ⇒ Session?
Create from a hash (JSON deserialization).
37 38 39 40 41 42 43 |
# File 'lib/simple_acp/models/session.rb', line 37 def self.from_hash(hash) return nil if hash.nil? instance = allocate instance.send(:initialize_from_hash, hash) instance end |
Instance Method Details
#add_to_history(message) ⇒ self
Add a message to the conversation history.
56 57 58 59 |
# File 'lib/simple_acp/models/session.rb', line 56 def add_to_history() @history << (.is_a?(Message) ? : Message.from_hash()) self end |
#clear_history! ⇒ self
Clear all conversation history.
64 65 66 67 |
# File 'lib/simple_acp/models/session.rb', line 64 def clear_history! @history = [] self end |
#clear_state! ⇒ self
Clear the session state.
81 82 83 84 |
# File 'lib/simple_acp/models/session.rb', line 81 def clear_state! @state = nil self end |
#empty? ⇒ Boolean
Check if the session has no history or state.
96 97 98 |
# File 'lib/simple_acp/models/session.rb', line 96 def empty? @history.empty? && @state.nil? end |
#message_count ⇒ Integer
Get the number of messages in history.
89 90 91 |
# File 'lib/simple_acp/models/session.rb', line 89 def @history.length end |
#set_state(state_data) ⇒ self
Set the session state.
73 74 75 76 |
# File 'lib/simple_acp/models/session.rb', line 73 def set_state(state_data) @state = state_data self end |
#valid? ⇒ Boolean
Validate the session.
103 104 105 |
# File 'lib/simple_acp/models/session.rb', line 103 def valid? Types.valid_uuid?(@id) end |