fix: uri utils

This commit is contained in:
Tomasz Stefaniak 2025-06-11 14:39:40 -07:00
parent 9dba4730b9
commit f1026c6a2c
4 changed files with 19 additions and 56 deletions

View file

@ -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";
});

View file

@ -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 (

View file

@ -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

View file

@ -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<RuleWithSource>,
@ -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";