A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
registry.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
15#ifndef A11_ACTIONS_REGISTRY_H_
16#define A11_ACTIONS_REGISTRY_H_
17
18#include <memory>
19#include <string>
20#include <string_view>
21#include <vector>
22
23#include <absl/base/thread_annotations.h>
24#include <absl/container/flat_hash_map.h>
25#include <absl/status/status.h>
26#include <absl/status/statusor.h>
27
28#include "a11/actions/action.h"
29#include "a11/actions/schema.h"
30#include "a11/data/types.h"
31#include "thread/boost_primitives.h"
32
33namespace a11::actions {
34
43class ActionRegistry : public std::enable_shared_from_this<ActionRegistry> {
44 public:
52 absl::Status Register(std::string action_name, ActionSchema schema,
53 ActionHandler handler = {});
59 absl::Status RegisterSync(std::string action_name, ActionSchema schema,
60 SyncActionHandler handler);
62 absl::Status Unregister(std::string_view action_name);
64 [[nodiscard]] bool IsRegistered(std::string_view action_name) const;
66 absl::StatusOr<ActionSchema> GetSchema(std::string_view action_name) const;
68 absl::StatusOr<ActionHandler> GetHandler(std::string_view action_name) const;
79 absl::StatusOr<std::shared_ptr<Action>> MakeAction(
80 std::string_view action_name, std::string action_id = {},
81 std::shared_ptr<nodes::NodeMap> node_map = nullptr,
82 std::shared_ptr<net::WireStream> stream = nullptr,
83 std::shared_ptr<service::Session> session = nullptr);
89 absl::StatusOr<data::ActionMessage> MakeActionMessage(
90 std::string_view action_name, std::string action_id = {});
92 [[nodiscard]] std::vector<std::string> ListRegisteredActions() const;
97 std::shared_ptr<ActionRegistry> Copy(bool clear_autofills = true) const;
98
99 private:
100 mutable thread::Mutex mu_;
101 absl::flat_hash_map<std::string, ActionSchema> schemas_ ABSL_GUARDED_BY(mu_);
102 absl::flat_hash_map<std::string, ActionHandler> handlers_
103 ABSL_GUARDED_BY(mu_);
104};
105
106} // namespace a11::actions
107
108#endif // A11_ACTIONS_REGISTRY_H_
A11's unit of work: the Action and its supporting types.
A thread-safe catalogue mapping action names to schema and handler.
Definition registry.h:43
absl::StatusOr< data::ActionMessage > MakeActionMessage(std::string_view action_name, std::string action_id={})
Builds the wire a11::data::ActionMessage for a registered action.
Definition registry.cc:120
std::vector< std::string > ListRegisteredActions() const
Returns the names of all registered actions.
Definition registry.cc:127
absl::Status Unregister(std::string_view action_name)
Removes the registration for action_name.
Definition registry.cc:55
absl::Status RegisterSync(std::string action_name, ActionSchema schema, SyncActionHandler handler)
Registers an action with a synchronous handler.
Definition registry.cc:45
absl::StatusOr< std::shared_ptr< Action > > MakeAction(std::string_view action_name, std::string action_id={}, std::shared_ptr< nodes::NodeMap > node_map=nullptr, std::shared_ptr< net::WireStream > stream=nullptr, std::shared_ptr< service::Session > session=nullptr)
Builds a runnable Action for a registered action.
Definition registry.cc:101
std::shared_ptr< ActionRegistry > Copy(bool clear_autofills=true) const
Returns a copy of this registry.
Definition registry.cc:138
absl::Status Register(std::string action_name, ActionSchema schema, ActionHandler handler={})
Registers an action under action_name.
Definition registry.cc:23
bool IsRegistered(std::string_view action_name) const
Whether action_name is registered.
Definition registry.cc:67
absl::StatusOr< ActionSchema > GetSchema(std::string_view action_name) const
Returns the schema for action_name, or NotFound.
Definition registry.cc:74
absl::StatusOr< ActionHandler > GetHandler(std::string_view action_name) const
Returns the handler for action_name, or NotFound.
Definition registry.cc:86
Definition action.cc:46
std::function< absl::Status(std::shared_ptr< Action >)> SyncActionHandler
Synchronous action handler returning a completion status.
Definition action.h:70
std::function< a11::Task(std::shared_ptr< Action >)> ActionHandler
Asynchronous action handler: runs the action, returns an awaitable.
Definition action.h:68
Schemas describing an action's typed interface and its settings.
The full typed interface of an action.
Definition schema.h:107
A11's core wire value types: chunks, node fragments and messages.