Files
Netcatty/components/ai/toolArtifacts/toolArtifactNames.ts
T
fc55e9ac17 feat(vault): let Skills + CLI operate notes with the same approval as MCP (#3512)
* feat(vault): let Skills + CLI operate notes with the same approval as MCP

Expose vault note list, read, write, and markdown import on the tool CLI so agents can save generated documents only after the user approves.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): keep explicit empty note fields in the CLI

A supplied empty string was treated as an omitted flag, so note clears and empty markdown imports never reached the note service.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): allow an explicit empty note body on CLI create

notes create --content "" was rejected as a missing flag, so it did not match vault_notes_create.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): stop echoing imported note bodies and recognize leading CLI flags

A batch import resent every document over IPC, and a leading --json hid the notes command from artifact inference.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): make note CLI import safe and fail writes cleanly

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-09-29 12:22:35 +08:00

132 lines
4.1 KiB
TypeScript

const MCP_TOOL_NAME_PREFIX = 'mcp__';
const KNOWN_ARTIFACT_TOOL_NAMES = [
'terminal_read_context',
'vault_notes_create',
'vault_notes_update',
'vault_notes_get',
'vault_notes_list',
'vault_notes_delete',
'vault_notes_import',
'vault_hosts_create',
'vault_hosts_import',
'vault_hosts_list',
'host_get',
'snippets_list',
'snippets_get',
'snippets_create',
'snippets_update',
'snippets_delete',
'snippets_run',
'scripts_list',
'scripts_get',
'scripts_create',
'scripts_update',
'scripts_delete',
'scripts_run',
'scripts_reference',
'scripts_runs_list',
'scripts_run_stop',
'scripts_run_pause',
'scripts_run_resume',
'scripts_targets_set',
] as const;
const CLI_FLAGS_WITHOUT_VALUES = new Set(['--json', '--content-stdin', '--documents-stdin']);
const CLI_ARTIFACT_TOOL_NAMES = new Map<string, string>([
['vault host get', 'host_get'],
['notes list', 'vault_notes_list'],
['notes get', 'vault_notes_get'],
['notes create', 'vault_notes_create'],
['notes update', 'vault_notes_update'],
['notes delete', 'vault_notes_delete'],
['notes import', 'vault_notes_import'],
]);
function readCommandString(args: Record<string, unknown> | undefined): string | null {
if (!args) return null;
const raw = args.command;
if (typeof raw === 'string') return raw || null;
if (!Array.isArray(raw) || raw.length === 0) return null;
const isShellWrap =
raw.length >= 3 &&
/(?:^|\/)(sh|bash|zsh|fish|ash|dash)$/.test(String(raw[0] ?? '')) &&
/^-l?c$/.test(String(raw[1] ?? ''));
return isShellWrap
? String(raw[raw.length - 1] ?? '') || null
: raw.map((part) => String(part)).join(' ');
}
function unwrapShellCommand(command: string): string {
const strWrap = command.match(
/^(?:\S*\/)?(?:sh|bash|zsh|fish|ash|dash)\s+-l?c\s+(['"])([\s\S]*)\1\s*$/,
);
return strWrap ? strWrap[2] : command;
}
function stripWrappingQuote(value: string): string {
const trimmed = value.trim();
if (trimmed.length < 2) return trimmed;
const first = trimmed[0];
const last = trimmed[trimmed.length - 1];
return (first === last && (first === '"' || first === "'"))
? trimmed.slice(1, -1)
: trimmed;
}
export function normalizeArtifactToolName(toolName: string | undefined): string | undefined {
const trimmed = toolName?.trim();
if (!trimmed) return undefined;
if (trimmed.startsWith(MCP_TOOL_NAME_PREFIX)) {
const segments = trimmed.split('__').filter(Boolean);
return segments[segments.length - 1] || trimmed;
}
const prefixedArtifactToolName = KNOWN_ARTIFACT_TOOL_NAMES.find((candidate) => (
trimmed.endsWith(`_${candidate}`) || trimmed.endsWith(`-${candidate}`)
));
if (prefixedArtifactToolName) return prefixedArtifactToolName;
return trimmed;
}
function collectCliCommandWords(afterCli: string): string[] {
const commandWords: string[] = [];
const parts = afterCli.match(/(?:[^\s"'\\]|\\.|"(?:\\.|[^"\\])*"|'[^']*')+/g) ?? [];
for (let index = 0; index < parts.length; index += 1) {
const part = parts[index];
if (!part) continue;
if (part.startsWith('-')) {
// Flags may appear before or between command words. Skip a valued flag's
// quoted or unquoted value so note content is never a command word.
if (CLI_FLAGS_WITHOUT_VALUES.has(part)) continue;
const next = parts[index + 1];
if (next && !next.startsWith('-')) index += 1;
continue;
}
commandWords.push(part);
if (CLI_ARTIFACT_TOOL_NAMES.has(commandWords.join(' ')) || commandWords.length === 3) break;
}
return commandWords;
}
export function inferArtifactToolNameFromCliArgs(
args: Record<string, unknown> | undefined,
): string | undefined {
const command = readCommandString(args);
if (!command) return undefined;
const unwrapped = unwrapShellCommand(command);
const cliMatch = unwrapped.match(/(?:^|\s|["'])(?:\S*[\\/])?netcatty-tool-cli(?:\.(?:cjs|cmd))?(?=["'\s]|$)([\s\S]*)$/);
if (!cliMatch) return undefined;
const afterCli = stripWrappingQuote(cliMatch[1] ?? '').replace(/^["']?\s*/, '');
const commandKey = collectCliCommandWords(afterCli).join(' ');
return CLI_ARTIFACT_TOOL_NAMES.get(commandKey);
}