@loopstack/sandbox-filesystem 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +234 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/sandbox-filesystem.module.d.ts +2 -0
- package/dist/sandbox-filesystem.module.js +46 -0
- package/dist/sandbox-filesystem.module.js.map +1 -0
- package/dist/tools/sandbox-create-directory.tool.d.ts +19 -0
- package/dist/tools/sandbox-create-directory.tool.js +77 -0
- package/dist/tools/sandbox-create-directory.tool.js.map +1 -0
- package/dist/tools/sandbox-delete.tool.d.ts +20 -0
- package/dist/tools/sandbox-delete.tool.js +77 -0
- package/dist/tools/sandbox-delete.tool.js.map +1 -0
- package/dist/tools/sandbox-exists.tool.d.ts +20 -0
- package/dist/tools/sandbox-exists.tool.js +89 -0
- package/dist/tools/sandbox-exists.tool.js.map +1 -0
- package/dist/tools/sandbox-file-info.tool.d.ts +27 -0
- package/dist/tools/sandbox-file-info.tool.js +96 -0
- package/dist/tools/sandbox-file-info.tool.js.map +1 -0
- package/dist/tools/sandbox-list-directory.tool.d.ts +26 -0
- package/dist/tools/sandbox-list-directory.tool.js +102 -0
- package/dist/tools/sandbox-list-directory.tool.js.map +1 -0
- package/dist/tools/sandbox-read-file.tool.d.ts +22 -0
- package/dist/tools/sandbox-read-file.tool.js +71 -0
- package/dist/tools/sandbox-read-file.tool.js.map +1 -0
- package/dist/tools/sandbox-write-file.tool.d.ts +24 -0
- package/dist/tools/sandbox-write-file.tool.js +96 -0
- package/dist/tools/sandbox-write-file.tool.js.map +1 -0
- package/package.json +51 -0
- package/src/index.ts +8 -0
- package/src/sandbox-filesystem.module.ts +48 -0
- package/src/tools/sandbox-create-directory.tool.ts +93 -0
- package/src/tools/sandbox-delete.tool.ts +87 -0
- package/src/tools/sandbox-exists.tool.ts +102 -0
- package/src/tools/sandbox-file-info.tool.ts +119 -0
- package/src/tools/sandbox-list-directory.tool.ts +131 -0
- package/src/tools/sandbox-read-file.tool.ts +82 -0
- package/src/tools/sandbox-write-file.tool.ts +121 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 The Loopstack Authors.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Injectable, Logger } from '@nestjs/common';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
import { BlockConfig, Tool, ToolResult, WithArguments } from '@loopstack/common';
|
|
19
|
+
import { ToolBase, WorkflowExecution } from '@loopstack/core';
|
|
20
|
+
import { SandboxCommand } from '@loopstack/sandbox-tool';
|
|
21
|
+
|
|
22
|
+
const propertiesSchema = z
|
|
23
|
+
.object({
|
|
24
|
+
containerId: z.string().describe('The ID of the container to create the directory in'),
|
|
25
|
+
path: z.string().describe('The path of the directory to create'),
|
|
26
|
+
recursive: z.boolean().default(true).describe("Whether to create parent directories if they don't exist"),
|
|
27
|
+
})
|
|
28
|
+
.strict();
|
|
29
|
+
|
|
30
|
+
type SandboxCreateDirectoryArgs = z.infer<typeof propertiesSchema>;
|
|
31
|
+
|
|
32
|
+
interface SandboxCreateDirectoryResult {
|
|
33
|
+
path: string;
|
|
34
|
+
created: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Injectable()
|
|
38
|
+
@BlockConfig({
|
|
39
|
+
config: {
|
|
40
|
+
description: 'Create a directory in a sandbox container',
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
@WithArguments(propertiesSchema)
|
|
44
|
+
export class SandboxCreateDirectory extends ToolBase<SandboxCreateDirectoryArgs> {
|
|
45
|
+
private readonly logger = new Logger(SandboxCreateDirectory.name);
|
|
46
|
+
|
|
47
|
+
@Tool() private sandboxCommand: SandboxCommand;
|
|
48
|
+
|
|
49
|
+
async execute(
|
|
50
|
+
args: SandboxCreateDirectoryArgs,
|
|
51
|
+
_ctx: WorkflowExecution,
|
|
52
|
+
): Promise<ToolResult<SandboxCreateDirectoryResult>> {
|
|
53
|
+
const { containerId, path: dirPath, recursive } = args;
|
|
54
|
+
|
|
55
|
+
this.logger.debug(`Creating directory ${dirPath} in container ${containerId} (recursive: ${recursive})`);
|
|
56
|
+
|
|
57
|
+
const mkdirArgs = recursive ? ['-p', dirPath] : [dirPath];
|
|
58
|
+
|
|
59
|
+
const result = await this.sandboxCommand.execute({
|
|
60
|
+
containerId,
|
|
61
|
+
executable: 'mkdir',
|
|
62
|
+
args: mkdirArgs,
|
|
63
|
+
workingDirectory: '/',
|
|
64
|
+
timeout: 10000,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (!result.data) {
|
|
68
|
+
this.logger.error(`Failed to create directory ${dirPath}: No result data`);
|
|
69
|
+
throw new Error(`Failed to create directory ${dirPath}: No result data`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Exit code 0 means success, exit code 1 with "File exists" is okay if directory already exists
|
|
73
|
+
const alreadyExists = result.data.exitCode !== 0 && result.data.stderr.includes('File exists');
|
|
74
|
+
|
|
75
|
+
if (result.data.exitCode !== 0 && !alreadyExists) {
|
|
76
|
+
this.logger.error(`Failed to create directory ${dirPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
77
|
+
throw new Error(`Failed to create directory ${dirPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (alreadyExists) {
|
|
81
|
+
this.logger.debug(`Directory ${dirPath} already exists`);
|
|
82
|
+
} else {
|
|
83
|
+
this.logger.log(`Successfully created directory ${dirPath} in container ${containerId}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
data: {
|
|
88
|
+
path: dirPath,
|
|
89
|
+
created: result.data.exitCode === 0,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 The Loopstack Authors.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Injectable, Logger } from '@nestjs/common';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
import { BlockConfig, Tool, ToolResult, WithArguments } from '@loopstack/common';
|
|
19
|
+
import { ToolBase, WorkflowExecution } from '@loopstack/core';
|
|
20
|
+
import { SandboxCommand } from '@loopstack/sandbox-tool';
|
|
21
|
+
|
|
22
|
+
const propertiesSchema = z
|
|
23
|
+
.object({
|
|
24
|
+
containerId: z.string().describe('The ID of the container to delete the file/directory from'),
|
|
25
|
+
path: z.string().describe('The path to the file or directory to delete'),
|
|
26
|
+
recursive: z.boolean().default(false).describe('Whether to recursively delete directories and their contents'),
|
|
27
|
+
force: z.boolean().default(false).describe('Whether to force deletion without prompting for confirmation'),
|
|
28
|
+
})
|
|
29
|
+
.strict();
|
|
30
|
+
|
|
31
|
+
type SandboxDeleteArgs = z.infer<typeof propertiesSchema>;
|
|
32
|
+
|
|
33
|
+
interface SandboxDeleteResult {
|
|
34
|
+
path: string;
|
|
35
|
+
deleted: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Injectable()
|
|
39
|
+
@BlockConfig({
|
|
40
|
+
config: {
|
|
41
|
+
description: 'Delete a file or directory in a sandbox container',
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
@WithArguments(propertiesSchema)
|
|
45
|
+
export class SandboxDelete extends ToolBase<SandboxDeleteArgs> {
|
|
46
|
+
private readonly logger = new Logger(SandboxDelete.name);
|
|
47
|
+
|
|
48
|
+
@Tool() private sandboxCommand: SandboxCommand;
|
|
49
|
+
|
|
50
|
+
async execute(args: SandboxDeleteArgs, _ctx: WorkflowExecution): Promise<ToolResult<SandboxDeleteResult>> {
|
|
51
|
+
const { containerId, path: targetPath, recursive, force } = args;
|
|
52
|
+
|
|
53
|
+
this.logger.debug(`Deleting ${targetPath} in container ${containerId} (recursive: ${recursive}, force: ${force})`);
|
|
54
|
+
|
|
55
|
+
const rmArgs: string[] = [];
|
|
56
|
+
if (recursive) rmArgs.push('-r');
|
|
57
|
+
if (force) rmArgs.push('-f');
|
|
58
|
+
rmArgs.push(targetPath);
|
|
59
|
+
|
|
60
|
+
const result = await this.sandboxCommand.execute({
|
|
61
|
+
containerId,
|
|
62
|
+
executable: 'rm',
|
|
63
|
+
args: rmArgs,
|
|
64
|
+
workingDirectory: '/',
|
|
65
|
+
timeout: 30000,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (!result.data) {
|
|
69
|
+
this.logger.error(`Failed to delete ${targetPath}: No result data`);
|
|
70
|
+
throw new Error(`Failed to delete ${targetPath}: No result data`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (result.data.exitCode !== 0) {
|
|
74
|
+
this.logger.error(`Failed to delete ${targetPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
75
|
+
throw new Error(`Failed to delete ${targetPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.logger.log(`Successfully deleted ${targetPath} in container ${containerId}`);
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
data: {
|
|
82
|
+
path: targetPath,
|
|
83
|
+
deleted: true,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 The Loopstack Authors.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Injectable, Logger } from '@nestjs/common';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
import { BlockConfig, Tool, ToolResult, WithArguments } from '@loopstack/common';
|
|
19
|
+
import { ToolBase, WorkflowExecution } from '@loopstack/core';
|
|
20
|
+
import { SandboxCommand } from '@loopstack/sandbox-tool';
|
|
21
|
+
|
|
22
|
+
const propertiesSchema = z
|
|
23
|
+
.object({
|
|
24
|
+
containerId: z.string().describe('The ID of the container to check for file existence'),
|
|
25
|
+
path: z.string().describe('The path to check for existence'),
|
|
26
|
+
})
|
|
27
|
+
.strict();
|
|
28
|
+
|
|
29
|
+
type SandboxExistsArgs = z.infer<typeof propertiesSchema>;
|
|
30
|
+
|
|
31
|
+
interface SandboxExistsResult {
|
|
32
|
+
path: string;
|
|
33
|
+
exists: boolean;
|
|
34
|
+
type: 'file' | 'directory' | 'symlink' | 'other' | null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Injectable()
|
|
38
|
+
@BlockConfig({
|
|
39
|
+
config: {
|
|
40
|
+
description: 'Check if a file or directory exists in a sandbox container',
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
@WithArguments(propertiesSchema)
|
|
44
|
+
export class SandboxExists extends ToolBase<SandboxExistsArgs> {
|
|
45
|
+
private readonly logger = new Logger(SandboxExists.name);
|
|
46
|
+
|
|
47
|
+
@Tool() private sandboxCommand: SandboxCommand;
|
|
48
|
+
|
|
49
|
+
async execute(args: SandboxExistsArgs, _ctx: WorkflowExecution): Promise<ToolResult<SandboxExistsResult>> {
|
|
50
|
+
const { containerId, path: targetPath } = args;
|
|
51
|
+
|
|
52
|
+
this.logger.debug(`Checking existence of ${targetPath} in container ${containerId}`);
|
|
53
|
+
|
|
54
|
+
// Use test command to check existence and stat to get type
|
|
55
|
+
const result = await this.sandboxCommand.execute({
|
|
56
|
+
containerId,
|
|
57
|
+
executable: 'sh',
|
|
58
|
+
args: [
|
|
59
|
+
'-c',
|
|
60
|
+
`if [ -e '${targetPath.replace(/'/g, "'\\''")}' ]; then stat -c '%F' '${targetPath.replace(/'/g, "'\\''")}'; else echo 'NOT_FOUND'; fi`,
|
|
61
|
+
],
|
|
62
|
+
workingDirectory: '/',
|
|
63
|
+
timeout: 10000,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
if (!result.data) {
|
|
67
|
+
this.logger.error(`Failed to check existence of ${targetPath}: No result data`);
|
|
68
|
+
throw new Error(`Failed to check existence of ${targetPath}: No result data`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (result.data.exitCode !== 0) {
|
|
72
|
+
this.logger.error(`Failed to check existence of ${targetPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
73
|
+
throw new Error(`Failed to check existence of ${targetPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const output = result.data.stdout.trim();
|
|
77
|
+
const exists = output !== 'NOT_FOUND';
|
|
78
|
+
|
|
79
|
+
let type: SandboxExistsResult['type'] = null;
|
|
80
|
+
if (exists) {
|
|
81
|
+
type = this.parseFileType(output);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this.logger.debug(`Path ${targetPath} exists: ${exists}${exists ? `, type: ${type}` : ''}`);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
data: {
|
|
88
|
+
path: targetPath,
|
|
89
|
+
exists,
|
|
90
|
+
type,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private parseFileType(statOutput: string): SandboxExistsResult['type'] {
|
|
96
|
+
const lower = statOutput.toLowerCase();
|
|
97
|
+
if (lower.includes('regular')) return 'file';
|
|
98
|
+
if (lower.includes('directory')) return 'directory';
|
|
99
|
+
if (lower.includes('symbolic link')) return 'symlink';
|
|
100
|
+
return 'other';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 The Loopstack Authors.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Injectable, Logger } from '@nestjs/common';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
import { BlockConfig, Tool, ToolResult, WithArguments } from '@loopstack/common';
|
|
19
|
+
import { ToolBase, WorkflowExecution } from '@loopstack/core';
|
|
20
|
+
import { SandboxCommand } from '@loopstack/sandbox-tool';
|
|
21
|
+
|
|
22
|
+
const propertiesSchema = z
|
|
23
|
+
.object({
|
|
24
|
+
containerId: z.string().describe('The ID of the container to get file info from'),
|
|
25
|
+
path: z.string().describe('The path to the file or directory'),
|
|
26
|
+
})
|
|
27
|
+
.strict();
|
|
28
|
+
|
|
29
|
+
type SandboxFileInfoArgs = z.infer<typeof propertiesSchema>;
|
|
30
|
+
|
|
31
|
+
interface SandboxFileInfoResult {
|
|
32
|
+
path: string;
|
|
33
|
+
name: string;
|
|
34
|
+
type: 'file' | 'directory' | 'symlink' | 'other';
|
|
35
|
+
size: number;
|
|
36
|
+
permissions: string;
|
|
37
|
+
owner: string;
|
|
38
|
+
group: string;
|
|
39
|
+
modifiedAt: string;
|
|
40
|
+
accessedAt: string;
|
|
41
|
+
createdAt: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@Injectable()
|
|
45
|
+
@BlockConfig({
|
|
46
|
+
config: {
|
|
47
|
+
description: 'Get detailed information about a file or directory in a sandbox container',
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
@WithArguments(propertiesSchema)
|
|
51
|
+
export class SandboxFileInfo extends ToolBase<SandboxFileInfoArgs> {
|
|
52
|
+
private readonly logger = new Logger(SandboxFileInfo.name);
|
|
53
|
+
|
|
54
|
+
@Tool() private sandboxCommand: SandboxCommand;
|
|
55
|
+
|
|
56
|
+
async execute(args: SandboxFileInfoArgs, _ctx: WorkflowExecution): Promise<ToolResult<SandboxFileInfoResult>> {
|
|
57
|
+
const { containerId, path: targetPath } = args;
|
|
58
|
+
|
|
59
|
+
this.logger.debug(`Getting file info for ${targetPath} in container ${containerId}`);
|
|
60
|
+
|
|
61
|
+
// Use stat to get detailed file information
|
|
62
|
+
// Format: type|size|permissions|owner|group|mtime|atime|ctime
|
|
63
|
+
const result = await this.sandboxCommand.execute({
|
|
64
|
+
containerId,
|
|
65
|
+
executable: 'stat',
|
|
66
|
+
args: ['-c', '%F|%s|%A|%U|%G|%y|%x|%w', targetPath],
|
|
67
|
+
workingDirectory: '/',
|
|
68
|
+
timeout: 10000,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (!result.data) {
|
|
72
|
+
this.logger.error(`Failed to get file info for ${targetPath}: No result data`);
|
|
73
|
+
throw new Error(`Failed to get file info for ${targetPath}: No result data`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (result.data.exitCode !== 0) {
|
|
77
|
+
this.logger.error(`Failed to get file info for ${targetPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
78
|
+
throw new Error(`Failed to get file info for ${targetPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const output = result.data.stdout.trim();
|
|
82
|
+
const parts = output.split('|');
|
|
83
|
+
|
|
84
|
+
if (parts.length < 8) {
|
|
85
|
+
this.logger.error(`Unexpected stat output format: ${output}`);
|
|
86
|
+
throw new Error(`Unexpected stat output format: ${output}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const [typeStr, sizeStr, permissions, owner, group, mtime, atime, ctime] = parts;
|
|
90
|
+
|
|
91
|
+
const name = targetPath.split('/').pop() || targetPath;
|
|
92
|
+
const fileType = this.parseFileType(typeStr);
|
|
93
|
+
|
|
94
|
+
this.logger.debug(`Retrieved info for ${targetPath}: type=${fileType}, size=${sizeStr}`);
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
data: {
|
|
98
|
+
path: targetPath,
|
|
99
|
+
name,
|
|
100
|
+
type: fileType,
|
|
101
|
+
size: parseInt(sizeStr, 10),
|
|
102
|
+
permissions,
|
|
103
|
+
owner,
|
|
104
|
+
group,
|
|
105
|
+
modifiedAt: mtime,
|
|
106
|
+
accessedAt: atime,
|
|
107
|
+
createdAt: ctime === '-' ? mtime : ctime,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private parseFileType(typeStr: string): SandboxFileInfoResult['type'] {
|
|
113
|
+
const lower = typeStr.toLowerCase();
|
|
114
|
+
if (lower.includes('regular')) return 'file';
|
|
115
|
+
if (lower.includes('directory')) return 'directory';
|
|
116
|
+
if (lower.includes('symbolic link')) return 'symlink';
|
|
117
|
+
return 'other';
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 The Loopstack Authors.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Injectable, Logger } from '@nestjs/common';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
import { BlockConfig, Tool, ToolResult, WithArguments } from '@loopstack/common';
|
|
19
|
+
import { ToolBase, WorkflowExecution } from '@loopstack/core';
|
|
20
|
+
import { SandboxCommand } from '@loopstack/sandbox-tool';
|
|
21
|
+
|
|
22
|
+
const propertiesSchema = z
|
|
23
|
+
.object({
|
|
24
|
+
containerId: z.string().describe('The ID of the container to list the directory from'),
|
|
25
|
+
path: z.string().describe('The path to the directory to list'),
|
|
26
|
+
recursive: z.boolean().default(false).describe('Whether to list directories recursively'),
|
|
27
|
+
})
|
|
28
|
+
.strict();
|
|
29
|
+
|
|
30
|
+
type SandboxListDirectoryArgs = z.infer<typeof propertiesSchema>;
|
|
31
|
+
|
|
32
|
+
interface FileEntry {
|
|
33
|
+
name: string;
|
|
34
|
+
type: 'file' | 'directory' | 'symlink' | 'other';
|
|
35
|
+
size: number;
|
|
36
|
+
path: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface SandboxListDirectoryResult {
|
|
40
|
+
path: string;
|
|
41
|
+
entries: FileEntry[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@Injectable()
|
|
45
|
+
@BlockConfig({
|
|
46
|
+
config: {
|
|
47
|
+
description: 'List files and directories in a sandbox container',
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
@WithArguments(propertiesSchema)
|
|
51
|
+
export class SandboxListDirectory extends ToolBase<SandboxListDirectoryArgs> {
|
|
52
|
+
private readonly logger = new Logger(SandboxListDirectory.name);
|
|
53
|
+
|
|
54
|
+
@Tool() private sandboxCommand: SandboxCommand;
|
|
55
|
+
|
|
56
|
+
async execute(
|
|
57
|
+
args: SandboxListDirectoryArgs,
|
|
58
|
+
_ctx: WorkflowExecution,
|
|
59
|
+
): Promise<ToolResult<SandboxListDirectoryResult>> {
|
|
60
|
+
const { containerId, path: dirPath, recursive } = args;
|
|
61
|
+
|
|
62
|
+
this.logger.debug(`Listing directory ${dirPath} in container ${containerId} (recursive: ${recursive})`);
|
|
63
|
+
|
|
64
|
+
// Use find for recursive, ls for non-recursive
|
|
65
|
+
// Output format: type size path
|
|
66
|
+
const command = recursive
|
|
67
|
+
? `find '${dirPath.replace(/'/g, "'\\''")}' -printf '%y %s %p\\n'`
|
|
68
|
+
: `find '${dirPath.replace(/'/g, "'\\''")}' -maxdepth 1 -printf '%y %s %p\\n'`;
|
|
69
|
+
|
|
70
|
+
const result = await this.sandboxCommand.execute({
|
|
71
|
+
containerId,
|
|
72
|
+
executable: 'sh',
|
|
73
|
+
args: ['-c', command],
|
|
74
|
+
workingDirectory: '/',
|
|
75
|
+
timeout: 30000,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (!result.data) {
|
|
79
|
+
this.logger.error(`Failed to list directory ${dirPath}: No result data`);
|
|
80
|
+
throw new Error(`Failed to list directory ${dirPath}: No result data`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (result.data.exitCode !== 0) {
|
|
84
|
+
this.logger.error(`Failed to list directory ${dirPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
85
|
+
throw new Error(`Failed to list directory ${dirPath}: ${result.data.stderr || 'Unknown error'}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const entries: FileEntry[] = [];
|
|
89
|
+
const lines = result.data.stdout.split('\n').filter((line) => line.trim());
|
|
90
|
+
|
|
91
|
+
for (const line of lines) {
|
|
92
|
+
const match = line.match(/^(\S)\s+(\d+)\s+(.+)$/);
|
|
93
|
+
if (match) {
|
|
94
|
+
const [, typeChar, sizeStr, fullPath] = match;
|
|
95
|
+
const name = fullPath.split('/').pop() || fullPath;
|
|
96
|
+
|
|
97
|
+
// Skip the directory itself in non-recursive mode
|
|
98
|
+
if (fullPath === dirPath) continue;
|
|
99
|
+
|
|
100
|
+
entries.push({
|
|
101
|
+
name,
|
|
102
|
+
type: this.parseFileType(typeChar),
|
|
103
|
+
size: parseInt(sizeStr, 10),
|
|
104
|
+
path: fullPath,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this.logger.debug(`Listed ${entries.length} entries in ${dirPath}`);
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
data: {
|
|
113
|
+
path: dirPath,
|
|
114
|
+
entries,
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private parseFileType(typeChar: string): FileEntry['type'] {
|
|
120
|
+
switch (typeChar) {
|
|
121
|
+
case 'f':
|
|
122
|
+
return 'file';
|
|
123
|
+
case 'd':
|
|
124
|
+
return 'directory';
|
|
125
|
+
case 'l':
|
|
126
|
+
return 'symlink';
|
|
127
|
+
default:
|
|
128
|
+
return 'other';
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 The Loopstack Authors.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { Injectable, Logger } from '@nestjs/common';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
import { BlockConfig, Tool, ToolResult, WithArguments } from '@loopstack/common';
|
|
19
|
+
import { ToolBase, WorkflowExecution } from '@loopstack/core';
|
|
20
|
+
import { SandboxCommand } from '@loopstack/sandbox-tool';
|
|
21
|
+
|
|
22
|
+
const propertiesSchema = z
|
|
23
|
+
.object({
|
|
24
|
+
containerId: z.string().describe('The ID of the container to read the file from'),
|
|
25
|
+
path: z.string().describe('The path to the file to read'),
|
|
26
|
+
encoding: z.enum(['utf8', 'base64']).default('utf8').describe('The encoding to use when reading the file'),
|
|
27
|
+
})
|
|
28
|
+
.strict();
|
|
29
|
+
|
|
30
|
+
type SandboxReadFileArgs = z.infer<typeof propertiesSchema>;
|
|
31
|
+
|
|
32
|
+
interface SandboxReadFileResult {
|
|
33
|
+
content: string;
|
|
34
|
+
encoding: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Injectable()
|
|
38
|
+
@BlockConfig({
|
|
39
|
+
config: {
|
|
40
|
+
description: 'Read file contents from a sandbox container',
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
@WithArguments(propertiesSchema)
|
|
44
|
+
export class SandboxReadFile extends ToolBase<SandboxReadFileArgs> {
|
|
45
|
+
private readonly logger = new Logger(SandboxReadFile.name);
|
|
46
|
+
|
|
47
|
+
@Tool() private sandboxCommand: SandboxCommand;
|
|
48
|
+
|
|
49
|
+
async execute(args: SandboxReadFileArgs, _ctx: WorkflowExecution): Promise<ToolResult<SandboxReadFileResult>> {
|
|
50
|
+
const { containerId, path, encoding } = args;
|
|
51
|
+
|
|
52
|
+
this.logger.debug(`Reading file ${path} from container ${containerId} (encoding: ${encoding})`);
|
|
53
|
+
|
|
54
|
+
const executable = encoding === 'base64' ? 'base64' : 'cat';
|
|
55
|
+
const result = await this.sandboxCommand.execute({
|
|
56
|
+
containerId,
|
|
57
|
+
executable,
|
|
58
|
+
args: [path],
|
|
59
|
+
workingDirectory: '/',
|
|
60
|
+
timeout: 30000,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (!result.data) {
|
|
64
|
+
this.logger.error(`Failed to read file ${path}: No result data`);
|
|
65
|
+
throw new Error(`Failed to read file ${path}: No result data`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (result.data.exitCode !== 0) {
|
|
69
|
+
this.logger.error(`Failed to read file ${path}: ${result.data.stderr || 'Unknown error'}`);
|
|
70
|
+
throw new Error(`Failed to read file ${path}: ${result.data.stderr || 'Unknown error'}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
this.logger.debug(`Successfully read file ${path} (${result.data.stdout.length} characters)`);
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
data: {
|
|
77
|
+
content: result.data.stdout,
|
|
78
|
+
encoding,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|