| 1 | import { parseConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | BookmarkIcon as BookmarkOutline, |
| 4 | PencilIcon, |
| 5 | PlusIcon, |
| 6 | } from "@heroicons/react/24/outline"; |
| 7 | import { BookmarkIcon as BookmarkSolid } from "@heroicons/react/24/solid"; |
| 8 | import { SlashCommandDescWithSource } from "core"; |
| 9 | import { useContext, useMemo } from "react"; |
| 10 | import { useMainEditor } from "../../../components/mainInput/TipTapEditor"; |
| 11 | import { ToolTip } from "../../../components/gui/Tooltip"; |
| 12 | import { Button, Card, EmptyState } from "../../../components/ui"; |
| 13 | import { useAuth } from "../../../context/Auth"; |
| 14 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 15 | import { useBookmarkedSlashCommands } from "../../../hooks/useBookmarkedSlashCommands"; |
| 16 | import { useAppSelector } from "../../../redux/hooks"; |
| 17 | import { fontSize } from "../../../util"; |
| 18 | import { ConfigHeader } from "../components/ConfigHeader"; |
| 19 | |
| 20 | interface PromptCommandWithSlug extends SlashCommandDescWithSource { |
| 21 | slug?: string; |
| 22 | } |
| 23 | |
| 24 | interface PromptRowProps { |
| 25 | prompt: PromptCommandWithSlug; |
| 26 | isBookmarked: boolean; |
| 27 | setIsBookmarked: (isBookmarked: boolean) => void; |
| 28 | onEdit?: () => void; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Displays a single prompt row with bookmark and edit controls |
| 33 | */ |
| 34 | function PromptRow({ |
| 35 | prompt, |
| 36 | isBookmarked, |
| 37 | setIsBookmarked, |
| 38 | onEdit, |
| 39 | }: PromptRowProps) { |
| 40 | const { mainEditor } = useMainEditor(); |
| 41 | |
| 42 | const handlePromptClick = (e: React.MouseEvent) => { |
| 43 | e.stopPropagation(); |
| 44 | mainEditor?.commands.insertPrompt({ |
| 45 | title: prompt.name, |
| 46 | description: prompt.description, |
| 47 | content: prompt.prompt, |
| 48 | }); |
| 49 | }; |
| 50 | |
| 51 | const handleBookmarkClick = (e: React.MouseEvent) => { |
| 52 | e.stopPropagation(); |
| 53 | setIsBookmarked(!isBookmarked); |
| 54 | }; |
| 55 | |
| 56 | const handleEditClick = (e: React.MouseEvent) => { |
| 57 | e.stopPropagation(); |
| 58 | if (onEdit) { |
| 59 | onEdit(); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | const canEdit = |
| 64 | prompt.promptFile && !prompt.promptFile.startsWith("builtin:"); |
| 65 | |
| 66 | return ( |
| 67 | <div |
| 68 | 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" |
| 69 | onClick={handlePromptClick} |
| 70 | style={{ |
| 71 | fontSize: fontSize(-3), |
| 72 | }} |
| 73 | > |
| 74 | <div className="flex min-w-0 flex-col"> |
| 75 | <span className="text-vscForeground shrink-0 font-medium"> |
| 76 | {prompt.name} |
| 77 | </span> |
| 78 | <span className="line-clamp-2 text-[11px] text-gray-400"> |
| 79 | {prompt.description} |
| 80 | </span> |
| 81 | </div> |
| 82 | <div className="flex items-center gap-2"> |
| 83 | <PencilIcon |
| 84 | className={`h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125 ${!canEdit ? "pointer-events-none cursor-not-allowed opacity-50" : ""}`} |
| 85 | onClick={canEdit ? handleEditClick : undefined} |
| 86 | aria-disabled={!canEdit} |
| 87 | /> |
| 88 | <div |
| 89 | onClick={handleBookmarkClick} |
| 90 | className="cursor-pointer pt-0.5 text-gray-400 hover:brightness-125" |
| 91 | > |
| 92 | {isBookmarked ? ( |
| 93 | <BookmarkSolid className="h-3 w-3" /> |
| 94 | ) : ( |
| 95 | <BookmarkOutline className="h-3 w-3" /> |
| 96 | )} |
| 97 | </div> |
| 98 | </div> |
| 99 | </div> |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Section that displays all available prompts with bookmarking functionality |
| 105 | */ |
| 106 | export function PromptsSection() { |
| 107 | const { selectedProfile } = useAuth(); |
| 108 | const { isCommandBookmarked, toggleBookmark } = useBookmarkedSlashCommands(); |
| 109 | const ideMessenger = useContext(IdeMessengerContext); |
| 110 | const isLocal = selectedProfile?.profileType === "local"; |
| 111 | |
| 112 | const slashCommands = useAppSelector( |
| 113 | (state) => state.config.config.slashCommands ?? [], |
| 114 | ); |
| 115 | |
| 116 | const handleEdit = (prompt: PromptCommandWithSlug) => { |
| 117 | if (prompt.promptFile) { |
| 118 | ideMessenger.post("openFile", { |
| 119 | path: prompt.promptFile, |
| 120 | }); |
| 121 | } else if (prompt.slug) { |
| 122 | void ideMessenger.request("controlPlane/openUrl", { |
| 123 | path: `${prompt.slug}/new-version`, |
| 124 | orgSlug: undefined, |
| 125 | }); |
| 126 | } else { |
| 127 | ideMessenger.post("config/openProfile", { |
| 128 | profileId: undefined, |
| 129 | element: { sourceFile: (prompt as any).sourceFile }, |
| 130 | }); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | const handleAddPrompt = () => { |
| 135 | if (isLocal) { |
| 136 | void ideMessenger.request("config/addLocalWorkspaceBlock", { |
| 137 | blockType: "prompts", |
| 138 | }); |
| 139 | } else { |
| 140 | void ideMessenger.request("controlPlane/openUrl", { |
| 141 | path: "new?type=block&blockType=prompts", |
| 142 | orgSlug: undefined, |
| 143 | }); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | const sortedCommands = useMemo(() => { |
| 148 | const promptsWithSlug: PromptCommandWithSlug[] = |
| 149 | structuredClone(slashCommands); |
| 150 | // get the slugs from rawYaml |
| 151 | if (selectedProfile?.rawYaml) { |
| 152 | const parsed = parseConfigYaml(selectedProfile.rawYaml); |
| 153 | const parsedPrompts = parsed.prompts ?? []; |
| 154 | |
| 155 | let index = 0; |
| 156 | for (const commandWithSlug of promptsWithSlug) { |
| 157 | // skip for local prompt files |
| 158 | if (commandWithSlug.promptFile) continue; |
| 159 | |
| 160 | const yamlPrompt = parsedPrompts[index]; |
| 161 | if (yamlPrompt) { |
| 162 | if ("uses" in yamlPrompt) { |
| 163 | commandWithSlug.slug = yamlPrompt.uses; |
| 164 | } else { |
| 165 | commandWithSlug.slug = `${selectedProfile?.fullSlug.ownerSlug}/${selectedProfile?.fullSlug.packageSlug}`; |
| 166 | } |
| 167 | } |
| 168 | index = index + 1; |
| 169 | } |
| 170 | } |
| 171 | return promptsWithSlug.sort((a, b) => { |
| 172 | const aBookmarked = isCommandBookmarked(a.name); |
| 173 | const bBookmarked = isCommandBookmarked(b.name); |
| 174 | if (aBookmarked && !bBookmarked) return -1; |
| 175 | if (!aBookmarked && bBookmarked) return 1; |
| 176 | return 0; |
| 177 | }); |
| 178 | }, [slashCommands, isCommandBookmarked, selectedProfile]); |
| 179 | |
| 180 | return ( |
| 181 | <div className="flex flex-col"> |
| 182 | <ConfigHeader |
| 183 | title="Prompts" |
| 184 | onAddClick={handleAddPrompt} |
| 185 | addButtonTooltip="Add prompt" |
| 186 | /> |
| 187 | |
| 188 | {sortedCommands.length > 0 ? ( |
| 189 | <div> |
| 190 | {sortedCommands.map((prompt) => ( |
| 191 | <PromptRow |
| 192 | key={prompt.name} |
| 193 | prompt={prompt} |
| 194 | isBookmarked={isCommandBookmarked(prompt.name)} |
| 195 | setIsBookmarked={() => toggleBookmark(prompt)} |
| 196 | onEdit={() => handleEdit(prompt)} |
| 197 | /> |
| 198 | ))} |
| 199 | </div> |
| 200 | ) : ( |
| 201 | <Card> |
| 202 | <EmptyState message="No prompts configured. Click the + button to add your first prompt." /> |
| 203 | </Card> |
| 204 | )} |
| 205 | </div> |
| 206 | ); |
| 207 | } |