Add readonly and no-tools modes with tool permission filtering
This commit is contained in:
parent
4f32148ec9
commit
514b3b2c4a
15 changed files with 104 additions and 32 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@continuedev/cli",
|
||||
"version": "1.3.3",
|
||||
"version": "1.3.5",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@continuedev/cli",
|
||||
"version": "1.3.3",
|
||||
"version": "1.3.5",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@continuedev/config-yaml": "^1.0.96",
|
||||
|
|
|
|||
11
spec/modes.md
Normal file
11
spec/modes.md
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# Modes
|
||||
|
||||
Modes are flags that can be passed to the CLI for pre-configured tool permissions. The following modes are available:
|
||||
|
||||
## `readonly`
|
||||
|
||||
If `cn` is called with the `--readonly` flag, then only readonly tools are given to the model. Tools that can write to disk or make edits are left out. Each tool definition is marked with `readonly: boolean` to determine this.
|
||||
|
||||
## `no-tools`
|
||||
|
||||
If `cn` is called with the `--no-tools` flag, then no tools should be given to the model at all.
|
||||
10
src/args.ts
10
src/args.ts
|
|
@ -3,6 +3,8 @@ export interface CommandLineArgs {
|
|||
configPath?: string;
|
||||
prompt?: string; // Optional prompt argument
|
||||
resume?: boolean; // Resume from last session
|
||||
readonly?: boolean; // Only allow readonly tools
|
||||
noTools?: boolean; // Disable all tools
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -26,6 +28,14 @@ export function parseArgs(): CommandLineArgs {
|
|||
result.resume = true;
|
||||
}
|
||||
|
||||
if (args.includes("--readonly")) {
|
||||
result.readonly = true;
|
||||
}
|
||||
|
||||
if (args.includes("--no-tools")) {
|
||||
result.noTools = true;
|
||||
}
|
||||
|
||||
// Get config path from --config flag
|
||||
const configIndex = args.indexOf("--config");
|
||||
if (configIndex !== -1 && configIndex + 1 < args.length) {
|
||||
|
|
|
|||
10
src/index.ts
10
src/index.ts
|
|
@ -7,13 +7,13 @@ import { logout } from "./commands/logout.js";
|
|||
import { getVersion } from "./version.js";
|
||||
|
||||
// Add global error handlers to prevent uncaught errors from crashing the process
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
||||
process.on("unhandledRejection", (reason, promise) => {
|
||||
console.error("Unhandled Rejection at:", promise, "reason:", reason);
|
||||
// Don't exit the process, just log the error
|
||||
});
|
||||
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('Uncaught Exception:', error);
|
||||
process.on("uncaughtException", (error) => {
|
||||
console.error("Uncaught Exception:", error);
|
||||
// Don't exit the process, just log the error
|
||||
});
|
||||
|
||||
|
|
@ -30,6 +30,8 @@ program
|
|||
.option("--headless", "Run in headless mode (non-interactive)")
|
||||
.option("--config <path>", "Path to configuration file")
|
||||
.option("--resume", "Resume from last session")
|
||||
.option("--readonly", "Only allow readonly tools")
|
||||
.option("--no-tools", "Disable all tools")
|
||||
.action(async (prompt, options) => {
|
||||
await chat(prompt, options);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -15,6 +15,13 @@ import { chatCompletionStreamWithBackoff } from "./util/exponentialBackoff.js";
|
|||
dotenv.config();
|
||||
|
||||
export function getAllTools() {
|
||||
const args = parseArgs();
|
||||
|
||||
// If no-tools mode is enabled, return empty array
|
||||
if (args.noTools) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const allTools: ChatCompletionTool[] = BUILTIN_TOOLS.map((tool) => ({
|
||||
type: "function" as const,
|
||||
function: {
|
||||
|
|
@ -35,6 +42,7 @@ export function getAllTools() {
|
|||
},
|
||||
}));
|
||||
|
||||
// Add MCP tools if not in no-tools mode
|
||||
const mcpTools = MCPService.getInstance()?.getTools() ?? [];
|
||||
allTools.push(
|
||||
...mcpTools.map((tool) => ({
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ export const exitTool: Tool = {
|
|||
description:
|
||||
"Exit the current process with status code 1, indicating a failure or error",
|
||||
parameters: {},
|
||||
readonly: false,
|
||||
run: async (args: { dirpath: string }): Promise<string> => {
|
||||
process.exit(1);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
@ -45,6 +45,7 @@ export const fetchTool: Tool = {
|
|||
required: false,
|
||||
},
|
||||
},
|
||||
readonly: true,
|
||||
run: async (args: { url: string; timeout?: number }): Promise<string> => {
|
||||
const { url, timeout = 10000 } = args;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import { writeFileTool } from "./writeFile.js";
|
|||
|
||||
export type { Tool, ToolParameters };
|
||||
|
||||
export const BUILTIN_TOOLS: Tool[] = [
|
||||
const ALL_BUILTIN_TOOLS: Tool[] = [
|
||||
readFileTool,
|
||||
writeFileTool,
|
||||
listFilesTool,
|
||||
|
|
@ -24,8 +24,22 @@ export const BUILTIN_TOOLS: Tool[] = [
|
|||
...(parseArgs().isHeadless ? [exitTool] : []),
|
||||
];
|
||||
|
||||
export const BUILTIN_TOOLS: Tool[] = (() => {
|
||||
const args = parseArgs();
|
||||
|
||||
if (args.noTools) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (args.readonly) {
|
||||
return ALL_BUILTIN_TOOLS.filter((tool) => tool.readonly === true);
|
||||
}
|
||||
|
||||
return ALL_BUILTIN_TOOLS;
|
||||
})();
|
||||
|
||||
export function getToolDisplayName(toolName: string): string {
|
||||
const tool = BUILTIN_TOOLS.find(t => t.name === toolName);
|
||||
const tool = BUILTIN_TOOLS.find((t) => t.name === toolName);
|
||||
return tool?.displayName || toolName;
|
||||
}
|
||||
|
||||
|
|
@ -100,21 +114,39 @@ export async function executeToolCall(toolCall: {
|
|||
name: string;
|
||||
arguments: Record<string, any>;
|
||||
}): Promise<string> {
|
||||
const allTools: Tool[] = [
|
||||
...BUILTIN_TOOLS,
|
||||
...(MCPService.getInstance()
|
||||
?.getTools()
|
||||
.map((t) => ({
|
||||
name: t.name,
|
||||
displayName: t.name.replace("mcp__", "").replace("ide__", ""),
|
||||
description: t.description ?? "",
|
||||
parameters: convertInputSchemaToParameters(t.inputSchema),
|
||||
run: async (args: any) => {
|
||||
const result = await MCPService.getInstance()?.runTool(t.name, args);
|
||||
return JSON.stringify(result?.content) ?? "";
|
||||
},
|
||||
})) || []),
|
||||
];
|
||||
const args = parseArgs();
|
||||
|
||||
let mcpTools: Tool[] = [];
|
||||
|
||||
// Don't load MCP tools if no-tools mode is enabled
|
||||
if (!args.noTools) {
|
||||
mcpTools =
|
||||
MCPService.getInstance()
|
||||
?.getTools()
|
||||
.map((t) => ({
|
||||
name: t.name,
|
||||
displayName: t.name.replace("mcp__", "").replace("ide__", ""),
|
||||
description: t.description ?? "",
|
||||
parameters: convertInputSchemaToParameters(t.inputSchema),
|
||||
readonly: undefined, // MCP tools don't have readonly property, so we include them in readonly mode
|
||||
run: async (args: any) => {
|
||||
const result = await MCPService.getInstance()?.runTool(
|
||||
t.name,
|
||||
args
|
||||
);
|
||||
return JSON.stringify(result?.content) ?? "";
|
||||
},
|
||||
})) || [];
|
||||
|
||||
// Filter MCP tools in readonly mode - include them since they don't have readonly property
|
||||
if (args.readonly) {
|
||||
// MCP tools are included in readonly mode since they don't have an explicit readonly property
|
||||
// This is a design decision - you may want to exclude them entirely or handle them differently
|
||||
}
|
||||
}
|
||||
|
||||
const allTools: Tool[] = [...BUILTIN_TOOLS, ...mcpTools];
|
||||
|
||||
const tool = allTools.find((t) => t.name === toolCall.name);
|
||||
|
||||
if (!tool) {
|
||||
|
|
@ -138,4 +170,4 @@ export async function executeToolCall(toolCall: {
|
|||
error instanceof Error ? error.message : String(error)
|
||||
}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ export const listFilesTool: Tool = {
|
|||
required: true,
|
||||
},
|
||||
},
|
||||
readonly: true,
|
||||
run: async (args: { dirpath: string }): Promise<string> => {
|
||||
try {
|
||||
if (!fs.existsSync(args.dirpath)) {
|
||||
|
|
@ -40,4 +41,4 @@ export const listFilesTool: Tool = {
|
|||
}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
@ -12,6 +12,7 @@ export const readFileTool: Tool = {
|
|||
required: true,
|
||||
},
|
||||
},
|
||||
readonly: true,
|
||||
run: async (args: { filepath: string }): Promise<string> => {
|
||||
try {
|
||||
if (!fs.existsSync(args.filepath)) {
|
||||
|
|
@ -23,4 +24,4 @@ export const readFileTool: Tool = {
|
|||
return `Error reading file: ${error instanceof Error ? error.message : String(error)}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
@ -12,6 +12,7 @@ export const runTerminalCommandTool: Tool = {
|
|||
required: true,
|
||||
},
|
||||
},
|
||||
readonly: false,
|
||||
run: async ({ command }: { command: string }): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(command, (error, stdout, stderr) => {
|
||||
|
|
@ -27,4 +28,4 @@ export const runTerminalCommandTool: Tool = {
|
|||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
@ -26,6 +26,7 @@ export const searchCodeTool: Tool = {
|
|||
required: false,
|
||||
},
|
||||
},
|
||||
readonly: true,
|
||||
run: async (args: {
|
||||
pattern: string;
|
||||
path?: string;
|
||||
|
|
@ -67,4 +68,4 @@ export const searchCodeTool: Tool = {
|
|||
return `Error searching code: ${error instanceof Error ? error.message : String(error)}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
@ -13,4 +13,5 @@ export interface Tool {
|
|||
description: string;
|
||||
parameters: ToolParameters;
|
||||
run: (args: any) => Promise<string>;
|
||||
}
|
||||
readonly?: boolean; // Indicates if the tool is readonly
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ export const viewDiffTool: Tool = {
|
|||
required: false,
|
||||
},
|
||||
},
|
||||
readonly: true,
|
||||
run: async (args: { path?: string }): Promise<string> => {
|
||||
try {
|
||||
const repoPath = args.path || process.cwd();
|
||||
|
|
@ -49,4 +50,4 @@ export const viewDiffTool: Tool = {
|
|||
return `Error running git diff: ${error instanceof Error ? error.message : String(error)}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
@ -31,6 +31,7 @@ export const writeFileTool: Tool = {
|
|||
required: true,
|
||||
},
|
||||
},
|
||||
readonly: false,
|
||||
run: async (args: { filepath: string; content: string }): Promise<string> => {
|
||||
try {
|
||||
const dirPath = path.dirname(args.filepath);
|
||||
|
|
@ -58,4 +59,4 @@ export const writeFileTool: Tool = {
|
|||
return `Error writing to file: ${error instanceof Error ? error.message : String(error)}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue