Class: Llmemory::MCP::Tools::MemoryTimeline

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/llmemory/mcp/tools/memory_timeline.rb

Class Method Summary collapse

Class Method Details

.call(user_id:, hours: nil, include_messages: nil, server_context: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/llmemory/mcp/tools/memory_timeline.rb', line 19

def call(user_id:, hours: nil, include_messages: nil, server_context: nil)
  hours = hours || 24
  include_msgs = include_messages.nil? ? true : include_messages

  timeline = []

  # Get recent items from long-term memory
  if graph_based?
    append_graph_timeline(timeline, user_id, hours)
  else
    append_file_timeline(timeline, user_id, hours)
  end

  # Get recent messages from short-term
  if include_msgs
    store = build_short_term_store
    sessions = store.list_sessions(user_id: user_id)
    sessions.each do |session_id|
      state = store.load(user_id, session_id)
      next unless state.is_a?(Hash)
      messages = state[:messages] || state["messages"] || []
      messages.each_with_index do |m, idx|
        timeline << {
          type: "message",
          timestamp: state[:updated_at] || Time.now,
          session_id: session_id,
          role: m[:role] || m["role"],
          content: m[:content] || m["content"],
          order: idx
        }
      end
    end
  end

  # Sort by timestamp (most recent first)
  timeline.sort_by! { |t| t[:timestamp].to_s }.reverse!

  ::MCP::Tool::Response.new([{
    type: "text",
    text: format_timeline(timeline, hours)
  }])
rescue => e
  ::MCP::Tool::Response.new([{
    type: "text",
    text: "Error getting timeline: #{e.message}"
  }], error: true)
end