Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

21.0 KB
Raw Blame
/**
 * Agent settings page "Hooks" tab: the hook packages installed on this Agent
 * (agent_state/hooks/<name>/ — a manifest plus the scripts the harness runs at the loop's
 * hook points, e.g. after every Task). The files are the single source of truth, so the list
 * is re-fetched from the API after every mutation instead of trusting client state, the way
 * the Skills tab does. Rows lead with the icon of the plugin the package came from (the hook
 * glyph when it has none), then the package name with the hook points it answers at right
 * beside it (one bare chip each — `stop`, `user_prompt` — wrapping with the name), its
 * localized description below, and the trailing slot: the version, export and uninstall.
 * Export downloads the installed directory as a zip, and a refused one leaves its reason under
 * the row that asked (hook-export-refusal.ts); the "Import hook" modal offers the
 * Skills tab's two paths: the recommended chat import (a source field taking a URL / repo /
 * local path / description / another tool's hooks config, whose generated review-then-install
 * prompt — hook-import.ts — can be copied or prefilled into a new chat with this Agent) and a
 * zip upload that takes such a package back (409 hook_exists asks before overwriting). Read,
 * import/export and uninstall are member-level, matching the hooks routes.
 *
 * The Agent-level switch card sits at the top (usePromptInjection, the Skills tab's slot):
 * `hooks.enabled` in system_config.yaml decides whether a new Session assembles any hooks at
 * all — the packages are never touched by it. It is the owner's, like the Vault and Schedules
 * switches; members see the state. Hooks have no prompt half, so the card comes alone.
 */
import { useCallback, useEffect, useState } from "react";
import type { ChangeEvent } from "react";
import type { HookItem } from "@adelie/server/api";
import {
  Badge,
  Button,
  Card,
  ConfirmModal,
  CopiedStatus,
  CopyCheckGlyph,
  DownloadIcon,
  GlyphIcon,
  HelpFold,
  HiddenFileInput,
  ICONS,
  Modal,
  SettingsEmpty,
  SkeletonList,
  Textarea,
  toastSuccess,
  useCopied,
} from "@adelie/ui";
import * as api from "../../api/endpoints";
import { ApiError } from "../../api/client";
import { S } from "../../lib/strings";
import { apiErrorText } from "../../lib/api-error";
import { toneInk } from "../../lib/tone";
import { useLocale } from "../../state/locale";
import { agentDisplayName, useProject } from "../../state/project";
import { localizedText } from "../chat/skill-use";
import { SkillTile } from "../skills/skill-icon-view";
import { useAiBridge } from "../ai-create";
import { downloadArchive } from "./archive-download";
import { hookExportRefusalFor, hookExportRefused } from "./hook-export-refusal";
import type { HookExportRefusal } from "./hook-export-refusal";
import { buildHookImportPrompt } from "./hook-import";
import { usePromptInjection } from "./prompt-injection-controls";
import { UPLOAD_LABEL_CLASS } from "./skills-tab";

/** Zip pending an overwrite confirmation: the payload to resend with overwrite: true plus the package name for the confirm copy. */
interface PendingOverwrite {
  dataBase64: string;
  name: string;
}

