fix(cli): load rules into system message (#10408)
* fix(cli): load rules into system message * show loaded rules in config * Update extensions/cli/src/systemMessage.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * fix config service tests * fix tests --------- Co-authored-by: Nate Sesti <33237525+sestinj@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
parent
432f613310
commit
df98f99976
5 changed files with 105 additions and 0 deletions
|
|
@ -5,3 +5,5 @@ export const constructSystemMessage = vi
|
|||
.mockResolvedValue(
|
||||
"You are an agent in the Continue CLI. Given the user's prompt, you should use the tools available to you to answer the user's question.",
|
||||
);
|
||||
|
||||
export const loadMarkdownRulesWithMetadata = vi.fn().mockReturnValue([]);
|
||||
|
|
|
|||
|
|
@ -19,6 +19,9 @@ vi.mock("../configLoader.js", () => ({
|
|||
vi.mock("../util/logger.js");
|
||||
vi.mock("./ServiceContainer.js");
|
||||
vi.mock("@continuedev/config-yaml");
|
||||
vi.mock("src/systemMessage.js", () => ({
|
||||
loadMarkdownRulesWithMetadata: vi.fn().mockReturnValue([]),
|
||||
}));
|
||||
|
||||
const defaultModel = {
|
||||
provider: "anthropic",
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {
|
|||
import { DefaultApiInterface } from "@continuedev/sdk/dist/api/dist/index.js";
|
||||
|
||||
import { isStringRule } from "src/hubLoader.js";
|
||||
import { loadMarkdownRulesWithMetadata } from "src/systemMessage.js";
|
||||
import { getErrorString } from "src/util/error.js";
|
||||
|
||||
import { AuthConfig, loadAuthConfig } from "../auth/workos.js";
|
||||
|
|
@ -275,6 +276,17 @@ export class ConfigService
|
|||
const loadedConfig = result.config;
|
||||
const merged = mergeUnrolledAssistants(loadedConfig, additional);
|
||||
|
||||
const markdownRules = loadMarkdownRulesWithMetadata();
|
||||
if (markdownRules.length > 0) {
|
||||
const existingRuleContents = new Set(
|
||||
(merged.rules ?? []).map((r) => (typeof r === "string" ? r : r?.rule)),
|
||||
);
|
||||
const newRules = markdownRules.filter(
|
||||
(r) => !existingRuleContents.has(r.rule),
|
||||
);
|
||||
merged.rules = [...(merged.rules ?? []), ...newRules];
|
||||
}
|
||||
|
||||
const withModel = await this.addDefaultChatModelIfNone(
|
||||
merged,
|
||||
apiClient,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@ import { execSync } from "child_process";
|
|||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
import { parseMarkdownRule, RuleObject } from "@continuedev/config-yaml";
|
||||
|
||||
import { env } from "./env.js";
|
||||
import { processRule } from "./hubLoader.js";
|
||||
import { PermissionMode } from "./permissions/types.js";
|
||||
import { serviceContainer } from "./services/ServiceContainer.js";
|
||||
|
|
@ -74,6 +77,79 @@ async function getConfigYamlRules(): Promise<string[]> {
|
|||
return [];
|
||||
}
|
||||
|
||||
function getRuleNameFromPath(filePath: string): string {
|
||||
const segments = filePath.split(/[/\\]/);
|
||||
const lastTwoParts = segments.slice(-2);
|
||||
return lastTwoParts.filter(Boolean).join("/").replace(/\.md$/, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan .continue/rules/ directories for markdown rule files and return the rules with metadata that should be always-applied
|
||||
*/
|
||||
export function loadMarkdownRulesWithMetadata(): RuleObject[] {
|
||||
const cwd = process.cwd();
|
||||
const rulesDirs = [
|
||||
path.join(cwd, ".continue", "rules"),
|
||||
path.join(env.continueHome, "rules"),
|
||||
];
|
||||
|
||||
const rules: RuleObject[] = [];
|
||||
|
||||
for (const dir of rulesDirs) {
|
||||
if (!fs.existsSync(dir)) continue;
|
||||
|
||||
let files: string[];
|
||||
try {
|
||||
files = fs.readdirSync(dir, { recursive: true }) as string[];
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const file of files) {
|
||||
if (!String(file).endsWith(".md")) continue;
|
||||
|
||||
const filePath = path.join(dir, String(file));
|
||||
try {
|
||||
const stat = fs.statSync(filePath);
|
||||
if (!stat.isFile()) continue;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const { frontmatter, markdown } = parseMarkdownRule(content);
|
||||
|
||||
if (frontmatter.invokable) continue;
|
||||
|
||||
const isAlwaysApply =
|
||||
frontmatter.alwaysApply === true ||
|
||||
(frontmatter.alwaysApply === undefined &&
|
||||
!frontmatter.globs &&
|
||||
!frontmatter.regex);
|
||||
|
||||
if (isAlwaysApply && markdown.trim()) {
|
||||
const ruleName =
|
||||
frontmatter.name || getRuleNameFromPath(String(file));
|
||||
rules.push({
|
||||
name: ruleName,
|
||||
rule: markdown,
|
||||
description: frontmatter.description,
|
||||
globs: frontmatter.globs,
|
||||
regex: frontmatter.regex,
|
||||
alwaysApply: true,
|
||||
sourceFile: filePath,
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Skip files that can't be read or parsed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and construct a comprehensive system message with base message and rules section
|
||||
* @param additionalRules - Additional rules from --rule flags
|
||||
|
|
@ -124,6 +200,17 @@ export async function constructSystemMessage(
|
|||
const configYamlRules = await getConfigYamlRules();
|
||||
processedRules.push(...configYamlRules);
|
||||
|
||||
// Load markdown rules from .continue/rules/ directories
|
||||
const markdownRules = loadMarkdownRulesWithMetadata();
|
||||
// Deduplicate against already-loaded rules
|
||||
const existingRulesSet = new Set(processedRules);
|
||||
for (const rule of markdownRules) {
|
||||
if (!existingRulesSet.has(rule.rule)) {
|
||||
processedRules.push(rule.rule);
|
||||
existingRulesSet.add(rule.rule);
|
||||
}
|
||||
}
|
||||
|
||||
// Construct the comprehensive system message
|
||||
let systemMessage = baseSystemMessage;
|
||||
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ vi.mock("./src/systemMessage.js", () => ({
|
|||
.mockResolvedValue(
|
||||
"You are an agent in the Continue CLI. Given the user's prompt, you should use the tools available to you to answer the user's question.",
|
||||
),
|
||||
loadMarkdownRulesWithMetadata: vi.fn().mockReturnValue([]),
|
||||
}));
|
||||
|
||||
// Mock environment for tests
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue