Class: Ask::Agent::Session
- Inherits:
-
Object
- Object
- Ask::Agent::Session
- Defined in:
- lib/ask/agent/session.rb
Instance Attribute Summary collapse
-
#chat ⇒ Object
readonly
Returns the value of attribute chat.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#meta_agent_results ⇒ Object
readonly
Returns the value of attribute meta_agent_results.
-
#tool_calls_made ⇒ Object
readonly
Returns the value of attribute tool_calls_made.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
-
#turn_count ⇒ Object
readonly
Returns the value of attribute turn_count.
Class Method Summary collapse
Instance Method Summary collapse
- #abort ⇒ Object
- #abort_requested? ⇒ Boolean
- #delete ⇒ Object
- #deleted? ⇒ Boolean
- #emit(event) ⇒ Object
-
#initialize(model:, tools: [], max_turns: 25, max_tool_retries: 3, compactor: nil, hooks: {}, persistence: nil, id: nil, system_prompt: nil, parallel_tools: true, reflector: nil, telemetry: true, meta_agent: nil, **chat_options) ⇒ Session
constructor
A new instance of Session.
- #on(type, &block) ⇒ Object
- #on_event(&block) ⇒ Object
- #reflection_count ⇒ Object
- #reset_messages! ⇒ Object
- #run(message, tools: nil) ⇒ Object
- #running? ⇒ Boolean
- #save ⇒ Object
Constructor Details
#initialize(model:, tools: [], max_turns: 25, max_tool_retries: 3, compactor: nil, hooks: {}, persistence: nil, id: nil, system_prompt: nil, parallel_tools: true, reflector: nil, telemetry: true, meta_agent: nil, **chat_options) ⇒ Session
Returns a new instance of Session.
18 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 |
# File 'lib/ask/agent/session.rb', line 18 def initialize(model:, tools: [], max_turns: 25, max_tool_retries: 3, compactor: nil, hooks: {}, persistence: nil, id: nil, system_prompt: nil, parallel_tools: true, reflector: nil, telemetry: true, meta_agent: nil, **) @id = id || SecureRandom.uuid @max_turns = max_turns @max_tool_retries = max_tool_retries @parallel_tools = parallel_tools @event_handlers = { all: [] } @running = false @deleted = false @abort_requested = false @turn_count = 0 @created_at = Time.now @_no_tools_instructed = false @telemetry = telemetry.is_a?(Telemetry) ? telemetry : Telemetry.new(enabled: !!telemetry) @chat = build_chat(model, system_prompt, tools, **) @tools = resolve_tools(tools) register_tools_on_chat @loop = Loop.new(max_turns: max_turns) @tool_executor = ToolExecutor.new(max_retries: max_tool_retries, parallel: parallel_tools) @compactor = compactor ? build_compactor(compactor) : nil @hooks = Hooks.new(hooks) @persistence = persistence reflector_opts = reflector.is_a?(Hash) ? reflector : {} @reflector = if reflector Reflector.new( model: @chat, max_reflections: reflector_opts[:max_reflections] || 1 ) end @meta_agent_config = @meta_agent_results = nil @compactor&.chat = @chat end |
Instance Attribute Details
#chat ⇒ Object (readonly)
Returns the value of attribute chat.
9 10 11 |
# File 'lib/ask/agent/session.rb', line 9 def chat @chat end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
9 10 11 |
# File 'lib/ask/agent/session.rb', line 9 def created_at @created_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/ask/agent/session.rb', line 9 def id @id end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
9 10 11 |
# File 'lib/ask/agent/session.rb', line 9 def @messages end |
#meta_agent_results ⇒ Object (readonly)
Returns the value of attribute meta_agent_results.
16 17 18 |
# File 'lib/ask/agent/session.rb', line 16 def @meta_agent_results end |
#tool_calls_made ⇒ Object (readonly)
Returns the value of attribute tool_calls_made.
10 11 12 |
# File 'lib/ask/agent/session.rb', line 10 def tool_calls_made @tool_calls_made end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
9 10 11 |
# File 'lib/ask/agent/session.rb', line 9 def tools @tools end |
#turn_count ⇒ Object (readonly)
Returns the value of attribute turn_count.
9 10 11 |
# File 'lib/ask/agent/session.rb', line 9 def turn_count @turn_count end |
Class Method Details
.handle_tool_calls(response) ⇒ Object
216 217 218 219 |
# File 'lib/ask/agent/session.rb', line 216 def @chat.handle_tool_calls(response, &) @on[:end_message]&.call(response) if @on[:end_message] response end |
.load(id, adapter:) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/ask/agent/session.rb', line 173 def self.load(id, adapter:) data = adapter.load(id) return nil unless data session = new( id: data[:id], model: data.dig(:metadata, :model), tools: data.dig(:metadata, :tools)&.map(&:constantize) || [], persistence: adapter ) data[:messages].each do |msg| session.chat.( role: msg[:role].to_sym, content: msg[:content], tool_call_id: msg[:tool_call_id] ) end session end |
Instance Method Details
#abort ⇒ Object
200 201 202 |
# File 'lib/ask/agent/session.rb', line 200 def abort @abort_requested = true end |
#abort_requested? ⇒ Boolean
204 |
# File 'lib/ask/agent/session.rb', line 204 def abort_requested? = @abort_requested |
#delete ⇒ Object
195 196 197 198 |
# File 'lib/ask/agent/session.rb', line 195 def delete @deleted = true @persistence&.delete(@id) end |
#deleted? ⇒ Boolean
167 |
# File 'lib/ask/agent/session.rb', line 167 def deleted? = @deleted |
#emit(event) ⇒ Object
160 161 162 163 164 |
# File 'lib/ask/agent/session.rb', line 160 def emit(event) @event_handlers[:all].each { |h| h.call(event) } handlers = @event_handlers[event.class] handlers&.each { |h| h.call(event) } end |
#on(type, &block) ⇒ Object
154 155 156 157 158 |
# File 'lib/ask/agent/session.rb', line 154 def on(type, &block) @event_handlers[type] ||= [] @event_handlers[type] << block self end |
#on_event(&block) ⇒ Object
149 150 151 152 |
# File 'lib/ask/agent/session.rb', line 149 def on_event(&block) @event_handlers[:all] << block self end |
#reflection_count ⇒ Object
12 13 14 |
# File 'lib/ask/agent/session.rb', line 12 def reflection_count @reflector&.reflection_count || 0 end |
#reset_messages! ⇒ Object
206 207 208 209 |
# File 'lib/ask/agent/session.rb', line 206 def @chat. @messages = [] end |
#run(message, tools: nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ask/agent/session.rb', line 59 def run(, tools: nil) raise "Session deleted" if @deleted raise "Session already running" if @running @running = true @abort_requested = false @turn_count = 0 @loop.reset! emit(Events::SessionStart.new) active_tools = resolve_tools(tools || []) active_tools = @tools if active_tools.empty? if active_tools.empty? && !@_no_tools_instructed @chat.(role: :system, content: "You have no tools available. Do not claim you can look up information or use tools of any kind. Just respond based on your existing knowledge.") @_no_tools_instructed = true end begin @tool_executor.telemetry = @telemetry response = @loop.run_turn( chat: @chat, message: , tools: active_tools, tool_executor: @tool_executor, compactor: @compactor, hooks: @hooks, event_emitter: self, session_id: @id ) rescue MaxTurnsExceeded => e emit(Events::MaxTurnsExceeded.new(max_turns: @max_turns)) @telemetry.log(:max_turns_exceeded, session_id: @id, max_turns: @max_turns) response = last_content rescue LoopDetected => e emit(Events::LoopDetected.new(tool_name: e., repeated_count: 3)) @telemetry.log(:loop_detected, session_id: @id, tool_name: e., repeated_count: 3) response = last_content rescue RubyLLM::ContextLengthExceededError if @compactor && !@compactor.overflow_recovered? @compactor.recover_from_overflow retry end response = "I'm sorry, the conversation has grown too long. Please start a new session." rescue StandardError => e emit(Events::Error.new(error: e., recoverable: true)) raise ensure @running = false persist! if @persistence end @tool_calls_made = @tool_executor.total_executions if @reflector && @reflector.reflect?(@tool_calls_made) && !@abort_requested eval_result = @reflector.evaluate(response: response, event_emitter: self) @telemetry.log(:reflection_end, session_id: @id, decision: eval_result[:decision], feedback: eval_result[:feedback]) if eval_result[:decision] == :improve && !@abort_requested @chat.( role: :system, content: "Improve your last response: #{eval_result[:feedback]}" ) response = @loop.run_turn( chat: @chat, message: "", tools: active_tools, tool_executor: @tool_executor, compactor: @compactor, hooks: @hooks, event_emitter: self, session_id: @id ) end end if @meta_agent_config @telemetry.increment_session_count! end emit(Events::SessionEnd.new(result: response, turn_count: @turn_count, tool_calls_made: @tool_calls_made)) @messages = @chat..dup response end |
#running? ⇒ Boolean
166 |
# File 'lib/ask/agent/session.rb', line 166 def running? = @running |
#save ⇒ Object
169 170 171 |
# File 'lib/ask/agent/session.rb', line 169 def save persist! if @persistence end |