import { Injectable, Logger } from "@nestjs/common"; @Injectable() export class HermesClientService { private readonly logger = new Logger(HermesClientService.name); private readonly baseUrl: string; private readonly apiKey: string; constructor() { this.baseUrl = process.env.HERMES_GATEWAY_URL || "http://canina-bot:8642"; this.apiKey = process.env.HERMES_API_KEY || process.env.API_SERVER_KEY || ""; } async checkHealth(): Promise { try { const res = await fetch(`${this.baseUrl}/v1/health`, { headers: { Authorization: `Bearer ${this.apiKey}`, }, }); return res.status === 200; } catch (error: any) { this.logger.error(`Hermes health check failed: ${error.message}`); return false; } } async triggerEvent(eventName: string, payload: Record) { try { this.logger.log(`Dispatching event ${eventName} to Hermes Gateway`); const res = await fetch(`${this.baseUrl}/v1/chat/completions`, { method: "POST", headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: "hermes-agent", messages: [ { role: "user", content: `[EVENT:${eventName}] ${JSON.stringify(payload)}`, }, ], }), }); const data = await res.json(); return { success: true, data }; } catch (error: any) { this.logger.warn(`Could not dispatch event to Hermes: ${error.message}`); return { success: false, error: error.message }; } } }