Class: SmartPrompt::Session
- Inherits:
-
Object
- Object
- SmartPrompt::Session
- Defined in:
- lib/smart_prompt/session.rb
Overview
Session represents an isolated conversation session with its own message history
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
-
#add_message(message_data) ⇒ Object
Add a message to the session.
-
#clear(preserve_system: true) ⇒ Object
Clear all messages except system messages if preserve_system is true.
-
#get_importance_score(message_index) ⇒ Object
Get importance score for a message at given index.
-
#get_messages(count = nil) ⇒ Object
Get messages from the session.
-
#initialize(id, config = {}) ⇒ Session
constructor
A new instance of Session.
-
#message_count ⇒ Object
Get the number of messages in the session.
-
#to_h ⇒ Object
Convert session to hash format for serialization.
-
#total_tokens ⇒ Object
Calculate total token count for all messages.
Constructor Details
#initialize(id, config = {}) ⇒ Session
Returns a new instance of Session.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/smart_prompt/session.rb', line 8 def initialize(id, config = {}) @id = id @messages = [] @metadata = {} @config = config @token_cache = {} @importance_scores = {} @created_at = Time.now @updated_at = Time.now @token_counter = TokenCounter.new end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/smart_prompt/session.rb', line 6 def config @config end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
6 7 8 |
# File 'lib/smart_prompt/session.rb', line 6 def created_at @created_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/smart_prompt/session.rb', line 6 def id @id end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
6 7 8 |
# File 'lib/smart_prompt/session.rb', line 6 def @messages end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
6 7 8 |
# File 'lib/smart_prompt/session.rb', line 6 def @metadata end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
6 7 8 |
# File 'lib/smart_prompt/session.rb', line 6 def updated_at @updated_at end |
Instance Method Details
#add_message(message_data) ⇒ Object
Add a message to the session
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/smart_prompt/session.rb', line 21 def () = .is_a?(Message) ? : Message.new() # Calculate token count for the message .calculate_tokens(@token_counter) @messages << @updated_at = Time.now enforce_limits end |
#clear(preserve_system: true) ⇒ Object
Clear all messages except system messages if preserve_system is true
49 50 51 52 53 54 55 56 |
# File 'lib/smart_prompt/session.rb', line 49 def clear(preserve_system: true) if preserve_system @messages = @messages.select(&:system_message?) else @messages = [] end @updated_at = Time.now end |
#get_importance_score(message_index) ⇒ Object
Get importance score for a message at given index
59 60 61 |
# File 'lib/smart_prompt/session.rb', line 59 def get_importance_score() @importance_scores[] ||= calculate_importance() end |
#get_messages(count = nil) ⇒ Object
Get messages from the session
34 35 36 |
# File 'lib/smart_prompt/session.rb', line 34 def (count = nil) count ? @messages.last(count) : @messages end |
#message_count ⇒ Object
Get the number of messages in the session
44 45 46 |
# File 'lib/smart_prompt/session.rb', line 44 def @messages.length end |
#to_h ⇒ Object
Convert session to hash format for serialization
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/smart_prompt/session.rb', line 64 def to_h { id: @id, messages: @messages.map(&:to_h), metadata: @metadata, created_at: @created_at.iso8601, updated_at: @updated_at.iso8601, config: @config } end |
#total_tokens ⇒ Object
Calculate total token count for all messages
39 40 41 |
# File 'lib/smart_prompt/session.rb', line 39 def total_tokens @messages.sum { |msg| msg.token_count || 0 } end |