| 1 | import { |
| 2 | createRuleMarkdown, |
| 3 | getRuleType, |
| 4 | RuleType, |
| 5 | RuleTypeDescriptions, |
| 6 | } from "@continuedev/config-yaml"; |
| 7 | import { InformationCircleIcon } from "@heroicons/react/24/outline"; |
| 8 | import { createRuleFilePath } from "core/config/markdown/utils"; |
| 9 | import { CreateRuleBlockArgs } from "core/tools/implementations/createRuleBlock"; |
| 10 | import { useContext, useEffect, useState } from "react"; |
| 11 | import { useForm } from "react-hook-form"; |
| 12 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 13 | import Spinner from "../gui/Spinner"; |
| 14 | import { ToolTip } from "../gui/Tooltip"; |
| 15 | import { Button } from "../ui"; |
| 16 | import { useRuleGeneration } from "./useRuleGeneration"; |
| 17 | |
| 18 | interface GenerationScreenProps { |
| 19 | inputPrompt: string; |
| 20 | onBack: () => void; |
| 21 | onSuccess: () => void; |
| 22 | isManualMode?: boolean; |
| 23 | } |
| 24 | |
| 25 | export function GenerationScreen({ |
| 26 | inputPrompt, |
| 27 | onBack, |
| 28 | onSuccess, |
| 29 | isManualMode = false, |
| 30 | }: GenerationScreenProps) { |
| 31 | const ideMessenger = useContext(IdeMessengerContext); |
| 32 | |
| 33 | const { register, watch, setValue, reset } = useForm<CreateRuleBlockArgs>({ |
| 34 | defaultValues: { |
| 35 | name: "", |
| 36 | description: "", |
| 37 | globs: "", |
| 38 | alwaysApply: undefined, |
| 39 | rule: "", |
| 40 | }, |
| 41 | }); |
| 42 | |
| 43 | const formData = watch(); |
| 44 | |
| 45 | // Track rule type separately from form data |
| 46 | const [selectedRuleType, setSelectedRuleType] = useState<RuleType>( |
| 47 | RuleType.Always, |
| 48 | ); |
| 49 | const [formError, setFormError] = useState<string | null>(null); |
| 50 | |
| 51 | // Use the generation hook with the input prompt |
| 52 | const { generateRule, isGenerating, error } = useRuleGeneration( |
| 53 | inputPrompt, |
| 54 | (args) => { |
| 55 | // Streaming causes a lot of jank, so wait until done generating |
| 56 | if (!isGenerating) { |
| 57 | reset(args); |
| 58 | handleRuleTypeChange(getRuleType(args)); |
| 59 | } |
| 60 | }, |
| 61 | ); |
| 62 | |
| 63 | // Start generation once when component mounts (only if not in manual mode) |
| 64 | useEffect(() => { |
| 65 | if (!isManualMode) { |
| 66 | void generateRule(); |
| 67 | } |
| 68 | }, [isManualMode]); |
| 69 | |
| 70 | const handleRuleTypeChange = (newRuleType: RuleType) => { |
| 71 | setSelectedRuleType(newRuleType); |
| 72 | |
| 73 | // Update alwaysApply based on rule type |
| 74 | const alwaysApply = newRuleType === RuleType.Always; |
| 75 | setValue("alwaysApply", alwaysApply); |
| 76 | |
| 77 | // Don't clear optional fields - preserve their state |
| 78 | // Users can manually clear them if needed |
| 79 | }; |
| 80 | |
| 81 | const handleContinue = async () => { |
| 82 | // Clear any previous errors |
| 83 | setFormError(null); |
| 84 | |
| 85 | if (!formData.name) { |
| 86 | setFormError("Rule name is required"); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if (!formData.rule) { |
| 91 | setFormError("Rule content is required"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | const options: any = { |
| 97 | alwaysApply: formData.alwaysApply, |
| 98 | }; |
| 99 | |
| 100 | if (formData.description) { |
| 101 | options.description = formData.description; |
| 102 | } |
| 103 | |
| 104 | if (formData.globs) { |
| 105 | options.globs = formData.globs; |
| 106 | } |
| 107 | |
| 108 | const fileContent = createRuleMarkdown( |
| 109 | formData.name, |
| 110 | formData.rule, |
| 111 | options, |
| 112 | ); |
| 113 | |
| 114 | const workspaceDirs = await ideMessenger.request( |
| 115 | "getWorkspaceDirs", |
| 116 | undefined, |
| 117 | ); |
| 118 | |
| 119 | if (workspaceDirs.status !== "success") { |
| 120 | setFormError("Failed to get workspace directory"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | const localContinueDir = workspaceDirs.content[0]; |
| 125 | const ruleFilePath = createRuleFilePath(localContinueDir, formData.name); |
| 126 | |
| 127 | await ideMessenger.request("writeFile", { |
| 128 | path: ruleFilePath, |
| 129 | contents: fileContent, |
| 130 | }); |
| 131 | ideMessenger.post("openFile", { path: ruleFilePath }); |
| 132 | |
| 133 | onSuccess(); |
| 134 | } catch (err) { |
| 135 | const errorMessage = |
| 136 | err instanceof Error ? err.message : "Unknown error occurred"; |
| 137 | setFormError(`Failed to create rule file: ${errorMessage}`); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | const showNameSpinner = isGenerating && !formData.name && !isManualMode; |
| 142 | |
| 143 | return ( |
| 144 | <div className="px-2 pb-2 pt-4 sm:px-4"> |
| 145 | <div> |
| 146 | <div className="text-center"> |
| 147 | <h2 className="mb-0">Your rule</h2> |
| 148 | <p className="text-description m-0 mt-2 p-0"> |
| 149 | Review and edit your generated rule below |
| 150 | </p> |
| 151 | </div> |
| 152 | <div className="mt-5"> |
| 153 | <div className="flex flex-col gap-4"> |
| 154 | {/* Rule metadata form */} |
| 155 | <div className="space-y-4"> |
| 156 | {/* Rule Name - Always visible */} |
| 157 | <div className="space-y-1"> |
| 158 | <label className="text-foreground text-sm font-medium"> |
| 159 | Rule Name |
| 160 | </label> |
| 161 | <div className="relative"> |
| 162 | <input |
| 163 | type="text" |
| 164 | className="border-input-border bg-input text-input-foreground placeholder:text-input-placeholder focus:border-border-focus box-border w-full rounded-md border px-3 py-2 text-xs focus:outline-none" |
| 165 | placeholder={showNameSpinner ? "" : "Enter rule name..."} |
| 166 | disabled={isGenerating && !isManualMode} |
| 167 | {...register("name")} |
| 168 | /> |
| 169 | {showNameSpinner && ( |
| 170 | <div className="absolute left-3 top-1/2 -translate-y-1/2"> |
| 171 | <Spinner /> |
| 172 | </div> |
| 173 | )} |
| 174 | </div> |
| 175 | </div> |
| 176 | |
| 177 | {/* Rule Type Selector - Always visible */} |
| 178 | <div className="space-y-1"> |
| 179 | <div className="flex items-center gap-2"> |
| 180 | <label className="text-foreground text-sm font-medium"> |
| 181 | Rule Type |
| 182 | </label> |
| 183 | <ToolTip |
| 184 | style={{ zIndex: 100001 }} |
| 185 | content={RuleTypeDescriptions[selectedRuleType]} |
| 186 | > |
| 187 | <InformationCircleIcon className="h-4 w-4 text-gray-500" /> |
| 188 | </ToolTip> |
| 189 | </div> |
| 190 | <div className="relative"> |
| 191 | <select |
| 192 | className="border-input-border bg-input text-input-foreground focus:border-border-focus w-full rounded-md border px-3 py-2 text-xs focus:outline-none" |
| 193 | value={isGenerating ? "" : selectedRuleType} |
| 194 | onChange={(e) => |
| 195 | handleRuleTypeChange(e.target.value as RuleType) |
| 196 | } |
| 197 | disabled={isGenerating && !isManualMode} |
| 198 | > |
| 199 | {isGenerating && !isManualMode ? ( |
| 200 | <option value=""></option> |
| 201 | ) : ( |
| 202 | <> |
| 203 | <option value={RuleType.Always}>Always</option> |
| 204 | <option value={RuleType.AutoAttached}> |
| 205 | Auto Attached |
| 206 | </option> |
| 207 | <option value={RuleType.AgentRequested}> |
| 208 | Agent Requested |
| 209 | </option> |
| 210 | <option value={RuleType.Manual}>Manual</option> |
| 211 | </> |
| 212 | )} |
| 213 | </select> |
| 214 | </div> |
| 215 | </div> |
| 216 | |
| 217 | {/* Description (for Agent Requested only) */} |
| 218 | <div |
| 219 | className={`space-y-1 ${selectedRuleType === RuleType.AgentRequested ? "" : "hidden"}`} |
| 220 | > |
| 221 | <label className="text-foreground text-sm font-medium"> |
| 222 | Description |
| 223 | </label> |
| 224 | <textarea |
| 225 | className="border-input-border bg-input text-input-foreground placeholder:text-input-placeholder focus:border-border-focus box-border w-full resize-none rounded-md border px-3 py-2 text-xs focus:outline-none" |
| 226 | rows={3} |
| 227 | placeholder="Description of the task this rule is helpful for..." |
| 228 | {...register("description")} |
| 229 | /> |
| 230 | </div> |
| 231 | |
| 232 | {/* File Pattern (for Auto Attached only) */} |
| 233 | <div |
| 234 | className={`space-y-1 ${selectedRuleType === RuleType.AutoAttached ? "" : "hidden"}`} |
| 235 | > |
| 236 | <label className="text-foreground text-sm font-medium"> |
| 237 | File pattern matches |
| 238 | </label> |
| 239 | <input |
| 240 | type="text" |
| 241 | className="border-input-border bg-input text-input-foreground placeholder:text-input-placeholder focus:border-border-focus box-border w-full rounded-md border px-3 py-2 font-mono text-xs focus:outline-none" |
| 242 | placeholder="*.tsx, **/*.{ts,tsx}, tests/**/*.ts ..." |
| 243 | {...register("globs")} |
| 244 | /> |
| 245 | </div> |
| 246 | </div> |
| 247 | |
| 248 | {/* Rule Content */} |
| 249 | <div className="relative"> |
| 250 | <label className="text-foreground text-sm font-medium"> |
| 251 | Rule Content |
| 252 | </label> |
| 253 | <textarea |
| 254 | className="border-input-border bg-input text-input-foreground placeholder:text-input-placeholder focus:border-border-focus mt-1 box-border w-full resize-none rounded border p-2 text-xs focus:outline-none" |
| 255 | rows={10} |
| 256 | disabled={isGenerating && !isManualMode} |
| 257 | placeholder="Your rule content..." |
| 258 | {...register("rule")} |
| 259 | /> |
| 260 | </div> |
| 261 | {error && <p className="text-sm text-red-500">{error}</p>} |
| 262 | |
| 263 | <div className="my-4 flex flex-col items-center gap-2"> |
| 264 | <div className="flex flex-row justify-center gap-3"> |
| 265 | <Button |
| 266 | type="button" |
| 267 | className="min-w-16" |
| 268 | onClick={onBack} |
| 269 | variant="outline" |
| 270 | disabled={isGenerating && !isManualMode} |
| 271 | > |
| 272 | Back |
| 273 | </Button> |
| 274 | <Button |
| 275 | className="min-w-16" |
| 276 | onClick={handleContinue} |
| 277 | disabled={ |
| 278 | (isGenerating && !isManualMode) || |
| 279 | (!formData.rule && !error) || |
| 280 | !formData.name |
| 281 | } |
| 282 | > |
| 283 | Continue |
| 284 | </Button> |
| 285 | </div> |
| 286 | {formError && ( |
| 287 | <span className="text-error text-center text-xs"> |
| 288 | Error creating rule: {formError} |
| 289 | </span> |
| 290 | )} |
| 291 | </div> |
| 292 | </div> |
| 293 | </div> |
| 294 | </div> |
| 295 | </div> |
| 296 | ); |
| 297 | } |