Class: SimpleAcp::Server::Agent

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(manifest:, handler:) ⇒ Agent

Create a new agent.

Parameters:

  • manifest (Models::AgentManifest) —

    agent metadata

  • handler (#call) —

    callable that accepts a Context and returns messages



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.

Returns:

  • (#call) —

    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.

Returns:



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.

Parameters:

  • context (Context) —

    the execution context

Returns:



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.

Returns:

  • (String, nil) —

    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.

Returns:

  • (String) —

    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.

Returns:

  • (Boolean) —

    true if manifest is valid and handler is callable



71
72
73
# File 'lib/simple_acp/server/agent.rb', line 71

def valid?
  @manifest.valid? && @handler.respond_to?(:call)
end