@framers/agentos 0.1.94 → 0.1.96

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 (57) hide show
  1. package/README.md +10 -0
  2. package/dist/api/AgentOS.d.ts +13 -0
  3. package/dist/api/AgentOS.d.ts.map +1 -1
  4. package/dist/api/AgentOS.js +36 -1
  5. package/dist/api/AgentOS.js.map +1 -1
  6. package/dist/api/usageLedger.d.ts.map +1 -1
  7. package/dist/api/usageLedger.js.map +1 -1
  8. package/dist/core/tools/IToolOrchestrator.d.ts +38 -1
  9. package/dist/core/tools/IToolOrchestrator.d.ts.map +1 -1
  10. package/dist/core/tools/ToolOrchestrator.d.ts +58 -1
  11. package/dist/core/tools/ToolOrchestrator.d.ts.map +1 -1
  12. package/dist/core/tools/ToolOrchestrator.js +116 -1
  13. package/dist/core/tools/ToolOrchestrator.js.map +1 -1
  14. package/dist/discovery/CapabilityDiscoveryEngine.d.ts +19 -0
  15. package/dist/discovery/CapabilityDiscoveryEngine.d.ts.map +1 -1
  16. package/dist/discovery/CapabilityDiscoveryEngine.js +54 -0
  17. package/dist/discovery/CapabilityDiscoveryEngine.js.map +1 -1
  18. package/dist/discovery/types.d.ts +8 -1
  19. package/dist/discovery/types.d.ts.map +1 -1
  20. package/dist/discovery/types.js.map +1 -1
  21. package/dist/emergent/ComposableToolBuilder.d.ts +125 -0
  22. package/dist/emergent/ComposableToolBuilder.d.ts.map +1 -0
  23. package/dist/emergent/ComposableToolBuilder.js +318 -0
  24. package/dist/emergent/ComposableToolBuilder.js.map +1 -0
  25. package/dist/emergent/EmergentCapabilityEngine.d.ts +168 -0
  26. package/dist/emergent/EmergentCapabilityEngine.d.ts.map +1 -0
  27. package/dist/emergent/EmergentCapabilityEngine.js +437 -0
  28. package/dist/emergent/EmergentCapabilityEngine.js.map +1 -0
  29. package/dist/emergent/EmergentJudge.d.ts +283 -0
  30. package/dist/emergent/EmergentJudge.d.ts.map +1 -0
  31. package/dist/emergent/EmergentJudge.js +463 -0
  32. package/dist/emergent/EmergentJudge.js.map +1 -0
  33. package/dist/emergent/EmergentToolRegistry.d.ts +286 -0
  34. package/dist/emergent/EmergentToolRegistry.d.ts.map +1 -0
  35. package/dist/emergent/EmergentToolRegistry.js +546 -0
  36. package/dist/emergent/EmergentToolRegistry.js.map +1 -0
  37. package/dist/emergent/ForgeToolMetaTool.d.ts +124 -0
  38. package/dist/emergent/ForgeToolMetaTool.d.ts.map +1 -0
  39. package/dist/emergent/ForgeToolMetaTool.js +170 -0
  40. package/dist/emergent/ForgeToolMetaTool.js.map +1 -0
  41. package/dist/emergent/SandboxedToolForge.d.ts +185 -0
  42. package/dist/emergent/SandboxedToolForge.d.ts.map +1 -0
  43. package/dist/emergent/SandboxedToolForge.js +383 -0
  44. package/dist/emergent/SandboxedToolForge.js.map +1 -0
  45. package/dist/emergent/index.d.ts +25 -0
  46. package/dist/emergent/index.d.ts.map +1 -0
  47. package/dist/emergent/index.js +20 -0
  48. package/dist/emergent/index.js.map +1 -0
  49. package/dist/emergent/types.d.ts +596 -0
  50. package/dist/emergent/types.d.ts.map +1 -0
  51. package/dist/emergent/types.js +36 -0
  52. package/dist/emergent/types.js.map +1 -0
  53. package/dist/index.d.ts +1 -0
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +2 -0
  56. package/dist/index.js.map +1 -1
  57. package/package.json +1 -1
