import { PersonaProtocol, PersonaProtocolBaseConfig, PersonaConfig } from '../types'; import type { PersonaLogger } from '../logging'; import { PersonaRESTProtocol, PersonaRESTProtocolConfig, PersonaTransactionProtocol, PersonaWebRTCProtocol, PersonaWebRTCProtocolConfig, PersonaWebSocketProtocol, PersonaWebSocketProtocolConfig, } from '../protocol'; type ProtocolsConfig = PersonaConfig['protocols']; /** * Initializes protocols based on configuration */ export function initializeProtocols( _protocols: ProtocolsConfig, config: { dev: boolean; baseUrl?: string; apiKey: string; agentId: string; userId?: string; tools?: PersonaConfig['tools']; logger?: PersonaLogger; }, ): PersonaProtocol[] { if (Array.isArray(_protocols)) { return _protocols; } if (typeof _protocols === 'object' && _protocols !== null) { const baseEndpoint = config.dev ? 'localhost:8000' : config.baseUrl || 'persona.applica.guru/api'; const baseEndpointProtocol = config.dev ? 'http' : 'https'; const baseWebSocketProtocol = config.dev ? 'ws' : 'wss'; const availableProtocols = Object.keys(_protocols) .map((key) => { switch (key) { case 'rest': return createRESTProtocol(_protocols[key], { baseEndpointProtocol, baseEndpoint, apiKey: config.apiKey, agentId: config.agentId, userId: config.userId, logger: config.logger, }); case 'webrtc': return createWebRTCProtocol(_protocols[key], { baseWebSocketProtocol, baseEndpoint, apiKey: config.apiKey, agentId: config.agentId, userId: config.userId, logger: config.logger, }); case 'websocket': return createWebSocketProtocol(_protocols[key], { baseWebSocketProtocol, baseEndpoint, apiKey: config.apiKey, agentId: config.agentId, userId: config.userId, logger: config.logger, }); default: throw new Error(`Unknown protocol: ${key}`); } }) .filter((protocol) => protocol !== null) as PersonaProtocol[]; if (config.tools) { availableProtocols.push( new PersonaTransactionProtocol({ apiUrl: `${baseEndpointProtocol}://${baseEndpoint}`, apiKey: config.apiKey, agentId: config.agentId, tools: config.tools, logger: config.logger, }), ); } return availableProtocols; } throw new Error('Invalid protocols configuration'); } function createRESTProtocol( config: PersonaProtocolBaseConfig | boolean | undefined, baseConfig: { baseEndpointProtocol: string; baseEndpoint: string; apiKey: string; agentId: string; userId?: string; logger?: PersonaLogger; }, ): PersonaRESTProtocol | null { if (config === true) { return new PersonaRESTProtocol({ apiUrl: `${baseConfig.baseEndpointProtocol}://${baseConfig.baseEndpoint}`, apiKey: baseConfig.apiKey, agentId: baseConfig.agentId, userId: baseConfig.userId, logger: baseConfig.logger, }); } else if (typeof config === 'object' && config !== null) { return new PersonaRESTProtocol(config as PersonaRESTProtocolConfig); } return null; } function createWebRTCProtocol( config: PersonaProtocolBaseConfig | boolean | undefined, baseConfig: { baseWebSocketProtocol: string; baseEndpoint: string; apiKey: string; agentId: string; userId?: string; logger?: PersonaLogger; }, ): PersonaWebRTCProtocol | null { if (config === true) { return new PersonaWebRTCProtocol({ webrtcUrl: `${baseConfig.baseWebSocketProtocol}://${baseConfig.baseEndpoint}/webrtc`, apiKey: baseConfig.apiKey, agentId: baseConfig.agentId, userId: baseConfig.userId, logger: baseConfig.logger, }); } else if (typeof config === 'object' && config !== null) { return new PersonaWebRTCProtocol(config as PersonaWebRTCProtocolConfig); } return null; } function createWebSocketProtocol( config: PersonaProtocolBaseConfig | boolean | undefined, baseConfig: { baseWebSocketProtocol: string; baseEndpoint: string; apiKey: string; agentId: string; userId?: string; logger?: PersonaLogger; }, ): PersonaWebSocketProtocol | null { if (config === true) { return new PersonaWebSocketProtocol({ webSocketUrl: `${baseConfig.baseWebSocketProtocol}://${baseConfig.baseEndpoint}/websocket`, apiKey: baseConfig.apiKey, agentId: baseConfig.agentId, userId: baseConfig.userId, logger: baseConfig.logger, }); } else if (typeof config === 'object' && config !== null) { return new PersonaWebSocketProtocol(config as PersonaWebSocketProtocolConfig); } return null; }