From f1026c6a2cfdd9424097d2103a5ffc2216e402d9 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Wed, 11 Jun 2025 14:39:40 -0700 Subject: [PATCH] fix: uri utils --- core/config/markdown/loadCodebaseRules.ts | 23 ++----------- core/core.ts | 9 +++--- core/llm/rules/getSystemMessageWithRules.ts | 32 ++++--------------- core/tools/implementations/createRuleBlock.ts | 11 ++++--- 4 files changed, 19 insertions(+), 56 deletions(-) diff --git a/core/config/markdown/loadCodebaseRules.ts b/core/config/markdown/loadCodebaseRules.ts index 12d88290b..b7e6b0db4 100644 --- a/core/config/markdown/loadCodebaseRules.ts +++ b/core/config/markdown/loadCodebaseRules.ts @@ -1,27 +1,9 @@ import { ConfigValidationError } from "@continuedev/config-yaml"; import { IDE, RuleWithSource } from "../.."; import { walkDirs } from "../../indexing/walkDir"; +import { getUriPathBasename } from "../../util/uri"; import { convertMarkdownRuleToContinueRule } from "./parseMarkdownRule"; -/** - * Gets the directory part of a path (without using Node.js path module) - */ -const getDirname = (filePath: string): string => { - // Normalize path separators to forward slash - const normalizedPath = filePath.replace(/\\/g, "/"); - - // Find the last slash - const lastSlashIndex = normalizedPath.lastIndexOf("/"); - - // If no slash found, return empty string (current directory) - if (lastSlashIndex === -1) { - return ""; - } - - // Return everything up to the last slash - return normalizedPath.substring(0, lastSlashIndex); -}; - /** * Loads rules from rules.md files colocated in the codebase */ @@ -38,8 +20,7 @@ export async function loadCodebaseRules(ide: IDE): Promise<{ // Filter to just rules.md files const rulesMdFiles = allFiles.filter((file) => { - const parts = file.split("/"); - const filename = parts[parts.length - 1]; + const filename = getUriPathBasename(file); return filename === "rules.md"; }); diff --git a/core/core.ts b/core/core.ts index 3a4079b1a..6673c7fce 100644 --- a/core/core.ts +++ b/core/core.ts @@ -64,6 +64,7 @@ import type { FromCoreProtocol, ToCoreProtocol } from "./protocol"; import { OnboardingModes } from "./protocol/core"; import type { IMessenger, Message } from "./protocol/messenger"; import { StreamAbortManager } from "./util/abortManager"; +import { getUriPathBasename } from "./util/uri"; export class Core { configHandler: ConfigHandler; @@ -581,8 +582,7 @@ export class Core { // Check for rules.md files being created let rulesFileCreated = false; for (const uri of data.uris) { - const parts = uri.split("/"); - const filename = parts[parts.length - 1]; + const filename = getUriPathBasename(uri); if (filename === "rules.md") { rulesFileCreated = true; break; @@ -613,8 +613,7 @@ export class Core { // Check for rules.md files being deleted let rulesFileDeleted = false; for (const uri of data.uris) { - const parts = uri.split("/"); - const filename = parts[parts.length - 1]; + const filename = getUriPathBasename(uri); if (filename === "rules.md") { rulesFileDeleted = true; break; @@ -851,7 +850,7 @@ export class Core { uri.endsWith(".prompt") || uri.endsWith(SYSTEM_PROMPT_DOT_FILE) || (uri.includes(".continue") && uri.endsWith(".yaml")) || - uri.endsWith("rules.md") // Add check for rules.md files + uri.endsWith("rules.md") ) { await this.configHandler.reloadConfig(); } else if ( diff --git a/core/llm/rules/getSystemMessageWithRules.ts b/core/llm/rules/getSystemMessageWithRules.ts index 8fb4e49e1..279917b16 100644 --- a/core/llm/rules/getSystemMessageWithRules.ts +++ b/core/llm/rules/getSystemMessageWithRules.ts @@ -6,6 +6,7 @@ import { UserChatMessage, } from "../.."; import { renderChatMessage } from "../../util/messageContent"; +import { getCleanUriPath } from "../../util/uri"; import { extractPathsFromCodeBlocks } from "../utils/extractPathsFromCodeBlocks"; /** @@ -50,25 +51,6 @@ const matchesGlobs = ( return false; }; -/** - * Gets the directory part of a path (without using Node.js path module) - */ -const getDirname = (filePath: string): string => { - // Normalize path separators to forward slash - const normalizedPath = filePath.replace(/\\/g, "/"); - - // Find the last slash - const lastSlashIndex = normalizedPath.lastIndexOf("/"); - - // If no slash found, return empty string (current directory) - if (lastSlashIndex === -1) { - return ""; - } - - // Return everything up to the last slash - return normalizedPath.substring(0, lastSlashIndex); -}; - /** * Determines if a file path is within a specific directory or its subdirectories * @@ -100,10 +82,7 @@ const isFileInDirectory = ( // This is a simple check to see if the file might be in this directory const containsDir = normalizedFilePath.includes(`${dirName}/`); - // For logging - const result = containsDir; - - return result; + return containsDir; }; /** @@ -163,11 +142,14 @@ export const shouldApplyRule = ( // For non-root rules, we need to check if any files are in the rule's directory if (!isRootRule && rule.ruleFile) { - const ruleDirectory = getDirname(rule.ruleFile); + const ruleDirectory = getCleanUriPath(rule.ruleFile); + const lastSlashIndex = ruleDirectory.lastIndexOf("/"); + const ruleDirPath = + lastSlashIndex !== -1 ? ruleDirectory.substring(0, lastSlashIndex) : ""; // Filter to only files in this directory or its subdirectories const filesInRuleDirectory = filePaths.filter((filePath) => - isFileInDirectory(filePath, ruleDirectory), + isFileInDirectory(filePath, ruleDirPath), ); // If no files are in this directory, don't apply the rule diff --git a/core/tools/implementations/createRuleBlock.ts b/core/tools/implementations/createRuleBlock.ts index 0d2d58f68..1cae7659d 100644 --- a/core/tools/implementations/createRuleBlock.ts +++ b/core/tools/implementations/createRuleBlock.ts @@ -2,7 +2,7 @@ import * as YAML from "yaml"; import { ToolImpl } from "."; import { RuleWithSource } from "../.."; import { RuleFrontmatter } from "../../config/markdown/parseMarkdownRule"; -import { joinPathsToUri } from "../../util/uri"; +import { joinPathsToUri, pathToUriPathSegment } from "../../util/uri"; export type CreateRuleBlockArgs = Pick< Required, @@ -14,11 +14,12 @@ export const createRuleBlockImpl: ToolImpl = async ( args: CreateRuleBlockArgs, extras, ) => { - // Sanitize rule name for use in filename (remove special chars, replace spaces with dashes) - const safeRuleName = args.name + // Use the pathToUriPathSegment utility for sanitizing the name + const safeRuleName = pathToUriPathSegment(args.name) .toLowerCase() - .replace(/[^a-z0-9\s-]/g, "") - .replace(/\s+/g, "-"); + .replace(/[^a-z0-9-]/g, "") // Further sanitize to remove any remaining special chars + .replace(/-+/g, "-") // Replace multiple dashes with a single dash + .replace(/^-|-$/g, ""); // Remove leading/trailing dashes const fileExtension = "md";