Class: Kodo::Tools::UpdateFact

Inherits:
RubyLLM::Tool
  • Object
show all
Extended by:
PromptContributor
Defined in:
lib/kodo/tools/update_fact.rb

Constant Summary collapse

MAX_PER_TURN =
5
MAX_CONTENT_LENGTH =
500

Instance Method Summary collapse

Methods included from PromptContributor

capability_name, capability_primary, disabled_guidance, enabled_guidance

Constructor Details

#initialize(knowledge:, audit:) ⇒ UpdateFact

Returns a new instance of UpdateFact.



21
22
23
24
25
26
# File 'lib/kodo/tools/update_fact.rb', line 21

def initialize(knowledge:, audit:)
  super()
  @knowledge = knowledge
  @audit = audit
  @turn_count = 0
end

Instance Method Details

#execute(id:, content:) ⇒ Object



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
66
# File 'lib/kodo/tools/update_fact.rb', line 32

def execute(id:, content:)
  if content.length > MAX_CONTENT_LENGTH
    return "Content too long (#{content.length} chars). Maximum is #{MAX_CONTENT_LENGTH}."
  end

  if Memory::Redactor.sensitive?(content)
    return "Cannot store sensitive data (passwords, API keys, SSNs, credit card numbers)."
  end

  @turn_count += 1
  if @turn_count > MAX_PER_TURN
    return "Rate limit reached (max #{MAX_PER_TURN} updates per message). Try again next message."
  end

  old_fact = @knowledge.all_active.find { |f| f["id"] == id }
  unless old_fact
    return "No active fact found with id: #{id}"
  end

  @knowledge.forget(id)
  new_fact = @knowledge.remember(
    category: old_fact["category"],
    content: content,
    source: old_fact["source"]
  )

  @audit.log(
    event: "knowledge_updated",
    detail: "old:#{id} new:#{new_fact['id']} cat:#{old_fact['category']}"
  )

  "Updated fact: #{content} (new id: #{new_fact['id']}, replaces: #{id})"
rescue Kodo::Error => e
  e.message
end

#name ⇒ Object



68
69
70
# File 'lib/kodo/tools/update_fact.rb', line 68

def name
  "update_fact"
end

#reset_turn_count! ⇒ Object



28
29
30
# File 'lib/kodo/tools/update_fact.rb', line 28

def reset_turn_count!
  @turn_count = 0
end