UNPKG

@artinet/sdk

Version:
61 lines 1.66 kB
import { ServiceDispatcher } from "../types/services/dispatcher.js"; import { v4 as uuidv4 } from "uuid"; /** * @description The service manager class. */ export class ServiceManager extends ServiceDispatcher { /** * @description The agent card. * @type {AgentCard} */ card; /** * @description The constructor. * @param {ManagerOptions} params The service manager params. */ constructor(params) { super(params); this.card = params.card; } /** * @description Creates a request context. * @param {T} req The request. * @returns {T} The request context. */ createRequestContext(req) { if (!req.protocol || !req.method || !req.params) { throw new Error("Invalid request parameters"); } const context = { ...req, id: req.id ?? uuidv4(), protocol: req.protocol, method: req.method, params: req.params, }; return context; } /** * @description Gets the agent card. * @returns {AgentCard} The agent card. */ getCard() { return this.card; } /** * @description Gets a service by protocol. * @param {Protocol} protocol The protocol. * @returns {Service} The service. */ getService(protocol) { return this.services[protocol]; } /** * @description Stops the manager. * @returns {Promise<void>} The promise. */ async destroy() { await Promise.all(Object.values(this.services).map((service) => service.stop())); } } //# sourceMappingURL=manager.js.map