| 1 | import { parseConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | ArrowsPointingOutIcon, |
| 4 | EyeIcon, |
| 5 | PencilIcon, |
| 6 | } from "@heroicons/react/24/outline"; |
| 7 | import { RuleWithSource } from "core"; |
| 8 | import { |
| 9 | DEFAULT_AGENT_SYSTEM_MESSAGE, |
| 10 | DEFAULT_CHAT_SYSTEM_MESSAGE, |
| 11 | DEFAULT_SYSTEM_MESSAGES_URL, |
| 12 | } from "core/llm/defaultSystemMessages"; |
| 13 | import { useContext, useMemo } from "react"; |
| 14 | import { useAuth } from "../../../../context/Auth"; |
| 15 | import { IdeMessengerContext } from "../../../../context/IdeMessenger"; |
| 16 | import { useAppDispatch, useAppSelector } from "../../../../redux/hooks"; |
| 17 | import { |
| 18 | DEFAULT_RULE_SETTING, |
| 19 | setDialogMessage, |
| 20 | setShowDialog, |
| 21 | toggleRuleSetting, |
| 22 | } from "../../../../redux/slices/uiSlice"; |
| 23 | import HeaderButtonWithToolTip from "../../../gui/HeaderButtonWithToolTip"; |
| 24 | import Switch from "../../../gui/Switch"; |
| 25 | import { useFontSize } from "../../../ui/font"; |
| 26 | import { ExploreBlocksButton } from "./ExploreBlocksButton"; |
| 27 | |
| 28 | interface RuleCardProps { |
| 29 | rule: RuleWithSource; |
| 30 | } |
| 31 | |
| 32 | const RuleCard: React.FC<RuleCardProps> = ({ rule }) => { |
| 33 | const dispatch = useAppDispatch(); |
| 34 | const ideMessenger = useContext(IdeMessengerContext); |
| 35 | const mode = useAppSelector((store) => store.session.mode); |
| 36 | const policy = useAppSelector((state) => |
| 37 | rule.name |
| 38 | ? state.ui.ruleSettings[rule.name] || DEFAULT_RULE_SETTING |
| 39 | : undefined, |
| 40 | ); |
| 41 | |
| 42 | const isDisabled = policy === "off"; |
| 43 | |
| 44 | const handleOpen = async () => { |
| 45 | if (rule.slug) { |
| 46 | void ideMessenger.request("controlPlane/openUrl", { |
| 47 | path: `${rule.slug}/new-version`, |
| 48 | orgSlug: undefined, |
| 49 | }); |
| 50 | } else if (rule.ruleFile) { |
| 51 | ideMessenger.post("openFile", { |
| 52 | path: rule.ruleFile, |
| 53 | }); |
| 54 | } else if ( |
| 55 | rule.source === "default-chat" || |
| 56 | rule.source === "default-plan" || |
| 57 | rule.source === "default-agent" |
| 58 | ) { |
| 59 | ideMessenger.post("openUrl", DEFAULT_SYSTEM_MESSAGES_URL); |
| 60 | } else { |
| 61 | ideMessenger.post("config/openProfile", { |
| 62 | profileId: undefined, |
| 63 | element: { sourceFile: (rule as any).sourceFile }, |
| 64 | }); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | const handleTogglePolicy = () => { |
| 69 | if (rule.name) { |
| 70 | dispatch(toggleRuleSetting(rule.name)); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | const title = useMemo(() => { |
| 75 | if (rule.name) { |
| 76 | return rule.name; |
| 77 | } else { |
| 78 | if (rule.source === ".continuerules") { |
| 79 | return "Project rules"; |
| 80 | } else if (rule.source === "default-chat") { |
| 81 | return "Default chat system message"; |
| 82 | } else if (rule.source === "default-agent") { |
| 83 | return "Default agent system message"; |
| 84 | } else if (rule.source === "json-systemMessage") { |
| 85 | return "JSON systemMessage)"; |
| 86 | } else if (rule.source === "model-options-agent") { |
| 87 | return "Base System Agent Message"; |
| 88 | } else if (rule.source === "model-options-plan") { |
| 89 | return "Base System Plan Message"; |
| 90 | } else if (rule.source === "model-options-chat") { |
| 91 | return "Base System Chat Message"; |
| 92 | } else { |
| 93 | return "Assistant rule"; |
| 94 | } |
| 95 | } |
| 96 | }, [rule]); |
| 97 | |
| 98 | function onClickExpand() { |
| 99 | dispatch(setShowDialog(true)); |
| 100 | dispatch( |
| 101 | setDialogMessage( |
| 102 | <div className="max-h-4/5 p-4"> |
| 103 | <h3>{title}</h3> |
| 104 | <pre className="max-w-full overflow-scroll">{rule.rule}</pre> |
| 105 | </div>, |
| 106 | ), |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | const smallFont = useFontSize(-2); |
| 111 | const tinyFont = useFontSize(-3); |
| 112 | return ( |
| 113 | <div |
| 114 | className={`border-border flex flex-col rounded-sm px-2 py-1.5 transition-colors ${isDisabled ? "opacity-50" : ""}`} |
| 115 | > |
| 116 | <div className="flex flex-col"> |
| 117 | <div className="flex flex-row justify-between gap-1"> |
| 118 | <span |
| 119 | className={`line-clamp-2 ${isDisabled ? "text-gray-400" : "text-vsc-foreground"}`} |
| 120 | style={{ |
| 121 | fontSize: smallFont, |
| 122 | }} |
| 123 | > |
| 124 | {title} |
| 125 | </span> |
| 126 | <div className="flex flex-row items-center gap-2"> |
| 127 | {rule.name && policy && ( |
| 128 | <div className="flex cursor-pointer flex-row items-center justify-end gap-1 px-2 py-0.5"> |
| 129 | <Switch |
| 130 | isToggled={policy === "on"} |
| 131 | onToggle={() => handleTogglePolicy()} |
| 132 | size={10} |
| 133 | text="" |
| 134 | /> |
| 135 | </div> |
| 136 | )} |
| 137 | <div className="flex flex-row items-start gap-1"> |
| 138 | <HeaderButtonWithToolTip onClick={onClickExpand} text="Expand"> |
| 139 | <ArrowsPointingOutIcon className="h-3 w-3 text-gray-400" /> |
| 140 | </HeaderButtonWithToolTip>{" "} |
| 141 | {rule.source === "default-chat" || |
| 142 | rule.source === "default-agent" ? ( |
| 143 | <HeaderButtonWithToolTip onClick={handleOpen} text="View"> |
| 144 | <EyeIcon className="h-3 w-3 text-gray-400" /> |
| 145 | </HeaderButtonWithToolTip> |
| 146 | ) : ( |
| 147 | <HeaderButtonWithToolTip onClick={handleOpen} text="Edit"> |
| 148 | <PencilIcon className="h-3 w-3 text-gray-400" /> |
| 149 | </HeaderButtonWithToolTip> |
| 150 | )} |
| 151 | </div> |
| 152 | </div> |
| 153 | </div> |
| 154 | |
| 155 | <span |
| 156 | style={{ |
| 157 | fontSize: tinyFont, |
| 158 | }} |
| 159 | className={`mt-1 line-clamp-3 ${isDisabled ? "text-gray-500" : "text-gray-400"}`} |
| 160 | > |
| 161 | {rule.rule} |
| 162 | </span> |
| 163 | {rule.globs ? ( |
| 164 | <div |
| 165 | style={{ |
| 166 | fontSize: tinyFont, |
| 167 | }} |
| 168 | className="mt-1.5 flex flex-col gap-1" |
| 169 | > |
| 170 | <span className="italic">Applies to files</span> |
| 171 | <code |
| 172 | className={`line-clamp-1 px-1 py-0.5 ${isDisabled ? "text-gray-500" : "text-gray-400"}`} |
| 173 | > |
| 174 | {rule.globs} |
| 175 | </code> |
| 176 | </div> |
| 177 | ) : null} |
| 178 | </div> |
| 179 | </div> |
| 180 | ); |
| 181 | }; |
| 182 | |
| 183 | export function RulesSection() { |
| 184 | const { selectedProfile } = useAuth(); |
| 185 | const config = useAppSelector((store) => store.config.config); |
| 186 | const mode = useAppSelector((store) => store.session.mode); |
| 187 | const sortedRules: RuleWithSource[] = useMemo(() => { |
| 188 | const rules = [...config.rules.map((rule) => ({ ...rule }))]; |
| 189 | |
| 190 | // Use profile rawYaml to infer slugs |
| 191 | if (selectedProfile?.rawYaml) { |
| 192 | try { |
| 193 | const parsed = parseConfigYaml(selectedProfile.rawYaml); |
| 194 | const parsedRules = parsed?.rules ?? []; |
| 195 | let index = 0; |
| 196 | for (const rule of rules) { |
| 197 | if (rule.source === "rules-block") { |
| 198 | let slug: string | undefined = undefined; |
| 199 | const yamlRule = parsedRules[index]; |
| 200 | if (yamlRule) { |
| 201 | if (typeof yamlRule !== "string" && "uses" in yamlRule) { |
| 202 | slug = yamlRule.uses; |
| 203 | } else { |
| 204 | slug = `${selectedProfile?.fullSlug.ownerSlug}/${selectedProfile?.fullSlug.packageSlug}`; |
| 205 | } |
| 206 | } |
| 207 | if (slug) { |
| 208 | rule.slug = slug; |
| 209 | } |
| 210 | |
| 211 | index++; |
| 212 | } |
| 213 | } |
| 214 | } catch (e) { |
| 215 | console.error( |
| 216 | "Rules notch section: failed to parse selected profile", |
| 217 | e, |
| 218 | ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (mode === "chat") { |
| 223 | if (config.selectedModelByRole.chat?.baseChatSystemMessage) { |
| 224 | rules.unshift({ |
| 225 | rule: config.selectedModelByRole.chat?.baseChatSystemMessage, |
| 226 | source: "model-options-chat", |
| 227 | }); |
| 228 | } else { |
| 229 | rules.unshift({ |
| 230 | rule: DEFAULT_CHAT_SYSTEM_MESSAGE, |
| 231 | source: "default-chat", |
| 232 | }); |
| 233 | } |
| 234 | } else if (mode === "agent") { |
| 235 | // agent |
| 236 | if (config.selectedModelByRole.chat?.baseAgentSystemMessage) { |
| 237 | rules.unshift({ |
| 238 | rule: config.selectedModelByRole.chat?.baseAgentSystemMessage, |
| 239 | source: "model-options-agent", |
| 240 | }); |
| 241 | } else { |
| 242 | rules.unshift({ |
| 243 | rule: DEFAULT_AGENT_SYSTEM_MESSAGE, |
| 244 | source: "default-agent", |
| 245 | }); |
| 246 | } |
| 247 | } else { |
| 248 | // plan |
| 249 | if (config.selectedModelByRole.chat?.basePlanSystemMessage) { |
| 250 | rules.unshift({ |
| 251 | rule: config.selectedModelByRole.chat?.basePlanSystemMessage, |
| 252 | source: "model-options-plan", |
| 253 | }); |
| 254 | } else { |
| 255 | rules.unshift({ |
| 256 | rule: DEFAULT_AGENT_SYSTEM_MESSAGE, |
| 257 | source: "default-agent", |
| 258 | }); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return rules; |
| 263 | }, [config, selectedProfile, mode]); |
| 264 | |
| 265 | return ( |
| 266 | <div className="flex flex-col gap-3"> |
| 267 | {sortedRules.map((rule, index) => ( |
| 268 | <RuleCard key={index} rule={rule} /> |
| 269 | ))} |
| 270 | <ExploreBlocksButton blockType="rules" /> |
| 271 | </div> |
| 272 | ); |
| 273 | } |