Class: Google::ADK::InvocationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/google/adk/context.rb

Overview

Full invocation context for agent execution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session:, agent:, invocation_id:, session_service:, artifact_service: nil, memory_service: nil, context_cache_config: nil, run_config: nil) ⇒ InvocationContext

Initialize invocation context

Parameters:

  • session (Object)

    Current session

  • agent (BaseAgent)

    Current agent

  • invocation_id (String)

    Unique invocation ID

  • session_service (Object)

    Session service

  • artifact_service (Object) (defaults to: nil)

    Artifact service (optional)

  • memory_service (Object) (defaults to: nil)

    Memory service (optional)

  • context_cache_config (ContextCacheConfig) (defaults to: nil)

    Cache config (optional)

  • run_config (RunConfig) (defaults to: nil)

    Run configuration (optional)



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/google/adk/context.rb', line 156

def initialize(session:, agent:, invocation_id:, session_service:,
               artifact_service: nil, memory_service: nil,
               context_cache_config: nil, run_config: nil)
  @session = session
  @agent = agent
  @invocation_id = invocation_id
  @session_service = session_service
  @artifact_service = artifact_service
  @memory_service = memory_service
  @agent_states = {}
  @context_cache_config = context_cache_config || ContextCacheConfig.new
  @run_config = run_config || RunConfig.new
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def agent
  @agent
end

#agent_statesObject (readonly)

Returns the value of attribute agent_states.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def agent_states
  @agent_states
end

#artifact_serviceObject (readonly)

Returns the value of attribute artifact_service.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def artifact_service
  @artifact_service
end

#context_cache_configObject (readonly)

Returns the value of attribute context_cache_config.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def context_cache_config
  @context_cache_config
end

#invocation_idObject (readonly)

Returns the value of attribute invocation_id.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def invocation_id
  @invocation_id
end

#memory_serviceObject (readonly)

Returns the value of attribute memory_service.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def memory_service
  @memory_service
end

#run_configObject (readonly)

Returns the value of attribute run_config.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def run_config
  @run_config
end

#sessionObject (readonly)

Returns the value of attribute session.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def session
  @session
end

#session_serviceObject (readonly)

Returns the value of attribute session_service.



142
143
144
# File 'lib/google/adk/context.rb', line 142

def session_service
  @session_service
end

Instance Method Details

#add_event(event) ⇒ Object

Add event to session

Parameters:

  • event (Event)

    Event to add



203
204
205
# File 'lib/google/adk/context.rb', line 203

def add_event(event)
  @session.events << event
end

#eventsArray<Event>

Get conversation history

Returns:

  • (Array<Event>)

    Event history



210
211
212
# File 'lib/google/adk/context.rb', line 210

def events
  @session.events
end

#get_agent_state(agent_name) ⇒ Hash

Get agent state

Parameters:

  • agent_name (String)

    Agent name

Returns:

  • (Hash)

    Agent state or empty hash



174
175
176
# File 'lib/google/adk/context.rb', line 174

def get_agent_state(agent_name)
  @agent_states[agent_name] || {}
end

#stateHash

Get current session state

Returns:

  • (Hash)

    Session state



189
190
191
# File 'lib/google/adk/context.rb', line 189

def state
  @session.state
end

#to_callback_contextCallbackContext

Create callback context

Returns:



217
218
219
220
221
222
223
# File 'lib/google/adk/context.rb', line 217

def to_callback_context
  CallbackContext.new(
    invocation_id: @invocation_id,
    agent_name: @agent.name,
    session: @session
  )
end

#to_tool_context(auth_service: nil) ⇒ ToolContext

Create tool context

Parameters:

  • auth_service (Object) (defaults to: nil)

    Auth service (optional)

Returns:



229
230
231
232
233
234
235
236
237
238
# File 'lib/google/adk/context.rb', line 229

def to_tool_context(auth_service: nil)
  ToolContext.new(
    invocation_id: @invocation_id,
    agent_name: @agent.name,
    session: @session,
    auth_service: auth_service,
    artifact_service: @artifact_service,
    memory_service: @memory_service
  )
end

#update_agent_state(agent_name, state) ⇒ Object

Update agent state

Parameters:

  • agent_name (String)

    Agent name

  • state (Hash)

    New state



182
183
184
# File 'lib/google/adk/context.rb', line 182

def update_agent_state(agent_name, state)
  @agent_states[agent_name] = state
end

#update_state(updates) ⇒ Object

Update session state

Parameters:

  • updates (Hash)

    State updates



196
197
198
# File 'lib/google/adk/context.rb', line 196

def update_state(updates)
  @session.state.merge!(updates)
end