Skip to content

Commit 943c964

Browse files
author
linyuan.yang
committed
合并 mv cp tool
1 parent 3cfdc43 commit 943c964

6 files changed

Lines changed: 29 additions & 91 deletions

File tree

‎packages/sbot/prompts/tools/fs/cp.txt‎

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Move/rename or copy files and directories, like bash mv / cp. Supports multiple sources.
2+
- move [src] [dest]: if dest is an existing directory, src is moved/copied into it; otherwise src is renamed/copied to dest.
3+
- move [src1] [src2] ... [destDir]: moves/copies all sources into an existing directory.
4+
- Set copy=true to copy (cp) instead of move (mv).
5+
- Copying a directory requires recursive=true (-r); moving a directory does not.
6+
- Set overwrite=false to skip existing destinations instead of overwriting (-n).
7+
Parent directories of dest are created automatically. All paths must be absolute.

‎packages/sbot/prompts/tools/fs/mv.txt‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎packages/sbot/src/Tools/FileSystem/index.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export { createGlobTool } from './operations/glob';
2020
export { createLsTool } from './operations/ls';
2121
export { createRmTool } from './operations/rm';
2222
export { createMkdirTool } from './operations/mkdir';
23-
export { createMvTool } from './operations/mv';
24-
export { createCpTool } from './operations/cp';
23+
export { createMoveTool } from './operations/move';
2524

2625
import { createReadTool } from './content/read';
2726
import { createReadMediaFileTool } from './content/readMediaFile';
@@ -32,8 +31,7 @@ import { createGlobTool } from './operations/glob';
3231
import { createLsTool } from './operations/ls';
3332
import { createRmTool } from './operations/rm';
3433
import { createMkdirTool } from './operations/mkdir';
35-
import { createMvTool } from './operations/mv';
36-
import { createCpTool } from './operations/cp';
34+
import { createMoveTool } from './operations/move';
3735

3836
export interface FileSystemToolParams {
3937
/** 只读模式:仅暴露读类工具,过滤掉 write/edit/mkdir/rm/mv/cp。admin 端传字符串 "true"/"false",所以这里要兼容两种形态 */
@@ -62,7 +60,6 @@ export function createFileSystemTools(params?: FileSystemToolParams): Structured
6260
createEditFileTool(),
6361
createMkdirTool(),
6462
createRmTool(),
65-
createMvTool(),
66-
createCpTool(),
63+
createMoveTool(),
6764
];
6865
}

‎packages/sbot/src/Tools/FileSystem/operations/cp.ts‎

Lines changed: 0 additions & 66 deletions
This file was deleted.

packages/sbot/src/Tools/FileSystem/operations/mv.ts renamed to packages/sbot/src/Tools/FileSystem/operations/move.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { createTextContent, createErrorResult, createSuccessResult, formatError,
77
import { resolvePath } from '../utils';
88
import { loadPrompt } from '../../../Core/PromptLoader';
99

10-
const logger = LoggerService.getLogger('Tools/FileSystem/operations/mv.ts');
10+
const logger = LoggerService.getLogger('Tools/FileSystem/operations/move.ts');
1111

1212
function moveOne(src: string, dst: string): void {
1313
try {
@@ -23,23 +23,28 @@ function moveOne(src: string, dst: string): void {
2323
}
2424
}
2525

26-
/** Move or rename files/directories, like bash mv */
27-
export function createMvTool(): StructuredToolInterface {
26+
/** Move/rename or copy files and directories, like bash mv / cp. Supports multiple sources. */
27+
export function createMoveTool(): StructuredToolInterface {
2828
return new DynamicStructuredTool({
29-
name: 'mv',
30-
description: loadPrompt('tools/fs/mv.txt'),
29+
name: 'move',
30+
description: loadPrompt('tools/fs/move.txt'),
3131
schema: z.object({
3232
sources: z.array(z.string()).min(1).describe('One or more absolute source paths'),
3333
dest: z.string().describe('Absolute destination path (directory or new name)'),
34-
noClobber: z.boolean().optional().default(false).describe('Do not overwrite existing files (-n), default false'),
34+
copy: z.boolean().optional().default(false).describe('Copy instead of move (cp). Default false (mv)'),
35+
recursive: z.boolean().optional().default(false).describe('Copy directories recursively (-r). Only meaningful with copy=true. Default false'),
36+
overwrite: z.boolean().optional().default(true).describe('Overwrite existing destination files. Default true (matches bash mv/cp)'),
3537
}) as any,
36-
func: async ({ sources, dest, noClobber = false }: any): Promise<MCPToolResult> => {
38+
func: async ({ sources, dest, copy = false, recursive = false, overwrite = true }: any): Promise<MCPToolResult> => {
3739
try {
3840
const srcs = (sources as string[]).map(resolvePath);
3941
const dst = resolvePath(dest);
4042

4143
for (const src of srcs) {
4244
if (!fs.existsSync(src)) throw new Error(`Source path does not exist: ${src}`);
45+
if (copy && fs.statSync(src).isDirectory() && !recursive) {
46+
return createErrorResult(`${src} is a directory. Set recursive=true to copy directories`);
47+
}
4348
}
4449

4550
const dstExists = fs.existsSync(dst);
@@ -54,21 +59,25 @@ export function createMvTool(): StructuredToolInterface {
5459
for (const src of srcs) {
5560
const finalDst = dstIsDir ? path.join(dst, path.basename(src)) : dst;
5661

57-
if (fs.existsSync(finalDst) && noClobber) {
62+
if (fs.existsSync(finalDst) && !overwrite) {
5863
results.push(`Skipped (destination already exists): ${finalDst}`);
5964
continue;
6065
}
6166

6267
const parentDir = path.dirname(finalDst);
6368
if (!fs.existsSync(parentDir)) fs.mkdirSync(parentDir, { recursive: true });
6469

65-
moveOne(src, finalDst);
70+
if (copy) {
71+
fs.cpSync(src, finalDst, { recursive, force: overwrite });
72+
} else {
73+
moveOne(src, finalDst);
74+
}
6675
results.push(`${src} -> ${finalDst}`);
6776
}
6877

6978
return createSuccessResult(createTextContent(results.join('\n')));
7079
} catch (e: any) {
71-
logger.error(`mv: ${formatError(e, true)}`);
80+
logger.error(`move: ${formatError(e, true)}`);
7281
return createErrorResult(formatError(e));
7382
}
7483
}

0 commit comments

Comments
 (0)