@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,121 @@
|
|
|
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 * as path from 'path';
|
|
18
|
+
import { z } from 'zod';
|
|
19
|
+
import { BlockConfig, Tool, ToolResult, WithArguments } from '@loopstack/common';
|
|
20
|
+
import { ToolBase, WorkflowExecution } from '@loopstack/core';
|
|
21
|
+
import { SandboxCommand } from '@loopstack/sandbox-tool';
|
|
22
|
+
|
|
23
|
+
const propertiesSchema = z
|
|
24
|
+
.object({
|
|
25
|
+
containerId: z.string().describe('The ID of the container to write the file to'),
|
|
26
|
+
path: z.string().describe('The path where the file should be written'),
|
|
27
|
+
content: z.string().describe('The content to write to the file'),
|
|
28
|
+
encoding: z.enum(['utf8', 'base64']).default('utf8').describe('The encoding of the content'),
|
|
29
|
+
createParentDirs: z.boolean().default(true).describe("Whether to create parent directories if they don't exist"),
|
|
30
|
+
})
|
|
31
|
+
.strict();
|
|
32
|
+
|
|
33
|
+
type SandboxWriteFileArgs = z.infer<typeof propertiesSchema>;
|
|
34
|
+
|
|
35
|
+
interface SandboxWriteFileResult {
|
|
36
|
+
path: string;
|
|
37
|
+
bytesWritten: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@Injectable()
|
|
41
|
+
@BlockConfig({
|
|
42
|
+
config: {
|
|
43
|
+
description: 'Write content to a file in a sandbox container',
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
@WithArguments(propertiesSchema)
|
|
47
|
+
export class SandboxWriteFile extends ToolBase<SandboxWriteFileArgs> {
|
|
48
|
+
private readonly logger = new Logger(SandboxWriteFile.name);
|
|
49
|
+
|
|
50
|
+
@Tool() private sandboxCommand: SandboxCommand;
|
|
51
|
+
|
|
52
|
+
async execute(args: SandboxWriteFileArgs, _ctx: WorkflowExecution): Promise<ToolResult<SandboxWriteFileResult>> {
|
|
53
|
+
const { containerId, path: filePath, content, encoding, createParentDirs } = args;
|
|
54
|
+
|
|
55
|
+
this.logger.debug(`Writing file ${filePath} to container ${containerId} (encoding: ${encoding})`);
|
|
56
|
+
|
|
57
|
+
// Create parent directories if needed
|
|
58
|
+
if (createParentDirs) {
|
|
59
|
+
const parentDir = path.posix.dirname(filePath);
|
|
60
|
+
if (parentDir !== '/' && parentDir !== '.') {
|
|
61
|
+
this.logger.debug(`Creating parent directory ${parentDir}`);
|
|
62
|
+
const mkdirResult = await this.sandboxCommand.execute({
|
|
63
|
+
containerId,
|
|
64
|
+
executable: 'mkdir',
|
|
65
|
+
args: ['-p', parentDir],
|
|
66
|
+
workingDirectory: '/',
|
|
67
|
+
timeout: 5000,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (!mkdirResult.data) {
|
|
71
|
+
this.logger.error(`Failed to create parent directory ${parentDir}: No result data`);
|
|
72
|
+
throw new Error(`Failed to create parent directory ${parentDir}: No result data`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (mkdirResult.data.exitCode !== 0) {
|
|
76
|
+
this.logger.error(
|
|
77
|
+
`Failed to create parent directory ${parentDir}: ${mkdirResult.data.stderr || 'Unknown error'}`,
|
|
78
|
+
);
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Failed to create parent directory ${parentDir}: ${mkdirResult.data.stderr || 'Unknown error'}`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Encode content as base64 for safe transfer
|
|
87
|
+
const base64Content =
|
|
88
|
+
encoding === 'utf8' ? Buffer.from(content, 'utf8').toString('base64') : content.replace(/[^A-Za-z0-9+/=]/g, '');
|
|
89
|
+
|
|
90
|
+
// Write file using base64 decode
|
|
91
|
+
const result = await this.sandboxCommand.execute({
|
|
92
|
+
containerId,
|
|
93
|
+
executable: 'sh',
|
|
94
|
+
args: ['-c', `echo '${base64Content}' | base64 -d > '${filePath.replace(/'/g, "'\\''")}'`],
|
|
95
|
+
workingDirectory: '/',
|
|
96
|
+
timeout: 30000,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (!result.data) {
|
|
100
|
+
this.logger.error(`Failed to write file ${filePath}: No result data`);
|
|
101
|
+
throw new Error(`Failed to write file ${filePath}: No result data`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (result.data.exitCode !== 0) {
|
|
105
|
+
this.logger.error(`Failed to write file ${filePath}: ${result.data.stderr || 'Unknown error'}`);
|
|
106
|
+
throw new Error(`Failed to write file ${filePath}: ${result.data.stderr || 'Unknown error'}`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const bytesWritten =
|
|
110
|
+
encoding === 'utf8' ? Buffer.from(content, 'utf8').length : Buffer.from(content, 'base64').length;
|
|
111
|
+
|
|
112
|
+
this.logger.log(`Successfully wrote ${bytesWritten} bytes to ${filePath} in container ${containerId}`);
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
data: {
|
|
116
|
+
path: filePath,
|
|
117
|
+
bytesWritten,
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|