@frontmcp/plugins 0.4.1 → 0.5.1
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/package.json +8 -3
- package/src/cache/cache.plugin.js +27 -25
- package/src/cache/cache.plugin.js.map +1 -1
- package/src/cache/providers/cache-memory.provider.js +2 -1
- package/src/cache/providers/cache-memory.provider.js.map +1 -1
- package/src/cache/providers/cache-redis.provider.js +1 -0
- package/src/cache/providers/cache-redis.provider.js.map +1 -1
- package/src/codecall/README.md +999 -0
- package/src/codecall/codecall.plugin.d.ts +41 -0
- package/src/codecall/codecall.plugin.js +152 -0
- package/src/codecall/codecall.plugin.js.map +1 -0
- package/src/codecall/codecall.symbol.d.ts +106 -0
- package/src/codecall/codecall.symbol.js +4 -0
- package/src/codecall/codecall.symbol.js.map +1 -0
- package/src/codecall/codecall.types.d.ts +289 -0
- package/src/codecall/codecall.types.js +258 -0
- package/src/codecall/codecall.types.js.map +1 -0
- package/src/codecall/errors/index.d.ts +1 -0
- package/src/codecall/errors/index.js +6 -0
- package/src/codecall/errors/index.js.map +1 -0
- package/src/codecall/errors/tool-call.errors.d.ts +79 -0
- package/src/codecall/errors/tool-call.errors.js +119 -0
- package/src/codecall/errors/tool-call.errors.js.map +1 -0
- package/src/codecall/index.d.ts +2 -0
- package/src/codecall/index.js +8 -0
- package/src/codecall/index.js.map +1 -0
- package/src/codecall/providers/code-call.config.d.ts +29 -0
- package/src/codecall/providers/code-call.config.js +120 -0
- package/src/codecall/providers/code-call.config.js.map +1 -0
- package/src/codecall/security/index.d.ts +2 -0
- package/src/codecall/security/index.js +7 -0
- package/src/codecall/security/index.js.map +1 -0
- package/src/codecall/security/self-reference-guard.d.ts +32 -0
- package/src/codecall/security/self-reference-guard.js +70 -0
- package/src/codecall/security/self-reference-guard.js.map +1 -0
- package/src/codecall/security/tool-access-control.service.d.ts +104 -0
- package/src/codecall/security/tool-access-control.service.js +170 -0
- package/src/codecall/security/tool-access-control.service.js.map +1 -0
- package/src/codecall/services/audit-logger.service.d.ts +186 -0
- package/src/codecall/services/audit-logger.service.js +322 -0
- package/src/codecall/services/audit-logger.service.js.map +1 -0
- package/src/codecall/services/enclave.service.d.ts +62 -0
- package/src/codecall/services/enclave.service.js +214 -0
- package/src/codecall/services/enclave.service.js.map +1 -0
- package/src/codecall/services/error-enrichment.service.d.ts +94 -0
- package/src/codecall/services/error-enrichment.service.js +387 -0
- package/src/codecall/services/error-enrichment.service.js.map +1 -0
- package/src/codecall/services/index.d.ts +6 -0
- package/src/codecall/services/index.js +13 -0
- package/src/codecall/services/index.js.map +1 -0
- package/src/codecall/services/output-sanitizer.d.ts +86 -0
- package/src/codecall/services/output-sanitizer.js +260 -0
- package/src/codecall/services/output-sanitizer.js.map +1 -0
- package/src/codecall/services/synonym-expansion.service.d.ts +66 -0
- package/src/codecall/services/synonym-expansion.service.js +374 -0
- package/src/codecall/services/synonym-expansion.service.js.map +1 -0
- package/src/codecall/services/tool-search.service.d.ts +175 -0
- package/src/codecall/services/tool-search.service.js +587 -0
- package/src/codecall/services/tool-search.service.js.map +1 -0
- package/src/codecall/tools/describe.schema.d.ts +28 -0
- package/src/codecall/tools/describe.schema.js +67 -0
- package/src/codecall/tools/describe.schema.js.map +1 -0
- package/src/codecall/tools/describe.tool.d.ts +35 -0
- package/src/codecall/tools/describe.tool.js +207 -0
- package/src/codecall/tools/describe.tool.js.map +1 -0
- package/src/codecall/tools/execute.schema.d.ts +115 -0
- package/src/codecall/tools/execute.schema.js +116 -0
- package/src/codecall/tools/execute.schema.js.map +1 -0
- package/src/codecall/tools/execute.tool.d.ts +5 -0
- package/src/codecall/tools/execute.tool.js +238 -0
- package/src/codecall/tools/execute.tool.js.map +1 -0
- package/src/codecall/tools/index.d.ts +4 -0
- package/src/codecall/tools/index.js +13 -0
- package/src/codecall/tools/index.js.map +1 -0
- package/src/codecall/tools/invoke.schema.d.ts +99 -0
- package/src/codecall/tools/invoke.schema.js +27 -0
- package/src/codecall/tools/invoke.schema.js.map +1 -0
- package/src/codecall/tools/invoke.tool.d.ts +13 -0
- package/src/codecall/tools/invoke.tool.js +70 -0
- package/src/codecall/tools/invoke.tool.js.map +1 -0
- package/src/codecall/tools/search.schema.d.ts +30 -0
- package/src/codecall/tools/search.schema.js +60 -0
- package/src/codecall/tools/search.schema.js.map +1 -0
- package/src/codecall/tools/search.tool.d.ts +5 -0
- package/src/codecall/tools/search.tool.js +108 -0
- package/src/codecall/tools/search.tool.js.map +1 -0
- package/src/codecall/utils/describe.utils.d.ts +86 -0
- package/src/codecall/utils/describe.utils.js +531 -0
- package/src/codecall/utils/describe.utils.js.map +1 -0
- package/src/codecall/utils/index.d.ts +2 -0
- package/src/codecall/utils/index.js +7 -0
- package/src/codecall/utils/index.js.map +1 -0
- package/src/codecall/utils/mcp-result.d.ts +6 -0
- package/src/codecall/utils/mcp-result.js +36 -0
- package/src/codecall/utils/mcp-result.js.map +1 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -1
- package/src/index.js.map +1 -1
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// file: libs/plugins/src/codecall/services/enclave.service.ts
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ScriptTooLargeError = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const sdk_1 = require("@frontmcp/sdk");
|
|
7
|
+
const enclave_vm_1 = require("enclave-vm");
|
|
8
|
+
/**
|
|
9
|
+
* Service for executing AgentScript code using enclave-vm
|
|
10
|
+
*
|
|
11
|
+
* This service wraps the Enclave class and provides:
|
|
12
|
+
* - Safe AgentScript execution with AST validation
|
|
13
|
+
* - Automatic code transformation (callTool -> __safe_callTool)
|
|
14
|
+
* - Runtime limits (timeout, iterations, tool calls)
|
|
15
|
+
* - Tool call integration with FrontMCP pipeline
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Error thrown when script exceeds maximum length and sidecar is disabled
|
|
19
|
+
*/
|
|
20
|
+
class ScriptTooLargeError extends Error {
|
|
21
|
+
code = 'SCRIPT_TOO_LARGE';
|
|
22
|
+
scriptLength;
|
|
23
|
+
maxLength;
|
|
24
|
+
constructor(scriptLength, maxLength) {
|
|
25
|
+
super(`Script length (${scriptLength} characters) exceeds maximum allowed length (${maxLength} characters). ` +
|
|
26
|
+
`Enable sidecar to handle large data, or reduce script size.`);
|
|
27
|
+
this.name = 'ScriptTooLargeError';
|
|
28
|
+
this.scriptLength = scriptLength;
|
|
29
|
+
this.maxLength = maxLength;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.ScriptTooLargeError = ScriptTooLargeError;
|
|
33
|
+
let EnclaveService = class EnclaveService {
|
|
34
|
+
vmOptions;
|
|
35
|
+
sidecarOptions;
|
|
36
|
+
constructor(config) {
|
|
37
|
+
// Use getAll() to avoid deep type instantiation with DottedPath<T>
|
|
38
|
+
const all = config.getAll();
|
|
39
|
+
this.vmOptions = all.resolvedVm;
|
|
40
|
+
this.sidecarOptions = all.sidecar;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Execute AgentScript code in the enclave
|
|
44
|
+
*
|
|
45
|
+
* @param code - The AgentScript code to execute (raw, not transformed)
|
|
46
|
+
* @param environment - The VM environment with callTool, getTool, etc.
|
|
47
|
+
* @returns Execution result with success/error and logs
|
|
48
|
+
* @throws ScriptTooLargeError if script exceeds max length and sidecar is disabled
|
|
49
|
+
*/
|
|
50
|
+
async execute(code, environment) {
|
|
51
|
+
const logs = [];
|
|
52
|
+
// Validate script length when sidecar is disabled
|
|
53
|
+
if (!this.sidecarOptions.enabled && this.sidecarOptions.maxScriptLengthWhenDisabled !== null) {
|
|
54
|
+
const maxLength = this.sidecarOptions.maxScriptLengthWhenDisabled;
|
|
55
|
+
if (code.length > maxLength) {
|
|
56
|
+
throw new ScriptTooLargeError(code.length, maxLength);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Create tool handler that bridges to CodeCallVmEnvironment
|
|
60
|
+
const toolHandler = async (toolName, args) => {
|
|
61
|
+
return environment.callTool(toolName, args);
|
|
62
|
+
};
|
|
63
|
+
// Build sidecar configuration if enabled
|
|
64
|
+
const sidecar = this.sidecarOptions.enabled
|
|
65
|
+
? {
|
|
66
|
+
enabled: true,
|
|
67
|
+
maxTotalSize: this.sidecarOptions.maxTotalSize,
|
|
68
|
+
maxReferenceSize: this.sidecarOptions.maxReferenceSize,
|
|
69
|
+
extractionThreshold: this.sidecarOptions.extractionThreshold,
|
|
70
|
+
maxResolvedSize: this.sidecarOptions.maxResolvedSize,
|
|
71
|
+
allowComposites: this.sidecarOptions.allowComposites,
|
|
72
|
+
}
|
|
73
|
+
: undefined;
|
|
74
|
+
// Create enclave with configuration from CodeCallConfig
|
|
75
|
+
const enclave = new enclave_vm_1.Enclave({
|
|
76
|
+
timeout: this.vmOptions.timeoutMs,
|
|
77
|
+
maxToolCalls: this.vmOptions.maxSteps || 100,
|
|
78
|
+
maxIterations: 10000,
|
|
79
|
+
toolHandler,
|
|
80
|
+
validate: true,
|
|
81
|
+
transform: true,
|
|
82
|
+
sidecar,
|
|
83
|
+
// Allow functions in globals since we intentionally provide getTool, mcpLog, mcpNotify, and console
|
|
84
|
+
allowFunctionsInGlobals: true,
|
|
85
|
+
globals: {
|
|
86
|
+
// Provide getTool as a custom global
|
|
87
|
+
getTool: environment.getTool,
|
|
88
|
+
// Provide logging functions if available
|
|
89
|
+
...(environment.mcpLog
|
|
90
|
+
? {
|
|
91
|
+
mcpLog: (level, message, metadata) => {
|
|
92
|
+
environment.mcpLog(level, message, metadata);
|
|
93
|
+
logs.push(`[mcp:${level}] ${message}`);
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
: {}),
|
|
97
|
+
...(environment.mcpNotify
|
|
98
|
+
? {
|
|
99
|
+
mcpNotify: (event, payload) => {
|
|
100
|
+
environment.mcpNotify(event, payload);
|
|
101
|
+
logs.push(`[notify] ${event}`);
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
: {}),
|
|
105
|
+
// Provide console if allowed
|
|
106
|
+
...(this.vmOptions.allowConsole
|
|
107
|
+
? {
|
|
108
|
+
console: {
|
|
109
|
+
log: (...args) => {
|
|
110
|
+
const message = args.map((arg) => String(arg)).join(' ');
|
|
111
|
+
logs.push(`[log] ${message}`);
|
|
112
|
+
},
|
|
113
|
+
warn: (...args) => {
|
|
114
|
+
const message = args.map((arg) => String(arg)).join(' ');
|
|
115
|
+
logs.push(`[warn] ${message}`);
|
|
116
|
+
},
|
|
117
|
+
error: (...args) => {
|
|
118
|
+
const message = args.map((arg) => String(arg)).join(' ');
|
|
119
|
+
logs.push(`[error] ${message}`);
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
}
|
|
123
|
+
: {}),
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
try {
|
|
127
|
+
const result = await enclave.run(code);
|
|
128
|
+
return this.mapEnclaveResult(result, logs);
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
enclave.dispose();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Map Enclave ExecutionResult to EnclaveExecutionResult
|
|
136
|
+
*/
|
|
137
|
+
mapEnclaveResult(result, logs) {
|
|
138
|
+
if (result.success) {
|
|
139
|
+
return {
|
|
140
|
+
success: true,
|
|
141
|
+
result: result.value,
|
|
142
|
+
logs,
|
|
143
|
+
timedOut: false,
|
|
144
|
+
stats: {
|
|
145
|
+
duration: result.stats.duration,
|
|
146
|
+
toolCallCount: result.stats.toolCallCount,
|
|
147
|
+
iterationCount: result.stats.iterationCount,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// Handle error cases
|
|
152
|
+
const error = result.error;
|
|
153
|
+
const timedOut = error.message?.includes('timed out') || error.code === 'TIMEOUT';
|
|
154
|
+
// Check if it's a validation error
|
|
155
|
+
if (error.code === 'VALIDATION_ERROR') {
|
|
156
|
+
return {
|
|
157
|
+
success: false,
|
|
158
|
+
error: {
|
|
159
|
+
message: error.message,
|
|
160
|
+
name: 'ValidationError',
|
|
161
|
+
code: error.code,
|
|
162
|
+
},
|
|
163
|
+
logs,
|
|
164
|
+
timedOut: false,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
// Check if it's a tool error (has toolName in the error data)
|
|
168
|
+
const errorData = error.data;
|
|
169
|
+
const toolName = errorData?.['toolName'];
|
|
170
|
+
if (toolName) {
|
|
171
|
+
return {
|
|
172
|
+
success: false,
|
|
173
|
+
error: {
|
|
174
|
+
message: error.message,
|
|
175
|
+
name: error.name,
|
|
176
|
+
stack: error.stack,
|
|
177
|
+
code: error.code,
|
|
178
|
+
toolName,
|
|
179
|
+
toolInput: errorData?.['toolInput'],
|
|
180
|
+
details: errorData?.['details'],
|
|
181
|
+
},
|
|
182
|
+
logs,
|
|
183
|
+
timedOut,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
// Generic error
|
|
187
|
+
return {
|
|
188
|
+
success: false,
|
|
189
|
+
error: {
|
|
190
|
+
message: error.message,
|
|
191
|
+
name: error.name,
|
|
192
|
+
stack: error.stack,
|
|
193
|
+
code: error.code,
|
|
194
|
+
},
|
|
195
|
+
logs,
|
|
196
|
+
timedOut,
|
|
197
|
+
stats: {
|
|
198
|
+
duration: result.stats.duration,
|
|
199
|
+
toolCallCount: result.stats.toolCallCount,
|
|
200
|
+
iterationCount: result.stats.iterationCount,
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
EnclaveService = tslib_1.__decorate([
|
|
206
|
+
(0, sdk_1.Provider)({
|
|
207
|
+
name: 'codecall:enclave',
|
|
208
|
+
description: 'Executes AgentScript code in a secure enclave',
|
|
209
|
+
scope: sdk_1.ProviderScope.GLOBAL,
|
|
210
|
+
}),
|
|
211
|
+
tslib_1.__metadata("design:paramtypes", [Function])
|
|
212
|
+
], EnclaveService);
|
|
213
|
+
exports.default = EnclaveService;
|
|
214
|
+
//# sourceMappingURL=enclave.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enclave.service.js","sourceRoot":"","sources":["../../../../src/codecall/services/enclave.service.ts"],"names":[],"mappings":";AAAA,8DAA8D;;;;AAE9D,uCAAwD;AACxD,2CAA2G;AA8B3G;;;;;;;;GAQG;AACH;;GAEG;AACH,MAAa,mBAAoB,SAAQ,KAAK;IACnC,IAAI,GAAG,kBAAkB,CAAC;IAC1B,YAAY,CAAS;IACrB,SAAS,CAAS;IAE3B,YAAY,YAAoB,EAAE,SAAiB;QACjD,KAAK,CACH,kBAAkB,YAAY,gDAAgD,SAAS,gBAAgB;YACrG,6DAA6D,CAChE,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;CACF;AAdD,kDAcC;AAOc,IAAM,cAAc,GAApB,MAAM,cAAc;IAChB,SAAS,CAA4B;IACrC,cAAc,CAAyB;IAExD,YAAY,MAAsB;QAChC,mEAAmE;QACnE,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QAC5B,IAAI,CAAC,SAAS,GAAG,GAAG,CAAC,UAAU,CAAC;QAChC,IAAI,CAAC,cAAc,GAAG,GAAG,CAAC,OAAO,CAAC;IACpC,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,OAAO,CAAC,IAAY,EAAE,WAAkC;QAC5D,MAAM,IAAI,GAAa,EAAE,CAAC;QAE1B,kDAAkD;QAClD,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,IAAI,IAAI,CAAC,cAAc,CAAC,2BAA2B,KAAK,IAAI,EAAE,CAAC;YAC7F,MAAM,SAAS,GAAG,IAAI,CAAC,cAAc,CAAC,2BAA2B,CAAC;YAClE,I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KAC5B,CAAC;;GACmB,cAAc,CA0LlC;kBA1LoB,cAAc","sourcesContent":["// file: libs/plugins/src/codecall/services/enclave.service.ts\n\nimport { Provider, ProviderScope } from '@frontmcp/sdk';\nimport { Enclave, type ExecutionResult, type ToolHandler, type ReferenceSidecarOptions } from 'enclave-vm';\nimport type CodeCallConfig from '../providers/code-call.config';\nimport type { CodeCallVmEnvironment, ResolvedCodeCallVmOptions } from '../codecall.symbol';\nimport type { CodeCallSidecarOptions } from '../codecall.types';\n\n/**\n * Result from enclave execution - maps to existing VmExecutionResult interface\n */\nexport interface EnclaveExecutionResult {\n success: boolean;\n result?: unknown;\n error?: {\n message: string;\n name: string;\n stack?: string;\n code?: string;\n toolName?: string;\n toolInput?: unknown;\n details?: unknown;\n [key: string]: unknown;\n };\n logs: string[];\n timedOut: boolean;\n stats?: {\n duration: number;\n toolCallCount: number;\n iterationCount: number;\n };\n}\n\n/**\n * Service for executing AgentScript code using enclave-vm\n *\n * This service wraps the Enclave class and provides:\n * - Safe AgentScript execution with AST validation\n * - Automatic code transformation (callTool -> __safe_callTool)\n * - Runtime limits (timeout, iterations, tool calls)\n * - Tool call integration with FrontMCP pipeline\n */\n/**\n * Error thrown when script exceeds maximum length and sidecar is disabled\n */\nexport class ScriptTooLargeError extends Error {\n readonly code = 'SCRIPT_TOO_LARGE';\n readonly scriptLength: number;\n readonly maxLength: number;\n\n constructor(scriptLength: number, maxLength: number) {\n super(\n `Script length (${scriptLength} characters) exceeds maximum allowed length (${maxLength} characters). ` +\n `Enable sidecar to handle large data, or reduce script size.`,\n );\n this.name = 'ScriptTooLargeError';\n this.scriptLength = scriptLength;\n this.maxLength = maxLength;\n }\n}\n\n@Provider({\n name: 'codecall:enclave',\n description: 'Executes AgentScript code in a secure enclave',\n scope: ProviderScope.GLOBAL,\n})\nexport default class EnclaveService {\n private readonly vmOptions: ResolvedCodeCallVmOptions;\n private readonly sidecarOptions: CodeCallSidecarOptions;\n\n constructor(config: CodeCallConfig) {\n // Use getAll() to avoid deep type instantiation with DottedPath<T>\n const all = config.getAll();\n this.vmOptions = all.resolvedVm;\n this.sidecarOptions = all.sidecar;\n }\n\n /**\n * Execute AgentScript code in the enclave\n *\n * @param code - The AgentScript code to execute (raw, not transformed)\n * @param environment - The VM environment with callTool, getTool, etc.\n * @returns Execution result with success/error and logs\n * @throws ScriptTooLargeError if script exceeds max length and sidecar is disabled\n */\n async execute(code: string, environment: CodeCallVmEnvironment): Promise<EnclaveExecutionResult> {\n const logs: string[] = [];\n\n // Validate script length when sidecar is disabled\n if (!this.sidecarOptions.enabled && this.sidecarOptions.maxScriptLengthWhenDisabled !== null) {\n const maxLength = this.sidecarOptions.maxScriptLengthWhenDisabled;\n if (code.length > maxLength) {\n throw new ScriptTooLargeError(code.length, maxLength);\n }\n }\n\n // Create tool handler that bridges to CodeCallVmEnvironment\n const toolHandler: ToolHandler = async (toolName: string, args: Record<string, unknown>) => {\n return environment.callTool(toolName, args);\n };\n\n // Build sidecar configuration if enabled\n const sidecar: ReferenceSidecarOptions | undefined = this.sidecarOptions.enabled\n ? {\n enabled: true,\n maxTotalSize: this.sidecarOptions.maxTotalSize,\n maxReferenceSize: this.sidecarOptions.maxReferenceSize,\n extractionThreshold: this.sidecarOptions.extractionThreshold,\n maxResolvedSize: this.sidecarOptions.maxResolvedSize,\n allowComposites: this.sidecarOptions.allowComposites,\n }\n : undefined;\n\n // Create enclave with configuration from CodeCallConfig\n const enclave = new Enclave({\n timeout: this.vmOptions.timeoutMs,\n maxToolCalls: this.vmOptions.maxSteps || 100,\n maxIterations: 10000,\n toolHandler,\n validate: true,\n transform: true,\n sidecar,\n // Allow functions in globals since we intentionally provide getTool, mcpLog, mcpNotify, and console\n allowFunctionsInGlobals: true,\n globals: {\n // Provide getTool as a custom global\n getTool: environment.getTool,\n // Provide logging functions if available\n ...(environment.mcpLog\n ? {\n mcpLog: (\n level: 'debug' | 'info' | 'warn' | 'error',\n message: string,\n metadata?: Record<string, unknown>,\n ) => {\n environment.mcpLog!(level, message, metadata);\n logs.push(`[mcp:${level}] ${message}`);\n },\n }\n : {}),\n ...(environment.mcpNotify\n ? {\n mcpNotify: (event: string, payload: Record<string, unknown>) => {\n environment.mcpNotify!(event, payload);\n logs.push(`[notify] ${event}`);\n },\n }\n : {}),\n // Provide console if allowed\n ...(this.vmOptions.allowConsole\n ? {\n console: {\n log: (...args: unknown[]) => {\n const message = args.map((arg) => String(arg)).join(' ');\n logs.push(`[log] ${message}`);\n },\n warn: (...args: unknown[]) => {\n const message = args.map((arg) => String(arg)).join(' ');\n logs.push(`[warn] ${message}`);\n },\n error: (...args: unknown[]) => {\n const message = args.map((arg) => String(arg)).join(' ');\n logs.push(`[error] ${message}`);\n },\n },\n }\n : {}),\n },\n });\n\n try {\n const result = await enclave.run<unknown>(code);\n return this.mapEnclaveResult(result, logs);\n } finally {\n enclave.dispose();\n }\n }\n\n /**\n * Map Enclave ExecutionResult to EnclaveExecutionResult\n */\n private mapEnclaveResult(result: ExecutionResult<unknown>, logs: string[]): EnclaveExecutionResult {\n if (result.success) {\n return {\n success: true,\n result: result.value,\n logs,\n timedOut: false,\n stats: {\n duration: result.stats.duration,\n toolCallCount: result.stats.toolCallCount,\n iterationCount: result.stats.iterationCount,\n },\n };\n }\n\n // Handle error cases\n const error = result.error!;\n const timedOut = error.message?.includes('timed out') || error.code === 'TIMEOUT';\n\n // Check if it's a validation error\n if (error.code === 'VALIDATION_ERROR') {\n return {\n success: false,\n error: {\n message: error.message,\n name: 'ValidationError',\n code: error.code,\n },\n logs,\n timedOut: false,\n };\n }\n\n // Check if it's a tool error (has toolName in the error data)\n const errorData = error.data as Record<string, unknown> | undefined;\n const toolName = errorData?.['toolName'] as string | undefined;\n if (toolName) {\n return {\n success: false,\n error: {\n message: error.message,\n name: error.name,\n stack: error.stack,\n code: error.code,\n toolName,\n toolInput: errorData?.['toolInput'],\n details: errorData?.['details'],\n },\n logs,\n timedOut,\n };\n }\n\n // Generic error\n return {\n success: false,\n error: {\n message: error.message,\n name: error.name,\n stack: error.stack,\n code: error.code,\n },\n logs,\n timedOut,\n stats: {\n duration: result.stats.duration,\n toolCallCount: result.stats.toolCallCount,\n iterationCount: result.stats.iterationCount,\n },\n };\n }\n}\n"]}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error categories for classification.
|
|
3
|
+
*/
|
|
4
|
+
export declare const ERROR_CATEGORIES: {
|
|
5
|
+
/** Script syntax or parsing error */
|
|
6
|
+
readonly SYNTAX: "syntax";
|
|
7
|
+
/** AST validation blocked dangerous code */
|
|
8
|
+
readonly SECURITY: "security";
|
|
9
|
+
/** Script exceeded timeout */
|
|
10
|
+
readonly TIMEOUT: "timeout";
|
|
11
|
+
/** Tool not found */
|
|
12
|
+
readonly TOOL_NOT_FOUND: "tool_not_found";
|
|
13
|
+
/** Tool access denied */
|
|
14
|
+
readonly TOOL_ACCESS_DENIED: "tool_access_denied";
|
|
15
|
+
/** Tool validation error */
|
|
16
|
+
readonly TOOL_VALIDATION: "tool_validation";
|
|
17
|
+
/** Tool execution error */
|
|
18
|
+
readonly TOOL_EXECUTION: "tool_execution";
|
|
19
|
+
/** Runtime error in script */
|
|
20
|
+
readonly RUNTIME: "runtime";
|
|
21
|
+
/** Unknown error */
|
|
22
|
+
readonly UNKNOWN: "unknown";
|
|
23
|
+
};
|
|
24
|
+
export type ErrorCategory = (typeof ERROR_CATEGORIES)[keyof typeof ERROR_CATEGORIES];
|
|
25
|
+
/**
|
|
26
|
+
* Enriched error with actionable suggestions.
|
|
27
|
+
*/
|
|
28
|
+
export interface EnrichedError {
|
|
29
|
+
/** Error category */
|
|
30
|
+
category: ErrorCategory;
|
|
31
|
+
/** User-friendly error message */
|
|
32
|
+
message: string;
|
|
33
|
+
/** Actionable suggestions for fixing the error */
|
|
34
|
+
suggestions: string[];
|
|
35
|
+
/** Related documentation links */
|
|
36
|
+
docs?: string[];
|
|
37
|
+
/** Example of correct usage (if applicable) */
|
|
38
|
+
example?: string;
|
|
39
|
+
/** Original error code (if available) */
|
|
40
|
+
code?: string;
|
|
41
|
+
/** Whether the error is recoverable */
|
|
42
|
+
recoverable: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Error Enrichment Service
|
|
46
|
+
*
|
|
47
|
+
* Transforms raw errors into user-friendly, actionable error messages.
|
|
48
|
+
* Provides suggestions for fixing common errors and links to documentation.
|
|
49
|
+
*
|
|
50
|
+
* Security: Never exposes internal details, only provides helpful guidance.
|
|
51
|
+
*/
|
|
52
|
+
export declare class ErrorEnrichmentService {
|
|
53
|
+
/**
|
|
54
|
+
* Enrich an error with category, suggestions, and examples.
|
|
55
|
+
*
|
|
56
|
+
* @param error - The error to enrich (Error object, string, or unknown)
|
|
57
|
+
* @param context - Optional context for more specific suggestions
|
|
58
|
+
* @returns Enriched error with actionable information
|
|
59
|
+
*/
|
|
60
|
+
enrich(error: unknown, context?: {
|
|
61
|
+
toolName?: string;
|
|
62
|
+
scriptSnippet?: string;
|
|
63
|
+
}): EnrichedError;
|
|
64
|
+
/**
|
|
65
|
+
* Enrich a tool-specific error.
|
|
66
|
+
*/
|
|
67
|
+
enrichToolError(toolName: string, errorCode: string, rawMessage?: string): EnrichedError;
|
|
68
|
+
/**
|
|
69
|
+
* Create a brief error summary for logging.
|
|
70
|
+
*/
|
|
71
|
+
summarize(error: unknown): string;
|
|
72
|
+
/**
|
|
73
|
+
* Extract error message from various error types.
|
|
74
|
+
*/
|
|
75
|
+
private extractMessage;
|
|
76
|
+
/**
|
|
77
|
+
* Extract error code if available.
|
|
78
|
+
*/
|
|
79
|
+
private extractCode;
|
|
80
|
+
/**
|
|
81
|
+
* Format error message for user consumption.
|
|
82
|
+
* Removes technical details while keeping useful information.
|
|
83
|
+
*/
|
|
84
|
+
private formatMessage;
|
|
85
|
+
/**
|
|
86
|
+
* Get a human-readable prefix for error category.
|
|
87
|
+
*/
|
|
88
|
+
private getCategoryPrefix;
|
|
89
|
+
/**
|
|
90
|
+
* Contextualize suggestions based on context.
|
|
91
|
+
*/
|
|
92
|
+
private contextualizeSuggestions;
|
|
93
|
+
}
|
|
94
|
+
export default ErrorEnrichmentService;
|