Merge pull request #5677 from continuedev/tomasz/display-rules

feat: display rules used
This commit is contained in:
Tomasz Stefaniak 2025-05-15 14:22:43 -07:00 • committed by GitHub
commit 680887a9ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 285 additions and 43 deletions

19
core/index.d.ts vendored
View file

@ -459,6 +459,7 @@ export interface ChatHistoryItem {
toolCallState?: ToolCallState;
isGatheringContext?: boolean;
reasoning?: Reasoning;
appliedRules?: RuleWithSource[];
}
export interface LLMFullCompletionOptions extends BaseCompletionOptions {
@ -1497,17 +1498,19 @@ export interface TerminalOptions {
waitForCompletion?: boolean;
}
export type RuleSource =
| "default-chat"
| "default-agent"
| "model-chat-options"
| "model-agent-options"
| "rules-block"
| "json-systemMessage"
| ".continuerules";
export interface RuleWithSource {
name?: string;
slug?: string;
source:
| "default-chat"
| "default-agent"
| "model-chat-options"
| "model-agent-options"
| "rules-block"
| "json-systemMessage"
| ".continuerules";
source: RuleSource;
globs?: string | string[];
rule: string;
description?: string;

View file

@ -230,6 +230,7 @@ describe("LLM", () => {
testFim: true,
skip: false,
testToolCall: true,
timeout: 60000,
},
);
testLLM(

View file

@ -23,6 +23,29 @@ const matchesGlobs = (
return false;
};
/**
* Filters rules that apply to the given message
*/
export const getApplicableRules = (
userMessage: UserChatMessage | ToolResultChatMessage | undefined,
rules: RuleWithSource[],
): RuleWithSource[] => {
const filePathsFromMessage = userMessage
? extractPathsFromCodeBlocks(renderChatMessage(userMessage))
: [];
return rules.filter((rule) => {
// A rule is active if it has no globs (applies to all files)
// or if at least one file path matches its globs
const hasNoGlobs = !rule.globs;
const matchesAnyFilePath = filePathsFromMessage.some((path) =>
matchesGlobs(path, rule.globs),
);
return hasNoGlobs || matchesAnyFilePath;
});
};
export const getSystemMessageWithRules = ({
baseSystemMessage,
userMessage,
@ -32,23 +55,11 @@ export const getSystemMessageWithRules = ({
userMessage: UserChatMessage | ToolResultChatMessage | undefined;
rules: RuleWithSource[];
}) => {
const filePathsFromMessage = userMessage
? extractPathsFromCodeBlocks(renderChatMessage(userMessage))
: [];
const applicableRules = getApplicableRules(userMessage, rules);
let systemMessage = baseSystemMessage ?? "";
for (const rule of rules) {
// A rule is active if it has no globs (applies to all files)
// or if at least one file path matches its globs
const hasNoGlobs = !rule.globs;
const matchesAnyFilePath = filePathsFromMessage.some((path) =>
matchesGlobs(path, rule.globs),
);
if (hasNoGlobs || matchesAnyFilePath) {
systemMessage += `\n\n${rule.rule}`;
}
for (const rule of applicableRules) {
systemMessage += `\n\n${rule.rule}`;
}
return systemMessage;

View file

@ -46,7 +46,7 @@ export class GUISelectors {
}
public static getToolCallStatusMessage(view: WebView) {
return SelectorUtils.getElementByDataTestId(view, "toggle-div-title");
return SelectorUtils.getElementByDataTestId(view, "tool-call-title");
}
public static getToolButton(view: WebView) {
@ -89,6 +89,14 @@ export class GUISelectors {
);
}
public static getRulesPeek(view: WebView) {
return SelectorUtils.getElementByDataTestId(view, "rules-peek");
}
public static getFirstRulesPeekItem(view: WebView) {
return SelectorUtils.getElementByDataTestId(view, "rules-peek-item");
}
public static getNthHistoryTableRow(view: WebView, index: number) {
return SelectorUtils.getElementByDataTestId(view, `history-row-${index}`);
}

View file

@ -245,6 +245,47 @@ describe("GUI Test", () => {
await GUIActions.selectModeFromDropdown(view, "Agent");
});
it("should display rules peek and show rule details", async () => {
// Send a message to trigger the model response
const [messageInput] = await GUISelectors.getMessageInputFields(view);
await messageInput.sendKeys("Hello");
await messageInput.sendKeys(Key.ENTER);
// Wait for the response to appear
await TestUtils.waitForSuccess(() =>
GUISelectors.getThreadMessageByText(view, "I'm going to call a tool:"),
);
// Verify that "1 rule" text appears
const rulesPeek = await TestUtils.waitForSuccess(() =>
GUISelectors.getRulesPeek(view),
);
const rulesPeekText = await rulesPeek.getText();
expect(rulesPeekText).to.include("1 rule");
// Click on the rules peek to expand it
await rulesPeek.click();
// Wait for the rule details to appear
const ruleItem = await TestUtils.waitForSuccess(() =>
GUISelectors.getFirstRulesPeekItem(view),
);
await TestUtils.waitForSuccess(async () => {
const text = await ruleItem.getText();
if (!text || text.trim() === "") {
throw new Error("Rule item text is empty");
}
return ruleItem;
});
// Verify the rule content
const ruleItemText = await ruleItem.getText();
expect(ruleItemText).to.include("Assistant rule");
expect(ruleItemText).to.include("Always applied");
expect(ruleItemText).to.include("TEST_SYS_MSG");
}).timeout(DEFAULT_TIMEOUT.MD);
it("should render tool call", async () => {
const [messageInput] = await GUISelectors.getMessageInputFields(view);
await messageInput.sendKeys("Hello");
@ -258,7 +299,9 @@ describe("GUI Test", () => {
expect(await statusMessage.getText()).contain(
"Continue viewed the git diff",
);
}).timeout(DEFAULT_TIMEOUT.MD);
// wait for 30 seconds, promise
await new Promise((resolve) => setTimeout(resolve, 30000));
}).timeout(DEFAULT_TIMEOUT.MD * 100);
it("should call tool after approval", async () => {
await GUIActions.toggleToolPolicy(view, "builtin_view_diff", 2);

View file

@ -6,15 +6,21 @@ interface ToggleProps {
children: React.ReactNode;
title: React.ReactNode;
icon?: ComponentType<React.SVGProps<SVGSVGElement>>;
testId?: string;
}
function ToggleDiv({ children, title, icon: Icon }: ToggleProps) {
function ToggleDiv({
children,
title,
icon: Icon,
testId = "context-items-peek",
}: ToggleProps) {
const [open, setOpen] = useState(false);
const [isHovered, setIsHovered] = useState(false);
return (
<div
className={`pl-2 pt-2`}
className={`pl-2`}
style={{
backgroundColor: vscBackground,
}}
@ -24,7 +30,7 @@ function ToggleDiv({ children, title, icon: Icon }: ToggleProps) {
onClick={() => setOpen((prev) => !prev)}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
data-testid="context-items-peek"
data-testid={testId}
>
<div className="relative mr-1 h-4 w-4">
{Icon && !isHovered && !open ? (
@ -44,10 +50,7 @@ function ToggleDiv({ children, title, icon: Icon }: ToggleProps) {
</>
)}
</div>
<span
className="ml-1 text-xs text-gray-400 transition-colors duration-200"
data-testid="toggle-div-title"
>
<span className="ml-1 text-xs text-gray-400 transition-colors duration-200">
{title}
</span>
</div>

View file

@ -1,11 +1,12 @@
import { Editor, JSONContent } from "@tiptap/react";
import { ContextItemWithId, InputModifiers } from "core";
import { ContextItemWithId, InputModifiers, RuleWithSource } from "core";
import { useMemo } from "react";
import styled, { keyframes } from "styled-components";
import { defaultBorderRadius, vscBackground } from "..";
import { useAppSelector } from "../../redux/hooks";
import { selectSlashCommandComboBoxInputs } from "../../redux/selectors";
import { ContextItemsPeek } from "./belowMainInput/ContextItemsPeek";
import { RulesPeek } from "./belowMainInput/RulesPeek";
import { ToolbarOptions } from "./InputToolbar";
import { Lump } from "./Lump";
import { TipTapEditor } from "./TipTapEditor";
@ -20,6 +21,7 @@ interface ContinueInputBoxProps {
) => void;
editorState?: JSONContent;
contextItems?: ContextItemWithId[];
appliedRules?: RuleWithSource[];
hidden?: boolean;
inputId: string; // used to keep track of things per input in redux
}
@ -116,6 +118,8 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
}
: {};
const { appliedRules = [], contextItems = [] } = props;
return (
<div
className={`${props.hidden ? "hidden" : ""}`}
@ -143,10 +147,15 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
/>
</GradientBorder>
</div>
<ContextItemsPeek
contextItems={props.contextItems}
isCurrentContextPeek={props.isLastUserInput}
/>
{(appliedRules.length > 0 || contextItems.length > 0) && (
<div className="mt-2 flex flex-col">
<RulesPeek appliedRules={props.appliedRules} />
<ContextItemsPeek
contextItems={props.contextItems}
isCurrentContextPeek={props.isLastUserInput}
/>
</div>
)}
</div>
);
}

View file

@ -0,0 +1,122 @@
import { DocumentTextIcon, GlobeAltIcon } from "@heroicons/react/24/outline";
import { RuleSource, RuleWithSource } from "core";
import { ComponentType, useMemo, useState } from "react";
import ToggleDiv from "../../ToggleDiv";
interface RulesPeekProps {
appliedRules?: RuleWithSource[];
icon?: ComponentType<React.SVGProps<SVGSVGElement>>;
}
interface RulesPeekItemProps {
rule: RuleWithSource;
}
// Convert technical source to user-friendly text
const getSourceLabel = (source: RuleSource): string => {
switch (source) {
case "default-chat":
return "Default Chat";
case "default-agent":
return "Default Agent";
case "model-chat-options":
return "Model Chat Options";
case "model-agent-options":
return "Model Agent Options";
case "rules-block":
return "Rules Block";
case "json-systemMessage":
return "System Message";
case ".continuerules":
return "Project Rules";
default:
return source;
}
};
export function RulesPeekItem({ rule }: RulesPeekItemProps) {
const isGlobal = !rule.globs;
const [expanded, setExpanded] = useState(false);
// Define maximum length for rule text display
const maxRuleLength = 100;
const isRuleLong = rule.rule.length > maxRuleLength;
// Get the displayed rule text based on expanded state
const displayedRule =
isRuleLong && !expanded
? `${rule.rule.slice(0, maxRuleLength)}...`
: rule.rule;
const toggleExpand = () => {
if (isRuleLong) {
setExpanded(!expanded);
}
};
return (
<div
className={`group mr-2 flex flex-col overflow-hidden rounded px-1.5 py-1 text-xs hover:bg-white/10 ${isRuleLong ? "cursor-pointer hover:text-gray-200" : ""}`}
data-testid="rules-peek-item"
onClick={toggleExpand}
>
<div className="flex w-full items-center">
{isGlobal ? (
<GlobeAltIcon className="mr-2 h-4 w-4 flex-shrink-0 text-gray-400" />
) : (
<DocumentTextIcon className="mr-2 h-4 w-4 flex-shrink-0 text-gray-400" />
)}
<div className="flex min-w-0 flex-1 gap-2 text-xs">
<div className="max-w-[50%] flex-shrink-0 truncate font-medium">
{rule.name || "Assistant rule"}
</div>
<div className="min-w-0 flex-1 overflow-hidden truncate whitespace-nowrap text-xs text-gray-500">
{isGlobal
? "Always applied"
: `Pattern: ${typeof rule.globs === "string" ? rule.globs : Array.isArray(rule.globs) ? rule.globs.join(", ") : ""}`}
</div>
</div>
</div>
<div
className={`mt-1 whitespace-pre-line pl-6 pr-2 text-xs italic text-gray-300`}
title={
isRuleLong ? (expanded ? "Click to collapse" : "Click to expand") : ""
}
>
{displayedRule}
{isRuleLong && (
<span className="ml-1 text-gray-400 opacity-0 transition-opacity group-hover:opacity-100">
{expanded ? "(collapse)" : "(expand)"}
</span>
)}
</div>
<div className="mt-1 pl-6 pr-2 text-xs text-gray-500">
Source: {getSourceLabel(rule.source)}
</div>
</div>
);
}
export function RulesPeek({ appliedRules, icon }: RulesPeekProps) {
const rules = useMemo(() => {
return appliedRules ?? [];
}, [appliedRules]);
if (!rules || rules.length === 0) {
return null;
}
return (
<ToggleDiv
icon={icon}
title={`${rules.length} rule${rules.length > 1 ? "s" : ""}`}
testId="rules-peek"
>
{rules.map((rule, idx) => (
<RulesPeekItem key={`rule-${idx}`} rule={rule} />
))}
</ToggleDiv>
);
}

View file

@ -320,6 +320,7 @@ export function Chat() {
isMainInput={false}
editorState={item.editorState}
contextItems={item.contextItems}
appliedRules={item.appliedRules}
inputId={item.message.id}
/>
</>

View file

@ -61,7 +61,7 @@ export function SimpleToolCallUI({
</div>
<span
className="ml-1 text-xs text-gray-400 transition-colors duration-200"
data-testid="toggle-div-title"
data-testid="tool-call-title"
>
<ToolCallStatusMessage tool={tool} toolCallState={toolCallState} />
</span>

View file

@ -15,6 +15,7 @@ import {
FileSymbolMap,
MessageModes,
PromptLog,
RuleWithSource,
Session,
SessionMetadata,
ToolCallDelta,
@ -247,6 +248,19 @@ export const sessionSlice = createSlice({
...payload.contextItems,
];
},
setAppliedRulesAtIndex: (
state,
{
payload,
}: PayloadAction<{
index: number;
appliedRules: RuleWithSource[];
}>,
) => {
if (state.history[payload.index]) {
state.history[payload.index].appliedRules = payload.appliedRules;
}
},
setInactive: (state) => {
const curMessage = state.history.at(-1);
@ -698,6 +712,7 @@ export const {
updateFileSymbols,
setContextItemsAtIndex,
addContextItemsAtIndex,
setAppliedRulesAtIndex,
setInactive,
streamUpdate,
newSession,

View file

@ -1,12 +1,14 @@
import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
import { JSONContent } from "@tiptap/core";
import { InputModifiers } from "core";
import { InputModifiers, ToolResultChatMessage, UserChatMessage } from "core";
import { constructMessages } from "core/llm/constructMessages";
import { getApplicableRules } from "core/llm/rules/getSystemMessageWithRules";
import posthog from "posthog-js";
import { v4 as uuidv4 } from "uuid";
import { getBaseSystemMessage } from "../../util";
import { selectSelectedChatModel } from "../slices/configSlice";
import {
setAppliedRulesAtIndex,
submitEditorAndInitAtIndex,
updateHistoryItemAtIndex,
} from "../slices/sessionSlice";
@ -84,17 +86,41 @@ export const streamResponseThunk = createAsyncThunk<
}),
);
// Construct messages from updated history
// Get updated history after the update
const updatedHistory = getState().session.history;
const messageMode = getState().session.mode
const baseChatOrAgentSystemMessage = getBaseSystemMessage(selectedChatModel, messageMode)
// Determine which rules apply to this message
const userMsg = updatedHistory[inputIndex].message;
const rules = getState().config.config.rules;
// Calculate applicable rules once
// We need to check the message type to match what getApplicableRules expects
const applicableRules = getApplicableRules(
userMsg.role === "user" || userMsg.role === "tool"
? (userMsg as UserChatMessage | ToolResultChatMessage)
: undefined,
rules,
);
// Store in history for UI display
dispatch(
setAppliedRulesAtIndex({
index: inputIndex,
appliedRules: applicableRules,
}),
);
const messageMode = getState().session.mode;
const baseChatOrAgentSystemMessage = getBaseSystemMessage(
selectedChatModel,
messageMode,
);
const messages = constructMessages(
messageMode,
[...updatedHistory],
baseChatOrAgentSystemMessage,
state.config.config.rules,
applicableRules,
);
posthog.capture("step run", {