Class: OllamaAgent::Memory::ShortTerm
- Inherits:
-
Object
- Object
- OllamaAgent::Memory::ShortTerm
- Defined in:
- lib/ollama_agent/memory/short_term.rb
Overview
In-memory sliding window of recent tool calls and observations. Cleared at the end of each run. Never persisted.
Defined Under Namespace
Classes: Entry
Constant Summary collapse
- DEFAULT_MAX_ENTRIES =
20
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
-
#by_type(type) ⇒ Object
Return all entries of a given type.
- #clear! ⇒ Object
-
#initialize(max: DEFAULT_MAX_ENTRIES) ⇒ ShortTerm
constructor
A new instance of ShortTerm.
-
#recent(n = 5) ⇒ Object
Return the N most recent entries.
-
#recent_tool_calls(n = 5) ⇒ Object
Last N tool calls (name + args).
-
#recently_called?(tool_name, args, window: 6) ⇒ Boolean
True if the same tool+args was called recently (loop hint).
-
#record(type, content) ⇒ Object
Record a new entry.
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(max: DEFAULT_MAX_ENTRIES) ⇒ ShortTerm
Returns a new instance of ShortTerm.
14 15 16 17 |
# File 'lib/ollama_agent/memory/short_term.rb', line 14 def initialize(max: DEFAULT_MAX_ENTRIES) @max = max @entries = [] end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
12 13 14 |
# File 'lib/ollama_agent/memory/short_term.rb', line 12 def entries @entries end |
Instance Method Details
#by_type(type) ⇒ Object
Return all entries of a given type.
34 35 36 |
# File 'lib/ollama_agent/memory/short_term.rb', line 34 def by_type(type) @entries.select { |e| e.type == type.to_sym } end |
#clear! ⇒ Object
56 57 58 59 |
# File 'lib/ollama_agent/memory/short_term.rb', line 56 def clear! @entries.clear nil end |
#recent(n = 5) ⇒ Object
Return the N most recent entries.
29 30 31 |
# File 'lib/ollama_agent/memory/short_term.rb', line 29 def recent(n = 5) @entries.last(n) end |
#recent_tool_calls(n = 5) ⇒ Object
Last N tool calls (name + args)
39 40 41 |
# File 'lib/ollama_agent/memory/short_term.rb', line 39 def recent_tool_calls(n = 5) by_type(:tool_call).last(n) end |
#recently_called?(tool_name, args, window: 6) ⇒ Boolean
True if the same tool+args was called recently (loop hint).
44 45 46 47 48 49 50 |
# File 'lib/ollama_agent/memory/short_term.rb', line 44 def recently_called?(tool_name, args, window: 6) recent(window).any? do |e| e.type == :tool_call && e.content[:tool] == tool_name.to_s && e.content[:args].to_s == args.to_s end end |
#record(type, content) ⇒ Object
Record a new entry.
22 23 24 25 26 |
# File 'lib/ollama_agent/memory/short_term.rb', line 22 def record(type, content) @entries << Entry.new(type: type.to_sym, content: content, ts: Time.now.to_f) @entries.shift if @entries.size > @max nil end |
#size ⇒ Object
52 53 54 |
# File 'lib/ollama_agent/memory/short_term.rb', line 52 def size @entries.size end |
#to_a ⇒ Object
61 62 63 |
# File 'lib/ollama_agent/memory/short_term.rb', line 61 def to_a @entries.map(&:to_h) end |