Class: AI::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/session.rb

Overview

── Session ──────────────────────────────────────────────────────────

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

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

#messagesObject

Returns the value of attribute messages.



4
5
6
# File 'lib/session.rb', line 4

def messages
  @messages
end

#stampObject

Returns the value of attribute stamp.



4
5
6
# File 'lib/session.rb', line 4

def stamp
  @stamp
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/session.rb', line 4

def title
  @title
end

#usagesObject

Returns the value of attribute usages.



4
5
6
# File 'lib/session.rb', line 4

def usages
  @usages
end

Class Method Details

.allObject



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.messages = data["messages"]
    s.usages   = data["usages"] || data["usage_log"] || []
  elsif data["branches"].is_a?(Array)
    first = data["branches"].first || {}
    s.messages = 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"] = attachments unless attachments.empty?
  @messages << user_msg
  @messages << { "role" => "assistant", "content" => assistant_content }
  @usages   << usage if usage
end

#delete_filesObject



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_turnObject



41
# File 'lib/session.rb', line 41

def discard_last_turn = remove_turns(turns, turns)

#empty?Boolean

Returns:

  • (Boolean)


15
# File 'lib/session.rb', line 15

def empty? = @messages.empty?

#filesObject



52
# File 'lib/session.rb', line 52

def files        = [json_file, md_file, history_file]

#history_fileObject



49
# File 'lib/session.rb', line 49

def history_file = File.join(META_DIR, "#{@stamp}.readline")

#human_timeObject



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_fileObject



50
# File 'lib/session.rb', line 50

def json_file    = File.join(META_DIR, "#{@stamp}.json")

#md_fileObject



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

#turnsObject



14
# File 'lib/session.rb', line 14

def turns  = @messages.length / 2