| 1 | import { parseConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | ArrowsPointingOutIcon, |
| 4 | BookmarkIcon as BookmarkOutline, |
| 5 | EyeIcon, |
| 6 | PencilIcon, |
| 7 | PlusIcon, |
| 8 | } from "@heroicons/react/24/outline"; |
| 9 | import { BookmarkIcon as BookmarkSolid } from "@heroicons/react/24/solid"; |
| 10 | import { |
| 11 | BrowserSerializedContinueConfig, |
| 12 | RuleSource, |
| 13 | RuleWithSource, |
| 14 | SlashCommandDescWithSource, |
| 15 | } from "core"; |
| 16 | import { |
| 17 | DEFAULT_AGENT_SYSTEM_MESSAGE, |
| 18 | DEFAULT_CHAT_SYSTEM_MESSAGE, |
| 19 | DEFAULT_PLAN_SYSTEM_MESSAGE, |
| 20 | DEFAULT_SYSTEM_MESSAGES_URL, |
| 21 | } from "core/llm/defaultSystemMessages"; |
| 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 { Button, 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 | if (rule.name) { |
| 171 | return rule.name; |
| 172 | } else { |
| 173 | if (rule.source === ".continuerules") { |
| 174 | return "Project rules"; |
| 175 | } else if (rule.source === "default-chat") { |
| 176 | return "Default chat system message"; |
| 177 | } else if (rule.source === "default-agent") { |
| 178 | return "Default agent system message"; |
| 179 | } else if (rule.source === "json-systemMessage") { |
| 180 | return "JSON systemMessage)"; |
| 181 | } else if (rule.source === "model-options-agent") { |
| 182 | return "Base System Agent Message"; |
| 183 | } else if (rule.source === "model-options-plan") { |
| 184 | return "Base System Plan Message"; |
| 185 | } else if (rule.source === "model-options-chat") { |
| 186 | return "Base System Chat Message"; |
| 187 | } else { |
| 188 | return "Agent rule"; |
| 189 | } |
| 190 | } |
| 191 | }, [rule]); |
| 192 | |
| 193 | function onClickExpand() { |
| 194 | dispatch(setShowDialog(true)); |
| 195 | dispatch( |
| 196 | setDialogMessage( |
| 197 | <div className="max-h-4/5 p-4"> |
| 198 | <h3>{title}</h3> |
| 199 | <pre className="max-w-full overflow-scroll">{rule.rule}</pre> |
| 200 | </div>, |
| 201 | ), |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | const smallFont = useFontSize(-2); |
| 206 | const tinyFont = useFontSize(-3); |
| 207 | return ( |
| 208 | <div |
| 209 | className={`border-border flex flex-col rounded-sm px-2 py-1.5 transition-colors ${isDisabled ? "opacity-50" : ""}`} |
| 210 | > |
| 211 | <div className="flex flex-col"> |
| 212 | <div className="flex flex-row justify-between gap-1"> |
| 213 | <span |
| 214 | className={`line-clamp-2 ${isDisabled ? "text-gray-400" : "text-vsc-foreground"}`} |
| 215 | style={{ |
| 216 | fontSize: smallFont, |
| 217 | }} |
| 218 | > |
| 219 | {title} |
| 220 | </span> |
| 221 | <div className="flex flex-row items-center gap-2"> |
| 222 | {rule.name && policy && ( |
| 223 | <div className="flex cursor-pointer flex-row items-center justify-end gap-1 px-2 py-0.5"> |
| 224 | <Switch |
| 225 | isToggled={policy === "on"} |
| 226 | onToggle={() => handleTogglePolicy()} |
| 227 | size={10} |
| 228 | text="" |
| 229 | /> |
| 230 | </div> |
| 231 | )} |
| 232 | <div className="flex flex-row items-start gap-1"> |
| 233 | <HeaderButtonWithToolTip onClick={onClickExpand} text="Expand"> |
| 234 | <ArrowsPointingOutIcon className="h-3 w-3 text-gray-400" /> |
| 235 | </HeaderButtonWithToolTip>{" "} |
| 236 | {rule.source === "default-chat" || |
| 237 | rule.source === "default-agent" ? ( |
| 238 | <HeaderButtonWithToolTip onClick={handleOpen} text="View"> |
| 239 | <EyeIcon className="h-3 w-3 text-gray-400" /> |
| 240 | </HeaderButtonWithToolTip> |
| 241 | ) : ( |
| 242 | <HeaderButtonWithToolTip onClick={handleOpen} text="Edit"> |
| 243 | <PencilIcon className="h-3 w-3 text-gray-400" /> |
| 244 | </HeaderButtonWithToolTip> |
| 245 | )} |
| 246 | </div> |
| 247 | </div> |
| 248 | </div> |
| 249 | |
| 250 | <span |
| 251 | style={{ |
| 252 | fontSize: tinyFont, |
| 253 | }} |
| 254 | className={`mt-1 line-clamp-3 ${isDisabled ? "text-gray-500" : "text-gray-400"}`} |
| 255 | > |
| 256 | {rule.rule} |
| 257 | </span> |
| 258 | {rule.globs ? ( |
| 259 | <div |
| 260 | style={{ |
| 261 | fontSize: tinyFont, |
| 262 | }} |
| 263 | className="mt-1.5 flex flex-col gap-1" |
| 264 | > |
| 265 | <span className="italic">Applies to files</span> |
| 266 | <code |
| 267 | className={`line-clamp-1 px-1 py-0.5 ${isDisabled ? "text-gray-500" : "text-gray-400"}`} |
| 268 | > |
| 269 | {rule.globs} |
| 270 | </code> |
| 271 | </div> |
| 272 | ) : null} |
| 273 | </div> |
| 274 | </div> |
| 275 | ); |
| 276 | }; |
| 277 | |
| 278 | /** |
| 279 | * Section that displays all available prompts with bookmarking functionality |
| 280 | */ |
| 281 | function PromptsSubSection() { |
| 282 | const { selectedProfile } = useAuth(); |
| 283 | const { isCommandBookmarked, toggleBookmark } = useBookmarkedSlashCommands(); |
| 284 | const ideMessenger = useContext(IdeMessengerContext); |
| 285 | const isLocal = selectedProfile?.profileType === "local"; |
| 286 | |
| 287 | const slashCommands = useAppSelector( |
| 288 | (state) => state.config.config.slashCommands ?? [], |
| 289 | ); |
| 290 | |
| 291 | const handleEdit = (prompt: PromptCommandWithSlug) => { |
| 292 | if (prompt.promptFile) { |
| 293 | ideMessenger.post("openFile", { |
| 294 | path: prompt.promptFile, |
| 295 | }); |
| 296 | } else if (prompt.slug) { |
| 297 | void ideMessenger.request("controlPlane/openUrl", { |
| 298 | path: `${prompt.slug}/new-version`, |
| 299 | orgSlug: undefined, |
| 300 | }); |
| 301 | } else { |
| 302 | ideMessenger.post("config/openProfile", { |
| 303 | profileId: undefined, |
| 304 | element: { sourceFile: (prompt as any).sourceFile }, |
| 305 | }); |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | const handleAddPrompt = () => { |
| 310 | if (isLocal) { |
| 311 | void ideMessenger.request("config/addLocalWorkspaceBlock", { |
| 312 | blockType: "prompts", |
| 313 | }); |
| 314 | } else { |
| 315 | void ideMessenger.request("controlPlane/openUrl", { |
| 316 | path: "?type=prompts", |
| 317 | orgSlug: undefined, |
| 318 | }); |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | const sortedCommands = useMemo(() => { |
| 323 | const promptsWithSlug: PromptCommandWithSlug[] = |
| 324 | structuredClone(slashCommands); |
| 325 | // get the slugs from rawYaml |
| 326 | if (selectedProfile?.rawYaml) { |
| 327 | const parsed = parseConfigYaml(selectedProfile.rawYaml); |
| 328 | const parsedPrompts = parsed.prompts ?? []; |
| 329 | |
| 330 | let index = 0; |
| 331 | for (const commandWithSlug of promptsWithSlug) { |
| 332 | // skip for local prompt files |
| 333 | if (commandWithSlug.promptFile) continue; |
| 334 | |
| 335 | const yamlPrompt = parsedPrompts[index]; |
| 336 | if (yamlPrompt) { |
| 337 | if ("uses" in yamlPrompt) { |
| 338 | commandWithSlug.slug = yamlPrompt.uses; |
| 339 | } else { |
| 340 | commandWithSlug.slug = `${selectedProfile?.fullSlug.ownerSlug}/${selectedProfile?.fullSlug.packageSlug}`; |
| 341 | } |
| 342 | } |
| 343 | index = index + 1; |
| 344 | } |
| 345 | } |
| 346 | return promptsWithSlug.sort((a, b) => { |
| 347 | const aBookmarked = isCommandBookmarked(a.name); |
| 348 | const bBookmarked = isCommandBookmarked(b.name); |
| 349 | if (aBookmarked && !bBookmarked) return -1; |
| 350 | if (!aBookmarked && bBookmarked) return 1; |
| 351 | return 0; |
| 352 | }); |
| 353 | }, [slashCommands, isCommandBookmarked, selectedProfile]); |
| 354 | |
| 355 | return ( |
| 356 | <div> |
| 357 | <ConfigHeader |
| 358 | title="Prompts" |
| 359 | variant="sm" |
| 360 | onAddClick={handleAddPrompt} |
| 361 | addButtonTooltip="Add prompt" |
| 362 | /> |
| 363 | |
| 364 | {sortedCommands.length > 0 ? ( |
| 365 | <Card> |
| 366 | <div> |
| 367 | {sortedCommands.map((prompt) => ( |
| 368 | <PromptRow |
| 369 | key={prompt.name} |
| 370 | prompt={prompt} |
| 371 | isBookmarked={isCommandBookmarked(prompt.name)} |
| 372 | setIsBookmarked={() => toggleBookmark(prompt)} |
| 373 | onEdit={() => handleEdit(prompt)} |
| 374 | /> |
| 375 | ))} |
| 376 | </div> |
| 377 | </Card> |
| 378 | ) : ( |
| 379 | <Card> |
| 380 | <EmptyState message="No prompts configured. Click the + button to add your first prompt." /> |
| 381 | </Card> |
| 382 | )} |
| 383 | </div> |
| 384 | ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Helper function to add the appropriate default system message based on mode |
| 389 | */ |
| 390 | function addDefaultSystemMessage( |
| 391 | rules: RuleWithSource[], |
| 392 | mode: string, |
| 393 | config: BrowserSerializedContinueConfig, |
| 394 | ) { |
| 395 | const modeConfig = { |
| 396 | chat: { |
| 397 | customMessage: config.selectedModelByRole.chat?.baseChatSystemMessage, |
| 398 | defaultMessage: DEFAULT_CHAT_SYSTEM_MESSAGE, |
| 399 | customSource: "model-options-chat" as RuleSource, |
| 400 | defaultSource: "default-chat" as RuleSource, |
| 401 | }, |
| 402 | agent: { |
| 403 | customMessage: config.selectedModelByRole.chat?.baseAgentSystemMessage, |
| 404 | defaultMessage: DEFAULT_AGENT_SYSTEM_MESSAGE, |
| 405 | customSource: "model-options-agent" as RuleSource, |
| 406 | defaultSource: "default-agent" as RuleSource, |
| 407 | }, |
| 408 | plan: { |
| 409 | customMessage: config.selectedModelByRole.chat?.basePlanSystemMessage, |
| 410 | defaultMessage: DEFAULT_PLAN_SYSTEM_MESSAGE, |
| 411 | customSource: "model-options-plan" as RuleSource, |
| 412 | defaultSource: "default-plan" as RuleSource, |
| 413 | }, |
| 414 | }; |
| 415 | |
| 416 | const currentMode = modeConfig[mode as keyof typeof modeConfig]; |
| 417 | if (currentMode) { |
| 418 | const message = currentMode.customMessage || currentMode.defaultMessage; |
| 419 | const source = currentMode.customMessage |
| 420 | ? currentMode.customSource |
| 421 | : currentMode.defaultSource; |
| 422 | |
| 423 | rules.unshift({ |
| 424 | rule: message, |
| 425 | source, |
| 426 | }); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | function RulesSubSection() { |
| 431 | const { selectedProfile } = useAuth(); |
| 432 | const config = useAppSelector((store) => store.config.config); |
| 433 | const mode = useAppSelector((store) => store.session.mode); |
| 434 | const ideMessenger = useContext(IdeMessengerContext); |
| 435 | const isLocal = selectedProfile?.profileType === "local"; |
| 436 | |
| 437 | const handleAddRule = () => { |
| 438 | if (isLocal) { |
| 439 | void ideMessenger.request("config/addLocalWorkspaceBlock", { |
| 440 | blockType: "rules", |
| 441 | }); |
| 442 | } else { |
| 443 | void ideMessenger.request("controlPlane/openUrl", { |
| 444 | path: "?type=rules", |
| 445 | orgSlug: undefined, |
| 446 | }); |
| 447 | } |
| 448 | }; |
| 449 | |
| 450 | const sortedRules: RuleWithSource[] = useMemo(() => { |
| 451 | const rules = [...config.rules.map((rule) => ({ ...rule }))]; |
| 452 | |
| 453 | // Use profile rawYaml to infer slugs |
| 454 | if (selectedProfile?.rawYaml) { |
| 455 | try { |
| 456 | const parsed = parseConfigYaml(selectedProfile.rawYaml); |
| 457 | const parsedRules = parsed?.rules ?? []; |
| 458 | let index = 0; |
| 459 | for (const rule of rules) { |
| 460 | if (rule.source === "rules-block") { |
| 461 | let slug: string | undefined = undefined; |
| 462 | const yamlRule = parsedRules[index]; |
| 463 | if (yamlRule) { |
| 464 | if (typeof yamlRule !== "string" && "uses" in yamlRule) { |
| 465 | slug = yamlRule.uses; |
| 466 | } else { |
| 467 | slug = `${selectedProfile?.fullSlug.ownerSlug}/${selectedProfile?.fullSlug.packageSlug}`; |
| 468 | } |
| 469 | } |
| 470 | if (slug) { |
| 471 | rule.slug = slug; |
| 472 | } |
| 473 | |
| 474 | index++; |
| 475 | } |
| 476 | } |
| 477 | } catch (e) { |
| 478 | console.error( |
| 479 | "Rules notch section: failed to parse selected profile", |
| 480 | e, |
| 481 | ); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | addDefaultSystemMessage(rules, mode, config); |
| 486 | |
| 487 | return rules; |
| 488 | }, [config, selectedProfile, mode]); |
| 489 | |
| 490 | return ( |
| 491 | <div> |
| 492 | <ConfigHeader |
| 493 | title="Rules" |
| 494 | variant="sm" |
| 495 | onAddClick={handleAddRule} |
| 496 | addButtonTooltip="Add rule" |
| 497 | /> |
| 498 | |
| 499 | <Card> |
| 500 | {sortedRules.length > 0 ? ( |
| 501 | <div className="flex flex-col gap-3"> |
| 502 | {sortedRules.map((rule, index) => ( |
| 503 | <RuleCard key={index} rule={rule} /> |
| 504 | ))} |
| 505 | </div> |
| 506 | ) : ( |
| 507 | <EmptyState message="No rules configured. Click the + button to add your first rule." /> |
| 508 | )} |
| 509 | </Card> |
| 510 | </div> |
| 511 | ); |
| 512 | } |
| 513 | |
| 514 | export function RulesSection() { |
| 515 | return ( |
| 516 | <> |
| 517 | <ConfigHeader title="Rules" /> |
| 518 | |
| 519 | <div className="space-y-6"> |
| 520 | <RulesSubSection /> |
| 521 | <PromptsSubSection /> |
| 522 | </div> |
| 523 | </> |
| 524 | ); |
| 525 | } |