Class: Kward::SessionStore::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store:, id:, path:, cwd:, created_at:, name: nil, parent_id: nil, parent_path: nil, leaf_id: nil) ⇒ Session

Returns a new instance of Session.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kward/session_store.rb', line 24

def initialize(store:, id:, path:, cwd:, created_at:, name: nil, parent_id: nil, parent_path: nil, leaf_id: nil)
  @store = store
  @id = id
  @path = path
  @cwd = cwd
  @created_at = created_at
  @name = name
  @parent_id = parent_id
  @parent_path = parent_path
  @leaf_id = leaf_id
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



21
22
23
# File 'lib/kward/session_store.rb', line 21

def created_at
  @created_at
end

#cwdObject (readonly)

Returns the value of attribute cwd.



21
22
23
# File 'lib/kward/session_store.rb', line 21

def cwd
  @cwd
end

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/kward/session_store.rb', line 21

def id
  @id
end

#leaf_idObject

Returns the value of attribute leaf_id.



22
23
24
# File 'lib/kward/session_store.rb', line 22

def leaf_id
  @leaf_id
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/kward/session_store.rb', line 22

def name
  @name
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



21
22
23
# File 'lib/kward/session_store.rb', line 21

def parent_id
  @parent_id
end

#parent_pathObject (readonly)

Returns the value of attribute parent_path.



21
22
23
# File 'lib/kward/session_store.rb', line 21

def parent_path
  @parent_path
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/kward/session_store.rb', line 21

def path
  @path
end

Instance Method Details

#append_branch_summary(parent_id, from_id:, summary:, details: {}) ⇒ Object



90
91
92
93
94
95
# File 'lib/kward/session_store.rb', line 90

def append_branch_summary(parent_id, from_id:, summary:, details: {})
  record = @store.build_tree_record(@path, "branch_summary", parent_id, fromId: from_id, summary: summary, details: details || {})
  @leaf_id = record[:id]
  @store.append_record(@path, record)
  record[:id]
end

#append_label_change(entry_id, label) ⇒ Object



86
87
88
# File 'lib/kward/session_store.rb', line 86

def append_label_change(entry_id, label)
  @store.append_label_change(@path, entry_id, label)
end

#append_message(message) ⇒ Object



43
44
45
46
47
# File 'lib/kward/session_store.rb', line 43

def append_message(message)
  record = @store.build_tree_record(@path, "message", @leaf_id, message: message)
  @leaf_id = record[:id]
  @store.append_record(@path, record)
end

#append_tool_execution(tool_call, content) ⇒ Object



55
56
57
# File 'lib/kward/session_store.rb', line 55

def append_tool_execution(tool_call, content)
  @store.append_record(@path, RPC::ToolEventNormalizer.new(tool_call, content: content).execution_record)
end

#attach(conversation) ⇒ Object



36
37
38
39
40
41
# File 'lib/kward/session_store.rb', line 36

def attach(conversation)
  conversation.on_append = lambda { |message| append_message(message) }
  conversation.on_compact = lambda { |message| compact(message) }
  conversation.on_tool_execution = lambda { |tool_call, content| append_tool_execution(tool_call, content) }
  self
end

#branch(entry_id) ⇒ Object



77
78
79
80
# File 'lib/kward/session_store.rb', line 77

def branch(entry_id)
  @leaf_id = entry_id.to_s.empty? ? nil : entry_id.to_s
  @store.append_leaf_change(@path, @leaf_id)
end

#compact(message) ⇒ Object



49
50
51
52
53
# File 'lib/kward/session_store.rb', line 49

def compact(message)
  record = @store.build_tree_record(@path, "compaction", @leaf_id, message: message)
  @leaf_id = record[:id]
  @store.append_record(@path, record)
end

#delete_if_unusedObject



107
108
109
# File 'lib/kward/session_store.rb', line 107

def delete_if_unused
  @store.delete_unused_session(self)
end

#rename(name) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/kward/session_store.rb', line 68

def rename(name)
  @name = name.to_s.strip.empty? ? nil : name.to_s.strip
  @store.append_record(@path, {
    type: "session_info",
    timestamp: Time.now.utc.iso8601(3),
    name: @name
  })
end

#reset_leafObject



82
83
84
# File 'lib/kward/session_store.rb', line 82

def reset_leaf
  branch(nil)
end

#update_memory_state(session_memories:, last_retrieval: nil) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/kward/session_store.rb', line 59

def update_memory_state(session_memories:, last_retrieval: nil)
  @store.append_record(@path, {
    type: "memory_state",
    timestamp: Time.now.utc.iso8601(3),
    sessionMemories: Array(session_memories),
    lastRetrieval: last_retrieval
  })
end

#update_runtime(model:, reasoning_effort:) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/kward/session_store.rb', line 97

def update_runtime(model:, reasoning_effort:)
  @store.append_record(@path, {
    type: "session_info",
    timestamp: Time.now.utc.iso8601(3),
    name: @name,
    model: model.to_s,
    reasoningEffort: reasoning_effort.to_s
  }.delete_if { |_key, value| value.to_s.empty? })
end