Class: A2A::Middleware::FetchTask

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

Overview

Loads a task from the store and places it on ‘env`. Sets `env` 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.

Usage:

on "SendMessage" do
  use A2A::Middleware::FetchTask, store: sqlite_store, id_field: :task_id, from: :message
  respond_with -> (env) {
    existing = env["a2a.task"]  # nil if new task
  }
end

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/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/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