Class: A2A::Middleware::LimitHistoryLength

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

Overview

Resolves the effective history length limit and sets ‘env` to an integer.

The server max is required. The effective limit is the minimum of the client-requested value and the server cap. When the client doesn’t specify a history_length, the server cap is used.

env` is always an Integer (0..max). The handler applies it unconditionally:

result["history"] = task[:history]&.last(limit)

Usage:

on "GetTask" do
  use A2A::Middleware::FetchTaskOrRaise, store: sqlite_store
  use A2A::Middleware::LimitHistoryLength, 20
  respond_with -> (env) {
    task  = env["a2a.task"]
    limit = env["a2a.history_length"]
    # ...
    result["history"] = task[:history]&.last(limit)
  }
end

Instance Method Summary collapse

Constructor Details

#initialize(app, max) ⇒ LimitHistoryLength

Returns a new instance of LimitHistoryLength.



34
35
36
37
# File 'lib/a2a/middleware/limit_history_length.rb', line 34

def initialize(app, max)
  @app = app
  @max = max
end

Instance Method Details

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/a2a/middleware/limit_history_length.rb', line 39

def call(env)
  request = env["a2a.request"]
  limit = @max

  if request.respond_to?(:history_length) && request.history_length
    limit = [request.history_length.to_i, @max].min
  end

  env["a2a.history_length"] = limit

  @app.call(env)
end