Class: Kodo::Memory::Knowledge

Inherits:
Object
  • Object
show all
Defined in:
lib/kodo/memory/knowledge.rb

Constant Summary collapse

MAX_FACTS =
500
MAX_PROMPT_CHARS =
5_000
VALID_CATEGORIES =
%w[preference fact instruction context].freeze

Instance Method Summary collapse

Constructor Details

#initialize(passphrase: nil) ⇒ Knowledge

Returns a new instance of Knowledge.



14
15
16
17
18
19
# File 'lib/kodo/memory/knowledge.rb', line 14

def initialize(passphrase: nil)
  @knowledge_dir = File.join(Kodo.home_dir, "memory", "knowledge")
  @passphrase = passphrase
  FileUtils.mkdir_p(@knowledge_dir)
  @facts = load_facts
end

Instance Method Details

#all_active ⇒ Object



57
58
59
# File 'lib/kodo/memory/knowledge.rb', line 57

def all_active
  @facts.select { |f| f["active"] }
end

#count ⇒ Object



76
77
78
# File 'lib/kodo/memory/knowledge.rb', line 76

def count
  all_active.length
end

#for_prompt ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kodo/memory/knowledge.rb', line 80

def for_prompt
  active = all_active
  return nil if active.empty?

  grouped = active.group_by { |f| f["category"] }
  lines = ["## What You Know About the User\n"]

  VALID_CATEGORIES.each do |cat|
    facts = grouped[cat]
    next unless facts&.any?

    lines << "### #{cat.capitalize}s"
    facts.each { |f| lines << "- #{f['content']}" }
    lines << ""
  end

  result = lines.join("\n")
  if result.length > MAX_PROMPT_CHARS
    result = result[0...MAX_PROMPT_CHARS] + "\n\n_[Knowledge truncated]_"
  end
  result
end

#forget(id) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/kodo/memory/knowledge.rb', line 47

def forget(id)
  fact = @facts.find { |f| f["id"] == id && f["active"] }
  return nil unless fact

  fact["active"] = false
  fact["updated_at"] = Time.now.iso8601
  save_facts
  fact
end

#recall(query: nil, category: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kodo/memory/knowledge.rb', line 61

def recall(query: nil, category: nil)
  results = all_active

  if category
    results = results.select { |f| f["category"] == category }
  end

  if query
    pattern = query.downcase
    results = results.select { |f| f["content"].downcase.include?(pattern) }
  end

  results
end

#remember(category:, content:, source: "explicit") ⇒ Object



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
# File 'lib/kodo/memory/knowledge.rb', line 21

def remember(category:, content:, source: "explicit")
  unless VALID_CATEGORIES.include?(category)
    raise Kodo::Error, "Invalid category: #{category}. Must be one of: #{VALID_CATEGORIES.join(', ')}"
  end

  if count >= MAX_FACTS
    raise Kodo::Error, "Knowledge store is full (#{MAX_FACTS} facts). Forget some facts first."
  end

  now = Time.now.iso8601
  fact = {
    "id" => SecureRandom.uuid,
    "category" => category,
    "content" => content,
    "source" => source,
    "created_at" => now,
    "updated_at" => now,
    "supersedes" => nil,
    "active" => true
  }

  @facts << fact
  save_facts
  fact
end