Skip to content

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

TypeScript

feat(control-ui): send add hook to the org marketplace entries list

Andy Paviafa2538c05790
diff --git a/control-ui/components/HostGuardrailsSection/index.tsx b/control-ui/components/HostGuardrailsSection/index.tsx
index a85a67814..d501d73c8 100644
--- a/control-ui/components/HostGuardrailsSection/index.tsx
+++ b/control-ui/components/HostGuardrailsSection/index.tsx
@@ -1,43 +1,13 @@
'use client'
-import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
+import React, { useCallback, useMemo, useRef, useState } from 'react'
import { useRouter } from 'next/navigation'
import { useConfirmDialog } from '@components/ConfirmDialog'
-import { SelectionModal } from '@components/SelectionModal'
import { useToast } from '@components/Toast'
import { IconX } from '@components/icons'
-import { GUARDRAIL_ENTRY_TYPE } from '@constants/marketplaceEntryTypes'
import { CONTROL_ROUTES } from '@constants/routes'
-import { type LlmHookResource, getLlmHooks } from '@lib/api'
import { GUARDRAIL_PHASES, GUARDRAIL_PHASE_LABELS } from './constants'
-import type {
- GuardrailHookRef,
- GuardrailHookRow,
- GuardrailPhase,
- HostGuardrails,
- HostGuardrailsSectionProps,
-} from './types'
-
-// The picker only offers hooks already installed on the cluster, so keep the
-// route that installs new ones reachable from the section itself — it is the
-// path the old "Add hook" button used to take.
-const INSTALL_HOOK_ROUTE = CONTROL_ROUTES.marketplace.orgEntriesFiltered({
- type: GUARDRAIL_ENTRY_TYPE,
-})
-
-// The phases an installed hook declares it runs at. A hook is attached to every
-// phase it declares, so the operator picks the hook and never the phase.
-function hookPhases(hook: LlmHookResource): GuardrailPhase[] {
- const raw = (hook.spec as { lifecyclePoints?: unknown } | undefined)?.lifecyclePoints
- if (!Array.isArray(raw)) return []
- return raw.filter((point): point is GuardrailPhase =>
- GUARDRAIL_PHASES.includes(point as GuardrailPhase)
- )
-}
-
-function hookName(hook: LlmHookResource): string {
- return hook.metadata?.name ?? ''
-}
+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.
@@ -59,67 +29,28 @@ export function HostGuardrailsSection({
const { showToast } = useToast()
const mountedRef = useRef(true)
- const [installedHooks, setInstalledHooks] = useState<LlmHookResource[]>([])
- const [showAddHook, setShowAddHook] = useState(false)
- const [selectedHookNames, setSelectedHookNames] = useState<string[]>([])
const [saving, setSaving] = useState(false)
- useEffect(() => {
+ React.useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
- // The installed LlmHooks are the pool this agent can reference. A load
- // failure only empties the picker — the rows below come from the Host spec
- // and stay readable either way.
- useEffect(() => {
- void (async () => {
- try {
- const result = await getLlmHooks()
- if (!mountedRef.current) return
- setInstalledHooks(Array.isArray(result.items) ? result.items : [])
- } catch {
- if (mountedRef.current) setInstalledHooks([])
- }
- })()
- }, [])
-
const rows = useMemo<GuardrailHookRow[]>(() => {
const hooks = initialGuardrails?.hooks ?? {}
return GUARDRAIL_PHASES.flatMap(phase => (hooks[phase] ?? []).map(ref => ({ phase, ref })))
}, [initialGuardrails])
- const referencedIds = useMemo(() => new Set(rows.map(row => row.ref.id)), [rows])
-
- const addOptions = useMemo(
- () =>
- installedHooks
- .filter(hook => hookName(hook) && !referencedIds.has(hookName(hook)))
- .map(hook => {
- const phases = hookPhases(hook)
- return {
- value: hookName(hook),
- label: hookName(hook),
- description: phases.length
- ? phases.map(phase => GUARDRAIL_PHASE_LABELS[phase]).join(' · ')
- : 'No lifecycle points declared',
- }
- }),
- [installedHooks, referencedIds]
- )
-
const persist = useCallback(
async (nextHooks: HostGuardrails['hooks'], successMessage: string) => {
setSaving(true)
try {
await onSave(withHooks(initialGuardrails, nextHooks))
if (mountedRef.current) showToast(successMessage, { tone: 'success' })
- return true
} catch {
// The parent already surfaced the error/conflict banner.
- return false
} finally {
if (mountedRef.current) setSaving(false)
}
@@ -127,35 +58,6 @@ export function HostGuardrailsSection({
[initialGuardrails, onSave, showToast]
)
- async function addSelectedHooks() {
- const chosen = installedHooks.filter(hook => selectedHookNames.includes(hookName(hook)))
- const nextHooks: HostGuardrails['hooks'] = { ...(initialGuardrails?.hooks ?? {}) }
-
- for (const hook of chosen) {
- const id = hookName(hook)
- // Pin the digest the cluster actually reconciled, matching how existing
- // references are stored. An unpinned reference would float across hook
- // image updates.
- const ref: GuardrailHookRef = { id }
- const digest = hook.status?.observedDigest
- if (digest) ref.digest = digest
-
- for (const phase of hookPhases(hook)) {
- nextHooks[phase] = [...(nextHooks[phase] ?? []), ref]
- }
- }
-
- const added = chosen.length
- const ok = await persist(
- nextHooks,
- added === 1 ? `${hookName(chosen[0])} added to this agent.` : `${added} hooks added.`
- )
- if (ok && mountedRef.current) {
- setSelectedHookNames([])
- setShowAddHook(false)
- }
- }
-
async function removeHook(row: GuardrailHookRow) {
const shouldRemove = await confirm({
title: 'Remove guardrail hook',
@@ -181,17 +83,13 @@ export function HostGuardrailsSection({
<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.{' '}
- <a className="cu-link" href={INSTALL_HOOK_ROUTE}>
- Browse the Marketplace
- </a>{' '}
- to install more.
+ Guardrail hooks installed on the cluster and referenced by this agent.
</p>
{canWrite ? (
<button
type="button"
className="cu-btn cu-btn--primary cu-btn--sm"
- onClick={() => setShowAddHook(true)}
+ onClick={() => router.push(CONTROL_ROUTES.marketplace.orgEntries)}
disabled={disabled}
>
Add hook
@@ -253,29 +151,6 @@ export function HostGuardrailsSection({
</div>
</div>
- {showAddHook ? (
- <SelectionModal
- busy={disabled}
- emptyLabel="No installed guardrail hooks are available."
- id="agent-hook-picker"
- label="Hooks"
- onChange={setSelectedHookNames}
- onClose={() => {
- setSelectedHookNames([])
- setShowAddHook(false)
- }}
- onConfirm={addSelectedHooks}
- options={addOptions}
- placeholder="Select hooks"
- searchPlaceholder="Search hooks..."
- selectionLabel="Selected hooks"
- submitLabel={selectedHookNames.length > 1 ? 'Add hooks' : 'Add hook'}
- title="Add hook"
- titleId="add-hook-title"
- value={selectedHookNames}
- />
- ) : null}
-
{confirmDialog}
</section>
)
diff --git a/control-ui/components/__tests__/HostGuardrailsSection.test.tsx b/control-ui/components/__tests__/HostGuardrailsSection.test.tsx
index a743e7260..9202662c7 100644
--- a/control-ui/components/__tests__/HostGuardrailsSection.test.tsx
+++ b/control-ui/components/__tests__/HostGuardrailsSection.test.tsx
@@ -1,23 +1,11 @@
import React from 'react'
import type { ReactNode } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
-import {
- cleanup,
- fireEvent,
- render as rtlRender,
- screen,
- waitFor,
- within,
-} from '@testing-library/react'
-import * as api from '../../lib/api'
+import { cleanup, fireEvent, render as rtlRender, screen, waitFor } from '@testing-library/react'
import { HostGuardrailsSection } from '../HostGuardrailsSection'
import type { HostGuardrails } from '../HostGuardrailsSection/types'
import { ToastProvider } from '../Toast'
-vi.mock('../../lib/api', () => ({
- getLlmHooks: vi.fn(),
-}))
-
const confirmMock = vi.hoisted(() => vi.fn())
vi.mock('../ConfirmDialog', () => ({
useConfirmDialog: () => ({ confirm: confirmMock, confirmDialog: null }),
@@ -28,17 +16,6 @@ vi.mock('next/navigation', () => ({
useRouter: () => ({ push: navigation.push }),
}))
-const COMPACTOR = {
- metadata: { name: 'hook-token-compactor' },
- spec: { lifecyclePoints: ['preCall'] },
- status: { observedDigest: 'sha256:aaa' },
-}
-const RECORDER = {
- metadata: { name: 'hook-tool-use-recorder' },
- spec: { lifecyclePoints: ['postCallSuccess'] },
- status: { observedDigest: 'sha256:bbb' },
-}
-
// A hook already attached to the agent, plus fields this section must never
// touch on save.
const GUARDRAILS: HostGuardrails = {
@@ -55,7 +32,6 @@ afterEach(cleanup)
beforeEach(() => {
vi.clearAllMocks()
confirmMock.mockResolvedValue(true)
- vi.mocked(api.getLlmHooks).mockResolvedValue({ items: [COMPACTOR, RECORDER] } as never)
})
describe('HostGuardrailsSection', () => {
@@ -74,6 +50,21 @@ describe('HostGuardrailsSection', () => {
expect(screen.queryByText(/no guardrail hooks on this agent/i)).toBeNull()
})
+ it('Add hook goes to the org marketplace entries list', async () => {
+ render(
+ <HostGuardrailsSection
+ busy={false}
+ canWrite
+ initialGuardrails={GUARDRAILS}
+ onSave={vi.fn()}
+ />
+ )
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Add hook' }))
+
+ expect(navigation.push).toHaveBeenCalledWith('/marketplace/org/entries')
+ })
+
it('clicking a hook opens its guardrail detail page', async () => {
render(
<HostGuardrailsSection
@@ -110,35 +101,6 @@ describe('HostGuardrailsSection', () => {
expect(await screen.findByText(/no guardrail hooks on this agent yet/i)).toBeInTheDocument()
})
- it('Add hook offers only unreferenced hooks and saves with the declared phase and digest', async () => {
- const onSave = vi.fn().mockResolvedValue(undefined)
- render(
- <HostGuardrailsSection busy={false} canWrite initialGuardrails={GUARDRAILS} onSave={onSave} />
- )
-
- fireEvent.click(await screen.findByRole('button', { name: 'Add hook' }))
-
- // The already-attached hook must not be offered a second time.
- const option = await screen.findByRole('option', { name: 'hook-tool-use-recorder' })
- expect(screen.queryByRole('option', { name: 'hook-token-compactor' })).toBeNull()
-
- fireEvent.click(option)
- // Both the section header and the modal footer read "Add hook" — submit is
- // the one inside the dialog.
- fireEvent.click(within(screen.getByRole('dialog')).getByRole('button', { name: 'Add hook' }))
-
- await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1))
- const saved = onSave.mock.calls[0][0] as HostGuardrails
- expect(saved.hooks?.postCallSuccess).toEqual([
- { id: 'hook-tool-use-recorder', digest: 'sha256:bbb' },
- ])
- // The phase the operator never chose stays exactly as it was...
- expect(saved.hooks?.preCall).toEqual([{ id: 'hook-token-compactor', digest: 'sha256:aaa' }])
- // ...and so does everything this section does not edit.
- expect(saved.builtins).toEqual(GUARDRAILS.builtins)
- expect(saved.limits).toEqual(GUARDRAILS.limits)
- })
-
it('removing the last hook of a phase drops the phase key rather than leaving it empty', async () => {
const onSave = vi.fn().mockResolvedValue(undefined)
render(
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 285ef880b..314c50b2c 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.444",
+ "version": "0.1.445",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.444",
+ "version": "0.1.445",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-ui/package.json b/control-ui/package.json
index e63ae6c3c..b26a1c932 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.444",
+ "version": "0.1.445",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",