Skip to content

Self-improving AI Employees for everyone

43.8 KB
Raw Blame
/**
 * MasterSkillEditor - Editor for Master Skill node
 *
 * Split panel: left side has folder input, search, and skill toggles.
 * Right side shows selected skill's markdown instructions.
 *
 * Skills loaded from skillFolder (server/skills/<folder>/) or built-in list.
 * The skillsConfig uses skillName (folder name) as keys.
 *
 * User skills are created/edited inline in the right panel (no modal).
 */

import React, { useState, useEffect, useCallback, useMemo, useRef } from 'react';
import { Loader2, Info, Plus, Trash2, Save, X, RotateCcw, Search, Folder, Inbox } from 'lucide-react';

import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Checkbox } from '@/components/ui/checkbox';
import { Badge as DSBadge } from '@/components/ui/badge';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select';

import { ActionButton } from '@/components/ui/action-button';
import { Alert as DSAlert, AlertDescription } from '@/components/ui/alert';
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@/components/ui/tooltip';
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from '@/components/ui/alert-dialog';
import { toast } from 'sonner';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useNodeStatus, useWebSocket } from '../../contexts/WebSocketContext';
import { NodeIcon } from '../../assets/icons';
import { theme } from '../../styles/theme';
import { cn } from '@/lib/utils';
import { useFolderSkills } from '../../hooks/useFolderSkills';
import { USER_SKILLS_QUERY_KEY, useUserSkillsQuery, type UserSkill } from '../../hooks/useUserSkills';
import { useNodeAllowlist } from '../../hooks/useNodeAllowlist';
import { skillContentQueryKey } from '../../lib/skillContentQueryKey';

// Skill configuration stored in node parameters
// Key is skillName (folder name like 'whatsapp-skill'). Entries can carry
// more keys (a hired employee's Skills node adds `description`), so every
// edit below spreads the existing entry instead of rebuilding it.
interface SkillConfig {
  enabled: boolean;
  instructions: string;
  isCustomized: boolean;
  required?: boolean;
  description?: string;
}

interface MasterSkillConfig {
  [skillName: string]: SkillConfig;
}

interface AvailableSkill {
  type: string;        // Node type (e.g., 'whatsappSkill')
  skillName: string;   // Skill folder name (e.g., 'whatsapp-skill') - used as config key
  displayName: string;
  icon: string;
  color: string;
  description: string;
  isUserSkill?: boolean;  // True if this is a user-created skill from database
}

// Pending skill data for create/edit
interface PendingSkillData {
  name: string;
  display_name: string;
  description: string;
  instructions: string;
  icon: string;
  color: string;
}

// Stable empty-array references. `query.data ?? []` mints a NEW array
// identity every render, and the folder query is disabled (so `data`
// stays undefined) whenever the node's `skillFolder` is unset. Without
// a stable fallback the `availableSkills` memo recomputes on every
// render, and the "load selected user skill" effect setStates in an
// unbounded loop -> "Maximum update depth exceeded" -> the ErrorBoundary
// tears down the React Flow canvas.
const EMPTY_USER_SKILLS: UserSkill[] = [];
const EMPTY_FOLDER_SKILLS: AvailableSkill[] = [];
const REQUIRED_ASSISTANT_SKILL = 'skill';

/** The node's whole saved row (MasterSkillParams on the server). A direct
 *  `save_node_parameters` replaces the row, so both fields go every time,
 *  under the keys the panel and the agent read. */
const masterSkillRow = (skillsConfig: MasterSkillConfig, skillFolder: string | undefined, runtimeBehaviorVersion: number) => ({
  skills_config: skillsConfig,
  skill_folder: skillFolder || 'assistant',
  ...(runtimeBehaviorVersion === 1 ? { runtime_behavior_version: 1 } : {}),
});

interface MasterSkillEditorProps {
  skillsConfig: MasterSkillConfig;
  onConfigChange: (config: MasterSkillConfig) => void;
  skillFolder?: string;
  onSkillFolderChange?: (folder: string) => void;
  nodeId?: string;  // For persisting skillsConfig to database
  runtimeBehaviorVersion?: number;
}

// All skill-icon rendering routes through the shared <NodeIcon>
// primitive — see `assets/icons/NodeIcon.tsx`. Sizing comes from
// the wrapper's Tailwind classes (`h-4 w-4 text-base`); brand color
// flows via `color` and is picked up by lucide icons through
// currentColor (image / lobehub `.Color` icons ignore it).

