continue-custom/core/config/workspace/workspaceBlocks.ts
Keerthana Panyam b461fdc3cd
feat: added UI for global rule creation (#7467)
feat: add rule scope dropdown for global rule creation

- added global mode toggle
- implemented conditional styling (white when enabled, gray when disabled)
feat: improve global rules UI and fix debug mode issues

- added dropdown button component to switch between global and workspace rule creation.
- fixed global rules creation to use nested rules/ subdir in debug mode
- prevent default glob patterns for global rules by returning undefined globs
- generate unique rule names for global rules
- added watcher for rules in global directory
2025-10-01 11:03:10 -07:00

189 lines
4.9 KiB
TypeScript

import {
BlockType,
ConfigYaml,
createRuleMarkdown,
createPromptMarkdown,
RULE_FILE_EXTENSION,
} from "@continuedev/config-yaml";
import * as YAML from "yaml";
import { IDE } from "../..";
import { getContinueGlobalPath } from "../../util/paths";
import { joinPathsToUri } from "../../util/uri";
const BLOCK_TYPE_CONFIG: Record<
BlockType,
{ singular: string; filename: string }
> = {
context: { singular: "context", filename: "context" },
models: { singular: "model", filename: "model" },
rules: { singular: "rule", filename: "rule" },
docs: { singular: "doc", filename: "doc" },
prompts: { singular: "prompt", filename: "prompt" },
mcpServers: { singular: "MCP server", filename: "mcp-server" },
data: { singular: "data", filename: "data" },
};
function getContentsForNewBlock(blockType: BlockType): ConfigYaml {
const configYaml: ConfigYaml = {
name: `New ${BLOCK_TYPE_CONFIG[blockType]?.singular}`,
version: "0.0.1",
schema: "v1",
};
switch (blockType) {
case "context":
configYaml.context = [
{
provider: "file",
},
];
break;
case "models":
configYaml.models = [
{
provider: "anthropic",
model: "claude-3-7-sonnet-latest",
apiKey: "${{ secrets.ANTHROPIC_API_KEY }}",
name: "Claude 3.7 Sonnet",
roles: ["chat", "edit"],
},
];
break;
case "rules":
configYaml.rules = ["Always give concise responses"];
break;
case "docs":
configYaml.docs = [
{
name: "New docs",
startUrl: "https://docs.continue.dev",
},
];
break;
case "prompts":
configYaml.prompts = [
{
name: "New prompt",
description: "New prompt",
prompt:
"Please write a thorough suite of unit tests for this code, making sure to cover all relevant edge cases",
},
];
break;
case "mcpServers":
configYaml.mcpServers = [
{
name: "New MCP server",
command: "npx",
args: ["-y", "<your-mcp-server>"],
env: {},
},
];
break;
}
return configYaml;
}
function getFileExtension(blockType: BlockType): string {
if (blockType === "rules" || blockType === "prompts") {
return "md";
}
return "yaml";
}
export function getFileContent(blockType: BlockType): string {
if (blockType === "rules") {
return createRuleMarkdown("New Rule", "Your rule content", {
description: "A description of your rule",
});
} else if (blockType === "prompts") {
return createPromptMarkdown(
"New prompt",
"Please write a thorough suite of unit tests for this code, making sure to cover all relevant edge cases",
{
description: "New prompt",
invokable: true,
},
);
} else {
return YAML.stringify(getContentsForNewBlock(blockType));
}
}
export async function findAvailableFilename(
baseDirUri: string,
blockType: BlockType,
fileExists: (uri: string) => Promise<boolean>,
extension?: string,
isGlobal?: boolean,
): Promise<string> {
// Differentiate filename based on whether its a global rule or a workspace rule
const baseFilename =
blockType === "rules" && isGlobal
? "global-rule"
: `new-${BLOCK_TYPE_CONFIG[blockType]?.filename}`;
const fileExtension = extension ?? getFileExtension(blockType);
let counter = 0;
let fileUri: string;
do {
const suffix = counter === 0 ? "" : `-${counter}`;
fileUri = joinPathsToUri(
baseDirUri,
`${baseFilename}${suffix}.${fileExtension}`,
);
counter++;
} while (await fileExists(fileUri));
return fileUri;
}
export async function createNewWorkspaceBlockFile(
ide: IDE,
blockType: BlockType,
): Promise<void> {
const workspaceDirs = await ide.getWorkspaceDirs();
if (workspaceDirs.length === 0) {
throw new Error(
"No workspace directories found. Make sure you've opened a folder in your IDE.",
);
}
const baseDirUri = joinPathsToUri(workspaceDirs[0], `.continue/${blockType}`);
const fileUri = await findAvailableFilename(
baseDirUri,
blockType,
ide.fileExists.bind(ide),
);
const fileContent = getFileContent(blockType);
await ide.writeFile(fileUri, fileContent);
await ide.openFile(fileUri);
}
export async function createNewGlobalRuleFile(ide: IDE): Promise<void> {
try {
const globalDir = getContinueGlobalPath();
// Create the rules subdirectory within the global directory
const rulesDir = joinPathsToUri(globalDir, "rules");
const fileUri = await findAvailableFilename(
rulesDir,
"rules",
ide.fileExists.bind(ide),
undefined,
true, // isGlobal = true for global rules
);
const fileContent = getFileContent("rules");
await ide.writeFile(fileUri, fileContent);
await ide.openFile(fileUri);
} catch (error) {
throw error;
}
}