@memoryblock/tools 0.1.0-beta

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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/dist/base.d.ts +17 -0
  3. package/dist/base.d.ts.map +1 -0
  4. package/dist/base.js +12 -0
  5. package/dist/base.js.map +1 -0
  6. package/dist/core/channels.d.ts +9 -0
  7. package/dist/core/channels.d.ts.map +1 -0
  8. package/dist/core/channels.js +39 -0
  9. package/dist/core/channels.js.map +1 -0
  10. package/dist/core/identity.d.ts +5 -0
  11. package/dist/core/identity.d.ts.map +1 -0
  12. package/dist/core/identity.js +108 -0
  13. package/dist/core/identity.js.map +1 -0
  14. package/dist/dev/index.d.ts +7 -0
  15. package/dist/dev/index.d.ts.map +1 -0
  16. package/dist/dev/index.js +113 -0
  17. package/dist/dev/index.js.map +1 -0
  18. package/dist/fs/index.d.ts +11 -0
  19. package/dist/fs/index.d.ts.map +1 -0
  20. package/dist/fs/index.js +240 -0
  21. package/dist/fs/index.js.map +1 -0
  22. package/dist/index.d.ts +14 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +32 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/registry.d.ts +33 -0
  27. package/dist/registry.d.ts.map +1 -0
  28. package/dist/registry.js +96 -0
  29. package/dist/registry.js.map +1 -0
  30. package/dist/sandbox.d.ts +33 -0
  31. package/dist/sandbox.d.ts.map +1 -0
  32. package/dist/sandbox.js +136 -0
  33. package/dist/sandbox.js.map +1 -0
  34. package/dist/shell/index.d.ts +8 -0
  35. package/dist/shell/index.d.ts.map +1 -0
  36. package/dist/shell/index.js +85 -0
  37. package/dist/shell/index.js.map +1 -0
  38. package/package.json +17 -0
  39. package/src/base.ts +24 -0
  40. package/src/core/channels.ts +47 -0
  41. package/src/core/identity.ts +125 -0
  42. package/src/dev/index.ts +119 -0
  43. package/src/fs/index.ts +272 -0
  44. package/src/index.ts +31 -0
  45. package/src/registry.ts +108 -0
  46. package/src/sandbox.ts +169 -0
  47. package/src/shell/index.ts +96 -0
  48. package/tsconfig.json +10 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 memoryblock
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/base.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { ToolDefinition, ToolContext, ToolExecutionResult } from 'memoryblock';
2
+ /**
3
+ * Base Tool interface. All built-in and plugin tools must implement this.
4
+ */
5
+ export interface Tool {
6
+ readonly definition: ToolDefinition;
7
+ execute(params: Record<string, unknown>, context: ToolContext): Promise<ToolExecutionResult>;
8
+ }
9
+ /**
10
+ * Helper to create a JSON Schema object for tool parameters.
11
+ */
12
+ export declare function createSchema(properties: Record<string, {
13
+ type: string;
14
+ description: string;
15
+ enum?: string[];
16
+ }>, required?: string[]): Record<string, unknown>;
17
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEpF;;GAEG;AACH,MAAM,WAAW,IAAI;IACjB,QAAQ,CAAC,UAAU,EAAE,cAAc,CAAC;IACpC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;CAChG;AAED;;GAEG;AACH,wBAAgB,YAAY,CACxB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,EAClF,QAAQ,GAAE,MAAM,EAAO,GACxB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAOzB"}
package/dist/base.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Helper to create a JSON Schema object for tool parameters.
3
+ */
4
+ export function createSchema(properties, required = []) {
5
+ return {
6
+ type: 'object',
7
+ properties,
8
+ required,
9
+ additionalProperties: false,
10
+ };
11
+ }
12
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAUA;;GAEG;AACH,MAAM,UAAU,YAAY,CACxB,UAAkF,EAClF,WAAqB,EAAE;IAEvB,OAAO;QACH,IAAI,EAAE,QAAQ;QACd,UAAU;QACV,QAAQ;QACR,oBAAoB,EAAE,KAAK;KAC9B,CAAC;AACN,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { Tool } from '../base.js';
2
+ /**
3
+ * send_channel_message tool allows the monitor to proactively dispatch
4
+ * a message to a specific bound channel (e.g. Telegram or CLI), rather
5
+ * than just passively replying to the channel that initiated the turn.
6
+ */
7
+ export declare const dispatchMessageTool: Tool;
8
+ export declare const channelTools: Tool[];
9
+ //# sourceMappingURL=channels.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channels.d.ts","sourceRoot":"","sources":["../../src/core/channels.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAGvC;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,EAAE,IAmCjC,CAAC;AAEF,eAAO,MAAM,YAAY,QAAwB,CAAC"}
@@ -0,0 +1,39 @@
1
+ import { createSchema } from '../base.js';
2
+ /**
3
+ * send_channel_message tool allows the monitor to proactively dispatch
4
+ * a message to a specific bound channel (e.g. Telegram or CLI), rather
5
+ * than just passively replying to the channel that initiated the turn.
6
+ */
7
+ export const dispatchMessageTool = {
8
+ definition: {
9
+ name: 'send_channel_message',
10
+ description: 'Send a message proactively to a specific active channel (e.g., "telegram" or "cli"). Use this if the founder asks you to send them a message somewhere else.',
11
+ parameters: createSchema({
12
+ channel: { type: 'string', description: 'The exact name of the target channel (e.g., "telegram", "cli").' },
13
+ content: { type: 'string', description: 'The message content to send.' },
14
+ }, ['channel', 'content']),
15
+ requiresApproval: false,
16
+ },
17
+ async execute(params, context) {
18
+ const target = params.channel;
19
+ const content = params.content;
20
+ if (!context.dispatchMessage) {
21
+ return { content: 'Message dispatching is not supported in the current execution context.', isError: true };
22
+ }
23
+ try {
24
+ await context.dispatchMessage(target, content);
25
+ return {
26
+ content: `Message successfully dispatched proactively to channel: ${target}`,
27
+ isError: false,
28
+ };
29
+ }
30
+ catch (err) {
31
+ return {
32
+ content: `Failed to dispatch message to channel '${target}': ${err.message}`,
33
+ isError: true,
34
+ };
35
+ }
36
+ },
37
+ };
38
+ export const channelTools = [dispatchMessageTool];
39
+ //# sourceMappingURL=channels.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channels.js","sourceRoot":"","sources":["../../src/core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Tool: Tool;
3
+ export declare const updateFounderInfoTool: Tool;
4
+ export declare const identityTools: Tool[];
5
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../src/core/identity.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAWvC,eAAO,MAAM,yBAAyB,EAAE,IAqDvC,CAAC;AAGF,eAAO,MAAM,qBAAqB,EAAE,IAiDnC,CAAC;AAEF,eAAO,MAAM,aAAa,EAAE,IAAI,EAG/B,CAAC"}
@@ -0,0 +1,108 @@
1
+ import { promises as fsp } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { createSchema } from '../base.js';
4
+ // Resolve workspace root natively without pulling in core directly to avoid cycles
5
+ import { homedir } from 'node:os';
6
+ function getWsRoot() {
7
+ const custom = process.env.MEMORYBLOCK_WS_DIR;
8
+ return custom ? custom : join(homedir(), '.memoryblock', 'ws');
9
+ }
10
+ // ===== update_monitor_identity =====
11
+ export const updateMonitorIdentityTool = {
12
+ definition: {
13
+ name: 'update_monitor_identity',
14
+ description: 'Update the monitor name and emoji for this block. This changes how you are identified in the system and UI.',
15
+ parameters: createSchema({
16
+ name: { type: 'string', description: 'Your new chosen name (e.g. "Ana", "Nexus").' },
17
+ emoji: { type: 'string', description: 'A single emoji representing your persona (e.g. "🤖", "🦊").' },
18
+ }, ['name', 'emoji']),
19
+ requiresApproval: true,
20
+ },
21
+ async execute(params, context) {
22
+ try {
23
+ // 1. Update config.json
24
+ const configPath = join(context.blockPath, 'config.json');
25
+ const configRaw = await fsp.readFile(configPath, 'utf8');
26
+ const config = JSON.parse(configRaw);
27
+ config.monitorName = params.name;
28
+ config.monitorEmoji = params.emoji;
29
+ await fsp.writeFile(configPath, JSON.stringify(config, null, 2), 'utf8');
30
+ // 2. Update monitor.md to reflect the new identity explicitly
31
+ const monitorPath = join(context.blockPath, 'monitor.md');
32
+ let monitorContent = '';
33
+ try {
34
+ monitorContent = await fsp.readFile(monitorPath, 'utf8');
35
+ }
36
+ catch {
37
+ // file doesn't exist yet, that's fine
38
+ }
39
+ const header = `# Identity\nName: ${params.name}\nEmoji: ${params.emoji}\n\n`;
40
+ // Basic replacement logic if it already has an Identity header
41
+ if (monitorContent.includes('# Identity')) {
42
+ monitorContent = monitorContent.replace(/# Identity[\s\S]*?(?=\n#|$)/, header);
43
+ }
44
+ else {
45
+ monitorContent = header + monitorContent;
46
+ }
47
+ await fsp.writeFile(monitorPath, monitorContent, 'utf8');
48
+ return {
49
+ content: `Successfully updated monitor identity to ${params.emoji} ${params.name}. The system will reflect this change on the next interaction.`,
50
+ isError: false
51
+ };
52
+ }
53
+ catch (err) {
54
+ return { content: `Failed to update monitor identity: ${err.message}`, isError: true };
55
+ }
56
+ },
57
+ };
58
+ // ===== update_founder_info =====
59
+ export const updateFounderInfoTool = {
60
+ definition: {
61
+ name: 'update_founder_info',
62
+ description: 'Update the global founder profile. Use this when the user tells you about themselves (name, work, preferences). This data is globally shared across all your blocks.',
63
+ parameters: createSchema({
64
+ info: { type: 'string', description: 'The new information to append or update about the founder.' },
65
+ mode: { type: 'string', description: 'Either "append" (add new facts) or "rewrite" (completely rewrite the profile).' },
66
+ }, ['info', 'mode']),
67
+ requiresApproval: false,
68
+ },
69
+ async execute(params, _context) {
70
+ try {
71
+ const wsRoot = getWsRoot();
72
+ const founderPath = join(wsRoot, 'founder.md');
73
+ let content = '';
74
+ try {
75
+ content = await fsp.readFile(founderPath, 'utf8');
76
+ }
77
+ catch {
78
+ // file doesn't exist yet, that's fine
79
+ }
80
+ const newInfo = params.info;
81
+ const mode = params.mode;
82
+ if (mode === 'rewrite') {
83
+ content = `# Founder Profile\n\n${newInfo}\n`;
84
+ }
85
+ else {
86
+ // append intelligently
87
+ if (!content.includes('# Founder Profile')) {
88
+ content = `# Founder Profile\n\n`;
89
+ }
90
+ const timestamp = new Date().toISOString().split('T')[0];
91
+ content += `\n- [${timestamp}]: ${newInfo}`;
92
+ }
93
+ await fsp.writeFile(founderPath, content.trim() + '\n', 'utf8');
94
+ return {
95
+ content: `Successfully updated global founder profile at ${founderPath}.`,
96
+ isError: false
97
+ };
98
+ }
99
+ catch (err) {
100
+ return { content: `Failed to update founder profile: ${err.message}`, isError: true };
101
+ }
102
+ },
103
+ };
104
+ export const identityTools = [
105
+ updateMonitorIdentityTool,
106
+ updateFounderInfoTool,
107
+ ];
108
+ //# sourceMappingURL=identity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.js","sourceRoot":"","sources":["../../src/core/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,GAAG,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,mFAAmF;AACnF,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,SAAS,SAAS;IACd,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;IAC9C,OAAO,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,cAAc,EAAE,IAAI,CAAC,CAAC;AACnE,CAAC;AAED,sCAAsC;AACtC,MAAM,CAAC,MAAM,yBAAyB,GAAS;IAC3C,UAAU,EAAE;QACR,IAAI,EAAE,yBAAyB;QAC/B,WAAW,EAAE,6GAA6G;QAC1H,UAAU,EAAE,YAAY,CACpB;YACI,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,6CAA6C,EAAE;YACpF,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,6DAA6D,EAAE;SACxG,EACD,CAAC,MAAM,EAAE,OAAO,CAAC,CACpB;QACD,gBAAgB,EAAE,IAAI;KACzB;IACD,KAAK,CAAC,OAAO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AC;iBAAM,CAAC;gBACJ,uBAAuB;gBACvB,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAC,EAAE,CAAC;oBACzC,OAAO,GAAG,uBAAuB,CAAC;gBACtC,CAAC;gBACD,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;gBACzD,OAAO,IAAI,QAAQ,SAAS,MAAM,OAAO,EAAE,CAAC;YAChD,CAAC;YAED,MAAM,GAAG,CAAC,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,IAAI,EAAE,GAAG,IAAI,EAAE,MAAM,CAAC,CAAC;YAEhE,OAAO;gBACH,OAAO,EAAE,kDAAkD,WAAW,GAAG;gBACzE,OAAO,EAAE,KAAK;aACjB,CAAC;QACN,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,OAAO,EAAE,OAAO,EAAE,qCAAsC,GAAa,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QACrG,CAAC;IACL,CAAC;CACJ,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAW;IACjC,yBAAyB;IACzB,qBAAqB;CACxB,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { Tool } from '../base.js';
2
+ export declare const runLintTool: Tool;
3
+ export declare const runBuildTool: Tool;
4
+ export declare const runTestTool: Tool;
5
+ /** All dev tools. */
6
+ export declare const devTools: Tool[];
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dev/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAoDvC,eAAO,MAAM,WAAW,EAAE,IAezB,CAAC;AAGF,eAAO,MAAM,YAAY,EAAE,IAiB1B,CAAC;AAGF,eAAO,MAAM,WAAW,EAAE,IAoBzB,CAAC;AAEF,qBAAqB;AACrB,eAAO,MAAM,QAAQ,EAAE,IAAI,EAA6C,CAAC"}
@@ -0,0 +1,113 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { promisify } from 'node:util';
3
+ import { promises as fsp } from 'node:fs';
4
+ import { join } from 'node:path';
5
+ import { createSchema } from '../base.js';
6
+ const execFileAsync = promisify(execFile);
7
+ const DEV_TIMEOUT = 120_000; // 2 minutes for builds
8
+ const MAX_OUTPUT = 50_000;
9
+ /** Find the project root by looking for package.json. */
10
+ async function findProjectRoot(startDir) {
11
+ let dir = startDir;
12
+ for (let i = 0; i < 10; i++) {
13
+ try {
14
+ await fsp.access(join(dir, 'package.json'));
15
+ return dir;
16
+ }
17
+ catch {
18
+ const parent = join(dir, '..');
19
+ if (parent === dir)
20
+ break;
21
+ dir = parent;
22
+ }
23
+ }
24
+ return startDir;
25
+ }
26
+ function truncateOutput(output) {
27
+ if (output.length > MAX_OUTPUT) {
28
+ return output.slice(0, MAX_OUTPUT) + `\n...(truncated, ${output.length} total chars)`;
29
+ }
30
+ return output;
31
+ }
32
+ async function runCommand(command, cwd) {
33
+ try {
34
+ const { stdout, stderr } = await execFileAsync('/bin/sh', ['-c', command], {
35
+ cwd,
36
+ timeout: DEV_TIMEOUT,
37
+ maxBuffer: 2 * 1024 * 1024,
38
+ env: { ...process.env, HOME: process.env.HOME, FORCE_COLOR: '0' },
39
+ });
40
+ let output = '';
41
+ if (stdout)
42
+ output += stdout;
43
+ if (stderr)
44
+ output += (output ? '\n--- stderr ---\n' : '') + stderr;
45
+ return { content: truncateOutput(output || '(no output)'), isError: false };
46
+ }
47
+ catch (err) {
48
+ const e = err;
49
+ let msg = e.message;
50
+ if (e.stdout)
51
+ msg += '\n' + e.stdout.slice(0, 10_000);
52
+ if (e.stderr)
53
+ msg += '\n' + e.stderr.slice(0, 10_000);
54
+ return { content: truncateOutput(`Command failed: ${msg}`), isError: true };
55
+ }
56
+ }
57
+ // ===== run_lint =====
58
+ export const runLintTool = {
59
+ definition: {
60
+ name: 'run_lint',
61
+ description: 'Run ESLint.',
62
+ parameters: createSchema({ path: { type: 'string', description: 'Target path.' } }, []),
63
+ requiresApproval: false,
64
+ },
65
+ async execute(params, context) {
66
+ const projectRoot = await findProjectRoot(context.workingDir || context.blockPath);
67
+ const target = params.path || '.';
68
+ return runCommand(`npx eslint ${target} --no-color 2>&1 || true`, projectRoot);
69
+ },
70
+ };
71
+ // ===== run_build =====
72
+ export const runBuildTool = {
73
+ definition: {
74
+ name: 'run_build',
75
+ description: 'Run build command.',
76
+ parameters: createSchema({}, []),
77
+ requiresApproval: false,
78
+ },
79
+ async execute(_params, context) {
80
+ const projectRoot = await findProjectRoot(context.workingDir || context.blockPath);
81
+ // Try pnpm first, fall back to npm
82
+ try {
83
+ await fsp.access(join(projectRoot, 'pnpm-workspace.yaml'));
84
+ return runCommand('pnpm run build 2>&1', projectRoot);
85
+ }
86
+ catch {
87
+ return runCommand('npm run build 2>&1', projectRoot);
88
+ }
89
+ },
90
+ };
91
+ // ===== run_test =====
92
+ export const runTestTool = {
93
+ definition: {
94
+ name: 'run_test',
95
+ description: 'Run tests.',
96
+ parameters: createSchema({ filter: { type: 'string', description: 'Test filter.' } }, []),
97
+ requiresApproval: false,
98
+ },
99
+ async execute(params, context) {
100
+ const projectRoot = await findProjectRoot(context.workingDir || context.blockPath);
101
+ const filter = params.filter || '';
102
+ try {
103
+ await fsp.access(join(projectRoot, 'pnpm-workspace.yaml'));
104
+ return runCommand(`pnpm test ${filter} 2>&1 || true`, projectRoot);
105
+ }
106
+ catch {
107
+ return runCommand(`npm test ${filter} 2>&1 || true`, projectRoot);
108
+ }
109
+ },
110
+ };
111
+ /** All dev tools. */
112
+ export const devTools = [runLintTool, runBuildTool, runTestTool];
113
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/dev/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,M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,EAAE,YAAY;QACzB,UAAU,EAAE,YAAY,CACpB,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,EAAE,EAC3D,EAAE,CACL;QACD,gBAAgB,EAAE,KAAK;KAC1B;IACD,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO;QACzB,MAAM,WAAW,GAAG,MAAM,eAAe,CAAC,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC,SAAS,CAAC,CAAC;QACnF,MAAM,MAAM,GAAI,MAAM,CAAC,MAAiB,IAAI,EAAE,CAAC;QAC/C,IAAI,CAAC;YACD,MAAM,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,qBAAqB,CAAC,CAAC,CAAC;YAC3D,OAAO,UAAU,CAAC,aAAa,MAAM,eAAe,EAAE,WAAW,CAAC,CAAC;QACvE,CAAC;QAAC,MAAM,CAAC;YACL,OAAO,UAAU,CAAC,YAAY,MAAM,eAAe,EAAE,WAAW,CAAC,CAAC;QACtE,CAAC;IACL,CAAC;CACJ,CAAC;AAEF,qBAAqB;AACrB,MAAM,CAAC,MAAM,QAAQ,GAAW,CAAC,WAAW,EAAE,YAAY,EAAE,WAAW,CAAC,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { Tool } from '../base.js';
2
+ export declare const readFileTool: Tool;
3
+ export declare const writeFileTool: Tool;
4
+ export declare const listDirectoryTool: Tool;
5
+ export declare const createDirectoryTool: Tool;
6
+ export declare const searchFilesTool: Tool;
7
+ export declare const replaceInFileTool: Tool;
8
+ export declare const fileInfoTool: Tool;
9
+ /** All built-in FS tools. */
10
+ export declare const fsTools: Tool[];
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fs/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAsCvC,eAAO,MAAM,YAAY,EAAE,IA6B1B,CAAC;AAGF,eAAO,MAAM,aAAa,EAAE,IA0B3B,CAAC;AAGF,eAAO,MAAM,iBAAiB,EAAE,IAsB/B,CAAC;AAGF,eAAO,MAAM,mBAAmB,EAAE,IAmBjC,CAAC;AAGF,eAAO,MAAM,eAAe,EAAE,IAyC7B,CAAC;AAGF,eAAO,MAAM,iBAAiB,EAAE,IAyC/B,CAAC;AAGF,eAAO,MAAM,YAAY,EAAE,IA0B1B,CAAC;AAEF,6BAA6B;AAC7B,eAAO,MAAM,OAAO,EAAE,IAAI,EAGzB,CAAC"}
@@ -0,0 +1,240 @@
1
+ import { promises as fsp } from 'node:fs';
2
+ import { join, resolve, relative, isAbsolute } from 'node:path';
3
+ import { execFile } from 'node:child_process';
4
+ import { promisify } from 'node:util';
5
+ import { createSchema } from '../base.js';
6
+ const execFileAsync = promisify(execFile);
7
+ // Security: files that must never be read
8
+ const BLOCKED_PATTERNS = ['.env', 'auth.json', '.memoryblock/auth.json'];
9
+ function isBlockedPath(filePath) {
10
+ const normalized = filePath.replace(/\\/g, '/');
11
+ return BLOCKED_PATTERNS.some((p) => normalized.endsWith(p) || normalized.includes(`/${p}`));
12
+ }
13
+ /** Resolve a path. Scope is determined by block permissions. */
14
+ function resolvePath(context, targetPath) {
15
+ const base = context.workingDir || context.blockPath;
16
+ const resolved = isAbsolute(targetPath) ? targetPath : resolve(base, targetPath);
17
+ const scope = context.permissions?.scope || 'block';
18
+ if (scope === 'system') {
19
+ // Unrestricted — still block sensitive files
20
+ return resolved;
21
+ }
22
+ // Determine allowed root based on scope
23
+ const allowedRoot = scope === 'workspace' && context.workspacePath
24
+ ? context.workspacePath
25
+ : context.blockPath;
26
+ const rel = relative(allowedRoot, resolved);
27
+ if (rel.startsWith('..') || isAbsolute(rel)) {
28
+ const label = scope === 'workspace' ? 'workspace' : 'block directory';
29
+ throw new Error(`Access denied: path "${targetPath}" is outside the ${label}. Current scope: ${scope}.`);
30
+ }
31
+ return resolved;
32
+ }
33
+ // ===== read_file =====
34
+ export const readFileTool = {
35
+ definition: {
36
+ name: 'read_file',
37
+ description: 'Read the contents of a file.',
38
+ parameters: createSchema({ path: { type: 'string', description: 'Path to the file.' } }, ['path']),
39
+ requiresApproval: false,
40
+ },
41
+ async execute(params, context) {
42
+ const filePath = resolvePath(context, params.path);
43
+ if (isBlockedPath(filePath)) {
44
+ return { content: 'Access denied: this file is protected.', isError: true };
45
+ }
46
+ try {
47
+ const content = await fsp.readFile(filePath, 'utf-8');
48
+ // Truncate extremely large files to save tokens
49
+ if (content.length > 100_000) {
50
+ return {
51
+ content: content.slice(0, 100_000) + `\n...(truncated, ${content.length} total chars. Use search_files to find specific content.)`,
52
+ isError: false,
53
+ };
54
+ }
55
+ return { content, isError: false };
56
+ }
57
+ catch (err) {
58
+ return { content: `Failed to read file: ${err.message}`, isError: true };
59
+ }
60
+ },
61
+ };
62
+ // ===== write_file =====
63
+ export const writeFileTool = {
64
+ definition: {
65
+ name: 'write_file',
66
+ description: 'Write content to a file. Creates parent directories if needed.',
67
+ parameters: createSchema({
68
+ path: { type: 'string', description: 'Path to the file.' },
69
+ content: { type: 'string', description: 'Content to write.' },
70
+ }, ['path', 'content']),
71
+ requiresApproval: false,
72
+ },
73
+ async execute(params, context) {
74
+ const filePath = resolvePath(context, params.path);
75
+ if (isBlockedPath(filePath)) {
76
+ return { content: 'Access denied: this file is protected.', isError: true };
77
+ }
78
+ try {
79
+ await fsp.mkdir(join(filePath, '..'), { recursive: true });
80
+ await fsp.writeFile(filePath, params.content, 'utf-8');
81
+ return { content: `Written: ${params.path}`, isError: false };
82
+ }
83
+ catch (err) {
84
+ return { content: `Failed to write: ${err.message}`, isError: true };
85
+ }
86
+ },
87
+ };
88
+ // ===== list_directory =====
89
+ export const listDirectoryTool = {
90
+ definition: {
91
+ name: 'list_directory',
92
+ description: 'List files and directories in a path.',
93
+ parameters: createSchema({ path: { type: 'string', description: 'Path to list. Defaults to workspace root.' } }, []),
94
+ requiresApproval: false,
95
+ },
96
+ async execute(params, context) {
97
+ const dirPath = resolvePath(context, params.path || '.');
98
+ try {
99
+ const entries = await fsp.readdir(dirPath, { withFileTypes: true });
100
+ const listing = entries
101
+ .map((e) => `${e.isDirectory() ? '📁' : '📄'} ${e.name}`)
102
+ .join('\n');
103
+ return { content: listing || '(empty directory)', isError: false };
104
+ }
105
+ catch (err) {
106
+ return { content: `Failed to list: ${err.message}`, isError: true };
107
+ }
108
+ },
109
+ };
110
+ // ===== create_directory =====
111
+ export const createDirectoryTool = {
112
+ definition: {
113
+ name: 'create_directory',
114
+ description: 'Create a directory.',
115
+ parameters: createSchema({ path: { type: 'string', description: 'Path of the directory to create.' } }, ['path']),
116
+ requiresApproval: false,
117
+ },
118
+ async execute(params, context) {
119
+ const dirPath = resolvePath(context, params.path);
120
+ try {
121
+ await fsp.mkdir(dirPath, { recursive: true });
122
+ return { content: `Created: ${params.path}`, isError: false };
123
+ }
124
+ catch (err) {
125
+ return { content: `Failed to create: ${err.message}`, isError: true };
126
+ }
127
+ },
128
+ };
129
+ // ===== search_files =====
130
+ export const searchFilesTool = {
131
+ definition: {
132
+ name: 'search_files',
133
+ description: 'Search for text in files using grep. Returns matching lines with file paths and line numbers.',
134
+ parameters: createSchema({
135
+ query: { type: 'string', description: 'Text to search for.' },
136
+ path: { type: 'string', description: 'Directory to search in. Defaults to workspace root.' },
137
+ include: { type: 'string', description: 'File glob pattern to include, e.g. "*.ts".' },
138
+ }, ['query']),
139
+ requiresApproval: false,
140
+ },
141
+ async execute(params, context) {
142
+ const searchDir = resolvePath(context, params.path || '.');
143
+ const query = params.query;
144
+ const include = params.include;
145
+ try {
146
+ const args = ['-rnI', '--color=never', '-m', '50'];
147
+ if (include)
148
+ args.push('--include', include);
149
+ args.push(query, searchDir);
150
+ const { stdout } = await execFileAsync('grep', args, {
151
+ timeout: 15_000,
152
+ maxBuffer: 512 * 1024,
153
+ });
154
+ const output = stdout.trim();
155
+ if (!output)
156
+ return { content: 'No matches found.', isError: false };
157
+ // Truncate if too many results
158
+ if (output.length > 20_000) {
159
+ return { content: output.slice(0, 20_000) + '\n...(truncated)', isError: false };
160
+ }
161
+ return { content: output, isError: false };
162
+ }
163
+ catch (err) {
164
+ const e = err;
165
+ if (e.code === 1)
166
+ return { content: 'No matches found.', isError: false };
167
+ return { content: `Search failed: ${e.message}`, isError: true };
168
+ }
169
+ },
170
+ };
171
+ // ===== replace_in_file =====
172
+ export const replaceInFileTool = {
173
+ definition: {
174
+ name: 'replace_in_file',
175
+ description: 'Find and replace text in a file.',
176
+ parameters: createSchema({
177
+ path: { type: 'string', description: 'File path.' },
178
+ find: { type: 'string', description: 'Text to find.' },
179
+ replace: { type: 'string', description: 'Replacement text.' },
180
+ all: { type: 'string', description: 'Replace all? "true"/"false".' },
181
+ }, ['path', 'find', 'replace']),
182
+ requiresApproval: false,
183
+ },
184
+ async execute(params, context) {
185
+ const filePath = resolvePath(context, params.path);
186
+ if (isBlockedPath(filePath)) {
187
+ return { content: 'Access denied: this file is protected.', isError: true };
188
+ }
189
+ try {
190
+ const content = await fsp.readFile(filePath, 'utf-8');
191
+ const find = params.find;
192
+ const replace = params.replace;
193
+ const replaceAll = params.all === 'true';
194
+ if (!content.includes(find)) {
195
+ return { content: `Text not found in ${params.path}. Check exact whitespace/formatting.`, isError: true };
196
+ }
197
+ const updated = replaceAll
198
+ ? content.split(find).join(replace)
199
+ : content.replace(find, replace);
200
+ await fsp.writeFile(filePath, updated, 'utf-8');
201
+ const count = replaceAll ? content.split(find).length - 1 : 1;
202
+ return { content: `Replaced ${count} occurrence(s) in ${params.path}`, isError: false };
203
+ }
204
+ catch (err) {
205
+ return { content: `Failed: ${err.message}`, isError: true };
206
+ }
207
+ },
208
+ };
209
+ // ===== file_info =====
210
+ export const fileInfoTool = {
211
+ definition: {
212
+ name: 'file_info',
213
+ description: 'Get file metadata: size, modified date, type.',
214
+ parameters: createSchema({ path: { type: 'string', description: 'Path to the file or directory.' } }, ['path']),
215
+ requiresApproval: false,
216
+ },
217
+ async execute(params, context) {
218
+ const filePath = resolvePath(context, params.path);
219
+ try {
220
+ const stat = await fsp.stat(filePath);
221
+ const info = [
222
+ `Path: ${params.path}`,
223
+ `Type: ${stat.isDirectory() ? 'directory' : 'file'}`,
224
+ `Size: ${stat.size} bytes (${(stat.size / 1024).toFixed(1)} KB)`,
225
+ `Modified: ${stat.mtime.toISOString()}`,
226
+ `Created: ${stat.birthtime.toISOString()}`,
227
+ ].join('\n');
228
+ return { content: info, isError: false };
229
+ }
230
+ catch (err) {
231
+ return { content: `Failed: ${err.message}`, isError: true };
232
+ }
233
+ },
234
+ };
235
+ /** All built-in FS tools. */
236
+ export const fsTools = [
237
+ readFileTool, writeFileTool, listDirectoryTool, createDirectoryTool,
238
+ searchFilesTool, replaceInFileTool, fileInfoTool,
239
+ ];
240
+ //# sourceMappingURL=index.js.map