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) ⇒ Session

Returns a new instance of Session.



22
23
24
25
26
27
28
29
30
31
# File 'lib/kward/session_store.rb', line 22

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

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



19
20
21
# File 'lib/kward/session_store.rb', line 19

def created_at
  @created_at
end

#cwdObject (readonly)

Returns the value of attribute cwd.



19
20
21
# File 'lib/kward/session_store.rb', line 19

def cwd
  @cwd
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/kward/session_store.rb', line 19

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



19
20
21
# File 'lib/kward/session_store.rb', line 19

def parent_id
  @parent_id
end

#parent_pathObject (readonly)

Returns the value of attribute parent_path.



19
20
21
# File 'lib/kward/session_store.rb', line 19

def parent_path
  @parent_path
end

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/kward/session_store.rb', line 19

def path
  @path
end

Instance Method Details

#append_message(message) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/kward/session_store.rb', line 40

def append_message(message)
  @store.append_record(@path, {
    type: "message",
    timestamp: Time.now.utc.iso8601(3),
    message: message
  })
end

#append_tool_execution(tool_call, content) ⇒ Object



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

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

#attach(conversation) ⇒ Object



33
34
35
36
37
38
# File 'lib/kward/session_store.rb', line 33

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

#compact(message) ⇒ Object



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

def compact(message)
  @store.append_record(@path, {
    type: "compaction",
    timestamp: Time.now.utc.iso8601(3),
    message: message
  })
end

#delete_if_unusedObject



88
89
90
# File 'lib/kward/session_store.rb', line 88

def delete_if_unused
  @store.delete_unused_session(self)
end

#rename(name) ⇒ Object



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

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

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



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

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



78
79
80
81
82
83
84
85
86
# File 'lib/kward/session_store.rb', line 78

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