Class: A2A::Server::AgentExecution::SimpleAgentExecutor

Inherits:
AgentExecutor
  • Object
show all
Defined in:
lib/a2a/server/agent_execution/agent_executor.rb

Overview

Simple agent executor implementation

A basic implementation that can be used as a starting point for custom agents. Provides default behavior for task creation and status management.

Instance Method Summary collapse

Methods inherited from AgentExecutor

#publish_message, #publish_task, #publish_task_artifact_update, #publish_task_status_update

Constructor Details

#initialize(agent_card: nil, task_manager: nil) ⇒ SimpleAgentExecutor

Returns a new instance of SimpleAgentExecutor.



134
135
136
137
# File 'lib/a2a/server/agent_execution/agent_executor.rb', line 134

def initialize(agent_card: nil, task_manager: nil)
  @agent_card = agent_card
  @task_manager = task_manager
end

Instance Method Details

#cancel(context, event_queue) ⇒ Object

Cancel a task by updating its status

Parameters:

  • The request context

  • The event queue



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/a2a/server/agent_execution/agent_executor.rb', line 204

def cancel(context, event_queue)
  task_id = context.task_id
  return unless task_id

  canceled_status = A2A::Types::TaskStatus.new(
    state: A2A::Types::TASK_STATE_CANCELED,
    message: "Task canceled by request",
    updated_at: Time.now.utc.iso8601
  )

  publish_task_status_update(
    event_queue,
    task_id,
    context.context_id,
    canceled_status
  )
end

#ensure_task(context) ⇒ A2A::Types::Task (private)

Ensure a task exists for the request context

Parameters:

  • The request context

Returns:

  • The task object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/a2a/server/agent_execution/agent_executor.rb', line 246

def ensure_task(context)
  if context.task_id && @task_manager
    # Try to get existing task
    existing_task = begin
      @task_manager.get_task(context.task_id)
    rescue StandardError
      nil
    end
    return existing_task if existing_task
  end

  # Create new task
  task_id = context.task_id || SecureRandom.uuid
  context_id = context.context_id || SecureRandom.uuid

  A2A::Types::Task.new(
    id: task_id,
    context_id: context_id,
    status: A2A::Types::TaskStatus.new(
      state: A2A::Types::,
      message: "Task created",
      updated_at: Time.now.utc.iso8601
    ),
    history: context.message ? [context.message] : [],
    metadata: {
      created_at: Time.now.utc.iso8601,
      executor: self.class.name
    }
  )
end

#execute(context, event_queue) ⇒ Object

Execute a request by creating a task and processing the message

Parameters:

  • The request context

  • The event queue



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/a2a/server/agent_execution/agent_executor.rb', line 144

def execute(context, event_queue)
  # Create or get the task
  task = ensure_task(context)
  publish_task(event_queue, task)

  # Update task to working state
  working_status = A2A::Types::TaskStatus.new(
    state: A2A::Types::TASK_STATE_WORKING,
    message: "Processing request",
    updated_at: Time.now.utc.iso8601
  )

  publish_task_status_update(
    event_queue,
    task.id,
    task.context_id,
    working_status
  )

  # Process the message (delegate to subclass)
  result = process_message(context.message, task, context)

  # Update task with result
  completed_status = A2A::Types::TaskStatus.new(
    state: A2A::Types::TASK_STATE_COMPLETED,
    result: result,
    updated_at: Time.now.utc.iso8601
  )

  publish_task_status_update(
    event_queue,
    task.id,
    task.context_id,
    completed_status
  )
rescue StandardError => e
  # Handle errors by updating task status
  if task
    error_status = A2A::Types::TaskStatus.new(
      state: A2A::Types::TASK_STATE_FAILED,
      error: { message: e.message, type: e.class.name },
      updated_at: Time.now.utc.iso8601
    )

    publish_task_status_update(
      event_queue,
      task.id,
      task.context_id,
      error_status
    )
  end

  raise
end

#process_message(message, _task, _context) ⇒ Object (protected)

Process a message (to be implemented by subclasses)

Parameters:

  • The message to process

  • The associated task

  • The request context

Returns:

  • Processing result



231
232
233
234
235
236
237
# File 'lib/a2a/server/agent_execution/agent_executor.rb', line 231

def process_message(message, _task, _context)
  # Default implementation just echoes the message
  {
    echo: message.to_h,
    processed_at: Time.now.utc.iso8601
  }
end