Skip to content

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

TypeScript
5.8 KB
Raw Blame
'use client'

import React, { useCallback, useMemo, useRef, useState } from 'react'
import { useRouter } from 'next/navigation'
import { DataTable, TableViewport } from '@clerum/frontend-components'
import { useConfirmDialog } from '@components/ConfirmDialog'
import { RowActionsMenu } from '@components/RowActionsMenu'
import { useToast } from '@components/Toast'
import { CONTROL_ROUTES } from '@constants/routes'
import { GUARDRAIL_MARKETPLACE_ROUTE, GUARDRAIL_PHASES, GUARDRAIL_PHASE_LABELS } from './constants'
import type { GuardrailHookRow, HostGuardrails, HostGuardrailsSectionProps } from './types'

// Every field this section does not edit rides along untouched — dropping
// `builtins` or `limits` here would silently wipe them from the Host spec.
function withHooks(
  source: HostGuardrails | undefined,
  hooks: HostGuardrails['hooks']
): HostGuardrails {
  return { ...source, hooks }
}

export function HostGuardrailsSection({
  initialGuardrails,
  onSave,
  busy,
  canWrite,
  showAddAction = true,
}: HostGuardrailsSectionProps) {
  const router = useRouter()
  const { confirm, confirmDialog } = useConfirmDialog()
  const { showToast } = useToast()
  const mountedRef = useRef(true)

  const [saving, setSaving] = useState(false)

  React.useEffect(() => {
    mountedRef.current = true
    return () => {
      mountedRef.current = false
    }
  }, [])

  const rows = useMemo<GuardrailHookRow[]>(() => {
    const hooks = initialGuardrails?.hooks ?? {}
    return GUARDRAIL_PHASES.flatMap(phase => (hooks[phase] ?? []).map(ref => ({ phase, ref })))
  }, [initialGuardrails])

  const persist = useCallback(
    async (nextHooks: HostGuardrails['hooks'], successMessage: string) => {
      setSaving(true)
      try {
        await onSave(withHooks(initialGuardrails, nextHooks))
        if (mountedRef.current) showToast(successMessage, { tone: 'success' })
      } catch {
        // The parent already surfaced the error/conflict banner.
      } finally {
        if (mountedRef.current) setSaving(false)
      }
    },
    [initialGuardrails, onSave, showToast]
  )

  async function removeHook(row: GuardrailHookRow) {
    const shouldRemove = await confirm({
      title: 'Remove guardrail hook',
      message: `Remove ${row.ref.id} from ${GUARDRAIL_PHASE_LABELS[row.phase]} on this agent? The hook stays installed on the cluster.`,
      confirmLabel: 'Remove',
      tone: 'danger',
    })
    if (!shouldRemove) return

    const currentRefs = initialGuardrails?.hooks?.[row.phase] ?? []
    const nextRefs = currentRefs.filter(ref => ref.id !== row.ref.id)
    const nextHooks: HostGuardrails['hooks'] = { ...(initialGuardrails?.hooks ?? {}) }
    if (nextRefs.length > 0) nextHooks[row.phase] = nextRefs
    else delete nextHooks[row.phase]

    await persist(nextHooks, `${row.ref.id} removed from this agent.`)
  }

  const disabled = busy || saving

  return (
    <section aria-label="Hooks">
      <div className="cu-access-section">
        <div className="cu-access-section__header">
          <p className="cu-muted cu-access-section__description">
            Guardrail hooks installed on the cluster and referenced by this agent.
          </p>
          {canWrite && showAddAction ? (
            <button
              type="button"
              className="cu-btn cu-btn--primary cu-btn--sm"
              onClick={() => router.push(GUARDRAIL_MARKETPLACE_ROUTE)}
              disabled={disabled}
            >
              Add hook
            </button>
          ) : null}
        </div>

        <TableViewport className="cu-table-wrap">
          <DataTable className="eft-table cu-table cu-table--header-band">
            <thead>
              <tr>
                <th>Hook</th>
                <th>Phase</th>
                <th className="cu-table__col-actions">Actions</th>
              </tr>
            </thead>
            <tbody>
              {rows.length === 0 ? (
                <tr>
                  <td colSpan={3} className="cu-empty">
                    No guardrail hooks on this agent yet.
                  </td>
                </tr>
              ) : (
                rows.map(row => (
                  <tr key={`${row.phase}:${row.ref.id}`}>
                    <td>
                      <button
                        type="button"
                        className="cu-link"
                        title={row.ref.digest ? `digest ${row.ref.digest}` : undefined}
                        onClick={() => router.push(CONTROL_ROUTES.guardrails.detail(row.ref.id))}
                      >
                        {row.ref.id}
                      </button>
                    </td>
                    <td>{GUARDRAIL_PHASE_LABELS[row.phase]}</td>
                    <td className="cu-table__cell-actions">
                      {canWrite ? (
                        <RowActionsMenu
                          ariaLabel={`Actions for ${row.ref.id}`}
                          actions={[
                            {
                              key: 'view',
                              label: 'View details',
                              onClick: () =>
                                router.push(CONTROL_ROUTES.guardrails.detail(row.ref.id)),
                            },
                            {
                              key: 'remove',
                              label: `Remove from ${GUARDRAIL_PHASE_LABELS[row.phase]}`,
                              onClick: () => void removeHook(row),
                              disabled,
                              danger: true,
                            },
                          ]}
                        />
                      ) : null}
                    </td>
                  </tr>
                ))
              )}
            </tbody>
          </DataTable>
        </TableViewport>
      </div>

      {confirmDialog}
    </section>
  )
}