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