Class: SimpleAcp::Server::Agent
- Inherits:
-
Object
- Object
- SimpleAcp::Server::Agent
- Defined in:
- lib/simple_acp/server/agent.rb
Overview
Wrapper for registered agent handlers.
Encapsulates an agent's manifest (metadata) and handler (execution logic), normalizing different handler return types into a consistent format.
Instance Attribute Summary collapse
-
#handler ⇒ #call
readonly
The handler that processes requests.
-
#manifest ⇒ Models::AgentManifest
readonly
The agent's metadata.
Instance Method Summary collapse
-
#call(context) ⇒ Enumerable<RunYield, RunYieldAwait>
Execute the agent handler with the given context.
-
#description ⇒ String?
The agent's description.
-
#initialize(manifest:, handler:) ⇒ Agent
constructor
Create a new agent.
-
#name ⇒ String
The agent's name.
-
#valid? ⇒ Boolean
Check if the agent is valid for registration.
Constructor Details
#initialize(manifest:, handler:) ⇒ Agent
Create a new agent.
20 21 22 23 |
# File 'lib/simple_acp/server/agent.rb', line 20 def initialize(manifest:, handler:) @manifest = manifest @handler = handler end |
Instance Attribute Details
#handler ⇒ #call (readonly)
Returns the handler that processes requests.
14 15 16 |
# File 'lib/simple_acp/server/agent.rb', line 14 def handler @handler end |
#manifest ⇒ Models::AgentManifest (readonly)
Returns the agent's metadata.
11 12 13 |
# File 'lib/simple_acp/server/agent.rb', line 11 def manifest @manifest end |
Instance Method Details
#call(context) ⇒ Enumerable<RunYield, RunYieldAwait>
Execute the agent handler with the given context.
Normalizes various return types (Message, String, Array, Enumerator) into a consistent enumerable of RunYield or RunYieldAwait objects.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/simple_acp/server/agent.rb', line 42 def call(context) result = @handler.call(context) # Handle different return types case result when Enumerator, Enumerator::Lazy # Generator-style handler that yields messages result when RunYield, RunYieldAwait # Already wrapped [result] when Models::Message # Single message return [RunYield.new(result)] when Array # Array of messages or yields result.map { |m| wrap_result_item(m) } when String # Simple string response [RunYield.new(Models::Message.agent(result))] else # Try to convert to enumerator result.respond_to?(:each) ? result.each : [wrap_result_item(result)] end end |
#description ⇒ String?
Returns the agent's description.
31 32 33 |
# File 'lib/simple_acp/server/agent.rb', line 31 def description @manifest.description end |
#name ⇒ String
Returns the agent's name.
26 27 28 |
# File 'lib/simple_acp/server/agent.rb', line 26 def name @manifest.name end |
#valid? ⇒ Boolean
Check if the agent is valid for registration.
71 72 73 |
# File 'lib/simple_acp/server/agent.rb', line 71 def valid? @manifest.valid? && @handler.respond_to?(:call) end |