Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
3.1 KB
Raw Blame
import { HostOverview } from '../types.js'
import { ResourceService } from './resourceService.js'

export class HostOverviewService {
  constructor(
    private readonly resources: ResourceService,
    private readonly defaultNamespace: string
  ) {}

  async getHostOverview(hostName: string, namespace?: string): Promise<HostOverview> {
    const host = (await this.resources.getResource('hosts', hostName, namespace)) as {
      metadata?: { name?: string }
      spec?: { host?: string; contextRef?: string }
    }

    const allChannels = (await this.resources.listResource('communicationchannels', '*')) as Array<{
      spec?: {
        hostRef?: string
        telegram?: Array<{ userIds?: string[] }>
        email?: Array<{ emails?: string[] }>
        slack?: Array<{ userNames?: string[] }>
      }
    }>

    // A communication-channel's spec.hostRef is ALWAYS the host's RFC1123
    // metadata.name (see the channel wizard / CRD schema), never the display
    // name. Since PR #304 turned host.spec.host into an editable display label,
    // it must NOT be used to match channels: a display that collides with
    // another agent's metadata.name would surface that agent's channels — and
    // their recipients (telegram userIds / emails / slack userNames aggregated
    // below) — under the wrong agent. Match on the RFC1123 identifier only.
    const communicationChannels = allChannels.filter(ch => (ch.spec?.hostRef || '') === hostName)

    let context = null as unknown
    const contextRef = host.spec?.contextRef
    if (contextRef) {
      try {
        context = await this.resources.getResource('contexts', contextRef, this.defaultNamespace)
      } catch {
        const contexts = (await this.resources.listResource('contexts', '*')) as Array<{
          spec?: { contextId?: string }
        }>
        context = contexts.find(c => c.spec?.contextId === contextRef) || null
      }
    }

    const allMcpServers = (await this.resources.listResource('mcpservers', '*')) as Array<{
      metadata?: { name?: string }
    }>
    const allowed = new Set<string>(
      ((context as { spec?: { mcpServers?: string[] } } | null)?.spec?.mcpServers || []).filter(
        (v): v is string => Boolean(v)
      )
    )
    const mcpServers = allMcpServers.filter(s => allowed.has(s.metadata?.name || ''))

    const telegramUserIds = new Set<string>()
    const emails = new Set<string>()
    const slackUserNames = new Set<string>()

    for (const ch of communicationChannels) {
      for (const group of ch.spec?.telegram || []) {
        for (const id of group.userIds || []) telegramUserIds.add(id)
      }
      for (const group of ch.spec?.email || []) {
        for (const email of group.emails || []) emails.add(email)
      }
      for (const group of ch.spec?.slack || []) {
        for (const user of group.userNames || []) slackUserNames.add(user)
      }
    }

    return {
      host,
      context,
      communicationChannels,
      mcpServers,
      accessSummary: {
        telegramUserIds: [...telegramUserIds].sort(),
        emails: [...emails].sort(),
        slackUserNames: [...slackUserNames].sort(),
      },
    }
  }
}