export function HooksTab({
  agentId,
  onConfigChanged,
}: {
  agentId: string;
  /** The switch writes the Agent config directly, so the settings page must refetch its own copy — otherwise a later Prompt-tab save from stale data would silently revert it. */
  onConfigChanged?: () => void;
}) {
  const { locale } = useLocale();
  const { currentProject, agents, reloadAgents } = useProject();
  const { openAiChat } = useAiBridge();
  const projectId = currentProject?.projectId ?? null;
  const isOwner = currentProject?.role === "owner";
  // The Agent-level hook switch: owner-only, like the Vault and Schedules switches.
  const { applyConfig, toggleCard } = usePromptInjection({
    agentId,
    feature: "hooks",
    strings: S.hooks.injection,
    canEdit: isOwner,
    onConfigChanged,
  });

  const [hooks, setHooks] = useState<HookItem[] | null>(null);
  // Tab-level error is only the initial list load failure; row actions report in place (the
  // export's reason under its row, an uninstall's inside its confirmation) or inside the modal.
  const [error, setError] = useState<string | null>(null);
  const [busy, setBusy] = useState(false);
  // Package name pending uninstall confirmation (non-null shows the confirm modal).
  const [removing, setRemoving] = useState<string | null>(null);
  // The words for an uninstall the server refused, drawn inside the confirmation that asked for
  // it (see confirmRemove): retrying is the next thing the person does, and the button is in there.
  const [removeRefusal, setRemoveRefusal] = useState<string | null>(null);
  // The words for an export that did not land, drawn in the row that asked (hook-export-refusal.ts):
  // the row, the package and the same button are still there, which a six-second toast is not.
  const [exportRefusal, setExportRefusal] = useState<HookExportRefusal | null>(null);
  // Import modal: the source for the chat-import prompt + upload state travel with the modal.
  const [importOpen, setImportOpen] = useState(false);
  const [source, setSource] = useState("");
  const [uploading, setUploading] = useState(false);
  const [uploadError, setUploadError] = useState<string | null>(null);
  // Non-null shows the overwrite confirm (the archive POST answered 409 hook_exists).
  const [overwriting, setOverwriting] = useState<PendingOverwrite | null>(null);

  const load = useCallback(async () => {
    if (!projectId || !agentId) return;
    setHooks(null);
    setError(null);
    try {
      // The switch card's state loads in parallel with the tab's own list.
      const [res, configView] = await Promise.all([
        api.getAgentHooks(projectId, agentId),
        api.getAgentConfig(projectId, agentId),
      ]);
      setHooks(res.hooks);
      applyConfig(configView.config);
    } catch (e) {
      setError(apiErrorText(e));
    }
  }, [projectId, agentId, applyConfig]);

  useEffect(() => {
    void load();
  }, [load]);

  /** Display name of this Agent for toasts / confirm copy (falls back to the raw id). */
  const agent = agents.find((a) => a.agentId === agentId);
  const agentName = agent ? agentDisplayName(agent) : agentId;

  /**
   * "Export as zip": the shared archive download (archive-download.ts). A refused one leaves its
   * reason in the row that asked (hook-export-refusal.ts) — the row, the package and the same
   * button are all still there, so a toast that dismisses itself after six seconds is the one
   * thing that would carry that reason away. Starting an attempt clears the previous sentence:
   * it belonged to the attempt before, not to the package this one is about.
   */
  const exportHook = async (name: string) => {
    if (!projectId) return;
    setExportRefusal(null);
    try {
      await downloadArchive(api.agentHookArchiveUrl(projectId, agentId, name), name);
    } catch (e) {
      setExportRefusal({ hook: name, text: hookExportRefused(name, e) });
    }
  };

  /**
   * A row's uninstall button: opens the confirmation without the previous attempt's reason —
   * that sentence belonged to the attempt before, not to the package this one is about.
   */
  const askRemove = (name: string) => {
    setRemoving(name);
    setRemoveRefusal(null);
  };

  /**
   * Confirm modal's "Confirm": uninstall, then always re-fetch the list from disk — but close the
   * card only once the uninstall landed. A refused one changes nothing on either side (the
   * package is still installed), so the card stays open with the server's words in it and the
   * same button retries; closing it as the request left used to carry that reason into a toast
   * that dismisses itself after six seconds and sits above the card.
   */
  const confirmRemove = async () => {
    if (!projectId || removing === null) return;
    setBusy(true);
    try {
      await api.uninstallAgentHook(projectId, agentId, removing);
    } catch (e) {
      setRemoveRefusal(apiErrorText(e));
      return;
    } finally {
      setBusy(false);
    }
    toastSuccess(S.hooks.uninstalledToast(removing, agentName));
    setRemoving(null);
    await load();
    // The agent card's hook count (and its plugin-update marks) changed; refresh the list provider too.
    void reloadAgents();
  };

  /** Open the import modal (reset the form and upload state). */
  const openImport = () => {
    setSource("");
    setUploadError(null);
    setOverwriting(null);
    setImportOpen(true);
  };

  /**
   * POST the zip to the archive endpoint. A 409 hook_exists pops the overwrite confirm (the
   * package name is read from the server's fixed message tail, pinned by the route tests;
   * `fallbackName` — the picked file's stem — covers a parse miss). Success closes the modal
   * and re-fetches the list.
   */
  const upload = async (dataBase64: string, fallbackName: string, overwrite: boolean) => {
    if (!projectId) return;
    setUploading(true);
    setUploadError(null);
    try {
      await api.installAgentHookArchive(projectId, agentId, {
        dataBase64,
        ...(overwrite ? { overwrite: true } : {}),
      });
      setOverwriting(null);
      setImportOpen(false);
      toastSuccess(S.hooks.importDoneToast);
      await load();
      // The agent card's hook count changed; refresh the list provider too.
      void reloadAgents();
    } catch (e) {
      if (e instanceof ApiError && e.status === 409 && e.code === "hook_exists") {
        const name = /:\s*([A-Za-z0-9_-]+)$/.exec(e.message)?.[1] ?? fallbackName;
        setOverwriting({ dataBase64, name });
      } else {
        setOverwriting(null);
        setUploadError(apiErrorText(e));
      }
    } finally {
      setUploading(false);
    }
  };

  const onPickFile = (e: ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    e.target.value = "";
    if (!file) return;
    setUploadError(null);
    const fallbackName = file.name.replace(/\.zip$/i, "");
    const reader = new FileReader();
    reader.onload = () => {
      const dataUrl = reader.result as string;
      void upload(dataUrl.slice(dataUrl.indexOf(",") + 1), fallbackName, false); // strip the data:...;base64, prefix
    };
    reader.onerror = () => setUploadError(S.common.unknownError);
    reader.readAsDataURL(file);
  };

  // A URL / repo / path source gets an import lead sentence, free text is the lead itself (see
  // hook-import.ts); the preview substitutes a placeholder token until something is entered.
  const trimmedSource = source.trim();
  const chatPrompt =
    projectId !== null
      ? buildHookImportPrompt(trimmedSource || S.hooks.importSourceToken, projectId, agentId)
      : "";
  // Copy feedback lives at the button (the shared copy convention): its glyph flips to the
  // check while copied — no toast, and the button label never changes.
  const promptCopy = useCopied();

  /**
   * "Open a new chat" with this Agent: the AI bridge prefills the composer with the generated
   * import prompt (parking typed-but-unsent draft text first) and clears a stale handoff
   * target and skill pre-selection along with it.
   */
  const openChat = () => {
    if (projectId === null || trimmedSource === "") return;
    openAiChat({ agentId, text: buildHookImportPrompt(trimmedSource, projectId, agentId) });
    setImportOpen(false);
  };

  if (!projectId) return null;

  return (
    <div className="space-y-4">
      {/* Tab-level description: no title in the panel to anchor a "?" to (see help-fold.tsx). */}
      <HelpFold label={S.agent.tabHooks}>
        {S.hooks.agentTabDesc}
        {!isOwner && <span className="mt-1.5 block">{S.hooks.readOnlyHint}</span>}
      </HelpFold>

      {toggleCard}

      {/* Import entry point at the head of the installed list, right-aligned — the Skills tab's
          slot. It renders in every list state so the action never shifts. */}
      <div className="flex justify-end">
        <Button size="sm" variant="primary" disabled={hooks === null} onClick={openImport}>
          {S.hooks.importHook}
        </Button>
      </div>

      {hooks === null ? (
        <SkeletonList rows={3} />
      ) : hooks.length === 0 ? (
        <SettingsEmpty>{S.hooks.agentTabEmpty}</SettingsEmpty>
      ) : (
        <Card padding="none">
          {hooks.map((hook) => {
            const description = localizedText(locale, hook.description, hook.descriptionZh);
            // One refusal, one row: an attempt supersedes the reason of the attempt before it.
            const refusal = hookExportRefusalFor(exportRefusal, hook.name);
            return (
              // The row is a column so a refused export can put its reason under the line that
              // asked for it, without pushing any other row sideways (the Skills tab's shape).
              <div
                key={hook.name}
                className="border-b border-gray-100 last:border-b-0 dark:border-gray-800/60"
              >
                <div className="flex items-center gap-3 px-3 py-2.5 transition-colors duration-150 hover:bg-gray-50 dark:hover:bg-gray-800/40">
                  <SkillTile
                    icon={hook.icon}
                    name={hook.name}
                    fallback={ICONS.fishHook}
                    size={36}
                    glyph={20}
                  />
                  <div className="min-w-0 flex-1">
                    {/* Title row: the name with the hook points it answers at right beside it — bare
                        point names, wrapping onto a second line rather than truncating. */}
                    <div className="flex flex-wrap items-center gap-x-1.5 gap-y-1">
                      <span
                        className="max-w-full truncate font-mono text-[length:var(--ui-text-code-size)] font-semibold"
                        data-tooltip={hook.name}
                        data-tooltip-content="code"
                      >
                        {hook.name}
                      </span>
                      {hook.events.map((event) => (
                        <Badge key={event}>{event}</Badge>
                      ))}
                    </div>
                    {/* Description truncates to one line (the full text goes into title for hover reading). */}
                    <p
                      className="mt-0.5 truncate text-xs text-gray-500 dark:text-gray-400"
                      data-tooltip={description}
                      data-tooltip-content="text"
                    >
                      {description}
                    </p>
                  </div>
                  {hook.version !== "" && (
                    <span
                      className="hidden shrink-0 text-xs text-gray-400 sm:block dark:text-gray-500"
                      data-tooltip={hook.version}
                    >
                      {hook.version}
                    </span>
                  )}
                  {/* Icon-only row actions (the Skills tab's pair: neutral bordered icon for export,
                      danger variant with red text/hover for delete); the tooltip + aria-label carry the wording. */}
                  <Button
                    size="icon"
                    title={S.hooks.exportHook}
                    aria-label={`${S.hooks.exportHook} ${hook.name}`}
                    disabled={busy}
                    onClick={() => void exportHook(hook.name)}
                  >
                    <DownloadIcon size={14} className="text-gray-600 dark:text-gray-300" />
                  </Button>
                  <Button
                    size="icon"
                    variant="danger"
                    title={S.skills.uninstall}
                    aria-label={`${S.skills.uninstall} ${hook.name}`}
                    disabled={busy}
                    onClick={() => askRemove(hook.name)}
                  >
                    <GlyphIcon d={ICONS.trash} size={14} />
                  </Button>
                </div>
                {/* An export that did not land: this row still holds the button that retries it,
                    so the reason sits under it rather than in a notification that dismisses
                    itself (hook-export-refusal.ts). */}
                {refusal !== undefined && (
                  <p
                    role="alert"
                    data-testid="hook-export-refusal"
                    data-tooltip={refusal}
                    data-tooltip-content="text"
                    className={`px-3 pb-2.5 line-clamp-2 break-words text-xs ${toneInk.danger}`}
                  >
                    {refusal}
                  </p>
                )}
              </div>
            );
          })}
        </Card>
      )}

      {/* Import modal: recommended chat import on top, zip upload below (the Skills tab's shape). */}
      <Modal
        open={importOpen}
        title={S.hooks.importHook}
        onClose={() => setImportOpen(false)}
        widthClass="sm:max-w-lg"
      >
        <div className="space-y-4">
          <section>
            <p className="text-sm font-medium">{S.hooks.importChatTitle}</p>
            <p className="mt-0.5 text-xs text-gray-500 dark:text-gray-400">
              {S.hooks.importChatWhy}
            </p>
            <div className="mt-2.5 space-y-3">
              {/* A source may be a whole pasted hooks config block, so the field is multi-line. */}
              <Textarea
                label={S.hooks.importSourceLabel}
                hint={S.hooks.importSourceHint}
                size="sm"
                rows={3}
                value={source}
                onChange={(e) => setSource(e.target.value)}
                placeholder={S.hooks.importSourcePlaceholder}
              />
              <Textarea
                label={S.hooks.importPromptLabel}
                size="sm"
                rows={5}
                readOnly
                value={chatPrompt}
                className="text-gray-600 dark:text-gray-300"
              />
              <div className="flex gap-2">
                <Button
                  size="sm"
                  disabled={trimmedSource === ""}
                  onClick={() =>
                    promptCopy.flash(buildHookImportPrompt(trimmedSource, projectId, agentId))
                  }
                >
                  <CopyCheckGlyph copied={promptCopy.copied} size={12} />
                  {S.hooks.importCopyPrompt}
                </Button>
                <CopiedStatus copied={promptCopy.copied} />
                <Button
                  size="sm"
                  variant="primary"
                  disabled={trimmedSource === ""}
                  onClick={openChat}
                >
                  {S.hooks.importOpenChat}
                </Button>
              </div>
            </div>
          </section>

          <section className="border-t border-gray-200 pt-4 dark:border-gray-800">
            <p className="text-sm font-medium">{S.hooks.importUploadTitle}</p>
            <p className="mt-0.5 text-xs text-gray-500 dark:text-gray-400">
              {S.hooks.importUploadDesc}
            </p>
            <label
              className={`${UPLOAD_LABEL_CLASS} mt-2.5 ${uploading ? "pointer-events-none opacity-60" : ""}`}
            >
              <HiddenFileInput accept=".zip" disabled={uploading} onChange={onPickFile} />
              {uploading ? S.hooks.importUploading : S.hooks.importUploadAction}
            </label>
            {uploadError && (
              <p className="mt-1.5 text-xs text-red-600 dark:text-red-400">{uploadError}</p>
            )}
          </section>
        </div>
      </Modal>

      {/* Overwrite confirmation: the import modal stays underneath, so cancel returns to it; confirm resends the same zip with overwrite: true. */}
      <ConfirmModal
        open={overwriting !== null}
        title={S.hooks.importOverwriteTitle}
        confirmLabel={S.hooks.importOverwriteAction}
        cancelLabel={S.common.cancel}
        busy={uploading}
        onClose={() => setOverwriting(null)}
        onConfirm={() => {
          if (overwriting !== null) void upload(overwriting.dataBase64, overwriting.name, true);
        }}
      >
        <p className="text-sm text-gray-600 dark:text-gray-300">
          {overwriting !== null ? S.hooks.importOverwriteBody(overwriting.name) : ""}
        </p>
      </ConfirmModal>

      {/* Uninstall confirmation (shared ConfirmModal, the Skills tab's pattern): a refusal keeps
          this card and shows the server's words in it. */}
      <ConfirmModal
        open={removing !== null}
        title={removing !== null ? S.hooks.uninstallConfirmTitle(removing) : ""}
        busy={busy}
        onClose={() => setRemoving(null)}
        onConfirm={() => void confirmRemove()}
        confirmLabel={S.common.confirm}
        cancelLabel={S.common.cancel}
        refusal={removeRefusal === null ? undefined : S.common.refused(removeRefusal)}
      >
        <p className="text-sm text-gray-600 dark:text-gray-300">
          {removing !== null ? S.hooks.uninstallConfirmBody(removing, agentName) : ""}
        </p>
      </ConfirmModal>

      {error && <p className="text-xs text-red-600 dark:text-red-400">{error}</p>}
    </div>
  );
}