Class: SimpleAcp::Models::Session

Inherits:
Base
  • Object
show all
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.

Examples:

Using sessions with the client

client.use_session("my-session")
client.run_sync(agent: "counter", input: "increment")  # Count: 1
client.run_sync(agent: "counter", input: "increment")  # Count: 2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Returns:

  • (Array<Message>) —

    conversation history



21
# File 'lib/simple_acp/models/session.rb', line 21

attribute :history, default: -> { [] }

#id ⇒ String (readonly)

Returns unique session ID.

Returns:

  • (String) —

    unique session ID



17
# File 'lib/simple_acp/models/session.rb', line 17

attribute :id, required: true

#state ⇒ Object? (readonly)

Returns arbitrary state data.

Returns:

  • (Object, nil) —

    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.

Returns:



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).

Parameters:

  • hash (Hash, nil) —

    session data

Returns:

  • (Session, nil) —

    the session or nil



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.

Parameters:

  • message (Message, Hash) —

    the message to add

Returns:

  • (self) —

    for chaining



56
57
58
59
# File 'lib/simple_acp/models/session.rb', line 56

def add_to_history(message)
  @history << (message.is_a?(Message) ? message : Message.from_hash(message))
  self
end

#clear_history! ⇒ self

Clear all conversation history.

Returns:

  • (self) —

    for chaining



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.

Returns:

  • (self) —

    for chaining



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.

Returns:

  • (Boolean) —

    true if no history and no 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.

Returns:

  • (Integer) —

    message count



89
90
91
# File 'lib/simple_acp/models/session.rb', line 89

def message_count
  @history.length
end

#set_state(state_data) ⇒ self

Set the session state.

Parameters:

  • state_data (Object) —

    arbitrary state data

Returns:

  • (self) —

    for chaining



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.

Returns:

  • (Boolean) —

    true if ID is a valid UUID



103
104
105
# File 'lib/simple_acp/models/session.rb', line 103

def valid?
  Types.valid_uuid?(@id)
end