Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

3.8 KB
Raw Blame
import { Sparkles } from 'lucide-react'
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover'
import { Button } from '@/components/ui/button'
import { Switch } from '@/components/ui/switch'
import { useTranslation } from '@/i18n/react-i18next-compat'
import { useSkills, effectiveEnabled, storedEnabled } from '@/hooks/useSkills'
import { cn } from '@/lib/utils'

/**
 * Top-of-input control showing which skills will be advertised to the agent this
 * run, with per-skill on/off toggles. Backed by the project's `[skills].enabled`
 * whitelist — an empty whitelist means every skill is enabled.
 */
export default function SkillSelector({ folder }: { folder: string | null }) {
  const { t } = useTranslation()
  const { skills, enabled, setEnabled } = useSkills(folder)

  if (skills.length === 0) return null

  const allNames = skills.map((s) => s.name)
  const effective = effectiveEnabled(enabled, allNames)
  // #8878: the global store is what the Cowork agent actually reads, so its
  // skills are shown and active even with no folder. The `[skills].enabled`
  // whitelist lives in a project's agent.toml, so with no folder it has nowhere
  // to persist -- every global skill is active and the toggle is informational.
  const scoped = folder !== null

  const toggle = (name: string) => {
    if (!scoped) return
    const next = new Set(effective)
    if (next.has(name)) next.delete(name)
    else next.add(name)
    setEnabled(storedEnabled(next, allNames))
  }

  return (
    <Popover>
      <PopoverTrigger asChild>
        <Button
          variant="ghost"
          size={effective.size > 0 ? 'xs' : 'icon-xs'}
          className="shrink-0 text-muted-foreground"
          title={t('common:skillsInContext')}
          aria-label={t('common:skillsInContext')}
        >
          <Sparkles
            className={cn(
              'shrink-0',
              effective.size > 0 ? 'size-3.5' : 'size-[18px]'
            )}
          />
          {effective.size > 0 && (
            <span className="tabular-nums text-foreground">
              {effective.size}/{allNames.length}
            </span>
          )}
        </Button>
      </PopoverTrigger>
      <PopoverContent align="end" className="w-72 p-1">
        <div className="px-2 py-1.5 text-xs text-muted-foreground">
          {t('common:skillsInContext')}
        </div>
        {!scoped && (
          <div className="px-2 pb-1.5 text-xs text-muted-foreground">
            {t('common:skillsScopeGlobal')}
          </div>
        )}
        <div className="max-h-64 overflow-y-auto">
          {skills.map((s) => (
            <label
              key={s.name}
              className="flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 hover:bg-accent"
            >
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-1.5 min-w-0">
                  <span className="truncate text-sm font-medium">{s.name}</span>
                  {scoped && s.origin && (
                    <span className="shrink-0 rounded bg-muted px-1 text-[10px] leading-4 text-muted-foreground">
                      {t(
                        s.origin === 'project'
                          ? 'common:skillOriginFolder'
                          : 'common:skillOriginGlobal'
                      )}
                    </span>
                  )}
                </div>
                {s.description && (
                  <div className="truncate text-xs text-muted-foreground">
                    {s.description}
                  </div>
                )}
              </div>
              <Switch
                checked={effective.has(s.name)}
                disabled={!scoped}
                onCheckedChange={() => toggle(s.name)}
              />
            </label>
          ))}
        </div>
      </PopoverContent>
    </Popover>
  )
}