Elffuss BraidHub

kikocis/continue public

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