Class: AI::Session
- Inherits:
-
Object
- Object
- AI::Session
- Defined in:
- lib/session.rb
Overview
── Session ──────────────────────────────────────────────────────────
Instance Attribute Summary collapse
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#stamp ⇒ Object
Returns the value of attribute stamp.
-
#title ⇒ Object
Returns the value of attribute title.
-
#usages ⇒ Object
Returns the value of attribute usages.
Class Method Summary collapse
Instance Method Summary collapse
- #add_turn(user_content, assistant_content, usage = nil, attachments: []) ⇒ Object
- #delete_files ⇒ Object
- #discard_last_turn ⇒ Object
- #empty? ⇒ Boolean
- #files ⇒ Object
- #history_file ⇒ Object
- #human_time ⇒ Object
-
#initialize ⇒ Session
constructor
A new instance of Session.
- #json_file ⇒ Object
- #md_file ⇒ Object
- #remove_turns(first, last) ⇒ Object
- #save(profile) ⇒ Object
- #turns ⇒ Object
Constructor Details
#initialize ⇒ Session
Returns a new instance of Session.
6 7 8 9 10 11 12 |
# File 'lib/session.rb', line 6 def initialize @stamp = Time.now.strftime("%Y%m%d-%H%M%S-%L") @title = nil @messages = [] @usages = [] @mutex = Mutex.new end |
Instance Attribute Details
#messages ⇒ Object
Returns the value of attribute messages.
4 5 6 |
# File 'lib/session.rb', line 4 def @messages end |
#stamp ⇒ Object
Returns the value of attribute stamp.
4 5 6 |
# File 'lib/session.rb', line 4 def stamp @stamp end |
#title ⇒ Object
Returns the value of attribute title.
4 5 6 |
# File 'lib/session.rb', line 4 def title @title end |
#usages ⇒ Object
Returns the value of attribute usages.
4 5 6 |
# File 'lib/session.rb', line 4 def usages @usages end |
Class Method Details
.all ⇒ Object
71 72 73 74 75 76 |
# File 'lib/session.rb', line 71 def self.all Dir[File.join(META_DIR, "*.json")] .reject { |f| File.basename(f) == "stats.json" } .map { |f| File.basename(f, ".json") } .sort end |
.load(stamp) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/session.rb', line 78 def self.load(stamp) data = JSON.parse(File.read(File.join(META_DIR, "#{stamp}.json"))) rescue (return nil) return nil unless data.is_a?(Hash) s = new; s.stamp = stamp; s.title = data["title"] if data["messages"].is_a?(Array) s. = data["messages"] s.usages = data["usages"] || data["usage_log"] || [] elsif data["branches"].is_a?(Array) first = data["branches"].first || {} s. = first["messages"] || [] s.usages = first["usages"] || [] else return nil end s end |
Instance Method Details
#add_turn(user_content, assistant_content, usage = nil, attachments: []) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/session.rb', line 17 def add_turn(user_content, assistant_content, usage = nil, attachments: []) user_msg = { "role" => "user", "content" => user_content } user_msg["attachments"] = unless .empty? @messages << user_msg @messages << { "role" => "assistant", "content" => assistant_content } @usages << usage if usage end |
#delete_files ⇒ Object
67 68 69 |
# File 'lib/session.rb', line 67 def delete_files files.each { |f| File.delete(f) if File.exist?(f) } end |
#discard_last_turn ⇒ Object
41 |
# File 'lib/session.rb', line 41 def discard_last_turn = remove_turns(turns, turns) |
#empty? ⇒ Boolean
15 |
# File 'lib/session.rb', line 15 def empty? = @messages.empty? |
#files ⇒ Object
52 |
# File 'lib/session.rb', line 52 def files = [json_file, md_file, history_file] |
#history_file ⇒ Object
49 |
# File 'lib/session.rb', line 49 def history_file = File.join(META_DIR, "#{@stamp}.readline") |
#human_time ⇒ Object
43 44 45 46 47 |
# File 'lib/session.rb', line 43 def human_time Time.strptime(@stamp, "%Y%m%d-%H%M%S-%L").strftime("%b %-d, %Y · %-I:%M %p") rescue StandardError @stamp end |
#json_file ⇒ Object
50 |
# File 'lib/session.rb', line 50 def json_file = File.join(META_DIR, "#{@stamp}.json") |
#md_file ⇒ Object
51 |
# File 'lib/session.rb', line 51 def md_file = File.join(DATA_DIR, "#{@stamp}.md") |
#remove_turns(first, last) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/session.rb', line 25 def remove_turns(first, last) total = turns return 0 if total.zero? || first > total || last < 1 first = first.clamp(1, total) last = last.clamp(1, total) return 0 if first > last count = last - first + 1 @messages.slice!((first - 1) * 2, count * 2) if @usages.length >= first take = [count, @usages.length - first + 1].min @usages.slice!(first - 1, take) end @title = nil if @messages.empty? count end |
#save(profile) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/session.rb', line 54 def save(profile) @mutex.synchronize do FileUtils.mkdir_p(META_DIR) File.write(json_file, JSON.pretty_generate( "timestamp" => @stamp, "title" => @title, "messages" => @messages, "usages" => @usages )) write_markdown(profile) end end |
#turns ⇒ Object
14 |
# File 'lib/session.rb', line 14 def turns = @messages.length / 2 |