Skip to content

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

TypeScript
48.4 KB
Raw Blame
'use client'

import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useParams, useRouter } from 'next/navigation'
import { AuthGate } from '@components/AuthGate'
import { FormSectionsSkeleton } from '@components/BodyLoadingSkeleton'
import { ChannelCredentialsPanel } from '@components/ChannelCredentialsPanel'
import type { CredentialEditStates, CredentialKey } from '@components/ChannelCredentialsPanel/types'
import { CommunicationChannelAccessSelector } from '@components/CommunicationChannelAccessSelector'
import { CommunicationChannelConversationsTable } from '@components/CommunicationChannelConversations'
import type { CommunicationChannelConversation } from '@components/CommunicationChannelConversations/types'
import { useConfirmDialog } from '@components/ConfirmDialog'
import { CreateFlowPanel } from '@components/CreateFlowPanel'
import { CreatePageHeader } from '@components/CreatePageHeader'
import { DashboardLayout } from '@components/DashboardLayout'
import { SegmentedControl } from '@components/SegmentedControl'
import { SelectionDropdown } from '@components/SelectionDropdown'
import { IconBroadcast } from '@components/Sidebar/icons'
import { TeamsSetupPrerequisites } from '@components/TeamsSetupPrerequisites'
import { useToast } from '@components/Toast'
import { IconCopy } from '@components/icons'
import { Button } from '@components/ui'
import { CONTROL_ROUTES } from '@constants/routes'
import { apiGet, apiSend, isSilentApiError } from '@lib/api'
import {
  buildCredentialOperationPlan,
  cleanCredentialStates,
  credentialStateIsDirty,
} from '@lib/channelCredentialSave'
import type { ChannelType } from '@lib/channelTypes'
import { copyTextToClipboard } from '@lib/clipboard'
import {
  type CommunicationChannelDraftState,
  buildCommunicationChannelSpec,
  communicationChannelInitialTab,
  createCommunicationChannelDraft,
  hasSlackConfigForRequestUrl,
  hasTeamsConfigForRequestUrl,
} from '@lib/communicationChannelEdit'
import {
  COMMUNICATION_CHANNEL_PROVIDERS,
  COMMUNICATION_CHANNEL_PROVIDER_OPTIONS,
  type CommunicationChannelProvider,
  communicationChannelProviderServiceLabel,
} from '@lib/communicationChannelProviders'
import {
  type CommunicationChannelItem,
  slackWebhookUrlForChannelName,
  teamsWebhookUrlForChannelName,
} from '@lib/communicationChannels'
import { canGenerateSlackAppManifest, slackAppManifest } from '@lib/slackAppManifest'
import {
  LOCAL_TEAMS_ENDPOINT_ORIGIN,
  TEAMS_APP_NAME_MAX_LENGTH,
  buildTeamsAppCreateCommand,
  canGenerateTeamsCommand,
  teamsAppNameError,
  teamsPlaceholderEndpoint,
} from '@lib/teamsSetup'

type ChannelProvider = CommunicationChannelProvider
type DraftState = CommunicationChannelDraftState

const DEFAULT_SLACK_APP_NAME = 'Evenfire'

type HostItem = {
  metadata?: { name?: string; namespace?: string }
}

type HostsResponse = {
  hosts?: HostItem[]
  items?: HostItem[]
}

function extractChannel(response: unknown, name: string): CommunicationChannelItem | null {
  if (!response || typeof response !== 'object') return null
  const direct = response as CommunicationChannelItem
  if (direct.metadata || direct.spec) return direct
  const wrapped = response as {
    channel?: CommunicationChannelItem
    item?: CommunicationChannelItem
  }
  if (wrapped.item) return wrapped.item
  if (wrapped.channel) return wrapped.channel
  const list = response as { items?: CommunicationChannelItem[] }
  return list.items?.find(item => item.metadata?.name === name) ?? null
}

/** Detail shown under the panel's read-failed banner when the request itself
 *  succeeded but the body did not carry a usable key list. */
const CREDENTIALS_SHAPE_ERROR = 'The credentials response did not list the stored keys.'
/** Same, when the request threw something that is not an Error. */
const CREDENTIALS_REQUEST_ERROR = 'The credentials request failed.'

/**
 * Key names from `GET .../credentials`, or `undefined` when the answer is not
 * usable. Undefined is never "nothing stored": callers turn it into the panel's
 * read-failed state, which says so and offers a retry.
 */
function extractCredentialKeys(response: unknown): string[] | undefined {
  if (!response || typeof response !== 'object') return undefined
  const keys = (response as { keys?: unknown }).keys
  if (!Array.isArray(keys)) return undefined
  return keys.filter((key): key is string => typeof key === 'string')
}

function conversationsForProvider(
  provider: ChannelProvider,
  draft: DraftState
): CommunicationChannelConversation[] {
  const conversations =
    provider === 'telegram' ? draft.telegram : provider === 'slack' ? draft.slack : draft.teams
  return conversations.map(group => ({
    ...group,
    provider,
  }))
}

