Class: A2A::Server::Middleware::FetchTask

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/server/middleware/fetch_task.rb

Overview

Loads a task from the store and places it on env["a2a.task"]. Sets env["a2a.task"] to nil when the ID is absent or the task does not exist. Does not raise.

The task ID is read from the request — by default from request.id. Use id_field: to read from a different field, and from: to read from a nested object on the request.

Not part of the default A2A::Server stack (it needs a store, and the ID field varies per operation) — wrap your agent with it:

agent = A2A::Server::Middleware::FetchTask.new(
A2A.agent { |env| existing = env["a2a.task"] }, # nil if new task
store: sqlite_store, id_field: :task_id, from: :message
)

Instance Method Summary collapse

Constructor Details

#initialize(app, store:, id_field: :id, from: nil) ⇒ FetchTask

Returns a new instance of FetchTask.



26
27
28
29
30
31
# File 'lib/a2a/server/middleware/fetch_task.rb', line 26

def initialize(app, store:, id_field: :id, from: nil)
  @app      = app
  @store    = store
  @id_field = id_field
  @from     = from
end

Instance Method Details

#call(env) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/a2a/server/middleware/fetch_task.rb', line 33

def call(env)
  request = env["a2a.request"]
  source  = @from ? request.public_send(@from) : request
  id      = source.public_send(@id_field)

  env["a2a.task"] = id.to_s.empty? ? nil : @store.get(id)

  @app.call(env)
end