const MasterSkillEditor: React.FC<MasterSkillEditorProps> = ({
  skillsConfig,
  onConfigChange,
  skillFolder,
  onSkillFolderChange,
  nodeId,
  runtimeBehaviorVersion = 0,
}) => {
  const { sendRequest } = useWebSocket();
  const masterSkillStatus = useNodeStatus(nodeId || '');
  const displayedSkillActivity = useMemo(() => {
    const active = (masterSkillStatus?.data?.active_skills as Array<{ name: string; state: string }> | undefined) ?? [];
    if (active.length > 0) return active;
    return (masterSkillStatus?.data?.last_skills as Array<{ name: string; state: string }> | undefined) ?? [];
  }, [masterSkillStatus?.data?.active_skills, masterSkillStatus?.data?.last_skills]);
  const activeSkillStates = useMemo(
    () => new Map(
      displayedSkillActivity.map((item) => [item.name, item.state]),
    ),
    [displayedSkillActivity],
  );
  const [selectedSkillName, setSelectedSkillName] = useState<string | null>(null);
  const [isLoading, setIsLoading] = useState(false);
  const [searchQuery, setSearchQuery] = useState('');
  const editorWrapperRef = useRef<HTMLDivElement>(null);
  // Tracks which skill's data has been loaded into the edit buffer so
  // the load effect below fires once per selection instead of on every
  // background list refetch (avoids the render loop + clobbering edits).
  const initializedSkillRef = useRef<string | null>(null);
  const scope = `${nodeId || ''}:${runtimeBehaviorVersion}`;
  const currentScope = useRef(scope);
  currentScope.current = scope;
  const contentKey = useCallback((name: string) => skillContentQueryKey(name, nodeId, runtimeBehaviorVersion), [nodeId, runtimeBehaviorVersion]);
  const contentScope = useMemo(() => runtimeBehaviorVersion === 1 && nodeId ? { master_skill_node_id: nodeId } : {}, [nodeId, runtimeBehaviorVersion]);
  useEffect(() => {
    if (runtimeBehaviorVersion !== 1) return;
    initializedSkillRef.current = null;
    setSelectedSkillName(null);
    setPendingSkillData(null);
    setIsCreatingNew(false);
  }, [nodeId, runtimeBehaviorVersion]);

  const queryClient = useQueryClient();
  const userSkillsQuery = useUserSkillsQuery();
  const userSkills = userSkillsQuery.data ?? EMPTY_USER_SKILLS;
  const invalidateUserSkills = useCallback(
    () => queryClient.invalidateQueries({ queryKey: USER_SKILLS_QUERY_KEY }),
    [queryClient],
  );

  const foldersQuery = useQuery<Array<{ name: string; skill_count: number }>, Error>({
    queryKey: ['skillFolders'],
    queryFn: async () => {
      const response = await sendRequest<{
        success: boolean;
        folders: Array<{ name: string; skill_count: number }>;
      }>('list_skill_folders', {});
      return response?.success ? (response.folders ?? []) : [];
    },
    staleTime: Infinity,
  });

  // Filter out skill folders disabled via the allowlist
  // (server/config/node_allowlist.json -> disabled_skill_folders).
  // Same mode-independent enforcement as the credential-category and
  // node-group blocklists. Use to auto-hide skill folders tied to a
  // disabled feature (e.g. `android_agent` when android nodes +
  // credentials are blocked).
  const { isSkillFolderDisabled } = useNodeAllowlist();
  const availableFolders = useMemo(
    () => (foldersQuery.data ?? []).filter((f) => !isSkillFolderDisabled(f.name)),
    [foldersQuery.data, isSkillFolderDisabled],
  );
  const foldersLoaded = !foldersQuery.isLoading;

  const folderSkillsQuery = useFolderSkills(skillFolder);
  const folderSkills = folderSkillsQuery.data ?? EMPTY_FOLDER_SKILLS;
  const folderLoading = folderSkillsQuery.isLoading;

  // Inline editing state (no modal)
  const [isCreatingNew, setIsCreatingNew] = useState(false);
  const [pendingSkillData, setPendingSkillData] = useState<PendingSkillData | null>(null);
  const [savingSkill, setSavingSkill] = useState(false);
  const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);

  // Native DOM keydown stops React Flow's document listener from eating Ctrl+A/C/etc.
  useEffect(() => {
    const el = editorWrapperRef.current;
    if (!el) return;

    const handler = (e: KeyboardEvent) => {
      if (e.ctrlKey || e.metaKey) {
        e.stopPropagation();
      }
    };
    el.addEventListener('keydown', handler);
    return () => el.removeEventListener('keydown', handler);
  });

  // Build list of available skills - from folder scan, node definitions, and user skills
  const availableSkills = useMemo<AvailableSkill[]>(() => {
    const skills: AvailableSkill[] = [];

    // Add folder skills (backend-sourced). The legacy fallback to
    // built-in skill nodeDefinitions has been removed — SKILL_NODE_TYPES
    // was ['masterSkill'] and the filter always returned an empty list.
    if (skillFolder && folderSkills.length > 0) {
      skills.push(...folderSkills);
    }

    // Add user-created skills. A built-in added to the owner's library from
    // Home is a copy under the same name: the folder's row already shows it.
    const folderNames = new Set(skills.map((skill) => skill.skillName));
    userSkills.forEach(us => {
      if (folderNames.has(us.name)) return;
      skills.push({
        type: 'userSkill',
        skillName: us.name,
        displayName: us.display_name,
        icon: us.icon || '',
        color: us.color || '#6366F1',
        description: us.description || '',
        isUserSkill: true
      });
    });

    return skills;
  }, [skillFolder, folderSkills, userSkills]);

  // Filter skills based on search query
  const filteredSkills = useMemo(() => {
    if (!searchQuery.trim()) return availableSkills;
    const query = searchQuery.toLowerCase();
    return availableSkills.filter(skill =>
      skill.displayName.toLowerCase().includes(query) ||
      skill.description.toLowerCase().includes(query)
    );
  }, [availableSkills, searchQuery]);


  // Auto-select first skill
  useEffect(() => {
    if (!selectedSkillName && !isCreatingNew && availableSkills.length > 0) {
      setSelectedSkillName(availableSkills[0].skillName);
    }
  }, [selectedSkillName, isCreatingNew, availableSkills]);

  const fetchSkillContent = useCallback(async (skillName: string): Promise<string> => {
    try {
      setIsLoading(true);
      const content = await queryClient.fetchQuery<string>({
        queryKey: contentKey(skillName),
        queryFn: async () => {
          const response = await sendRequest<{ instructions: string; success: boolean; error?: string }>(
            'get_skill_content',
            { skill_name: skillName, ...contentScope },
          );
          if (response?.success && response.instructions) return response.instructions;
          if (runtimeBehaviorVersion === 1) throw new Error(response?.error || 'Could not load the saved skill defaults.');
          console.warn('[MasterSkillEditor] No content returned for skill:', skillName, response?.error);
          return '';
        },
        staleTime: Infinity,
      });
      return content;
    } catch (error) {
      console.error('[MasterSkillEditor] Failed to load skill content:', error);
      return '';
    } finally {
      setIsLoading(false);
    }
  }, [queryClient, sendRequest, contentKey, contentScope, runtimeBehaviorVersion]);

  // Existing Master Skill nodes may predate the required Skill-tool entry.
  // Once the assistant catalogue is available, persist the authoritative
  // prompt into skills_config so the checkbox and editor behave exactly like
  // every other skill row rather than relying on a frontend-only illusion.
  useEffect(() => {
    if (skillFolder !== 'assistant') return;
    if (!folderSkills.some((skill) => skill.skillName === REQUIRED_ASSISTANT_SKILL)) return;
    const existing = skillsConfig[REQUIRED_ASSISTANT_SKILL];
    if (existing?.enabled && existing?.instructions) return;
    let cancelled = false;
    void fetchSkillContent(REQUIRED_ASSISTANT_SKILL).then((instructions) => {
      if (cancelled || !instructions) return;
      onConfigChange({
        ...skillsConfig,
        [REQUIRED_ASSISTANT_SKILL]: {
          ...existing,
          enabled: true,
          required: true,
          instructions: existing?.instructions || instructions,
          isCustomized: existing?.isCustomized || false,
        },
      });
    });
    return () => { cancelled = true; };
  }, [skillFolder, folderSkills, skillsConfig, fetchSkillContent, onConfigChange]);

  const getCachedSkillContent = useCallback(
    (skillName: string): string | undefined => queryClient.getQueryData<string>(contentKey(skillName)),
    [queryClient, contentKey],
  );

  useEffect(() => {
    if (!selectedSkillName || isCreatingNew) return;
    const skillExists = availableSkills.some(s => s.skillName === selectedSkillName);
    if (!skillExists) return;
    if (getCachedSkillContent(selectedSkillName) !== undefined) return;
    fetchSkillContent(selectedSkillName);
  }, [selectedSkillName, isCreatingNew, fetchSkillContent, getCachedSkillContent, availableSkills]);

  // Background prefetch: as soon as the folder's skills are known,
  // queue up `get_skill_content` for each in parallel so the per-skill
  // round-trip is already cached when the user clicks a checkbox.
  // Without this, the first toggle of each skill awaits the WS call
  // inside handleToggleSkill, which the user perceives as "slow toggle"
  // -- the checkbox visually lags by the network round-trip.
  // queryClient.prefetchQuery is a no-op when the data is already
  // cached, so subsequent panel opens are free.
  useEffect(() => {
    if (availableSkills.length === 0) return;
    for (const skill of availableSkills) {
      if (skill.isUserSkill) continue;
      if (getCachedSkillContent(skill.skillName) !== undefined) continue;
      queryClient.prefetchQuery({
        queryKey: contentKey(skill.skillName),
        queryFn: async () => {
          const response = await sendRequest<{
            instructions: string;
            success: boolean;
            error?: string;
          }>('get_skill_content', { skill_name: skill.skillName, ...contentScope });
          if (runtimeBehaviorVersion === 1 && !(response?.success && response.instructions)) throw new Error(response?.error || 'Could not load the saved skill defaults.');
          return (response?.success && response.instructions) || '';
        },
        staleTime: Infinity,
      });
    }
  }, [availableSkills, queryClient, sendRequest, getCachedSkillContent, contentKey, contentScope, runtimeBehaviorVersion]);

  // When selecting a user skill, load its data into pendingSkillData for editing.
  // Guarded by initializedSkillRef so it runs ONCE per selection -- re-running on
  // every availableSkills/userSkills refetch would (a) clobber in-progress edits
  // and (b) drive an infinite setState loop (setPendingSkillData with a fresh
  // object every render). The ref is only advanced once the skill's data is
  // actually present, so a selection made before its data loads still resolves.
  useEffect(() => {
    if (isCreatingNew || !selectedSkillName) {
      initializedSkillRef.current = null;
      return;
    }
    if (initializedSkillRef.current === selectedSkillName) return;

    const selectedInfo = availableSkills.find(s => s.skillName === selectedSkillName);
    if (!selectedInfo) return; // not in the list yet -- retry when it loads

    if (selectedInfo.isUserSkill) {
      const userSkill = userSkills.find(us => us.name === selectedSkillName);
      if (!userSkill) return; // user-skill data not loaded yet -- retry
      setPendingSkillData({
        name: userSkill.name,
        display_name: userSkill.display_name,
        description: userSkill.description,
        instructions: userSkill.instructions,
        icon: userSkill.icon || '',
        color: userSkill.color || '#6366F1'
      });
      setHasUnsavedChanges(false);
    } else {
      setPendingSkillData(null);
      setHasUnsavedChanges(false);
    }
    initializedSkillRef.current = selectedSkillName;
  }, [selectedSkillName, isCreatingNew, availableSkills, userSkills]);

  // Toggle skill enabled/disabled
  const handleToggleSkill = useCallback(async (skillName: string, enabled: boolean) => {
    const currentConfig = skillsConfig[skillName];

    if (enabled && !currentConfig?.instructions) {
      // Load default instructions when enabling for first time
      const defaultContent = await fetchSkillContent(skillName);
      if (runtimeBehaviorVersion === 1 && currentScope.current !== scope) return;
      if (runtimeBehaviorVersion === 1 && !defaultContent) { toast.error('Could not load the saved skill defaults. Try again.'); return; }
      onConfigChange({
        ...skillsConfig,
        [skillName]: { ...currentConfig, enabled: true, instructions: defaultContent, isCustomized: false }
      });
    } else {
      onConfigChange({
        ...skillsConfig,
        [skillName]: {
          ...currentConfig,
          enabled,
          instructions: currentConfig?.instructions || '',
          isCustomized: currentConfig?.isCustomized || false
        }
      });
    }
  }, [skillsConfig, onConfigChange, fetchSkillContent, runtimeBehaviorVersion, scope]);

  const handleUpdateInstructions = useCallback((skillName: string, instructions: string) => {
    const currentConfig = skillsConfig[skillName];
    const defaultContent = getCachedSkillContent(skillName) ?? '';
    const isCustomized = instructions !== defaultContent;

    onConfigChange({
      ...skillsConfig,
      [skillName]: {
        ...currentConfig,
        enabled: currentConfig?.enabled || false,
        instructions,
        isCustomized
      }
    });
  }, [skillsConfig, onConfigChange, getCachedSkillContent]);

  const handleResetToDefault = useCallback(async (skillName: string) => {
    queryClient.removeQueries({ queryKey: contentKey(skillName), exact: true });
    const defaultContent = await fetchSkillContent(skillName);
    if (runtimeBehaviorVersion === 1 && currentScope.current !== scope) return;
    if (runtimeBehaviorVersion === 1 && !defaultContent) { toast.error('Could not load the saved skill defaults. Try again.'); return; }
    const currentConfig = skillsConfig[skillName];

    onConfigChange({
      ...skillsConfig,
      [skillName]: { ...currentConfig, enabled: currentConfig?.enabled || false, instructions: defaultContent, isCustomized: false }
    });
  }, [skillsConfig, onConfigChange, fetchSkillContent, queryClient, contentKey, scope, runtimeBehaviorVersion]);

  // Create new skill - show inline editor
  const handleCreateSkill = useCallback(() => {
    setIsCreatingNew(true);
    setSelectedSkillName(null);
    setPendingSkillData({
      name: '',
      display_name: '',
      description: '',
      instructions: '# Skill Instructions\n\nDescribe what this skill does and how the AI should use it.',
      icon: '',
      color: '#6366F1'
    });
    setHasUnsavedChanges(false);
  }, []);

  // Cancel creating new skill
  const handleCancelCreate = useCallback(() => {
    setIsCreatingNew(false);
    setPendingSkillData(null);
    setHasUnsavedChanges(false);
    // Select first available skill
    if (availableSkills.length > 0) {
      setSelectedSkillName(availableSkills[0].skillName);
    }
  }, [availableSkills]);

  // Update pending skill data
  const handlePendingDataChange = useCallback((field: keyof PendingSkillData, value: string) => {
    setPendingSkillData(prev => prev ? { ...prev, [field]: value } : null);
    setHasUnsavedChanges(true);
  }, []);

  // Save skill (create or update)
  const handleSaveSkill = useCallback(async () => {
    if (!pendingSkillData) return;

    // Validate required fields
    if (!pendingSkillData.display_name.trim()) {
      toast.error('Display name is required');
      return;
    }
    if (!pendingSkillData.instructions.trim()) {
      toast.error('Instructions are required');
      return;
    }

    // For new skills, generate name from display_name if not provided
    let skillName = pendingSkillData.name;
    if (isCreatingNew) {
      if (!skillName.trim()) {
        skillName = pendingSkillData.display_name
          .toLowerCase()
          .replace(/[^a-z0-9]+/g, '-')
          .replace(/^-|-$/g, '');
      }
      if (!skillName || !/^[a-z0-9-]+$/.test(skillName)) {
        toast.error('Invalid skill ID. Use lowercase letters, numbers, and hyphens only.');
        return;
      }
    }

    setSavingSkill(true);
    try {
      const handler = isCreatingNew ? 'create_user_skill' : 'update_user_skill';
      // No `is_active`: a new skill starts on, and an edit keeps whether the
      // owner has it on for new hires (Home's Settings > Skills).
      const payload = {
        name: skillName,
        display_name: pendingSkillData.display_name,
        description: pendingSkillData.description,
        instructions: pendingSkillData.instructions,
        icon: pendingSkillData.icon,
        color: pendingSkillData.color,
        category: skillFolder || 'custom',
      };

      const result = await sendRequest<{ skill?: UserSkill; success?: boolean; error?: string }>(handler, payload);

      if (result.skill || result.success) {
        toast.success(isCreatingNew ? 'Skill created' : 'Skill saved');
        await invalidateUserSkills();

        if (isCreatingNew) {
          // Add to config as enabled, then persist to the database so the
          // new skill survives panel close / reload -- mirrors
          // handleDeleteSkill. Without the save_node_parameters call the
          // enabled state lives only in React state and is lost unless the
          // user also clicks the panel-level Save.
          const newConfig = {
            ...skillsConfig,
            [skillName]: { enabled: true, instructions: pendingSkillData.instructions, isCustomized: false }
          };
          onConfigChange(newConfig);
          if (nodeId) {
            await sendRequest('save_node_parameters', {
              node_id: nodeId,
              parameters: masterSkillRow(newConfig, skillFolder, runtimeBehaviorVersion),
            });
          }
          setIsCreatingNew(false);
          setSelectedSkillName(skillName);
        }
        setHasUnsavedChanges(false);
      } else {
        toast.error(result.error || 'Failed to save skill');
      }
    } catch (err: any) {
      toast.error(err.message || 'Failed to save skill');
    } finally {
      setSavingSkill(false);
    }
  }, [pendingSkillData, isCreatingNew, skillFolder, sendRequest, invalidateUserSkills, skillsConfig, onConfigChange, nodeId, runtimeBehaviorVersion]);

  // Delete user skill
  const handleDeleteSkill = useCallback(async (skillName: string) => {
    try {
      const result = await sendRequest<{ success: boolean; error?: string }>('delete_user_skill', { name: skillName });
      if (result.success) {
        toast.success('Skill deleted');
        // Remove from config
        const newConfig = { ...skillsConfig };
        delete newConfig[skillName];
        onConfigChange(newConfig);

        // Persist cleaned config to database so deleted skill doesn't reappear
        if (nodeId) {
          await sendRequest('save_node_parameters', {
            node_id: nodeId,
            parameters: masterSkillRow(newConfig, skillFolder, runtimeBehaviorVersion),
          });
        }

        // Clear selection if deleted skill was selected
        if (selectedSkillName === skillName) {
          setSelectedSkillName(null);
          setPendingSkillData(null);
        }
        // Refresh user skills list from database
        await invalidateUserSkills();
      } else {
        toast.error(result.error || 'Failed to delete skill');
      }
    } catch (err: any) {
      toast.error(err.message || 'Failed to delete skill');
    }
  }, [sendRequest, invalidateUserSkills, skillsConfig, onConfigChange, selectedSkillName, nodeId, skillFolder, runtimeBehaviorVersion]);

  const selectedSkillInfo = availableSkills.find(s => s.skillName === selectedSkillName);
  const selectedSkillConfig = selectedSkillName ? skillsConfig[selectedSkillName] : undefined;
  const enabledCount = Object.values(skillsConfig).filter(c => c?.enabled).length;
  const isEditingUserSkill = selectedSkillInfo?.isUserSkill && pendingSkillData;

  return (
    <div className="flex flex-1 min-h-0 gap-3 overflow-hidden">
      {/* Left Panel - Skills List */}
      <div className="flex flex-[0_0_260px] flex-col overflow-hidden rounded-md border border-border-default bg-bg-elevated">
        {/* Header with count and create button */}
        <div className="flex items-center justify-between border-b border-border-default bg-bg-panel p-2">
          <span className="font-display tracking-[var(--type-tracking-display)] [text-transform:var(--type-uppercase)] text-sm font-semibold text-fg-default">
            Skills
          </span>
          <div className="flex items-center gap-1">
            <DSBadge className="h-5 min-w-5 justify-center rounded-full bg-action-tools px-1.5 text-white">
              {enabledCount}
            </DSBadge>
            <TooltipProvider>
              <Tooltip>
                <TooltipTrigger asChild>
                  <ActionButton
                    intent="tools"
                    onClick={handleCreateSkill}
                    disabled={isCreatingNew}
                    className="h-7 px-2"
                    aria-label="Create new skill"
                  >
                    <Plus className="h-3.5 w-3.5" />
                  </ActionButton>
                </TooltipTrigger>
                <TooltipContent>Create new skill</TooltipContent>
              </Tooltip>
            </TooltipProvider>
          </div>
        </div>

        {/* Search */}
        <div className="border-b border-border p-2">
          <div className="relative">
            <Search className="pointer-events-none absolute top-1/2 left-2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
            <Input
              placeholder="Search skills..."
              value={searchQuery}
              onChange={(e) => setSearchQuery(e.target.value)}
              className="h-9 pl-8 pr-8"
            />
            {searchQuery && (
              <button
                type="button"
                onClick={() => setSearchQuery('')}
                className="absolute top-1/2 right-2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
                aria-label="Clear search"
              >
                <X className="h-4 w-4" />
              </button>
            )}
          </div>
        </div>

        {/* Skill Folder Dropdown */}
        <div className="shrink-0 border-b border-border p-2">
          <Select
            value={skillFolder || 'assistant'}
            onValueChange={(value) => {
              onSkillFolderChange?.(value);
              setSelectedSkillName(null);
            }}
            disabled={!foldersLoaded}
          >
            <SelectTrigger className="w-full">
              <div className="flex items-center gap-2">
                <Folder className="h-4 w-4 text-muted-foreground" />
                <SelectValue placeholder="Choose folder" />
              </div>
            </SelectTrigger>
            <SelectContent>
              {availableFolders.map((f) => (
                <SelectItem key={f.name} value={f.name}>
                  {f.name} ({f.skill_count})
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>

        {/* Skills List */}
        <div className="flex-1 overflow-y-auto">
          {folderLoading ? (
            <div className="mt-4 flex items-center justify-center gap-2 text-sm text-muted-foreground">
              <Loader2 className="h-4 w-4 animate-spin" />
              Scanning folder...
            </div>
          ) : filteredSkills.length === 0 ? (
            <div className="mt-6 flex flex-col items-center gap-2 p-6 text-center text-sm text-muted-foreground">
              <Inbox className="h-10 w-10 opacity-50" />
              <p>{skillFolder ? `No skills found in skills/${skillFolder}/` : 'No skills found'}</p>
            </div>
          ) : (
            <div className="divide-y divide-border">
              {filteredSkills.map((skill) => {
                const config = skillsConfig[skill.skillName];
                const isSelected = selectedSkillName === skill.skillName && !isCreatingNew;
                const isRequired = skillFolder === 'assistant' && skill.skillName === REQUIRED_ASSISTANT_SKILL;
                const isEnabled = isRequired || config?.enabled || false;
                const isCustomized = config?.isCustomized || false;
                const runtimeState = activeSkillStates.get(skill.skillName);

                return (
                  <div
                    key={skill.skillName}
                    onClick={() => {
                      if (isCreatingNew) {
                        if (hasUnsavedChanges) {
                          if (!confirm('Discard unsaved changes?')) return;
                        }
                        setIsCreatingNew(false);
                      }
                      setSelectedSkillName(skill.skillName);
                    }}
                    className={cn(
                      'cursor-pointer border-l-[3px] px-3 py-2 transition-colors',
                      isSelected ? 'bg-tint-soft' : 'border-l-transparent',
                    )}
                    // currentColor is the skill's brand color when
                    // selected; `bg-tint-soft` mixes it at the
                    // canonical alpha (--tint-soft) and the left
                    // border picks up the same color.
                    style={isSelected ? { color: skill.color, borderLeftColor: skill.color } : undefined}
                  >
                    <div className="flex w-full items-center gap-2">
                      <Checkbox
                        checked={isEnabled}
                        disabled={isRequired}
                        onCheckedChange={(checked) => {
                          handleToggleSkill(skill.skillName, checked === true);
                        }}
                        onClick={(e) => e.stopPropagation()}
                      />
                      <NodeIcon icon={skill.icon} size={theme.iconSize.sm} />
                      <span
                        className={cn(
                          'flex-1 overflow-hidden text-sm whitespace-nowrap text-ellipsis',
                          isSelected ? 'font-semibold' : 'font-medium',
                          isEnabled ? 'text-fg-default' : 'text-fg-muted',
                        )}
                      >
                        {skill.displayName}
                      </span>
                      {isCustomized && (
                        <TooltipProvider>
                          <Tooltip>
                            <TooltipTrigger asChild>
                              <div className="h-2 w-2 shrink-0 rounded-full bg-warning" />
                            </TooltipTrigger>
                            <TooltipContent>Customized</TooltipContent>
                          </Tooltip>
                        </TooltipProvider>
                      )}
                      {runtimeState && (
                        <DSBadge
                          className={cn(
                            'h-4 px-1 text-[10px]',
                            runtimeState === 'failed'
                              ? 'bg-destructive/20 text-destructive'
                              : runtimeState === 'loading'
                                ? 'bg-info/20 text-info'
                                : 'bg-success/20 text-success',
                          )}
                        >
                          {runtimeState === 'loading' ? 'Loading' : runtimeState === 'failed' ? 'Failed' : runtimeState === 'used' ? 'Used' : 'Loaded'}
                        </DSBadge>
                      )}
                      {isRequired && (
                        <DSBadge className="h-4 bg-agent/20 px-1 text-[10px] text-agent">
                          Required
                        </DSBadge>
                      )}
                      {skill.isUserSkill && (
                        <DSBadge className="h-4 bg-info/20 px-1 text-[10px] text-info">
                          Custom
                        </DSBadge>
                      )}
                    </div>
                  </div>
                );
              })}
            </div>
          )}
        </div>
      </div>

      {/* Right Panel - Skill Editor */}
      <div className="flex flex-1 flex-col overflow-hidden rounded-md border border-border-default bg-bg-elevated">
        {/* Creating new skill */}
        {isCreatingNew && pendingSkillData ? (
          <>
            {/* New Skill Header */}
            <div className="flex items-center gap-3 border-b border-border-default bg-bg-panel p-3">
              <div
                className="flex h-10 w-10 items-center justify-center rounded-md border-2 border-dashed bg-tint-soft"
                // currentColor is the new skill's brand color;
                // `bg-tint-soft` mixes it against transparent at the
                // canonical alpha (--tint-soft); the dashed border
                // and inner glyph pick up the same color.
                style={{ color: pendingSkillData.color }}
              >
                {pendingSkillData.icon
                  ? <NodeIcon icon={pendingSkillData.icon} size={theme.iconSize.md} />
                  : <Plus className="h-5 w-5" />}
              </div>
              <div className="flex-1">
                <div className="font-display tracking-[var(--type-tracking-display)] [text-transform:var(--type-uppercase)] text-sm font-semibold text-success">
                  Create New Skill
                </div>
                <div className="mt-0.5 text-xs text-fg-muted">
                  Fill in the details below and save
                </div>
              </div>
              <ActionButton intent="stop" onClick={handleCancelCreate} className="h-8">
                <X className="h-3.5 w-3.5" />
                Cancel
              </ActionButton>
              <ActionButton
                intent="save"
                onClick={handleSaveSkill}
                disabled={savingSkill}
                className="h-8"
              >
                {savingSkill ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Save className="h-3.5 w-3.5" />}
                Save
              </ActionButton>
            </div>

            {/* New Skill Form */}
            <div
              ref={editorWrapperRef}
              className="flex flex-1 flex-col gap-3 overflow-auto p-3"
            >
              {/* Display Name */}
              <div>
                <label className="mb-1 block text-xs text-fg-muted">
                  Display Name *
                </label>
                <Input
                  value={pendingSkillData.display_name}
                  onChange={(e) => handlePendingDataChange('display_name', e.target.value)}
                  placeholder="My Custom Skill"
                  className="bg-bg-input border-border-default"
                />
              </div>

              {/* Description */}
              <div>
                <label className="mb-1 block text-xs text-fg-muted">
                  Description
                </label>
                <Input
                  value={pendingSkillData.description}
                  onChange={(e) => handlePendingDataChange('description', e.target.value)}
                  placeholder="Brief description of what this skill does"
                  className="bg-bg-input border-border-default"
                />
              </div>

              {/* Icon and Color */}
              <div className="flex gap-3">
                <div className="w-[100px]">
                  <label className="mb-1 block text-xs text-fg-muted">
                    Icon (emoji)
                  </label>
                  <Input
                    value={pendingSkillData.icon}
                    onChange={(e) => handlePendingDataChange('icon', e.target.value)}
                    placeholder=""
                    className="bg-bg-input border-border-default text-center"
                  />
                </div>
                <div className="w-20">
                  <label className="mb-1 block text-xs text-fg-muted">
                    Color
                  </label>
                  <Input
                    type="color"
                    value={pendingSkillData.color}
                    onChange={(e) => handlePendingDataChange('color', e.target.value)}
                    className="h-8 bg-bg-input border-border-default p-0.5"
                  />
                </div>
              </div>

              {/* Instructions */}
              <div className="flex flex-1 flex-col min-h-[200px]">
                <label className="mb-1 block text-xs text-fg-muted">
                  Instructions *
                </label>
                <Textarea
                  value={pendingSkillData.instructions}
                  onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => handlePendingDataChange('instructions', e.target.value)}
                  placeholder="# Skill Instructions..."
                  spellCheck={false}
                  className="flex-1 min-h-0 resize-none rounded-md border-border-default bg-bg-input font-mono text-[13px] leading-[1.5] text-fg-default"
                />
              </div>
            </div>
          </>
        ) : selectedSkillInfo ? (
          <>
            {/* Skill Header */}
            <div className="flex items-center gap-3 border-b border-border-default bg-bg-panel p-3">
              <NodeIcon icon={selectedSkillInfo.icon} size={theme.iconSize.md} />
              <div className="flex-1">
                {isEditingUserSkill ? (
                  <Input
                    value={pendingSkillData?.display_name || ''}
                    onChange={(e) => handlePendingDataChange('display_name', e.target.value)}
                    className="mb-1 bg-bg-elevated border-border-default text-sm font-semibold"
                  />
                ) : (
                  <div className="font-display tracking-[var(--type-tracking-display)] [text-transform:var(--type-uppercase)] flex items-center gap-2 text-sm font-semibold text-fg-default">
                    {selectedSkillInfo.displayName}
                    {selectedSkillConfig?.isCustomized && (
                      <DSBadge className="h-4 bg-warning/20 text-xs text-warning">
                        Customized
                      </DSBadge>
                    )}
                  </div>
                )}
                {isEditingUserSkill ? (
                  <Input
                    value={pendingSkillData?.description || ''}
                    onChange={(e) => handlePendingDataChange('description', e.target.value)}
                    placeholder="Description"
                    className="bg-bg-elevated border-border-default text-xs"
                  />
                ) : (
                  <div className="mt-0.5 text-xs text-fg-muted">
                    {selectedSkillInfo.description}
                  </div>
                )}
              </div>

              {/* User skill actions */}
              {isEditingUserSkill && (
                <>
                  {hasUnsavedChanges && (
                    <ActionButton
                      intent="save"
                      onClick={handleSaveSkill}
                      disabled={savingSkill}
                      className="h-8"
                    >
                      {savingSkill ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Save className="h-3.5 w-3.5" />}
                      Save
                    </ActionButton>
                  )}
                  <AlertDialog>
                    <AlertDialogTrigger asChild>
                      <ActionButton intent="stop" className="h-8">
                        <Trash2 className="h-3.5 w-3.5" />
                        Delete
                      </ActionButton>
                    </AlertDialogTrigger>
                    <AlertDialogContent>
                      <AlertDialogHeader>
                        <AlertDialogTitle>Delete this skill?</AlertDialogTitle>
                        <AlertDialogDescription>
                          This action cannot be undone. The skill and any custom
                          instructions will be permanently removed.
                        </AlertDialogDescription>
                      </AlertDialogHeader>
                      <AlertDialogFooter>
                        <AlertDialogCancel>Cancel</AlertDialogCancel>
                        <AlertDialogAction
                          onClick={() => handleDeleteSkill(selectedSkillName!)}
                          className="bg-destructive text-white hover:bg-destructive/90"
                        >
                          Delete
                        </AlertDialogAction>
                      </AlertDialogFooter>
                    </AlertDialogContent>
                  </AlertDialog>
                </>
              )}

              {/* Reset Button for built-in skills */}
              {!isEditingUserSkill && selectedSkillConfig?.isCustomized && (
                <ActionButton
                  intent="config"
                  onClick={() => handleResetToDefault(selectedSkillName!)}
                  disabled={isLoading}
                  className="h-8"
                >
                  {isLoading ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <RotateCcw className="h-3.5 w-3.5" />}
                  Reset
                </ActionButton>
              )}
            </div>

            {/* Skill Instructions Editor */}
            <div
              ref={editorWrapperRef}
              className="flex flex-1 flex-col overflow-hidden p-3"
            >
              {isLoading ? (
                <div className="flex flex-1 items-center justify-center gap-2 text-sm text-muted-foreground">
                  <Loader2 className="h-4 w-4 animate-spin" />
                  Loading...
                </div>
              ) : (
                <Textarea
                  value={
                    isEditingUserSkill
                      ? pendingSkillData?.instructions || ''
                      : (selectedSkillConfig?.instructions || getCachedSkillContent(selectedSkillName!) || '')
                  }
                  onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => {
                    if (isEditingUserSkill) {
                      handlePendingDataChange('instructions', e.target.value);
                    } else {
                      handleUpdateInstructions(selectedSkillName!, e.target.value);
                    }
                  }}
                  placeholder="Loading skill instructions..."
                  spellCheck={false}
                  className="flex-1 min-h-0 resize-none rounded-md border-border-default bg-bg-input font-mono text-[13px] leading-[1.5] text-fg-default"
                />
              )}

              {/* Enable hint */}
              {!selectedSkillConfig?.enabled && (
                <DSAlert variant="info" className="mt-3">
                  <Info className="h-4 w-4" />
                  <AlertDescription>
                    Enable this skill to include it when running the AI Agent.
                  </AlertDescription>
                </DSAlert>
              )}
            </div>
          </>
        ) : (
          <div className="m-auto flex flex-col items-center gap-2 p-6 text-center text-sm text-muted-foreground">
            <Inbox className="h-10 w-10 opacity-50" />
            <p>Select a skill to view instructions</p>
          </div>
        )}
      </div>
    </div>
  );
};

export default MasterSkillEditor;