export default function EditCommunicationChannelPage() {
  const router = useRouter()
  const params = useParams<{ name: string }>()
  const name = decodeURIComponent(params?.name ?? '')
  const { confirm, confirmDialog } = useConfirmDialog()
  const { showToast } = useToast()

  const [item, setItem] = useState<CommunicationChannelItem | null>(null)
  const [draft, setDraft] = useState<DraftState | null>(null)
  const [savedDraft, setSavedDraft] = useState<DraftState | null>(null)
  const [credentialEditStates, setCredentialEditStates] = useState<CredentialEditStates>({})
  const [activeTab, setActiveTab] = useState<ChannelProvider>('telegram')
  const [hosts, setHosts] = useState<HostItem[]>([])
  // Which credential keys the channel's Secret actually holds. Undefined until
  // the read lands, so the panel renders pending instead of "nothing stored".
  const [storedCredentialKeys, setStoredCredentialKeys] = useState<string[] | undefined>(undefined)
  // Why that read failed, when it did. Undefined keys ALONE cannot say whether
  // the read is still running or is over and broken, and the panel disables
  // itself while it believes a read is in flight — so swallowing the error left
  // every credential control dead until a page reload, with no way to tell.
  const [credentialKeysError, setCredentialKeysError] = useState('')
  // Drops a stale credentials response when the read is retried or `name` changes.
  const credentialsRequestId = useRef(0)
  const [loading, setLoading] = useState(true)
  const [loadError, setLoadError] = useState('')
  const [saving, setSaving] = useState(false)
  const [saveError, setSaveError] = useState('')
  const [saveRefreshPending, setSaveRefreshPending] = useState(false)

  function backToChannels() {
    router.push(CONTROL_ROUTES.externalChannels.root)
  }

  // Key names only — the API never returns values. A failure here must not take
  // the whole page down, must not be read as "no credentials", and must not be
  // read as "still loading" either: it is its own state, with a retry.
  const loadCredentialKeys = useCallback(async (channelName: string) => {
    if (!channelName) return
    const id = ++credentialsRequestId.current
    setStoredCredentialKeys(undefined)
    setCredentialKeysError('')
    try {
      const response = await apiGet(
        `/api/v1/admin/communication-channels/${encodeURIComponent(channelName)}/credentials`
      )
      if (id !== credentialsRequestId.current) return
      const keys = extractCredentialKeys(response)
      if (!keys) {
        setCredentialKeysError(CREDENTIALS_SHAPE_ERROR)
        return
      }
      setStoredCredentialKeys(keys)
      return keys
    } catch (error) {
      // A silent error is an expired session: the app is already redirecting,
      // so a banner about credentials would just be noise on the way out.
      if (id !== credentialsRequestId.current || isSilentApiError(error)) return
      setCredentialKeysError(error instanceof Error ? error.message : CREDENTIALS_REQUEST_ERROR)
    }
  }, [])

  useEffect(() => {
    let cancelled = false
    async function load() {
      setLoading(true)
      setLoadError('')
      try {
        const [channelResponse, hostsResponse] = await Promise.all([
          apiGet(`/api/v1/admin/communication-channels/${encodeURIComponent(name)}`).catch(
            async error => {
              if (isSilentApiError(error)) return null
              return apiGet('/api/v1/admin/communication-channels')
            }
          ),
          apiGet('/api/v1/admin/hosts') as Promise<HostsResponse | HostItem[]>,
          // Runs alongside the other two, but owns its own state: a credentials
          // failure is reported in the panel, not by failing the page load.
          loadCredentialKeys(name),
        ])
        if (cancelled || channelResponse === null) return
        const nextItem = extractChannel(channelResponse, name)
        if (!nextItem) {
          setLoadError(`Communication channel ${name} was not found.`)
          return
        }
        const nextHosts = Array.isArray(hostsResponse)
          ? hostsResponse
          : hostsResponse.items || hostsResponse.hosts || []
        setItem(nextItem)
        const nextDraft = createCommunicationChannelDraft(nextItem)
        setDraft(nextDraft)
        setSavedDraft(nextDraft)
        setCredentialEditStates({})
        setSaveRefreshPending(false)
        setActiveTab(communicationChannelInitialTab(nextItem))
        setHosts(nextHosts)
      } catch (error) {
        if (isSilentApiError(error)) return
        if (!cancelled) {
          setLoadError(
            error instanceof Error ? error.message : 'Failed to load communication channel'
          )
        }
      } finally {
        if (!cancelled) setLoading(false)
      }
    }
    if (name) void load()
    return () => {
      cancelled = true
    }
  }, [name, loadCredentialKeys])

  const visibleChannelTypes = useMemo<ChannelType[]>(() => [activeTab], [activeTab])
  const validCredentialChannelTypes = useMemo<ChannelType[]>(() => {
    if (!draft) return []
    const spec = buildCommunicationChannelSpec(draft)
    return [
      ...(spec.telegram || spec.telegramSettings ? (['telegram'] as const) : []),
      ...(spec.slack || spec.slackSettings ? (['slack'] as const) : []),
      ...(spec.teams || spec.teamsSettings ? (['teams'] as const) : []),
      ...(spec.email ? (['email'] as const) : []),
    ]
  }, [draft])
  const specDirty = useMemo(
    () =>
      Boolean(
        draft &&
        savedDraft &&
        JSON.stringify(buildCommunicationChannelSpec(draft)) !==
          JSON.stringify(buildCommunicationChannelSpec(savedDraft))
      ),
    [draft, savedDraft]
  )
  const credentialsDirty = Object.values(credentialEditStates).some(credentialStateIsDirty)
  const formDirty = specDirty || credentialsDirty || saveRefreshPending
  const activeConversations = draft ? conversationsForProvider(activeTab, draft) : []
  // Gated on the DRAFT, not the persisted item. Slack's own order is manifest
  // first, credentials second: the bot token only exists after the app has been
  // created and installed, so a manifest that needed a saved Slack spec could
  // only ever appear after the operator had already done the step it describes.
  // The URL depends only on namespace/name, both fixed at create time, and the
  // reader resolves that id regardless of slackSettings, so a draft-derived URL
  // is already server-truthful. The guard against a copyable dead end on a
  // non-Slack channel is kept: no Slack config in the draft, no URL. A bot
  // handle the draft only inherited from the clerum.io/slack-bot-label
  // annotation is not Slack config, which is why this is not hasSlackConfig.
  const slackRequestUrl =
    draft && hasSlackConfigForRequestUrl(draft)
      ? slackWebhookUrlForChannelName(
          item?.metadata?.name?.trim() || name,
          item?.metadata?.namespace
        )
      : null
  // Gated on the DRAFT, not the persisted item, for the same reason as Slack
  // above: the Teams bot credentials only exist after `teams app create` has
  // run, and that command needs this URL. A URL that required a saved
  // teamsSettings could only appear after the operator had already done the
  // step it describes. The URL depends only on namespace/name, both fixed at
  // create time, and the reader resolves that id regardless of teamsSettings.
  const teamsRequestUrl =
    draft && hasTeamsConfigForRequestUrl(draft)
      ? teamsWebhookUrlForChannelName(
          item?.metadata?.name?.trim() || name,
          item?.metadata?.namespace
        )
      : null
  // A relative request_url is invalid to Slack, so warn instead of handing over a manifest that
  // cannot work. slackWebhookUrlForChannelName falls back to a bare path when the deployment has
  // no public webhook address.
  const slackManifest =
    slackRequestUrl && canGenerateSlackAppManifest(slackRequestUrl)
      ? slackAppManifest(draft?.slackBotHandle.trim() || DEFAULT_SLACK_APP_NAME, slackRequestUrl)
      : null
  // A relative endpoint would point the Teams CLI at a host that does not
  // exist, so a deployment with no public webhook origin falls back to
  // teamsAppCreatePlaceholderCommand below instead of a command built from
  // that relative path. This is also the repair path when a channel is
  // recreated under a new name: retyping the Name field alone is enough to
  // regenerate this command.
  const teamsAppCreateCommand =
    teamsRequestUrl && canGenerateTeamsCommand(teamsRequestUrl)
      ? buildTeamsAppCreateCommand({
          botName: draft?.teamsAppName || '',
          endpoint: teamsRequestUrl,
        })
      : null
  // The minikube case: no public origin to build a real command from. Still
  // render a runnable-looking command rather than nothing, using the marker
  // origin LOCAL_TEAMS_ENDPOINT_ORIGIN, because substituting a real origin by
  // hand for that marker is the documented self-hosted workflow.
  const teamsAppCreatePlaceholderCommand =
    teamsRequestUrl && !canGenerateTeamsCommand(teamsRequestUrl)
      ? buildTeamsAppCreateCommand({
          botName: draft?.teamsAppName || '',
          endpoint: teamsPlaceholderEndpoint(teamsRequestUrl),
        })
      : null
  // hasTeamsConfigForRequestUrl is satisfied by CLIENT_ID or TENANT_ID alone,
  // so teamsAppCreateCommand can render with a blank Name -- which
  // buildTeamsAppCreateCommand fills with the literal placeholder <bot-name>.
  // Gate Copy on a usable name the same way the create page does, so that
  // placeholder is never handed over as if it were runnable.
  const teamsBotNameError = teamsAppNameError(draft?.teamsAppName || '')

  /**
   * The single validation boundary for this page: every path that persists a
   * full spec goes through here, Save and deleteConversation alike. Validating
   * in handleSave instead let deleteConversation persist a value Save refuses.
   *
   * It validates the SPEC, not the draft, because the spec is what is sent: an
   * empty name is omitted by buildCommunicationChannelSpec rather than written
   * as an empty string, which is exactly what the server accepts.
   *
   * Returns false when nothing was persisted, so callers do not navigate away
   * from an unsaved page -- neither on a rejected value nor on a failed PUT.
   */
  async function persistDraft(nextDraft: DraftState, successMessage: string): Promise<boolean> {
    const spec = buildCommunicationChannelSpec(nextDraft)
    const teamsAppName = spec.teamsSettings?.appName
    const nameError = teamsAppName === undefined ? null : teamsAppNameError(teamsAppName)
    if (nameError) {
      setSaveError(nameError)
      return false
    }
    setSaving(true)
    setSaveError('')
    try {
      await apiSend('PUT', `/api/v1/admin/communication-channels/${encodeURIComponent(name)}`, {
        spec,
      })
      setDraft(nextDraft)
      setSavedDraft(nextDraft)
      showToast(successMessage, { tone: 'success' })
      return true
    } catch (error) {
      setSaveError(error instanceof Error ? error.message : 'Failed to save communication channel')
      return false
    } finally {
      setSaving(false)
    }
  }

  async function handleSave(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault()
    if (!draft || saving) return
    const spec = buildCommunicationChannelSpec(draft)
    const teamsName = spec.teamsSettings?.appName
    const nameError = teamsName === undefined ? null : teamsAppNameError(teamsName)
    const plan = buildCredentialOperationPlan(credentialEditStates, validCredentialChannelTypes)
    if (nameError || plan.invalidLabels.length) {
      setSaveError(
        nameError || `Credential changes are no longer valid for: ${plan.invalidLabels.join(', ')}.`
      )
      return
    }
    if (!specDirty && plan.operations.length === 0 && !saveRefreshPending) return

    setSaving(true)
    setSaveError('')
    let mutationCommitted = saveRefreshPending
    let channelSettingsSaved = false
    const failedLabels: string[] = []
    try {
      if (specDirty) {
        await apiSend('PUT', `/api/v1/admin/communication-channels/${encodeURIComponent(name)}`, {
          spec,
        })
        mutationCommitted = true
        channelSettingsSaved = true
        setSavedDraft(draft)
        setSaveRefreshPending(true)
      }

      const channelResponse = await apiGet(
        `/api/v1/admin/communication-channels/${encodeURIComponent(name)}`
      )
      const authoritativeItem = extractChannel(channelResponse, name)
      if (!authoritativeItem) throw new Error(`Communication channel ${name} was not found.`)
      const authoritativeDraft = createCommunicationChannelDraft(authoritativeItem)
      const authoritativeSpec = buildCommunicationChannelSpec(authoritativeDraft)
      const authoritativeTypes: ChannelType[] = [
        ...(authoritativeSpec.telegram || authoritativeSpec.telegramSettings
          ? (['telegram'] as const)
          : []),
        ...(authoritativeSpec.slack || authoritativeSpec.slackSettings ? (['slack'] as const) : []),
        ...(authoritativeSpec.teams || authoritativeSpec.teamsSettings ? (['teams'] as const) : []),
        ...(authoritativeSpec.email ? (['email'] as const) : []),
      ]
      const validatedPlan = buildCredentialOperationPlan(credentialEditStates, authoritativeTypes)
      if (validatedPlan.invalidLabels.length) {
        setItem(authoritativeItem)
        setDraft(authoritativeDraft)
        setSavedDraft(authoritativeDraft)
        setSaveError(
          `${channelSettingsSaved ? 'Channel settings were saved, but ' : ''}credential changes are no longer valid for: ${validatedPlan.invalidLabels.join(', ')}.`
        )
        return
      }

      const successful: CredentialKey[] = []
      for (const operation of validatedPlan.operations) {
        try {
          if (operation.kind === 'replace') {
            await apiSend(
              'PUT',
              `/api/v1/admin/communication-channels/${encodeURIComponent(name)}/credentials`,
              { [operation.key]: operation.value }
            )
          } else {
            await apiSend(
              'DELETE',
              `/api/v1/admin/communication-channels/${encodeURIComponent(name)}/credentials/${encodeURIComponent(operation.key)}`
            )
          }
          successful.push(operation.key)
          mutationCommitted = true
        } catch {
          failedLabels.push(operation.label)
        }
      }

      if (successful.length) {
        setCredentialEditStates(current => cleanCredentialStates(current, successful))
        setSaveRefreshPending(true)
      }

      const [refreshedChannel, refreshedKeys] = await Promise.all([
        apiGet(`/api/v1/admin/communication-channels/${encodeURIComponent(name)}`),
        loadCredentialKeys(name),
      ])
      if (!refreshedKeys) {
        throw new Error('Could not refresh authoritative credential presence after saving.')
      }
      const refreshedItem = extractChannel(refreshedChannel, name) ?? authoritativeItem
      const refreshedDraft = createCommunicationChannelDraft(refreshedItem)
      setItem(refreshedItem)
      setDraft(refreshedDraft)
      setSavedDraft(refreshedDraft)
      setSaveRefreshPending(false)

      if (failedLabels.length) {
        setSaveError(
          `${channelSettingsSaved ? 'Channel settings were saved, but ' : ''}some credential changes failed: ${failedLabels.join(', ')}. Retry Save to apply the remaining changes.`
        )
        return
      }
      setCredentialEditStates({})
      showToast(`Communication channel ${name} updated.`, { tone: 'success' })
      backToChannels()
    } catch (error) {
      if (failedLabels.length) {
        const refreshError = error instanceof Error ? error.message : 'refresh failed'
        setSaveError(
          `${channelSettingsSaved ? 'Channel settings were saved, but ' : ''}credential changes failed for: ${failedLabels.join(', ')}. The authoritative refresh also failed: ${refreshError}. Retry Save to apply remaining changes and refresh.`
        )
      } else if (mutationCommitted) {
        setSaveRefreshPending(true)
        setSaveError(
          'Channel settings were saved, but the authoritative refresh failed. Retry Save to refresh without resending successful credential changes.'
        )
      } else {
        setSaveError(
          error instanceof Error ? error.message : 'Failed to save communication channel'
        )
      }
    } finally {
      setSaving(false)
    }
  }

  async function deleteConversation(conversation: CommunicationChannelConversation) {
    if (!draft || saving) return
    const shouldDelete = await confirm({
      title: 'Delete Conversation',
      message: 'Delete this confirmed conversation from the communication channel?',
      confirmLabel: 'Delete conversation',
      tone: 'danger',
    })
    if (!shouldDelete) return
    const nextDraft =
      conversation.provider === 'telegram'
        ? {
            ...draft,
            telegram: draft.telegram.filter(group => group.channelId !== conversation.channelId),
          }
        : conversation.provider === 'slack'
          ? {
              ...draft,
              slack: draft.slack.filter(group => group.channelId !== conversation.channelId),
            }
          : {
              ...draft,
              teams: draft.teams.filter(group => group.channelId !== conversation.channelId),
            }
    await persistDraft(nextDraft, 'Conversation deleted.')
  }

  async function copySlackRequestUrl() {
    if (!slackRequestUrl) return
    const copied = await copyTextToClipboard(slackRequestUrl)
    showToast(
      copied
        ? 'Slack Request URL copied.'
        : 'Could not copy to clipboard. Select the URL and copy it manually.',
      { tone: copied ? 'success' : 'error' }
    )
  }

  async function copySlackAppManifest() {
    if (!slackManifest) return
    const copied = await copyTextToClipboard(slackManifest)
    showToast(
      copied
        ? 'Slack app manifest copied.'
        : 'Could not copy to clipboard. Select the manifest and copy it manually.',
      { tone: copied ? 'success' : 'error' }
    )
  }

  async function copyTeamsRequestUrl() {
    if (!teamsRequestUrl) return
    const copied = await copyTextToClipboard(teamsRequestUrl)
    showToast(
      copied
        ? 'Teams Request URL copied.'
        : 'Could not copy to clipboard. Select the URL and copy it manually.',
      { tone: copied ? 'success' : 'error' }
    )
  }

  async function copyTeamsAppCreateCommand() {
    if (!teamsAppCreateCommand) return
    const copied = await copyTextToClipboard(teamsAppCreateCommand)
    showToast(
      copied
        ? 'Teams bot command copied.'
        : 'Could not copy to clipboard. Select the command and copy it manually.',
      { tone: copied ? 'success' : 'error' }
    )
  }

  async function copyTeamsAppCreatePlaceholderCommand() {
    if (!teamsAppCreatePlaceholderCommand) return
    const copied = await copyTextToClipboard(teamsAppCreatePlaceholderCommand)
    showToast(
      copied
        ? 'Teams bot command copied.'
        : 'Could not copy to clipboard. Select the command and copy it manually.',
      { tone: copied ? 'success' : 'error' }
    )
  }

  return (
    <AuthGate>
      <DashboardLayout isDetailPage>
        <CreateFlowPanel
          header={
            <CreatePageHeader
              icon={<IconBroadcast />}
              title={`Edit Communication Channel: ${name}`}
              subtitle="Update agent access, provider settings, and write-only credentials."
              backLabel="Back to channels"
              onBack={backToChannels}
            />
          }
        >
          {loading ? (
            <FormSectionsSkeleton
              className="cu-channel-edit-form"
              label="Communication channel"
              primaryActionLabel="Save channel"
              sections={3}
            />
          ) : loadError ? (
            <div className="cu-create-content cu-channel-edit-form">
              <div className="cu-banner cu-banner--error" role="alert">
                {loadError}
              </div>
            </div>
          ) : item && draft ? (
            <form className="cu-create-content cu-channel-edit-form" onSubmit={handleSave}>
              <div className="cu-field">
                <label htmlFor="ch-host-ref">Agent reference</label>
                <SelectionDropdown
                  id="ch-host-ref"
                  multiple={false}
                  value={draft.hostRef ? [draft.hostRef] : []}
                  onChange={next =>
                    setDraft(current =>
                      current
                        ? {
                            ...current,
                            accessTeamIds:
                              current.hostRef === (next[0] || '') ? current.accessTeamIds : [],
                            accessUserIds:
                              current.hostRef === (next[0] || '') ? current.accessUserIds : [],
                            hostRef: next[0] || '',
                          }
                        : current
                    )
                  }
                  options={hosts.map(host => ({
                    value: host.metadata?.name || '',
                    label: host.metadata?.name || '-',
                    description: host.metadata?.namespace || 'default',
                  }))}
                  placeholder="Select an agent..."
                  searchPlaceholder="Search agents..."
                  emptyLabel="No available agents."
                  disabled={saving}
                />
                <span className="cu-field__hint">
                  Changing the agent clears communication access until you reselect users or teams.
                </span>
              </div>

              <SegmentedControl<ChannelProvider>
                ariaLabel="Communication channel providers"
                value={activeTab}
                className="cu-segmented-control--flush cu-segmented-control--full"
                disabled={saving}
                onChange={setActiveTab}
                options={COMMUNICATION_CHANNEL_PROVIDER_OPTIONS}
              />

              <section className="cu-channel-provider-panel">
                <div className="cu-channel-provider-panel__head">
                  <div>
                    <p className="cu-section-title">
                      {communicationChannelProviderServiceLabel(activeTab)}
                    </p>
                    <p className="cu-muted">
                      {activeTab === 'slack'
                        ? 'Slack app credentials are write-only. Rotate regenerated values here.'
                        : 'Credentials are write-only. Stored values render masked and can be rotated.'}
                    </p>
                  </div>
                </div>
                <div className="cu-toggle-field">
                  <label className="cu-toggle-row">
                    <input
                      type="checkbox"
                      checked={
                        activeTab === 'telegram'
                          ? draft.telegramReplyOnlyWhenMentioned
                          : activeTab === 'slack'
                            ? draft.slackReplyOnlyWhenMentioned
                            : draft.teamsReplyOnlyWhenMentioned
                      }
                      disabled={saving}
                      onChange={event =>
                        setDraft(current =>
                          current
                            ? activeTab === 'telegram'
                              ? {
                                  ...current,
                                  telegramReplyOnlyWhenMentioned: event.target.checked,
                                }
                              : activeTab === 'slack'
                                ? { ...current, slackReplyOnlyWhenMentioned: event.target.checked }
                                : { ...current, teamsReplyOnlyWhenMentioned: event.target.checked }
                            : current
                        )
                      }
                    />
                    <span>
                      Answer only when the {activeTab === 'slack' ? 'app' : 'bot'} is mentioned
                    </span>
                  </label>
                  {activeTab === 'slack' ? (
                    // Operators assume a thread is an implicit mention. It is not:
                    // with "Reply in threads" on, the app answers in a thread and
                    // then drops every unmentioned follow-up posted there.
                    <p className="cu-field__hint cu-toggle-row__hint">
                      Replies inside a thread still require a mention: even in a thread the app
                      started, a follow-up that does not mention the app is ignored.
                    </p>
                  ) : null}
                </div>
                {activeTab === 'slack' ? (
                  <label className="cu-toggle-row">
                    <input
                      type="checkbox"
                      checked={draft.slackReplyInThreads}
                      disabled={saving}
                      onChange={event =>
                        setDraft(current =>
                          current
                            ? { ...current, slackReplyInThreads: event.target.checked }
                            : current
                        )
                      }
                    />
                    <span>
                      Reply in threads{' '}
                      <span
                        className="cu-help-tooltip"
                        tabIndex={0}
                        aria-label="When enabled, app responses are posted in a Slack thread and follow-up messages in that thread continue there. New top-level messages start a new thread."
                        data-tooltip="When enabled, app responses are posted in a Slack thread and follow-up messages in that thread continue there. New top-level messages start a new thread."
                      >
                        ?
                      </span>
                    </span>
                  </label>
                ) : null}
                {activeTab === 'telegram' ? (
                  <div className="cu-field">
                    <label htmlFor="telegram-bot-handle">
                      Telegram bot handle{' '}
                      <span
                        className="cu-help-tooltip"
                        tabIndex={0}
                        aria-label="We use this handle to show users which Telegram bot to message when connecting their account or a group."
                        data-tooltip="We use this handle to show users which Telegram bot to message when connecting their account or a group."
                      >
                        ?
                      </span>
                    </label>
                    <input
                      id="telegram-bot-handle"
                      className="cu-input"
                      value={draft.telegramBotHandle}
                      onChange={event =>
                        setDraft(current =>
                          current ? { ...current, telegramBotHandle: event.target.value } : current
                        )
                      }
                      placeholder="@your_bot"
                      disabled={saving}
                      autoComplete="off"
                    />
                    <span className="cu-field__hint">
                      Public bot username, with or without the leading @.
                    </span>
                  </div>
                ) : activeTab === 'slack' ? (
                  <>
                    <div className="cu-banner cu-banner--info">
                      Store the Slack app Signing Secret and Bot User OAuth token here. The Signing
                      Secret verifies Slack requests; the bot token lets us read and reply through
                      the Slack Web API. The workspace is detected when a user verifies a Slack
                      conversation.
                    </div>
                    <div className="cu-field">
                      <label htmlFor="slack-bot-handle">
                        Slack App Name{' '}
                        <span
                          className="cu-help-tooltip"
                          tabIndex={0}
                          aria-label="We use this name to show users which Slack App to message"
                          data-tooltip="We use this name to show users which Slack App to message"
                        >
                          ?
                        </span>
                      </label>
                      <input
                        id="slack-bot-handle"
                        className="cu-input"
                        value={draft.slackBotHandle}
                        onChange={event =>
                          setDraft(current =>
                            current
                              ? {
                                  ...current,
                                  slackBotHandle: event.target.value,
                                  // Typed, so no longer just an inherited label.
                                  slackBotHandleFromAnnotation: false,
                                }
                              : current
                          )
                        }
                        placeholder="Your Slack App"
                        disabled={saving}
                        autoComplete="off"
                      />
                      <span className="cu-field__hint">
                        Slack App name shown in Profile UI setup instructions.
                      </span>
                    </div>
                    <div className="cu-field">
                      <span className="cu-field__label">Slack Request URL</span>
                      <div className="cu-copy-field">
                        <div className="cu-readonly-field cu-copy-field__value">
                          {slackRequestUrl || 'Available once this channel has a Slack App Name'}
                        </div>
                        <button
                          type="button"
                          className="cu-btn"
                          onClick={copySlackRequestUrl}
                          disabled={!slackRequestUrl || saving}
                        >
                          Copy
                        </button>
                      </div>
                      <span className="cu-field__hint">
                        {slackRequestUrl
                          ? 'Use this URL for Slack Event Subscriptions and Interactivity.'
                          : 'Enter the Slack App Name above and this channel gets its Request URL and app manifest.'}
                      </span>
                    </div>
                    {slackRequestUrl ? (
                      slackManifest ? (
                        <div className="cu-field">
                          <span className="cu-field__label">Slack App Manifest</span>
                          <div className="cu-command-block">
                            <div className="cu-command-block__toolbar">
                              <span>YAML</span>
                              <Button
                                type="button"
                                variant="secondary"
                                size="sm"
                                className="cu-command-block__copy"
                                onClick={copySlackAppManifest}
                                disabled={saving}
                                aria-label="Copy Slack app manifest"
                              >
                                <IconCopy width={15} height={15} />
                                Copy
                              </Button>
                            </div>
                            <pre className="cu-command-block__pre cu-slack-manifest__pre">
                              <code>{slackManifest}</code>
                            </pre>
                          </div>
                          <span className="cu-field__hint">
                            Paste this into Slack, either at Create New App → From an app manifest
                            for a new app, or in an existing app under Settings → App Manifest. It
                            fills in both Request URLs, on Event Subscriptions and on Interactivity
                            &amp; Shortcuts. Setting only Event Subscriptions leaves approval
                            buttons dead with nothing in the logs.
                          </span>
                        </div>
                      ) : (
                        <div className="cu-banner cu-banner--warning">
                          No app manifest: this deployment has no public webhook address, so the
                          Request URL above is a path and Slack cannot reach it. Expose the webhook
                          proxy publicly and set NEXT_PUBLIC_WORKFLOW_APPROVAL_READER_BASE_URL to
                          that address, or prefix the path with it by hand in Slack.
                        </div>
                      )
                    ) : null}
                  </>
                ) : (
                  <>
                    <TeamsSetupPrerequisites />

                    <div className="cu-banner cu-banner--info">
                      Store the CLIENT_SECRET value here, not the Microsoft secret ID. CLIENT_ID and
                      TENANT_ID identify the Microsoft Teams bot that receives messages for this
                      channel.
                    </div>
                    <div className="cu-field">
                      <label htmlFor="teams-app-name">Name</label>
                      <input
                        id="teams-app-name"
                        className="cu-input"
                        value={draft.teamsAppName}
                        onChange={event =>
                          setDraft(current =>
                            current
                              ? {
                                  ...current,
                                  teamsAppName: event.target.value,
                                  // Typed, so no longer just an inherited label.
                                  teamsAppNameFromAnnotation: false,
                                }
                              : current
                          )
                        }
                        placeholder="Evenfire Bot"
                        disabled={saving}
                        autoComplete="off"
                      />
                      <span className="cu-field__hint">
                        Display name shown in the Teams bot creation output and Profile UI setup
                        instructions, up to {TEAMS_APP_NAME_MAX_LENGTH} characters.
                      </span>
                    </div>
                    <div className="cu-field">
                      <label htmlFor="teams-app-id">CLIENT_ID</label>
                      <input
                        id="teams-app-id"
                        className="cu-input"
                        value={draft.teamsAppId}
                        onChange={event =>
                          setDraft(current =>
                            current ? { ...current, teamsAppId: event.target.value } : current
                          )
                        }
                        placeholder="00000000-0000-0000-0000-000000000000"
                        disabled={saving}
                        autoComplete="off"
                      />
                      <span className="cu-field__hint">
                        CLIENT_ID from the generated .env file.
                      </span>
                    </div>
                    <div className="cu-field">
                      <label htmlFor="teams-tenant-id">TENANT_ID</label>
                      <input
                        id="teams-tenant-id"
                        className="cu-input"
                        value={draft.teamsTenantId}
                        onChange={event =>
                          setDraft(current =>
                            current ? { ...current, teamsTenantId: event.target.value } : current
                          )
                        }
                        placeholder="00000000-0000-0000-0000-000000000000"
                        disabled={saving}
                        autoComplete="off"
                      />
                      <span className="cu-field__hint">
                        TENANT_ID from the generated .env file.
                      </span>
                    </div>
                    <div className="cu-field">
                      <span className="cu-field__label">Teams Request URL</span>
                      <div className="cu-copy-field">
                        <div className="cu-readonly-field cu-copy-field__value">
                          {teamsRequestUrl || 'Unavailable'}
                        </div>
                        <button
                          type="button"
                          className="cu-btn"
                          onClick={copyTeamsRequestUrl}
                          disabled={!teamsRequestUrl || saving}
                        >
                          Copy
                        </button>
                      </div>
                      <span className="cu-field__hint">
                        {!teamsRequestUrl
                          ? 'Enter the Name above and this channel gets its Teams Request URL.'
                          : canGenerateTeamsCommand(teamsRequestUrl)
                            ? 'Use this URL as the Messaging endpoint for the Teams bot app.'
                            : 'This is a path, not a full URL. Prefix it with your public webhook origin before using it as the Messaging endpoint.'}
                      </span>
                    </div>
                    {teamsRequestUrl ? (
                      teamsAppCreateCommand ? (
                        <div className="cu-field">
                          <span className="cu-field__label">Create the Teams bot</span>
                          <div className="cu-command-block">
                            <div className="cu-command-block__toolbar">
                              <span>Bash</span>
                              <Button
                                type="button"
                                variant="secondary"
                                size="sm"
                                className="cu-command-block__copy"
                                onClick={copyTeamsAppCreateCommand}
                                disabled={saving || Boolean(teamsBotNameError)}
                                aria-label="Copy Teams bot create command"
                              >
                                <IconCopy width={15} height={15} />
                                Copy
                              </Button>
                            </div>
                            <pre className="cu-command-block__pre">
                              <code>{teamsAppCreateCommand}</code>
                            </pre>
                          </div>
                          <span className="cu-field__hint">
                            Run this to create or recreate the Teams bot, then paste the generated
                            CLIENT_ID, TENANT_ID, and CLIENT_SECRET above. This is also the repair
                            path when this channel was recreated under a new name.
                          </span>
                        </div>
                      ) : (
                        <div className="cu-field">
                          <div className="cu-banner cu-banner--warning">
                            This deployment has no public webhook origin. The command below uses the
                            placeholder origin <code>{LOCAL_TEAMS_ENDPOINT_ORIGIN}</code>, which
                            must be replaced before running it, or set{' '}
                            <code>NEXT_PUBLIC_WORKFLOW_APPROVAL_READER_BASE_URL</code> to have it
                            filled in automatically.
                          </div>
                          {teamsAppCreatePlaceholderCommand && (
                            <div className="cu-command-block">
                              <div className="cu-command-block__toolbar">
                                <span>Bash</span>
                                <Button
                                  type="button"
                                  variant="secondary"
                                  size="sm"
                                  className="cu-command-block__copy"
                                  onClick={copyTeamsAppCreatePlaceholderCommand}
                                  disabled={saving || Boolean(teamsBotNameError)}
                                  aria-label="Copy Teams bot create command"
                                >
                                  <IconCopy width={15} height={15} />
                                  Copy
                                </Button>
                              </div>
                              <pre className="cu-command-block__pre">
                                <code>{teamsAppCreatePlaceholderCommand}</code>
                              </pre>
                            </div>
                          )}
                        </div>
                      )
                    ) : null}
                  </>
                )}
                <ChannelCredentialsPanel
                  ccName={item.metadata?.name || name}
                  visibleChannelTypes={visibleChannelTypes}
                  storedKeys={storedCredentialKeys}
                  storedKeysError={credentialKeysError}
                  onRetryStoredKeys={() => void loadCredentialKeys(name)}
                  editStates={credentialEditStates}
                  onEditStateChange={(key, state) =>
                    setCredentialEditStates(current => ({ ...current, [key]: state }))
                  }
                  saving={saving}
                />
              </section>

              <CommunicationChannelAccessSelector
                agentName={draft.hostRef}
                disabled={saving}
                inlineDropdowns
                selectedTeamIds={draft.accessTeamIds}
                selectedUserIds={draft.accessUserIds}
                onSelectedTeamIdsChange={accessTeamIds =>
                  setDraft(current => (current ? { ...current, accessTeamIds } : current))
                }
                onSelectedUserIdsChange={accessUserIds =>
                  setDraft(current => (current ? { ...current, accessUserIds } : current))
                }
              />

              <section className="cu-channel-provider-panel">
                <div className="cu-channel-provider-panel__head">
                  <div>
                    <p className="cu-section-title">Confirmed conversations</p>
                    <p className="cu-muted">
                      Conversations are connected from Profile UI and can be removed here.
                    </p>
                  </div>
                </div>
                <CommunicationChannelConversationsTable
                  conversations={activeConversations}
                  onDelete={deleteConversation}
                />
              </section>

              {saveError ? (
                <div className="cu-banner cu-banner--error" role="alert">
                  {saveError}
                </div>
              ) : null}

              <div className="cu-create-actions">
                <button
                  type="button"
                  className="cu-btn cu-btn--ghost"
                  onClick={backToChannels}
                  disabled={saving}
                >
                  Cancel
                </button>
                <button
                  type="submit"
                  className="cu-btn cu-btn--primary"
                  disabled={saving || !formDirty}
                >
                  {saving ? 'Saving...' : 'Save changes'}
                </button>
              </div>
            </form>
          ) : null}
        </CreateFlowPanel>
      </DashboardLayout>
      {confirmDialog}
    </AuthGate>
  )
}