Mona commited on
Commit
9f8b551
·
1 Parent(s): fe11cca

feat: add file operations tool with workspace sandboxing

Browse files

File system operations confined to a workspace directory:
- read: load file content (50KB cap, truncation flag)
- write: create/overwrite with recursive directory creation
- list: enumerate directory entries with type info
- delete: recursive removal with force
- stat: file metadata (size, type, timestamps)

Path traversal prevention via absolute path resolution and
prefix check. Workspace defaults to ~/.mona-agent/workspace,
configurable via MONA_WORKSPACE env var.

Auto-creates workspace on first use.

Files changed (1) hide show
  1. src/tools/files.js +82 -0
src/tools/files.js ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // File system tools — sandboxed to a workspace directory.
2
+ // Default workspace: $MONA_WORKSPACE or ~/.mona-agent/workspace
3
+
4
+ import fs from 'node:fs/promises';
5
+ import path from 'node:path';
6
+ import { homedir } from 'node:os';
7
+
8
+ const WORKSPACE = process.env.MONA_WORKSPACE || path.join(homedir(), '.mona-agent', 'workspace');
9
+
10
+ function safePath(p) {
11
+ const resolved = path.resolve(WORKSPACE, p);
12
+ if (!resolved.startsWith(path.resolve(WORKSPACE))) {
13
+ throw new Error(`Path traversal denied: ${p}`);
14
+ }
15
+ return resolved;
16
+ }
17
+
18
+ async function ensureWorkspace() {
19
+ await fs.mkdir(WORKSPACE, { recursive: true });
20
+ }
21
+
22
+ export const files = {
23
+ name: 'files',
24
+ description: 'File system operations within the agent workspace (read, write, list, delete)',
25
+ args: {
26
+ action: 'string — read | write | list | delete | stat',
27
+ path: 'string — relative path within workspace',
28
+ content:'string — file content (for write)',
29
+ },
30
+
31
+ async run(args) {
32
+ await ensureWorkspace();
33
+ const action = String(args.action || 'list').toLowerCase();
34
+
35
+ switch (action) {
36
+ case 'read': {
37
+ if (!args.path) return { error: 'path required' };
38
+ const content = await fs.readFile(safePath(args.path), 'utf8');
39
+ return { path: args.path, content: content.slice(0, 50_000), truncated: content.length > 50_000 };
40
+ }
41
+
42
+ case 'write': {
43
+ if (!args.path) return { error: 'path required' };
44
+ if (args.content == null) return { error: 'content required' };
45
+ const fp = safePath(args.path);
46
+ await fs.mkdir(path.dirname(fp), { recursive: true });
47
+ await fs.writeFile(fp, args.content, 'utf8');
48
+ return { ok: true, path: args.path, bytes: Buffer.byteLength(args.content) };
49
+ }
50
+
51
+ case 'list': {
52
+ const dir = args.path ? safePath(args.path) : WORKSPACE;
53
+ const entries = await fs.readdir(dir, { withFileTypes: true });
54
+ return entries.map(e => ({
55
+ name: e.name,
56
+ type: e.isDirectory() ? 'dir' : 'file',
57
+ }));
58
+ }
59
+
60
+ case 'delete': {
61
+ if (!args.path) return { error: 'path required' };
62
+ await fs.rm(safePath(args.path), { recursive: true, force: true });
63
+ return { ok: true, path: args.path };
64
+ }
65
+
66
+ case 'stat': {
67
+ if (!args.path) return { error: 'path required' };
68
+ const st = await fs.stat(safePath(args.path));
69
+ return {
70
+ path: args.path,
71
+ size: st.size,
72
+ isDir: st.isDirectory(),
73
+ modified: st.mtime.toISOString(),
74
+ created: st.birthtime.toISOString(),
75
+ };
76
+ }
77
+
78
+ default:
79
+ return { error: `Unknown file action: ${action}`, available: ['read', 'write', 'list', 'delete', 'stat'] };
80
+ }
81
+ },
82
+ };