Class: Spree::AgentTools::Registry
- Inherits:
-
Object
- Object
- Spree::AgentTools::Registry
- Includes:
- Enumerable
- Defined in:
- lib/spree/agent_tools/registry.rb
Overview
The agent-tool registry. One per surface, the way the authentication strategies are split — Spree#admin_agent_tools for the back office, Spree.store_agent_tools for the storefront. Extensions append their own tool classes the same way they register tax or delivery-rate providers:
Spree.admin_agent_tools << MyApp::AgentTools::SyncSupplier
Registration is by class, so one registry serves every adapter on its surface: the MCP server and the dashboard assistant both read the admin one.
A tool also declares the surface it belongs to, so filing one in the wrong registry is refused rather than discovered when a storefront visitor is offered the back office. Ten admin tools carry no permission of their own — they are gated per resource instead — so the registry cannot be the only thing keeping them apart.
Writes are not registered as classes. Every Admin API action an admin key may call is already a tool, built from its OpenAPI operation by OperationCatalog — see docs/plans/6.0-agent-tools-from-openapi.md.
Instance Attribute Summary collapse
-
#audience ⇒ Symbol
readonly
The surface this registry serves.
- #exposed_workflows ⇒ Hash{Symbol => Hash} readonly deprecated Deprecated.
Instance Method Summary collapse
-
#<<(tool_class) ⇒ self
(also: #add)
Registered by name, resolved on use.
-
#available_for(context) ⇒ Array<Spree::AgentTool>
Tools this caller may actually use, instantiated for their context.
- #each ⇒ Object
-
#expose_workflows(**exposures) ⇒ self
deprecated
Deprecated.
Admin API operations are agent tools on their own, built from the OpenAPI document, so there is nothing to expose. A workflow behind a documented endpoint is that endpoint's tool; one without is not offered. Recorded and otherwise ignored, and removed in 7.0.
-
#find_for(name, context) ⇒ Spree::AgentTool?
A tool this caller may use, by name — including a 6.0.0 name that is no longer listed but still answers through 6.0.x, so a client that learned it keeps working.
-
#initialize(tool_names = [], audience: :admin) ⇒ Registry
constructor
A new instance of Registry.
- #to_a ⇒ Array<Class<Spree::AgentTool>>
Constructor Details
#initialize(tool_names = [], audience: :admin) ⇒ Registry
Returns a new instance of Registry.
30 31 32 33 34 |
# File 'lib/spree/agent_tools/registry.rb', line 30 def initialize(tool_names = [], audience: :admin) @tool_names = tool_names.map(&:to_s) @exposed_workflows = {} @audience = audience.to_sym end |
Instance Attribute Details
#audience ⇒ Symbol (readonly)
Returns the surface this registry serves.
28 29 30 |
# File 'lib/spree/agent_tools/registry.rb', line 28 def audience @audience end |
#exposed_workflows ⇒ Hash{Symbol => Hash} (readonly)
Returns what was passed to #expose_workflows.
85 86 87 |
# File 'lib/spree/agent_tools/registry.rb', line 85 def exposed_workflows @exposed_workflows end |
Instance Method Details
#<<(tool_class) ⇒ self Also known as: add
Registered by name, resolved on use.
Holding the class itself would pin the copy loaded at boot, so in
development every edit to a tool's description, params or permission
needed a restart to take effect — and tuning those descriptions is how
an agent's behaviour is shaped. Names survive reloading, which is the
same reason Spree.integrations stores strings.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/spree/agent_tools/registry.rb', line 46 def <<(tool_class) name = tool_class.to_s declared = tool_class.try(:audience) if declared && declared != audience raise ArgumentError, "#{name} belongs to the #{declared} surface and cannot be registered on the #{audience} one" end @tool_names << name unless @tool_names.include?(name) self end |
#available_for(context) ⇒ Array<Spree::AgentTool>
Tools this caller may actually use, instantiated for their context.
This is what an adapter offers the model — a caller without
write_products never learns that a price-changing tool exists.
119 120 121 122 123 124 |
# File 'lib/spree/agent_tools/registry.rb', line 119 def available_for(context) to_a. select { |tool_class| tool_class.try(:audience).nil? || tool_class.audience == audience }. map { |tool_class| tool_class.new(context) }. select(&:permitted?) end |
#each ⇒ Object
59 60 61 |
# File 'lib/spree/agent_tools/registry.rb', line 59 def each(&) to_a.each(&) end |
#expose_workflows(**exposures) ⇒ self
Admin API operations are agent tools on their own, built from the OpenAPI document, so there is nothing to expose. A workflow behind a documented endpoint is that endpoint's tool; one without is not offered. Recorded and otherwise ignored, and removed in 7.0.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/spree/agent_tools/registry.rb', line 71 def expose_workflows(**exposures) Spree::Deprecation.warn( 'Spree.admin_agent_tools.expose_workflows is deprecated and has no effect: every documented ' \ 'Admin API action is an agent tool, built from its OpenAPI operation. A workflow reaches agents ' \ 'through an admin endpoint whose rswag spec is generated into the openapi/admin.yaml your gem ships. ' \ 'See docs/plans/6.0-agent-tools-from-openapi.md.' ) @exposed_workflows.merge!(exposures.transform_keys(&:to_sym)) self end |
#find_for(name, context) ⇒ Spree::AgentTool?
A tool this caller may use, by name — including a 6.0.0 name that is no longer listed but still answers through 6.0.x, so a client that learned it keeps working.
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/spree/agent_tools/registry.rb', line 100 def find_for(name, context) name = name.to_s tool_class = registered_classes.find { |klass| klass.tool_name == name } if tool_class.nil? && operations? entry = Spree::AgentTools::OperationCatalog.find(name) tool_class = entry && Spree::AgentTools::OperationTool.for(entry) end return if tool_class.nil? || (tool_class.try(:audience) && tool_class.audience != audience) tool = tool_class.new(context) tool if tool.permitted? end |
#to_a ⇒ Array<Class<Spree::AgentTool>>
88 89 90 91 |
# File 'lib/spree/agent_tools/registry.rb', line 88 def to_a registered = registered_classes registered + operation_tool_classes(registered.map(&:tool_name)) end |