UNPKG

@wonderwhy-er/desktop-commander

Version:

MCP server for terminal operations and file editing

93 lines (92 loc) • 4.57 kB
/** * What the command check finds in a command's text. The check reads each * command with a reader for the shell that will run it (POSIX shells, * PowerShell, cmd). Every reader returns a CommandReading, and checkCommand() * in command-manager.ts turns it into a CommandCheck. */ /** Private inspection state: the arrays hold only the prefix before a bounded expansion failure. */ export interface ArgumentInspection { argumentError?: string; } /** An input budget failure, handled like unreadable input rather than a server failure. */ export declare class IncompleteArgumentsError extends Error { } /** Consumers call this only after their existing rules say they need the argument vector. */ export declare function requireCompleteArguments(inspection: ArgumentInspection): void; /** Explicit CMD delayed expansion; unknown retains the check's prior literal-name policy. */ export type CmdDelayedExpansion = 'on' | 'off' | 'unknown'; export interface CommandReadOptions { readonly cmdDelayedExpansion?: CmdDelayedExpansion; } /** One command the shell would run */ export interface FoundCommand extends ArgumentInspection { /** * The command's name as the shell reads it, with quotes and escapes removed. * In PowerShell it is what the name means there: an alias gives its target * (in Windows PowerShell 5.1 "sc" gives "Set-Content"), and a program gives * the program's file name. When the name is known only at run time ("$tool"), * this is the name as it is written in the text. */ name: string; /** * The command's words as the shell reads them, starting with the name as * written (before PowerShell gives its meaning). A word known only at run * time, such as "$CMD" or "$(date)", is null. With argumentError these arrays are only an inspected prefix. */ words: (string | null)[]; /** * The same words as they are written in the text, quotes included. The * wrapper rules use them: cmd's start takes a first word in quotes as the * window title, and a program known only at run time is named as written. */ written: string[]; /** * The same words as the shell reads them, but with each part known only at * run time kept as it is written: "sudo $X" gives sudo $X, where `words` * has null and `written` keeps the quotes. A shell started with a command * text (bash -c "sudo $X") gets such a word as its command, so the check * reads it to see the programs it names. */ wordTexts: string[]; /** * PowerShell's Start-Process only: the programs it may start, as this * PowerShell binds its FilePath, each with its value (null when known only * at run time) and its text as written. A splatted @params may hold * FilePath, so it counts as a program known only at run time. */ filePaths?: { word: string | null; written: string; }[]; /** Where the command starts: an index into the command's text */ start: number; /** True when the name is known only at run time: a variable, or the output of another command */ nameAtRunTime: boolean; } /** A function the command's text defines */ export interface DefinedFunction { /** The function's name as written */ name: string; /** Where the definition ends: the index just past its last character */ end: number; } /** What a reader found in a command's text */ export interface CommandReading { /** Every command the shell would run, including those nested in others, in the order they start */ commands: FoundCommand[]; /** * The functions defined by the text's own statements, which stay defined * for the rest of the text. A definition inside a subshell, a { } group, * another function's body, a pipeline, an if or loop body, after && or ||, * or in a PowerShell script block is not listed: it may not exist when a * later command runs. Nor is a function the text may remove or hide behind * an alias (unset -f halt, Remove-Item function:net, Set-Alias net net.exe), * wherever that is written, or one the PowerShell session has an alias of * (the user's profile can set one). There are none when the text runs code * the reader doesn't read (eval, a file run with ., Invoke-Expression, a * method call), which may remove any of them. */ functions: DefinedFunction[]; /** A syntax/name-inspection error, with empty lists. Deferred argument budgets are explicit on their commands. */ parseError?: string; }