| 1 | import { DocumentTextIcon, GlobeAltIcon } from "@heroicons/react/24/outline"; |
| 2 | import { RuleWithSource } from "core"; |
| 3 | import { getLastNPathParts } from "core/util/uri"; |
| 4 | import { ComponentType, useMemo, useState } from "react"; |
| 5 | import ToggleDiv from "../../ToggleDiv"; |
| 6 | |
| 7 | interface RulesPeekProps { |
| 8 | appliedRules?: RuleWithSource[]; |
| 9 | icon?: ComponentType<React.SVGProps<SVGSVGElement>>; |
| 10 | } |
| 11 | |
| 12 | interface RulesPeekItemProps { |
| 13 | rule: RuleWithSource; |
| 14 | } |
| 15 | |
| 16 | // Convert technical source to user-friendly text |
| 17 | const getSourceLabel = (rule: RuleWithSource): string => { |
| 18 | switch (rule.source) { |
| 19 | case "default-chat": |
| 20 | return "Default Chat"; |
| 21 | case "default-agent": |
| 22 | return "Default Agent"; |
| 23 | case "model-options-chat": |
| 24 | return "Model Chat Options"; |
| 25 | case "model-options-plan": |
| 26 | return "Model Plan Options"; |
| 27 | case "model-options-agent": |
| 28 | return "Model Agent Options"; |
| 29 | case "rules-block": |
| 30 | return "Rules Block"; |
| 31 | case "colocated-markdown": |
| 32 | if (rule.ruleFile) { |
| 33 | return getLastNPathParts(rule.ruleFile, 2); |
| 34 | } else { |
| 35 | return "rules.md"; |
| 36 | } |
| 37 | case "json-systemMessage": |
| 38 | return "System Message"; |
| 39 | case ".continuerules": |
| 40 | return "Project Rules"; |
| 41 | default: |
| 42 | return rule.source; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | export function RulesPeekItem({ rule }: RulesPeekItemProps) { |
| 47 | const isGlobal = rule.alwaysApply ?? !rule.globs; |
| 48 | const [expanded, setExpanded] = useState(false); |
| 49 | |
| 50 | // Define maximum length for rule text display |
| 51 | const maxRuleLength = 100; |
| 52 | const isRuleLong = rule.rule.length > maxRuleLength; |
| 53 | |
| 54 | // Get the displayed rule text based on expanded state |
| 55 | const displayedRule = |
| 56 | isRuleLong && !expanded |
| 57 | ? `${rule.rule.slice(0, maxRuleLength)}...` |
| 58 | : rule.rule; |
| 59 | |
| 60 | const toggleExpand = () => { |
| 61 | if (isRuleLong) { |
| 62 | setExpanded(!expanded); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | return ( |
| 67 | <div |
| 68 | className={`group mr-2 flex flex-col overflow-hidden rounded px-1.5 py-1 text-xs hover:bg-white/10 ${isRuleLong ? "cursor-pointer hover:text-gray-200" : ""}`} |
| 69 | data-testid="rules-peek-item" |
| 70 | onClick={toggleExpand} |
| 71 | > |
| 72 | <div className="flex w-full items-center"> |
| 73 | {isGlobal ? ( |
| 74 | <GlobeAltIcon className="text-description-muted mr-2 h-4 w-4 flex-shrink-0" /> |
| 75 | ) : ( |
| 76 | <DocumentTextIcon className="text-description-muted mr-2 h-4 w-4 flex-shrink-0" /> |
| 77 | )} |
| 78 | |
| 79 | <div className="flex min-w-0 flex-1 gap-2 text-xs"> |
| 80 | <div className="max-w-[50%] flex-shrink-0 truncate font-medium"> |
| 81 | {rule.name || "Agent rule"} |
| 82 | </div> |
| 83 | |
| 84 | <div className="min-w-0 flex-1 overflow-hidden truncate whitespace-nowrap text-xs text-gray-500"> |
| 85 | {isGlobal |
| 86 | ? "Always applied" |
| 87 | : `Pattern: ${typeof rule.globs === "string" ? rule.globs : Array.isArray(rule.globs) ? rule.globs.join(", ") : ""}`} |
| 88 | </div> |
| 89 | </div> |
| 90 | </div> |
| 91 | <div |
| 92 | className={`mt-1 whitespace-pre-line pl-6 pr-2 text-xs italic text-gray-300`} |
| 93 | title={ |
| 94 | isRuleLong ? (expanded ? "Click to collapse" : "Click to expand") : "" |
| 95 | } |
| 96 | > |
| 97 | {displayedRule} |
| 98 | {isRuleLong && ( |
| 99 | <span className="text-description-muted ml-1 opacity-0 transition-opacity group-hover:opacity-100"> |
| 100 | {expanded ? "(collapse)" : "(expand)"} |
| 101 | </span> |
| 102 | )} |
| 103 | </div> |
| 104 | <div className="mt-1 pl-6 pr-2 text-xs text-gray-500"> |
| 105 | Source: {getSourceLabel(rule)} |
| 106 | </div> |
| 107 | </div> |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | export function RulesPeek({ appliedRules, icon }: RulesPeekProps) { |
| 112 | const rules = useMemo(() => { |
| 113 | return appliedRules ?? []; |
| 114 | }, [appliedRules]); |
| 115 | |
| 116 | if (!rules || rules.length === 0) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | return ( |
| 121 | <ToggleDiv |
| 122 | icon={icon} |
| 123 | title={`${rules.length} rule${rules.length > 1 ? "s" : ""}`} |
| 124 | testId="rules-peek" |
| 125 | > |
| 126 | {rules.map((rule, idx) => ( |
| 127 | <RulesPeekItem key={`rule-${idx}`} rule={rule} /> |
| 128 | ))} |
| 129 | </ToggleDiv> |
| 130 | ); |
| 131 | } |