| 1 | import { parseConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | ArrowsPointingOutIcon, |
| 4 | BookmarkIcon as BookmarkOutline, |
| 5 | EyeIcon, |
| 6 | PencilIcon, |
| 7 | } from "@heroicons/react/24/outline"; |
| 8 | import { BookmarkIcon as BookmarkSolid } from "@heroicons/react/24/solid"; |
| 9 | import { |
| 10 | BrowserSerializedContinueConfig, |
| 11 | RuleSource, |
| 12 | RuleWithSource, |
| 13 | SlashCommandDescWithSource, |
| 14 | } from "core"; |
| 15 | import { |
| 16 | DEFAULT_AGENT_SYSTEM_MESSAGE, |
| 17 | DEFAULT_CHAT_SYSTEM_MESSAGE, |
| 18 | DEFAULT_PLAN_SYSTEM_MESSAGE, |
| 19 | DEFAULT_SYSTEM_MESSAGES_URL, |
| 20 | } from "core/llm/defaultSystemMessages"; |
| 21 | import { getRuleDisplayName } from "core/llm/rules/rules-utils"; |
| 22 | import { useContext, useMemo } from "react"; |
| 23 | import HeaderButtonWithToolTip from "../../../components/gui/HeaderButtonWithToolTip"; |
| 24 | import Switch from "../../../components/gui/Switch"; |
| 25 | import { useMainEditor } from "../../../components/mainInput/TipTapEditor"; |
| 26 | import { Card, EmptyState, useFontSize } from "../../../components/ui"; |
| 27 | import { useAuth } from "../../../context/Auth"; |
| 28 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 29 | import { useBookmarkedSlashCommands } from "../../../hooks/useBookmarkedSlashCommands"; |
| 30 | import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; |
| 31 | import { |
| 32 | DEFAULT_RULE_SETTING, |
| 33 | setDialogMessage, |
| 34 | setShowDialog, |
| 35 | toggleRuleSetting, |
| 36 | } from "../../../redux/slices/uiSlice"; |
| 37 | import { fontSize } from "../../../util"; |
| 38 | import { ConfigHeader } from "../components/ConfigHeader"; |
| 39 | |
| 40 | interface PromptCommandWithSlug extends SlashCommandDescWithSource { |
| 41 | slug?: string; |
| 42 | } |
| 43 | |
| 44 | interface PromptRowProps { |
| 45 | prompt: PromptCommandWithSlug; |
| 46 | isBookmarked: boolean; |
| 47 | setIsBookmarked: (isBookmarked: boolean) => void; |
| 48 | onEdit?: () => void; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Displays a single prompt row with bookmark and edit controls |
| 53 | */ |
| 54 | function PromptRow({ |
| 55 | prompt, |
| 56 | isBookmarked, |
| 57 | setIsBookmarked, |
| 58 | onEdit, |
| 59 | }: PromptRowProps) { |
| 60 | const { mainEditor } = useMainEditor(); |
| 61 | |
| 62 | const handlePromptClick = (e: React.MouseEvent) => { |
| 63 | e.stopPropagation(); |
| 64 | mainEditor?.commands.insertPrompt({ |
| 65 | title: prompt.name, |
| 66 | description: prompt.description, |
| 67 | content: prompt.prompt, |
| 68 | }); |
| 69 | }; |
| 70 | |
| 71 | const handleBookmarkClick = (e: React.MouseEvent) => { |
| 72 | e.stopPropagation(); |
| 73 | setIsBookmarked(!isBookmarked); |
| 74 | }; |
| 75 | |
| 76 | const handleEditClick = (e: React.MouseEvent) => { |
| 77 | e.stopPropagation(); |
| 78 | if (onEdit) { |
| 79 | onEdit(); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | const canEdit = |
| 84 | prompt.promptFile && !prompt.promptFile.startsWith("builtin:"); |
| 85 | |
| 86 | return ( |
| 87 | <div |
| 88 | className="hover:bg-list-active hover:text-list-active-foreground flex items-center justify-between gap-3 rounded-md px-2 py-1 hover:cursor-pointer" |
| 89 | onClick={handlePromptClick} |
| 90 | style={{ |
| 91 | fontSize: fontSize(-3), |
| 92 | }} |
| 93 | > |
| 94 | <div className="flex min-w-0 flex-col"> |
| 95 | <span className="text-vscForeground shrink-0 font-medium"> |
| 96 | {prompt.name} |
| 97 | </span> |
| 98 | <span className="line-clamp-2 text-[11px] text-gray-400"> |
| 99 | {prompt.description} |
| 100 | </span> |
| 101 | </div> |
| 102 | <div className="flex items-center gap-2"> |
| 103 | <PencilIcon |
| 104 | className={`h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125 ${!canEdit ? "pointer-events-none cursor-not-allowed opacity-50" : ""}`} |
| 105 | onClick={canEdit ? handleEditClick : undefined} |
| 106 | aria-disabled={!canEdit} |
| 107 | /> |
| 108 | <div |
| 109 | onClick={handleBookmarkClick} |
| 110 | className="cursor-pointer pt-0.5 text-gray-400 hover:brightness-125" |
| 111 | > |
| 112 | {isBookmarked ? ( |
| 113 | <BookmarkSolid className="h-3 w-3" /> |
| 114 | ) : ( |
| 115 | <BookmarkOutline className="h-3 w-3" /> |
| 116 | )} |
| 117 | </div> |
| 118 | </div> |
| 119 | </div> |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | interface RuleCardProps { |
| 124 | rule: RuleWithSource; |
| 125 | } |
| 126 | |
| 127 | const RuleCard: React.FC<RuleCardProps> = ({ rule }) => { |
| 128 | const dispatch = useAppDispatch(); |
| 129 | const ideMessenger = useContext(IdeMessengerContext); |
| 130 | const mode = useAppSelector((store) => store.session.mode); |
| 131 | const policy = useAppSelector((state) => |
| 132 | rule.name |
| 133 | ? state.ui.ruleSettings[rule.name] || DEFAULT_RULE_SETTING |
| 134 | : undefined, |
| 135 | ); |
| 136 | |
| 137 | const isDisabled = policy === "off"; |
| 138 | |
| 139 | const handleOpen = async () => { |
| 140 | if (rule.slug) { |
| 141 | void ideMessenger.request("controlPlane/openUrl", { |
| 142 | path: `${rule.slug}/new-version`, |
| 143 | orgSlug: undefined, |
| 144 | }); |
| 145 | } else if (rule.ruleFile) { |
| 146 | ideMessenger.post("openFile", { |
| 147 | path: rule.ruleFile, |
| 148 | }); |
| 149 | } else if ( |
| 150 | rule.source === "default-chat" || |
| 151 | rule.source === "default-plan" || |
| 152 | rule.source === "default-agent" |
| 153 | ) { |
| 154 | ideMessenger.post("openUrl", DEFAULT_SYSTEM_MESSAGES_URL); |
| 155 | } else { |
| 156 | ideMessenger.post("config/openProfile", { |
| 157 | profileId: undefined, |
| 158 | element: { sourceFile: (rule as any).sourceFile }, |
| 159 | }); |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | const handleTogglePolicy = () => { |
| 164 | if (rule.name) { |
| 165 | dispatch(toggleRuleSetting(rule.name)); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | const title = useMemo(() => { |
| 170 | return getRuleDisplayName(rule); |
| 171 | }, [rule]); |
| 172 | |
| 173 | function onClickExpand() { |
| 174 | dispatch(setShowDialog(true)); |
| 175 | dispatch( |
| 176 | setDialogMessage( |
| 177 | <div className="max-h-4/5 p-4"> |
| 178 | <h3>{title}</h3> |
| 179 | <pre className="max-w-full overflow-scroll">{rule.rule}</pre> |
| 180 | </div>, |
| 181 | ), |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | const smallFont = useFontSize(-2); |
| 186 | const tinyFont = useFontSize(-3); |
| 187 | return ( |
| 188 | <div |
| 189 | className={`border-border flex flex-col rounded-sm px-2 py-1.5 transition-colors ${isDisabled ? "opacity-50" : ""}`} |
| 190 | > |
| 191 | <div className="flex flex-col"> |
| 192 | <div className="flex flex-row justify-between gap-1"> |
| 193 | <span |
| 194 | className={`line-clamp-2 ${isDisabled ? "text-gray-400" : "text-vsc-foreground"}`} |
| 195 | style={{ |
| 196 | fontSize: smallFont, |
| 197 | }} |
| 198 | > |
| 199 | {title} |
| 200 | </span> |
| 201 | <div className="flex flex-row items-center gap-2"> |
| 202 | {rule.name && policy && ( |
| 203 | <div className="flex cursor-pointer flex-row items-center justify-end gap-1 px-2 py-0.5"> |
| 204 | <Switch |
| 205 | isToggled={policy === "on"} |
| 206 | onToggle={() => handleTogglePolicy()} |
| 207 | size={10} |
| 208 | text="" |
| 209 | /> |
| 210 | </div> |
| 211 | )} |
| 212 | <div className="flex flex-row items-start gap-1"> |
| 213 | <HeaderButtonWithToolTip onClick={onClickExpand} text="Expand"> |
| 214 | <ArrowsPointingOutIcon className="h-3 w-3 text-gray-400" /> |
| 215 | </HeaderButtonWithToolTip>{" "} |
| 216 | {rule.source === "default-chat" || |
| 217 | rule.source === "default-agent" ? ( |
| 218 | <HeaderButtonWithToolTip onClick={handleOpen} text="View"> |
| 219 | <EyeIcon className="h-3 w-3 text-gray-400" /> |
| 220 | </HeaderButtonWithToolTip> |
| 221 | ) : ( |
| 222 | <HeaderButtonWithToolTip onClick={handleOpen} text="Edit"> |
| 223 | <PencilIcon className="h-3 w-3 text-gray-400" /> |
| 224 | </HeaderButtonWithToolTip> |
| 225 | )} |
| 226 | </div> |
| 227 | </div> |
| 228 | </div> |
| 229 | |
| 230 | <span |
| 231 | style={{ |
| 232 | fontSize: tinyFont, |
| 233 | }} |
| 234 | className={`mt-1 line-clamp-3 ${isDisabled ? "text-gray-500" : "text-gray-400"}`} |
| 235 | > |
| 236 | {rule.rule} |
| 237 | </span> |
| 238 | {rule.globs ? ( |
| 239 | <div |
| 240 | style={{ |
| 241 | fontSize: tinyFont, |
| 242 | }} |
| 243 | className="mt-1.5 flex flex-col gap-1" |
| 244 | > |
| 245 | <span className="italic">Applies to files</span> |
| 246 | <code |
| 247 | className={`line-clamp-1 px-1 py-0.5 ${isDisabled ? "text-gray-500" : "text-gray-400"}`} |
| 248 | > |
| 249 | {rule.globs} |
| 250 | </code> |
| 251 | </div> |
| 252 | ) : null} |
| 253 | </div> |
| 254 | </div> |
| 255 | ); |
| 256 | }; |
| 257 | |
| 258 | /** |
| 259 | * Section that displays all available prompts with bookmarking functionality |
| 260 | */ |
| 261 | function PromptsSubSection() { |
| 262 | const { selectedProfile } = useAuth(); |
| 263 | const { isCommandBookmarked, toggleBookmark } = useBookmarkedSlashCommands(); |
| 264 | const ideMessenger = useContext(IdeMessengerContext); |
| 265 | const isLocal = selectedProfile?.profileType === "local"; |
| 266 | |
| 267 | const slashCommands = useAppSelector( |
| 268 | (state) => state.config.config.slashCommands ?? [], |
| 269 | ); |
| 270 | |
| 271 | const handleEdit = (prompt: PromptCommandWithSlug) => { |
| 272 | if (prompt.promptFile) { |
| 273 | ideMessenger.post("openFile", { |
| 274 | path: prompt.promptFile, |
| 275 | }); |
| 276 | } else if (prompt.slug) { |
| 277 | void ideMessenger.request("controlPlane/openUrl", { |
| 278 | path: `${prompt.slug}/new-version`, |
| 279 | orgSlug: undefined, |
| 280 | }); |
| 281 | } else { |
| 282 | ideMessenger.post("config/openProfile", { |
| 283 | profileId: undefined, |
| 284 | element: { sourceFile: (prompt as any).sourceFile }, |
| 285 | }); |
| 286 | } |
| 287 | }; |
| 288 | |
| 289 | const handleAddPrompt = () => { |
| 290 | if (isLocal) { |
| 291 | void ideMessenger.request("config/addLocalWorkspaceBlock", { |
| 292 | blockType: "prompts", |
| 293 | }); |
| 294 | } else { |
| 295 | void ideMessenger.request("controlPlane/openUrl", { |
| 296 | path: "?type=prompts", |
| 297 | orgSlug: undefined, |
| 298 | }); |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | const sortedCommands = useMemo(() => { |
| 303 | const promptsWithSlug: PromptCommandWithSlug[] = |
| 304 | structuredClone(slashCommands); |
| 305 | // get the slugs from rawYaml |
| 306 | if (selectedProfile?.rawYaml) { |
| 307 | const parsed = parseConfigYaml(selectedProfile.rawYaml); |
| 308 | const parsedPrompts = parsed.prompts ?? []; |
| 309 | |
| 310 | let index = 0; |
| 311 | for (const commandWithSlug of promptsWithSlug) { |
| 312 | // skip for local prompt files |
| 313 | if (commandWithSlug.promptFile) continue; |
| 314 | |
| 315 | const yamlPrompt = parsedPrompts[index]; |
| 316 | if (yamlPrompt) { |
| 317 | if ("uses" in yamlPrompt) { |
| 318 | commandWithSlug.slug = yamlPrompt.uses; |
| 319 | } else { |
| 320 | commandWithSlug.slug = `${selectedProfile?.fullSlug.ownerSlug}/${selectedProfile?.fullSlug.packageSlug}`; |
| 321 | } |
| 322 | } |
| 323 | index = index + 1; |
| 324 | } |
| 325 | } |
| 326 | return promptsWithSlug.sort((a, b) => { |
| 327 | const aBookmarked = isCommandBookmarked(a.name); |
| 328 | const bBookmarked = isCommandBookmarked(b.name); |
| 329 | if (aBookmarked && !bBookmarked) return -1; |
| 330 | if (!aBookmarked && bBookmarked) return 1; |
| 331 | return 0; |
| 332 | }); |
| 333 | }, [slashCommands, isCommandBookmarked, selectedProfile]); |
| 334 | |
| 335 | return ( |
| 336 | <div> |
| 337 | <ConfigHeader |
| 338 | title="Prompts" |
| 339 | variant="sm" |
| 340 | onAddClick={handleAddPrompt} |
| 341 | addButtonTooltip="Add prompt" |
| 342 | /> |
| 343 | |
| 344 | {sortedCommands.length > 0 ? ( |
| 345 | <Card> |
| 346 | <div> |
| 347 | {sortedCommands.map((prompt) => ( |
| 348 | <PromptRow |
| 349 | key={prompt.name} |
| 350 | prompt={prompt} |
| 351 | isBookmarked={isCommandBookmarked(prompt.name)} |
| 352 | setIsBookmarked={() => toggleBookmark(prompt)} |
| 353 | onEdit={() => handleEdit(prompt)} |
| 354 | /> |
| 355 | ))} |
| 356 | </div> |
| 357 | </Card> |
| 358 | ) : ( |
| 359 | <Card> |
| 360 | <EmptyState message="No prompts configured. Click the + button to add your first prompt." /> |
| 361 | </Card> |
| 362 | )} |
| 363 | </div> |
| 364 | ); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Helper function to add the appropriate default system message based on mode |
| 369 | */ |
| 370 | function addDefaultSystemMessage( |
| 371 | rules: RuleWithSource[], |
| 372 | mode: string, |
| 373 | config: BrowserSerializedContinueConfig, |
| 374 | ) { |
| 375 | const modeConfig = { |
| 376 | chat: { |
| 377 | customMessage: config.selectedModelByRole.chat?.baseChatSystemMessage, |
| 378 | defaultMessage: DEFAULT_CHAT_SYSTEM_MESSAGE, |
| 379 | customSource: "model-options-chat" as RuleSource, |
| 380 | defaultSource: "default-chat" as RuleSource, |
| 381 | }, |
| 382 | agent: { |
| 383 | customMessage: config.selectedModelByRole.chat?.baseAgentSystemMessage, |
| 384 | defaultMessage: DEFAULT_AGENT_SYSTEM_MESSAGE, |
| 385 | customSource: "model-options-agent" as RuleSource, |
| 386 | defaultSource: "default-agent" as RuleSource, |
| 387 | }, |
| 388 | plan: { |
| 389 | customMessage: config.selectedModelByRole.chat?.basePlanSystemMessage, |
| 390 | defaultMessage: DEFAULT_PLAN_SYSTEM_MESSAGE, |
| 391 | customSource: "model-options-plan" as RuleSource, |
| 392 | defaultSource: "default-plan" as RuleSource, |
| 393 | }, |
| 394 | }; |
| 395 | |
| 396 | const currentMode = modeConfig[mode as keyof typeof modeConfig]; |
| 397 | if (currentMode) { |
| 398 | const message = currentMode.customMessage || currentMode.defaultMessage; |
| 399 | const source = currentMode.customMessage |
| 400 | ? currentMode.customSource |
| 401 | : currentMode.defaultSource; |
| 402 | |
| 403 | rules.unshift({ |
| 404 | rule: message, |
| 405 | source, |
| 406 | }); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | function RulesSubSection() { |
| 411 | const { selectedProfile } = useAuth(); |
| 412 | const config = useAppSelector((store) => store.config.config); |
| 413 | const mode = useAppSelector((store) => store.session.mode); |
| 414 | const ideMessenger = useContext(IdeMessengerContext); |
| 415 | const isLocal = selectedProfile?.profileType === "local"; |
| 416 | |
| 417 | const handleAddRule = () => { |
| 418 | if (isLocal) { |
| 419 | void ideMessenger.request("config/addLocalWorkspaceBlock", { |
| 420 | blockType: "rules", |
| 421 | }); |
| 422 | } else { |
| 423 | void ideMessenger.request("controlPlane/openUrl", { |
| 424 | path: "?type=rules", |
| 425 | orgSlug: undefined, |
| 426 | }); |
| 427 | } |
| 428 | }; |
| 429 | |
| 430 | const sortedRules: RuleWithSource[] = useMemo(() => { |
| 431 | const rules = [...config.rules.map((rule) => ({ ...rule }))]; |
| 432 | |
| 433 | // Use profile rawYaml to infer slugs |
| 434 | if (selectedProfile?.rawYaml) { |
| 435 | try { |
| 436 | const parsed = parseConfigYaml(selectedProfile.rawYaml); |
| 437 | const parsedRules = parsed?.rules ?? []; |
| 438 | let index = 0; |
| 439 | for (const rule of rules) { |
| 440 | if (rule.source === "rules-block") { |
| 441 | let slug: string | undefined = undefined; |
| 442 | const yamlRule = parsedRules[index]; |
| 443 | if (yamlRule) { |
| 444 | if (typeof yamlRule !== "string" && "uses" in yamlRule) { |
| 445 | slug = yamlRule.uses; |
| 446 | } else { |
| 447 | slug = `${selectedProfile?.fullSlug.ownerSlug}/${selectedProfile?.fullSlug.packageSlug}`; |
| 448 | } |
| 449 | } |
| 450 | if (slug) { |
| 451 | rule.slug = slug; |
| 452 | } |
| 453 | |
| 454 | index++; |
| 455 | } |
| 456 | } |
| 457 | } catch (e) { |
| 458 | console.error( |
| 459 | "Rules notch section: failed to parse selected profile", |
| 460 | e, |
| 461 | ); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | addDefaultSystemMessage(rules, mode, config); |
| 466 | |
| 467 | return rules; |
| 468 | }, [config, selectedProfile, mode]); |
| 469 | |
| 470 | return ( |
| 471 | <div> |
| 472 | <ConfigHeader |
| 473 | title="Rules" |
| 474 | variant="sm" |
| 475 | onAddClick={handleAddRule} |
| 476 | addButtonTooltip="Add rule" |
| 477 | /> |
| 478 | |
| 479 | <Card> |
| 480 | {sortedRules.length > 0 ? ( |
| 481 | <div className="flex flex-col gap-3"> |
| 482 | {sortedRules.map((rule, index) => ( |
| 483 | <RuleCard key={index} rule={rule} /> |
| 484 | ))} |
| 485 | </div> |
| 486 | ) : ( |
| 487 | <EmptyState message="No rules configured. Click the + button to add your first rule." /> |
| 488 | )} |
| 489 | </Card> |
| 490 | </div> |
| 491 | ); |
| 492 | } |
| 493 | |
| 494 | export function RulesSection() { |
| 495 | return ( |
| 496 | <> |
| 497 | <ConfigHeader title="Rules" /> |
| 498 | |
| 499 | <div className="space-y-6"> |
| 500 | <RulesSubSection /> |
| 501 | <PromptsSubSection /> |
| 502 | </div> |
| 503 | </> |
| 504 | ); |
| 505 | } |