@@ -0,0 +1,124 @@
1
+ /**
2
+ * @fileoverview ForgeToolMetaTool — ITool implementation that agents call to
3
+ * create new tools at runtime via the Emergent Capability Engine.
4
+ *
5
+ * @module @framers/agentos/emergent/ForgeToolMetaTool
6
+ *
7
+ * This is the meta-tool that bridges the LLM tool-call interface with the
8
+ * {@link EmergentCapabilityEngine}. When an agent determines that no existing
9
+ * capability matches its need, it calls `forge_tool` with a name, description,
10
+ * schemas, implementation (compose or sandbox), and test cases.
11
+ *
12
+ * Only registered when the agent is configured with `emergent: true`.
13
+ * Adds ~120 tokens to the tool list.
14
+ */
15
+ import type { ITool, ToolExecutionResult, ToolExecutionContext, JSONSchemaObject } from '../core/tools/ITool.js';
16
+ import type { ForgeResult } from './types.js';
17
+ import type { EmergentCapabilityEngine } from './EmergentCapabilityEngine.js';
18
+ /**
19
+ * Input arguments accepted by the `forge_tool` meta-tool.
20
+ *
21
+ * Mirrors {@link ForgeToolRequest} but typed as a `Record<string, any>` to
22
+ * satisfy the {@link ITool} generic constraint while retaining semantic clarity.
23
+ */
24
+ export interface ForgeToolInput extends Record<string, any> {
25
+ /** Machine-readable name for the new tool. */
26
+ name: string;
27
+ /** Natural language description of the tool's purpose. */
28
+ description: string;
29
+ /** JSON Schema for the tool's input arguments. */
30
+ inputSchema: JSONSchemaObject;
31
+ /** JSON Schema for the tool's expected output (optional). */
32
+ outputSchema?: JSONSchemaObject;
33
+ /**
34
+ * Implementation specification — either compose (chain existing tools) or
35
+ * sandbox (arbitrary code). Discriminated on the `mode` field.
36
+ */
37
+ implementation: {
38
+ mode: 'compose';
39
+ steps: Array<{
40
+ name: string;
41
+ tool: string;
42
+ inputMapping: Record<string, unknown>;
43
+ }>;
44
+ } | {
45
+ mode: 'sandbox';
46
+ code: string;
47
+ allowlist: string[];
48
+ };
49
+ /**
50
+ * One or more test cases for the judge to evaluate.
51
+ * Each has an `input` object and optional `expectedOutput`.
52
+ */
53
+ testCases: Array<{
54
+ input: Record<string, unknown>;
55
+ expectedOutput?: unknown;
56
+ }>;
57
+ }
58
+ /**
59
+ * Meta-tool enabling agents to create new tools at runtime.
60
+ *
61
+ * Only registered when the agent is configured with `emergent: true`.
62
+ * Adds ~120 tokens to the tool list. Agents provide: name, description,
63
+ * schemas, implementation (compose existing tools or write sandboxed code),
64
+ * and test cases.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * const metaTool = new ForgeToolMetaTool(engine);
69
+ * // Register with ToolOrchestrator:
70
+ * orchestrator.registerTool(metaTool);
71
+ *
72
+ * // Agent calls via tool-call interface:
73
+ * const result = await metaTool.execute({
74
+ * name: 'add_numbers',
75
+ * description: 'Add two numbers together.',
76
+ * inputSchema: { type: 'object', properties: { a: { type: 'number' }, b: { type: 'number' } } },
77
+ * outputSchema: { type: 'object', properties: { sum: { type: 'number' } } },
78
+ * implementation: {
79
+ * mode: 'sandbox',
80
+ * code: 'function execute(input) { return { sum: input.a + input.b }; }',
81
+ * allowlist: [],
82
+ * },
83
+ * testCases: [{ input: { a: 2, b: 3 }, expectedOutput: { sum: 5 } }],
84
+ * }, context);
85
+ * ```
86
+ */
87
+ export declare class ForgeToolMetaTool implements ITool<ForgeToolInput, ForgeResult> {
88
+ /** @inheritdoc */
89
+ readonly id = "com.framers.emergent.forge-tool";
90
+ /** @inheritdoc */
91
+ readonly name = "forge_tool";
92
+ /** @inheritdoc */
93
+ readonly displayName = "Forge Tool";
94
+ /** @inheritdoc */
95
+ readonly description: string;
96
+ /** @inheritdoc */
97
+ readonly category = "emergent";
98
+ /** @inheritdoc */
99
+ readonly hasSideEffects = true;
100
+ /** @inheritdoc */
101
+ readonly inputSchema: JSONSchemaObject;
102
+ /** Reference to the engine that orchestrates the forge pipeline. */
103
+ private readonly engine;
104
+ /**
105
+ * Create a new ForgeToolMetaTool.
106
+ *
107
+ * @param engine - The {@link EmergentCapabilityEngine} that will handle the
108
+ * actual forge pipeline (build → test → judge → register).
109
+ */
110
+ constructor(engine: EmergentCapabilityEngine);
111
+ /**
112
+ * Execute the forge pipeline via the engine.
113
+ *
114
+ * Extracts the agent ID and session/correlation ID from the execution context
115
+ * and delegates to {@link EmergentCapabilityEngine.forge}.
116
+ *
117
+ * @param args - The forge tool input arguments (name, description, schemas,
118
+ * implementation, test cases).
119
+ * @param context - The tool execution context providing agent and session IDs.
120
+ * @returns A {@link ToolExecutionResult} wrapping the {@link ForgeResult}.
121
+ */
122
+ execute(args: ForgeToolInput, context: ToolExecutionContext): Promise<ToolExecutionResult<ForgeResult>>;
123
+ }
124
+ //# sourceMappingURL=ForgeToolMetaTool.d.ts.map
@@ -0,0 +1 @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CAAC;IAC1E,kBAAkB;IAClB,QAAQ,CAAC,EAAE,qCAAqC;IAEhD,kBAAkB;IAClB,QAAQ,CAAC,IAAI,gBAAgB;IAE7B,kBAAkB;IAClB,QAAQ,CAAC,WAAW,gBAAgB;IAEpC,kBAAkB;IAClB,QAAQ,CAAC,WAAW,SAGuB;IAE3C,kBAAkB;IAClB,QAAQ,CAAC,QAAQ,cAAc;IAE/B,kBAAkB;IAClB,QAAQ,CAAC,cAAc,QAAQ;IAE/B,kBAAkB;IAClB,QAAQ,CAAC,WAAW,EAAE,gBAAgB,CAwEpC;IAEF,oEAAoE;IACpE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2B;IAElD;;;;;OAKG;gBACS,MAAM,EAAE,wBAAwB;IAQ5C;;;;;;;;;;OAUG;IACG,OAAO,CACX,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC,WAAW,CAAC,CAAC;CAY7C"}
@@ -0,0 +1,170 @@
1
+ /**
2
+ * @fileoverview ForgeToolMetaTool — ITool implementation that agents call to
3
+ * create new tools at runtime via the Emergent Capability Engine.
4
+ *
5
+ * @module @framers/agentos/emergent/ForgeToolMetaTool
6
+ *
7
+ * This is the meta-tool that bridges the LLM tool-call interface with the
8
+ * {@link EmergentCapabilityEngine}. When an agent determines that no existing
9
+ * capability matches its need, it calls `forge_tool` with a name, description,
10
+ * schemas, implementation (compose or sandbox), and test cases.
11
+ *
12
+ * Only registered when the agent is configured with `emergent: true`.
13
+ * Adds ~120 tokens to the tool list.
14
+ */
15
+ // ============================================================================
16
+ // META-TOOL
17
+ // ============================================================================
18
+ /**
19
+ * Meta-tool enabling agents to create new tools at runtime.
20
+ *
21
+ * Only registered when the agent is configured with `emergent: true`.
22
+ * Adds ~120 tokens to the tool list. Agents provide: name, description,
23
+ * schemas, implementation (compose existing tools or write sandboxed code),
24
+ * and test cases.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const metaTool = new ForgeToolMetaTool(engine);
29
+ * // Register with ToolOrchestrator:
30
+ * orchestrator.registerTool(metaTool);
31
+ *
32
+ * // Agent calls via tool-call interface:
33
+ * const result = await metaTool.execute({
34
+ * name: 'add_numbers',
35
+ * description: 'Add two numbers together.',
36
+ * inputSchema: { type: 'object', properties: { a: { type: 'number' }, b: { type: 'number' } } },
37
+ * outputSchema: { type: 'object', properties: { sum: { type: 'number' } } },
38
+ * implementation: {
39
+ * mode: 'sandbox',
40
+ * code: 'function execute(input) { return { sum: input.a + input.b }; }',
41
+ * allowlist: [],
42
+ * },
43
+ * testCases: [{ input: { a: 2, b: 3 }, expectedOutput: { sum: 5 } }],
44
+ * }, context);
45
+ * ```
46
+ */
47
+ export class ForgeToolMetaTool {
48
+ /**
49
+ * Create a new ForgeToolMetaTool.
50
+ *
51
+ * @param engine - The {@link EmergentCapabilityEngine} that will handle the
52
+ * actual forge pipeline (build → test → judge → register).
53
+ */
54
+ constructor(engine) {
55
+ /** @inheritdoc */
56
+ this.id = 'com.framers.emergent.forge-tool';
57
+ /** @inheritdoc */
58
+ this.name = 'forge_tool';
59
+ /** @inheritdoc */
60
+ this.displayName = 'Forge Tool';
61
+ /** @inheritdoc */
62
+ this.description = 'Create a new tool when no existing capability matches your need. ' +
63
+ 'Provide a name, description, implementation (compose existing tools or ' +
64
+ 'write sandboxed code), and test cases.';
65
+ /** @inheritdoc */
66
+ this.category = 'emergent';
67
+ /** @inheritdoc */
68
+ this.hasSideEffects = true;
69
+ /** @inheritdoc */
70
+ this.inputSchema = {
71
+ type: 'object',
72
+ properties: {
73
+ name: {
74
+ type: 'string',
75
+ description: 'Machine-readable name for the new tool.',
76
+ },
77
+ description: {
78
+ type: 'string',
79
+ description: 'Natural language description of what the tool does.',
80
+ },
81
+ inputSchema: {
82
+ type: 'object',
83
+ description: 'JSON Schema for the tool input arguments.',
84
+ },
85
+ outputSchema: {
86
+ type: 'object',
87
+ description: 'JSON Schema for the tool output (optional).',
88
+ },
89
+ implementation: {
90
+ description: 'Implementation: compose existing tools or write sandboxed code.',
91
+ oneOf: [
92
+ {
93
+ type: 'object',
94
+ properties: {
95
+ mode: { type: 'string', const: 'compose' },
96
+ steps: {
97
+ type: 'array',
98
+ items: {
99
+ type: 'object',
100
+ properties: {
101
+ name: { type: 'string' },
102
+ tool: { type: 'string' },
103
+ inputMapping: { type: 'object' },
104
+ },
105
+ required: ['name', 'tool', 'inputMapping'],
106
+ },
107
+ minItems: 1,
108
+ },
109
+ },
110
+ required: ['mode', 'steps'],
111
+ },
112
+ {
113
+ type: 'object',
114
+ properties: {
115
+ mode: { type: 'string', const: 'sandbox' },
116
+ code: { type: 'string' },
117
+ allowlist: {
118
+ type: 'array',
119
+ items: { type: 'string', enum: ['fetch', 'fs.readFile', 'crypto'] },
120
+ },
121
+ },
122
+ required: ['mode', 'code', 'allowlist'],
123
+ },
124
+ ],
125
+ },
126
+ testCases: {
127
+ type: 'array',
128
+ description: 'One or more test cases for the judge to evaluate.',
129
+ items: {
130
+ type: 'object',
131
+ properties: {
132
+ input: { type: 'object' },
133
+ expectedOutput: {},
134
+ },
135
+ required: ['input'],
136
+ },
137
+ minItems: 1,
138
+ },
139
+ },
140
+ required: ['name', 'description', 'inputSchema', 'implementation', 'testCases'],
141
+ };
142
+ this.engine = engine;
143
+ }
144
+ // --------------------------------------------------------------------------
145
+ // EXECUTE
146
+ // --------------------------------------------------------------------------
147
+ /**
148
+ * Execute the forge pipeline via the engine.
149
+ *
150
+ * Extracts the agent ID and session/correlation ID from the execution context
151
+ * and delegates to {@link EmergentCapabilityEngine.forge}.
152
+ *
153
+ * @param args - The forge tool input arguments (name, description, schemas,
154
+ * implementation, test cases).
155
+ * @param context - The tool execution context providing agent and session IDs.
156
+ * @returns A {@link ToolExecutionResult} wrapping the {@link ForgeResult}.
157
+ */
158
+ async execute(args, context) {
159
+ const result = await this.engine.forge(args, {
160
+ agentId: context.gmiId || 'unknown',
161
+ sessionId: context.correlationId || 'unknown',
162
+ });
163
+ return {
164
+ success: result.success,
165
+ output: result,
166
+ error: result.error,
167
+ };
168
+ }
169
+ }
170
+ //# sourceMappingURL=ForgeToolMetaTool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ForgeToolMetaTool.js","sourceRoot":"","sources":["../../src/emergent/ForgeToolMetaTool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAiDH,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,OAAO,iBAAiB;IAoG5B;;;;;OAKG;IACH,YAAY,MAAgC;QAzG5C,kBAAkB;QACT,OAAE,GAAG,iCAAiC,CAAC;QAEhD,kBAAkB;QACT,SAAI,GAAG,YAAY,CAAC;QAE7B,kBAAkB;QACT,gBAAW,GAAG,YAAY,CAAC;QAEpC,kBAAkB;QACT,gBAAW,GAClB,mEAAmE;YACnE,yEAAyE;YACzE,wCAAwC,CAAC;QAE3C,kBAAkB;QACT,aAAQ,GAAG,UAAU,CAAC;QAE/B,kBAAkB;QACT,mBAAc,GAAG,IAAI,CAAC;QAE/B,kBAAkB;QACT,gBAAW,GAAqB;YACvC,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,IAAI,EAAE;oBACJ,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,yCAAyC;iBACvD;gBACD,WAAW,EAAE;oBACX,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,qDAAqD;iBACnE;gBACD,WAAW,EAAE;oBACX,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,2CAA2C;iBACzD;gBACD,YAAY,EAAE;oBACZ,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,6CAA6C;iBAC3D;gBACD,cAAc,EAAE;oBACd,WAAW,EACT,iEAAiE;oBACnE,KAAK,EAAE;wBACL;4BACE,IAAI,EAAE,QAAQ;4BACd,UAAU,EAAE;gCACV,IAAI,EAAE,EAAE,IAAI,EAAE,QA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Aa,EAAE,gBAAgB,EAAE,WAAW,CAAC;SAChF,CAAC;QAYA,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,6EAA6E;IAC7E,UAAU;IACV,6EAA6E;IAE7E;;;;;;;;;;OAUG;IACH,KAAK,CAAC,OAAO,CACX,IAAoB,EACpB,OAA6B;QAE7B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAmC,EAAE;YAC1E,OAAO,EAAE,OAAO,CAAC,KAAK,IAAI,SAAS;YACnC,SAAS,EAAE,OAAO,CAAC,aAAa,IAAI,SAAS;SAC9C,CAAC,CAAC;QAEH,OAAO;YACL,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,MAAM,EAAE,MAAM;YACd,KAAK,EAAE,MAAM,CAAC,KAAK;SACpB,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @fileoverview SandboxedToolForge — runs agent-generated JavaScript code in an
3
+ * isolated sandbox with strict resource limits and API allowlisting.
4
+ *
5
+ * @module @framers/agentos/emergent/SandboxedToolForge
6
+ *
7
+ * Overview:
8
+ * - Attempts to use `isolated-vm` for true V8 isolate sandboxing when available.
9
+ * - Falls back to Node.js `vm` module with timeout when `isolated-vm` is not installed.
10
+ * - Enforces configurable memory limits (default 128 MB), execution timeouts (default 5 s),
11
+ * and a strict API blocklist that prevents access to `eval`, `Function`, `process`,
12
+ * `require`, `import`, `child_process`, and `fs.write*`.
13
+ *
14
+ * Allowlisted APIs (each requires explicit opt-in via {@link SandboxAPI}):
15
+ * - `fetch` — HTTP requests (domain-restricted via {@link SandboxedToolForgeConfig.fetchDomainAllowlist}).
16
+ * - `fs.readFile` — Read-only file access (path-restricted, max 1 MB).
17
+ * - `crypto` — Hashing and HMAC only (`createHash`, `createHmac`).
18
+ *
19
+ * Security model:
20
+ * 1. **Static validation** ({@link validateCode}) rejects dangerous patterns
21
+ * (regex scan) before any code reaches the runtime.
22
+ * 2. **Runtime isolation** executes validated code inside a minimal context that
23
+ * exposes only JSON, Math, Date, TextEncoder, TextDecoder, and explicitly
24
+ * opted-in APIs.
25
+ * 3. **Resource bounding** enforces wall-clock timeout so runaway loops cannot
26
+ * starve the host process.
27
+ */
28
+ import type { SandboxExecutionRequest, SandboxExecutionResult, SandboxAPI } from './types.js';
29
+ /**
30
+ * Configuration options for the {@link SandboxedToolForge}.
31
+ *
32
+ * All fields are optional and fall back to sensible defaults.
33
+ */
34
+ export interface SandboxedToolForgeConfig {
35
+ /**
36
+ * Maximum heap memory in megabytes for the sandbox process.
37
+ * Used with `isolated-vm`; the `vm` fallback cannot enforce memory limits.
38
+ * @default 128
39
+ */
40
+ memoryMB?: number;
41
+ /**
42
+ * Maximum wall-clock execution time in milliseconds.
43
+ * @default 5000
44
+ */
45
+ timeoutMs?: number;
46
+ /**
47
+ * When `fetch` is in the allowlist, only requests to these domains are
48
+ * permitted. An empty array means all domains are allowed.
49
+ * Domain matching is case-insensitive and checks exact host equality.
50
+ * @default []
51
+ */
52
+ fetchDomainAllowlist?: string[];
53
+ /**
54
+ * Filesystem roots sandboxed `fs.readFile` calls may access.
55
+ * Relative paths are resolved from the current working directory.
56
+ * Defaults to the current working directory only.
57
+ */
58
+ fsReadRoots?: string[];
59
+ }
60
+ /**
61
+ * Runs agent-generated code in an isolated sandbox with strict resource limits.
62
+ *
63
+ * Attempts to use `isolated-vm` for true V8 isolate sandboxing. Falls back to
64
+ * Node.js `vm` module with timeout if `isolated-vm` is not installed.
65
+ *
66
+ * Resource limits:
67
+ * - Memory: configurable, default 128 MB
68
+ * - Execution time: configurable, default 5000 ms
69
+ * - Blocked APIs: eval, Function, process, require, import, child_process, fs.write*
70
+ *
71
+ * Allowlisted APIs (each requires explicit opt-in):
72
+ * - `fetch`: HTTP requests (domain-restricted)
73
+ * - `fs.readFile`: Read-only file access (path-restricted, max 1 MB)
74
+ * - `crypto`: Hashing and HMAC only
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const forge = new SandboxedToolForge({ timeoutMs: 3000 });
79
+ *
80
+ * const result = await forge.execute({
81
+ * code: 'function execute(input) { return input.a + input.b; }',
82
+ * input: { a: 2, b: 3 },
83
+ * allowlist: [],
84
+ * memoryMB: 128,
85
+ * timeoutMs: 3000,
86
+ * });
87
+ *
88
+ * console.log(result.output); // 5
89
+ * ```
90
+ */
91
+ export declare class SandboxedToolForge {
92
+ /** Resolved memory limit in MB. */
93
+ private readonly memoryMB;
94
+ /** Resolved timeout in milliseconds. */
95
+ private readonly timeoutMs;
96
+ /** Domain allowlist for sandboxed `fetch` calls. */
97
+ private readonly fetchDomainAllowlist;
98
+ /** Filesystem roots sandboxed reads may access. */
99
+ private readonly fsReadRoots;
100
+ /**
101
+ * Create a new SandboxedToolForge instance.
102
+ *
103
+ * @param config - Optional configuration overrides. All fields have sensible
104
+ * defaults (128 MB memory, 5000 ms timeout, no domain restrictions).
105
+ */
106
+ constructor(config?: SandboxedToolForgeConfig);
107
+ /**
108
+ * Static analysis of code — reject dangerous patterns before execution.
109
+ *
110
+ * Scans the source string for banned API usage patterns using regex matching.
111
+ * If an API is not present in the allowlist, references to it are also flagged.
112
+ *
113
+ * Checked patterns (always banned):
114
+ * - `eval()`, `new Function()`, `require()`, `import`, `process.*`
115
+ * - `child_process`, `fs.write*`, `fs.unlink`, `fs.rm`, `fs.rmdir`
116
+ *
117
+ * Conditionally banned (when not in allowlist):
118
+ * - `fetch(` — when `'fetch'` is not in the allowlist
119
+ * - `fs.*` — when `'fs.readFile'` is not in the allowlist
120
+ * - `crypto.*` — when `'crypto'` is not in the allowlist
121
+ *
122
+ * @param code - The raw source code string to validate.
123
+ * @param allowlist - The set of APIs the code is permitted to use.
124
+ * @returns An object with `valid: true` if no violations were found, or
125
+ * `valid: false` with a `violations` array describing each flagged pattern.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const forge = new SandboxedToolForge();
130
+ * const result = forge.validateCode('eval("exploit")', []);
131
+ * // result.valid === false
132
+ * // result.violations === ['eval() is forbidden']
133
+ * ```
134
+ */
135
+ validateCode(code: string, allowlist: SandboxAPI[]): {
136
+ valid: boolean;
137
+ violations: string[];
138
+ };
139
+ /**
140
+ * Execute agent-generated code in the sandbox.
141
+ *
142
+ * The code must define a function named `execute` that accepts a single
143
+ * argument and returns the output:
144
+ *
145
+ * ```js
146
+ * function execute(input) { return input.a + input.b; }
147
+ * ```
148
+ *
149
+ * Execution flow:
150
+ * 1. Run {@link validateCode} — reject immediately if violations are found.
151
+ * 2. Wrap the agent's code into a self-contained expression that calls `execute`.
152
+ * 3. Run in a Node.js `vm` sandbox with a restricted global context.
153
+ * 4. Parse the output, measure execution time, and return the result.
154
+ *
155
+ * @param request - The execution request containing code, input, allowlist,
156
+ * and resource limits.
157
+ * @returns A {@link SandboxExecutionResult} with the output (on success) or
158
+ * error description (on failure), plus execution time telemetry.
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * const result = await forge.execute({
163
+ * code: 'function execute(input) { return { sum: input.a + input.b }; }',
164
+ * input: { a: 10, b: 20 },
165
+ * allowlist: [],
166
+ * memoryMB: 128,
167
+ * timeoutMs: 5000,
168
+ * });
169
+ * // result.success === true
170
+ * // result.output === { sum: 30 }
171
+ * ```
172
+ */
173
+ execute(request: SandboxExecutionRequest): Promise<SandboxExecutionResult>;
174
+ /**
175
+ * Build the global context object for the VM sandbox.
176
+ *
177
+ * Provides a minimal set of safe built-ins (JSON, Math, Date, TextEncoder,
178
+ * TextDecoder) and conditionally injects allowlisted APIs.
179
+ *
180
+ * @param allowlist - The set of APIs to inject into the sandbox.
181
+ * @returns A plain object suitable for {@link createContext}.
182
+ */
183
+ private buildSandboxContext;
184
+ }
185
+ //# sourceMappingURL=SandboxedToolForge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SandboxedToolForge.d.ts","sourceRoot":"","sources":["../../src/emergent/SandboxedToolForge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0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