@aragon-agent/core 0.2.92 → 0.2.94
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/dist/engine/accept-steering.js +1 -15
- package/dist/engine/agent-loop.js +1 -533
- package/dist/engine/agent.js +1 -538
- package/dist/engine/compaction.js +1 -268
- package/dist/engine/context-manager.js +1 -13
- package/dist/engine/message-manager.js +1 -39
- package/dist/engine/steering.js +1 -72
- package/dist/engine/tool-result-content.js +1 -39
- package/dist/engine/watchdog.js +1 -73
- package/dist/index.js +1 -99
- package/dist/llm/context-window.js +1 -46
- package/dist/llm/image-tokens.js +1 -11
- package/dist/llm/model-discovery.js +1 -18
- package/dist/llm/model-registry.js +1 -370
- package/dist/llm/output-limit-recovery.js +1 -263
- package/dist/llm/output-limits.js +1 -283
- package/dist/llm/provider.js +1 -171
- package/dist/llm/providers/anthropic.js +1 -514
- package/dist/llm/providers/google.js +1 -464
- package/dist/llm/providers/index.js +1 -205
- package/dist/llm/providers/openai.js +1 -520
- package/dist/llm/providers/tool-result-images.js +1 -52
- package/dist/llm/retry.js +1 -948
- package/dist/llm/sse-parser.js +1 -103
- package/dist/llm/stream-utils.js +1 -118
- package/dist/llm/types.js +1 -17
- package/dist/sandbox/console-capture.js +1 -56
- package/dist/sandbox/http-bridge.js +1 -143
- package/dist/sandbox/isolated-vm-sandbox.js +1 -226
- package/dist/sandbox/tool-bridge.js +1 -98
- package/dist/sandbox/types.js +1 -8
- package/dist/skills/constants.js +1 -179
- package/dist/skills/disclosure.js +1 -788
- package/dist/skills/frontmatter.js +1 -177
- package/dist/skills/index.js +1 -18
- package/dist/skills/skill-find-tool.js +1 -117
- package/dist/skills/skill-registry.js +1 -208
- package/dist/skills/skill-tool.js +1 -96
- package/dist/skills/tool-policy.js +1 -272
- package/dist/skills/types.js +1 -8
- package/dist/skills/validate.js +1 -264
- package/dist/tools/executor.js +1 -281
- package/dist/tools/helpers.js +1 -87
- package/dist/tools/registry.js +1 -119
- package/dist/tools/types.js +1 -7
- package/dist/tools/validator.js +1 -210
- package/dist/types.js +1 -7
- package/package.json +3 -2
package/dist/skills/validate.js
CHANGED
|
@@ -1,264 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Skill validation — frontmatter contract (§4.4) and staged-install safety (§8.3).
|
|
3
|
-
*
|
|
4
|
-
* Pure functions only: no filesystem, no process. The staged validator takes a
|
|
5
|
-
* pre-listed file table so the same rules run identically over a real staging
|
|
6
|
-
* directory (CLI) and over a fixture (tests).
|
|
7
|
-
*/
|
|
8
|
-
import { SKILL_ALLOWED_TOOLS_MAX, SKILL_DESC_MAX, SKILL_KEYWORDS_MAX, SKILL_KEYWORD_LEN_MAX, SKILL_META_MAX, SKILL_NAME_MAX, SKILL_NAME_PATTERN, STAGED_MAX_DEPTH, STAGED_MAX_FILE_BYTES, STAGED_MAX_FILES, STAGED_MAX_TOTAL_BYTES, WINDOWS_RESERVED_NAMES, } from './constants.js';
|
|
9
|
-
const ACTIVATIONS = ['auto', 'always', 'manual'];
|
|
10
|
-
const SEMVER = /^\d+\.\d+\.\d+(?:[-+].+)?$/;
|
|
11
|
-
/**
|
|
12
|
-
* Characters that let a description escape the `<available_skills>` block.
|
|
13
|
-
*
|
|
14
|
-
* WARN, NOT ERROR — and that asymmetry is load-bearing (§4.4). Community skills
|
|
15
|
-
* under `.claude/skills` legitimately write `<div>` in a description; failing
|
|
16
|
-
* them would break interop (D8). Escaping is enforced UNCONDITIONALLY at every
|
|
17
|
-
* render point by `sanitizeForPromptBlock()`; validation only tells the user.
|
|
18
|
-
* Never let "validation passed" become a licence to skip sanitizing.
|
|
19
|
-
*/
|
|
20
|
-
function isControlCode(code) {
|
|
21
|
-
return code < 0x20 || code === 0x7f;
|
|
22
|
-
}
|
|
23
|
-
/** True when `s` holds a C0/DEL control character. */
|
|
24
|
-
export function hasControlChar(s) {
|
|
25
|
-
for (let i = 0; i < s.length; i += 1) {
|
|
26
|
-
if (isControlCode(s.charCodeAt(i)))
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
/** True when `s` holds an angle bracket or a control character. */
|
|
32
|
-
function hasAngleOrControl(s) {
|
|
33
|
-
for (let i = 0; i < s.length; i += 1) {
|
|
34
|
-
const code = s.charCodeAt(i);
|
|
35
|
-
if (code === 0x3c || code === 0x3e || isControlCode(code))
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
function asString(v) {
|
|
41
|
-
if (typeof v === 'string')
|
|
42
|
-
return v;
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
function asArray(v) {
|
|
46
|
-
if (Array.isArray(v))
|
|
47
|
-
return v;
|
|
48
|
-
if (typeof v === 'string' && v.trim().length > 0)
|
|
49
|
-
return [v.trim()];
|
|
50
|
-
return undefined;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Validate a parsed frontmatter map against the §4.2 contract.
|
|
54
|
-
*
|
|
55
|
-
* Error codes are a stable public surface — the UI and the tests both key off
|
|
56
|
-
* them, so renaming one is a breaking change.
|
|
57
|
-
*/
|
|
58
|
-
export function validateSkillFrontmatter(data, dirName) {
|
|
59
|
-
const issues = [];
|
|
60
|
-
const err = (code, message) => {
|
|
61
|
-
issues.push({ level: 'error', code, message });
|
|
62
|
-
};
|
|
63
|
-
const warn = (code, message) => {
|
|
64
|
-
issues.push({ level: 'warn', code, message });
|
|
65
|
-
};
|
|
66
|
-
const name = asString(data.name)?.trim();
|
|
67
|
-
if (!name) {
|
|
68
|
-
err('SKILL_NAME_MISSING', 'name is required');
|
|
69
|
-
}
|
|
70
|
-
else if (!SKILL_NAME_PATTERN.test(name) || name.length > SKILL_NAME_MAX) {
|
|
71
|
-
err('SKILL_NAME_INVALID', `name "${name}" must be kebab-case ([a-z0-9-], no leading/trailing/double dash) and at most ${SKILL_NAME_MAX} characters`);
|
|
72
|
-
}
|
|
73
|
-
else if (name !== dirName) {
|
|
74
|
-
warn('SKILL_NAME_DIR_MISMATCH', `frontmatter name "${name}" does not match directory "${dirName}"`);
|
|
75
|
-
}
|
|
76
|
-
const description = asString(data.description)?.trim();
|
|
77
|
-
if (!description) {
|
|
78
|
-
err('SKILL_DESC_MISSING', 'description is required');
|
|
79
|
-
}
|
|
80
|
-
else if (description.length > SKILL_DESC_MAX) {
|
|
81
|
-
err('SKILL_DESC_TOO_LONG', `description is ${description.length} characters (max ${SKILL_DESC_MAX})`);
|
|
82
|
-
}
|
|
83
|
-
else if (hasAngleOrControl(description)) {
|
|
84
|
-
warn('SKILL_DESC_UNSAFE_CHARS', 'description contains angle brackets or control characters; they will be neutralized before reaching the model');
|
|
85
|
-
}
|
|
86
|
-
const version = asString(data.version)?.trim();
|
|
87
|
-
if (version && !SEMVER.test(version)) {
|
|
88
|
-
warn('SKILL_VERSION_INVALID', `version "${version}" is not semver; falling back to 0.0.0`);
|
|
89
|
-
}
|
|
90
|
-
const activation = asString(data.activation)?.trim();
|
|
91
|
-
if (activation && !ACTIVATIONS.includes(activation)) {
|
|
92
|
-
warn('SKILL_ACTIVATION_INVALID', `unknown activation "${activation}"; falling back to auto`);
|
|
93
|
-
}
|
|
94
|
-
const allowedTools = data['allowed-tools'] ?? data.allowedTools;
|
|
95
|
-
if (allowedTools !== undefined && !Array.isArray(allowedTools) && typeof allowedTools !== 'string') {
|
|
96
|
-
warn('SKILL_ALLOWED_TOOLS_INVALID', 'allowed-tools must be a list of tool names');
|
|
97
|
-
}
|
|
98
|
-
else if (Array.isArray(allowedTools) && allowedTools.length > SKILL_ALLOWED_TOOLS_MAX) {
|
|
99
|
-
warn('SKILL_ALLOWED_TOOLS_INVALID', `allowed-tools has more than ${SKILL_ALLOWED_TOOLS_MAX} entries`);
|
|
100
|
-
}
|
|
101
|
-
return issues;
|
|
102
|
-
}
|
|
103
|
-
/** Build the normalized frontmatter view, applying every documented default. */
|
|
104
|
-
export function normalizeFrontmatter(data, fallbackName) {
|
|
105
|
-
const activationRaw = asString(data.activation)?.trim();
|
|
106
|
-
const activation = ACTIVATIONS.includes(activationRaw ?? '')
|
|
107
|
-
? activationRaw
|
|
108
|
-
: 'auto';
|
|
109
|
-
const versionRaw = asString(data.version)?.trim();
|
|
110
|
-
const version = versionRaw && SEMVER.test(versionRaw) ? versionRaw : '0.0.0';
|
|
111
|
-
const keywords = (asArray(data.keywords) ?? [])
|
|
112
|
-
.slice(0, SKILL_KEYWORDS_MAX)
|
|
113
|
-
.map((k) => k.slice(0, SKILL_KEYWORD_LEN_MAX));
|
|
114
|
-
const allowedTools = (asArray(data['allowed-tools'] ?? data.allowedTools) ?? []).slice(0, SKILL_ALLOWED_TOOLS_MAX);
|
|
115
|
-
const meta = (key) => {
|
|
116
|
-
const v = asString(data[key])?.trim();
|
|
117
|
-
return v && v.length > 0 ? v.slice(0, SKILL_META_MAX) : undefined;
|
|
118
|
-
};
|
|
119
|
-
return {
|
|
120
|
-
name: asString(data.name)?.trim() || fallbackName,
|
|
121
|
-
description: asString(data.description)?.trim() ?? '',
|
|
122
|
-
version,
|
|
123
|
-
...(meta('license') ? { license: meta('license') } : {}),
|
|
124
|
-
...(meta('author') ? { author: meta('author') } : {}),
|
|
125
|
-
...(meta('homepage') ? { homepage: meta('homepage') } : {}),
|
|
126
|
-
keywords,
|
|
127
|
-
allowedTools,
|
|
128
|
-
activation,
|
|
129
|
-
raw: data,
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
// ---------------------------------------------------------------------------
|
|
133
|
-
// Staged install validation (§8.3 step 2)
|
|
134
|
-
// ---------------------------------------------------------------------------
|
|
135
|
-
/**
|
|
136
|
-
* Reject a relative path that could escape the staging root or is unusable on
|
|
137
|
-
* Windows. This is the SECOND line of defence — the archive extractor applies
|
|
138
|
-
* the same rules while writing (§9.2.1). Both are required: the extractor
|
|
139
|
-
* guards what lands on disk, this guards what we then promote into the skills
|
|
140
|
-
* root (a plain local-directory source never goes through the extractor).
|
|
141
|
-
*/
|
|
142
|
-
export function checkStagedPath(path) {
|
|
143
|
-
const normalized = path.replace(/\\/g, '/');
|
|
144
|
-
if (normalized.length === 0) {
|
|
145
|
-
return { level: 'error', code: 'SKILL_PATH_EMPTY', message: 'empty entry path' };
|
|
146
|
-
}
|
|
147
|
-
if (/^[A-Za-z]:/.test(normalized) || normalized.startsWith('/')) {
|
|
148
|
-
return {
|
|
149
|
-
level: 'error',
|
|
150
|
-
code: 'SKILL_PATH_ABSOLUTE',
|
|
151
|
-
message: `absolute path not allowed: ${path}`,
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
const segments = normalized.split('/').filter((s) => s.length > 0);
|
|
155
|
-
if (segments.some((s) => s === '..')) {
|
|
156
|
-
return {
|
|
157
|
-
level: 'error',
|
|
158
|
-
code: 'SKILL_PATH_TRAVERSAL',
|
|
159
|
-
message: `path escapes the skill directory: ${path}`,
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
if (hasControlChar(normalized)) {
|
|
163
|
-
return {
|
|
164
|
-
level: 'error',
|
|
165
|
-
code: 'SKILL_PATH_CONTROL_CHARS',
|
|
166
|
-
message: `control characters in path: ${JSON.stringify(path)}`,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
for (const segment of segments) {
|
|
170
|
-
const base = (segment.split('.')[0] ?? '').toLowerCase();
|
|
171
|
-
if (WINDOWS_RESERVED_NAMES.has(base)) {
|
|
172
|
-
return {
|
|
173
|
-
level: 'error',
|
|
174
|
-
code: 'SKILL_PATH_RESERVED_NAME',
|
|
175
|
-
message: `Windows reserved name in path: ${path}`,
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
if (segments.length > STAGED_MAX_DEPTH) {
|
|
180
|
-
return {
|
|
181
|
-
level: 'error',
|
|
182
|
-
code: 'SKILL_PATH_TOO_DEEP',
|
|
183
|
-
message: `path nests deeper than ${STAGED_MAX_DEPTH} levels: ${path}`,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Validate a staged skill before it is promoted into a skills root.
|
|
190
|
-
*
|
|
191
|
-
* `entryText` is the raw SKILL.md; `files` is the full staged file table
|
|
192
|
-
* (SKILL.md included). Returns `ok: false` if ANY error-level issue is found —
|
|
193
|
-
* the installer must then abort and leave the target untouched.
|
|
194
|
-
*/
|
|
195
|
-
export function validateStagedSkill(entryText, files, dirName, parse) {
|
|
196
|
-
const issues = [];
|
|
197
|
-
let totalBytes = 0;
|
|
198
|
-
if (entryText === null) {
|
|
199
|
-
issues.push({
|
|
200
|
-
level: 'error',
|
|
201
|
-
code: 'SKILL_MD_MISSING',
|
|
202
|
-
message: 'SKILL.md not found in the source',
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
if (files.length > STAGED_MAX_FILES) {
|
|
206
|
-
issues.push({
|
|
207
|
-
level: 'error',
|
|
208
|
-
code: 'SKILL_TOO_MANY_FILES',
|
|
209
|
-
message: `${files.length} files exceeds the limit of ${STAGED_MAX_FILES}`,
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
for (const file of files) {
|
|
213
|
-
if (file.isSymlink) {
|
|
214
|
-
issues.push({
|
|
215
|
-
level: 'error',
|
|
216
|
-
code: 'SKILL_SYMLINK_REJECTED',
|
|
217
|
-
message: `symbolic links are not allowed: ${file.path}`,
|
|
218
|
-
});
|
|
219
|
-
continue;
|
|
220
|
-
}
|
|
221
|
-
const pathIssue = checkStagedPath(file.path);
|
|
222
|
-
if (pathIssue) {
|
|
223
|
-
issues.push(pathIssue);
|
|
224
|
-
continue;
|
|
225
|
-
}
|
|
226
|
-
if (file.bytes > STAGED_MAX_FILE_BYTES) {
|
|
227
|
-
issues.push({
|
|
228
|
-
level: 'error',
|
|
229
|
-
code: 'SKILL_FILE_TOO_LARGE',
|
|
230
|
-
message: `${file.path} is ${file.bytes} bytes (max ${STAGED_MAX_FILE_BYTES})`,
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
totalBytes += file.bytes;
|
|
234
|
-
}
|
|
235
|
-
if (totalBytes > STAGED_MAX_TOTAL_BYTES) {
|
|
236
|
-
issues.push({
|
|
237
|
-
level: 'error',
|
|
238
|
-
code: 'SKILL_TOTAL_TOO_LARGE',
|
|
239
|
-
message: `total size ${totalBytes} bytes exceeds ${STAGED_MAX_TOTAL_BYTES}`,
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
let frontmatter = null;
|
|
243
|
-
if (entryText !== null) {
|
|
244
|
-
const parsed = parse(entryText);
|
|
245
|
-
if (!parsed) {
|
|
246
|
-
issues.push({
|
|
247
|
-
level: 'error',
|
|
248
|
-
code: 'SKILL_FRONTMATTER_MISSING',
|
|
249
|
-
message: 'SKILL.md has no parseable YAML frontmatter block',
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
issues.push(...validateSkillFrontmatter(parsed.data, dirName));
|
|
254
|
-
frontmatter = normalizeFrontmatter(parsed.data, dirName);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return {
|
|
258
|
-
ok: !issues.some((i) => i.level === 'error'),
|
|
259
|
-
issues,
|
|
260
|
-
frontmatter,
|
|
261
|
-
fileCount: files.length,
|
|
262
|
-
totalBytes,
|
|
263
|
-
};
|
|
264
|
-
}
|
|
1
|
+
const _0x20a6f2=_0x13a5;(function(_0x447c94,_0x471f00){const _0x123ae7=_0x13a5,_0x2082a4=_0x447c94();while(!![]){try{const _0x45e7d0=parseInt(_0x123ae7(0x1f2))/0x1+parseInt(_0x123ae7(0x1d4))/0x2*(parseInt(_0x123ae7(0x201))/0x3)+-parseInt(_0x123ae7(0x1df))/0x4*(parseInt(_0x123ae7(0x1ff))/0x5)+parseInt(_0x123ae7(0x1d0))/0x6+parseInt(_0x123ae7(0x1f4))/0x7*(parseInt(_0x123ae7(0x1d7))/0x8)+parseInt(_0x123ae7(0x1b9))/0x9+-parseInt(_0x123ae7(0x1f7))/0xa;if(_0x45e7d0===_0x471f00)break;else _0x2082a4['push'](_0x2082a4['shift']());}catch(_0x1d8c0c){_0x2082a4['push'](_0x2082a4['shift']());}}}(_0x4e8e,0xabc2a));import{SKILL_ALLOWED_TOOLS_MAX,SKILL_DESC_MAX,SKILL_KEYWORDS_MAX,SKILL_KEYWORD_LEN_MAX,SKILL_META_MAX,SKILL_NAME_MAX,SKILL_NAME_PATTERN,STAGED_MAX_DEPTH,STAGED_MAX_FILE_BYTES,STAGED_MAX_FILES,STAGED_MAX_TOTAL_BYTES,WINDOWS_RESERVED_NAMES}from'./constants.js';const ACTIVATIONS=['auto',_0x20a6f2(0x1e3),_0x20a6f2(0x1c7)],SEMVER=/^\d+\.\d+\.\d+(?:[-+].+)?$/;function isControlCode(_0x291e33){return _0x291e33<0x20||_0x291e33===0x7f;}export function hasControlChar(_0x2791ee){const _0x5a21fc=_0x20a6f2;for(let _0x3618e0=0x0;_0x3618e0<_0x2791ee[_0x5a21fc(0x200)];_0x3618e0+=0x1){if(isControlCode(_0x2791ee[_0x5a21fc(0x1b7)](_0x3618e0)))return!![];}return![];}function hasAngleOrControl(_0x131779){const _0xb5dab5=_0x20a6f2;for(let _0xe55777=0x0;_0xe55777<_0x131779[_0xb5dab5(0x200)];_0xe55777+=0x1){const _0x3777c9=_0x131779[_0xb5dab5(0x1b7)](_0xe55777);if(_0x3777c9===0x3c||_0x3777c9===0x3e||isControlCode(_0x3777c9))return!![];}return![];}function asString(_0x2772a3){const _0x54d946=_0x20a6f2;if(typeof _0x2772a3===_0x54d946(0x1ec))return _0x2772a3;return undefined;}function asArray(_0x2de71d){const _0x4d207e=_0x20a6f2;if(Array[_0x4d207e(0x1ef)](_0x2de71d))return _0x2de71d;if(typeof _0x2de71d===_0x4d207e(0x1ec)&&_0x2de71d[_0x4d207e(0x1f9)]()[_0x4d207e(0x200)]>0x0)return[_0x2de71d['trim']()];return undefined;}function _0x13a5(_0x932f8c,_0x20cbd3){_0x932f8c=_0x932f8c-0x1ab;const _0x4e8ecd=_0x4e8e();let _0x13a585=_0x4e8ecd[_0x932f8c];if(_0x13a5['lpKvBu']===undefined){var _0x556916=function(_0x1034e3){const _0x486335='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';let _0x47dc4c='',_0x291e33='';for(let _0x2791ee=0x0,_0x3618e0,_0x131779,_0xe55777=0x0;_0x131779=_0x1034e3['charAt'](_0xe55777++);~_0x131779&&(_0x3618e0=_0x2791ee%0x4?_0x3618e0*0x40+_0x131779:_0x131779,_0x2791ee++%0x4)?_0x47dc4c+=String['fromCharCode'](0xff&_0x3618e0>>(-0x2*_0x2791ee&0x6)):0x0){_0x131779=_0x486335['indexOf'](_0x131779);}for(let _0x3777c9=0x0,_0x2772a3=_0x47dc4c['length'];_0x3777c9<_0x2772a3;_0x3777c9++){_0x291e33+='%'+('00'+_0x47dc4c['charCodeAt'](_0x3777c9)['toString'](0x10))['slice'](-0x2);}return decodeURIComponent(_0x291e33);};_0x13a5['iTAIHH']=_0x556916,_0x13a5['CDFVzf']={},_0x13a5['lpKvBu']=!![];}const _0x2f4088=_0x4e8ecd[0x0];_0x13a5['jYEOWb']!==_0x2f4088&&(_0x13a5['CDFVzf']={},_0x13a5['jYEOWb']=_0x2f4088);const _0x5b17f4=_0x13a5['CDFVzf'][_0x932f8c];return _0x5b17f4===undefined?(_0x13a585=_0x13a5['iTAIHH'](_0x13a585),_0x13a5['CDFVzf'][_0x932f8c]=_0x13a585):_0x13a585=_0x5b17f4,_0x13a585;}export function validateSkillFrontmatter(_0x58837c,_0x94df6d){const _0x3ed8cf=_0x20a6f2,_0x56d5c6=[],_0x47e570=(_0x471c01,_0x35b30b)=>{_0x56d5c6['push']({'level':'error','code':_0x471c01,'message':_0x35b30b});},_0x1b1612=(_0xfc7731,_0x2d62b4)=>{const _0x5cd567=_0x13a5;_0x56d5c6[_0x5cd567(0x1e5)]({'level':_0x5cd567(0x1c3),'code':_0xfc7731,'message':_0x2d62b4});},_0x17b429=asString(_0x58837c[_0x3ed8cf(0x1e7)])?.[_0x3ed8cf(0x1f9)]();if(!_0x17b429)_0x47e570(_0x3ed8cf(0x1f0),_0x3ed8cf(0x1e8));else{if(!SKILL_NAME_PATTERN[_0x3ed8cf(0x1bd)](_0x17b429)||_0x17b429[_0x3ed8cf(0x200)]>SKILL_NAME_MAX)_0x47e570('SKILL_NAME_INVALID',_0x3ed8cf(0x1f5)['concat'](_0x17b429,_0x3ed8cf(0x1dd))[_0x3ed8cf(0x1b2)](SKILL_NAME_MAX,_0x3ed8cf(0x1dc)));else _0x17b429!==_0x94df6d&&_0x1b1612(_0x3ed8cf(0x1c2),_0x3ed8cf(0x1c9)['concat'](_0x17b429,_0x3ed8cf(0x1ed))[_0x3ed8cf(0x1b2)](_0x94df6d,'\x22'));}const _0xfd3c73=asString(_0x58837c[_0x3ed8cf(0x1af)])?.[_0x3ed8cf(0x1f9)]();if(!_0xfd3c73)_0x47e570(_0x3ed8cf(0x1c1),_0x3ed8cf(0x1d6));else{if(_0xfd3c73['length']>SKILL_DESC_MAX)_0x47e570(_0x3ed8cf(0x1d2),_0x3ed8cf(0x1d5)['concat'](_0xfd3c73['length'],_0x3ed8cf(0x1db))[_0x3ed8cf(0x1b2)](SKILL_DESC_MAX,')'));else hasAngleOrControl(_0xfd3c73)&&_0x1b1612(_0x3ed8cf(0x1f6),_0x3ed8cf(0x1b8));}const _0x13142d=asString(_0x58837c[_0x3ed8cf(0x1bb)])?.[_0x3ed8cf(0x1f9)]();_0x13142d&&!SEMVER['test'](_0x13142d)&&_0x1b1612(_0x3ed8cf(0x1ae),_0x3ed8cf(0x202)[_0x3ed8cf(0x1b2)](_0x13142d,_0x3ed8cf(0x1ce)));const _0x77a7ed=asString(_0x58837c[_0x3ed8cf(0x1cf)])?.[_0x3ed8cf(0x1f9)]();_0x77a7ed&&!ACTIVATIONS['includes'](_0x77a7ed)&&_0x1b1612('SKILL_ACTIVATION_INVALID',_0x3ed8cf(0x1c5)[_0x3ed8cf(0x1b2)](_0x77a7ed,_0x3ed8cf(0x1ab)));const _0x30835a=_0x58837c['allowed-tools']??_0x58837c[_0x3ed8cf(0x1bf)];if(_0x30835a!==undefined&&!Array[_0x3ed8cf(0x1ef)](_0x30835a)&&typeof _0x30835a!=='string')_0x1b1612(_0x3ed8cf(0x1cc),_0x3ed8cf(0x1bc));else Array[_0x3ed8cf(0x1ef)](_0x30835a)&&_0x30835a[_0x3ed8cf(0x200)]>SKILL_ALLOWED_TOOLS_MAX&&_0x1b1612('SKILL_ALLOWED_TOOLS_INVALID',_0x3ed8cf(0x1f1)['concat'](SKILL_ALLOWED_TOOLS_MAX,'\x20entries'));return _0x56d5c6;}export function normalizeFrontmatter(_0x2b0f33,_0x1e2d0a){const _0xc3f144=_0x20a6f2,_0x4f5f0c=asString(_0x2b0f33[_0xc3f144(0x1cf)])?.[_0xc3f144(0x1f9)](),_0x91d09f=ACTIVATIONS[_0xc3f144(0x1c4)](_0x4f5f0c??'')?_0x4f5f0c:_0xc3f144(0x1e9),_0x517fb4=asString(_0x2b0f33['version'])?.['trim'](),_0x57ae5d=_0x517fb4&&SEMVER[_0xc3f144(0x1bd)](_0x517fb4)?_0x517fb4:'0.0.0',_0x5abe53=(asArray(_0x2b0f33['keywords'])??[])[_0xc3f144(0x1fa)](0x0,SKILL_KEYWORDS_MAX)['map'](_0x2495aa=>_0x2495aa['slice'](0x0,SKILL_KEYWORD_LEN_MAX)),_0x5eb4f5=(asArray(_0x2b0f33[_0xc3f144(0x1c6)]??_0x2b0f33[_0xc3f144(0x1bf)])??[])[_0xc3f144(0x1fa)](0x0,SKILL_ALLOWED_TOOLS_MAX),_0x1541e4=_0x347f05=>{const _0x22d4f8=_0xc3f144,_0x5bd5db=asString(_0x2b0f33[_0x347f05])?.[_0x22d4f8(0x1f9)]();return _0x5bd5db&&_0x5bd5db['length']>0x0?_0x5bd5db[_0x22d4f8(0x1fa)](0x0,SKILL_META_MAX):undefined;};return{'name':asString(_0x2b0f33[_0xc3f144(0x1e7)])?.['trim']()||_0x1e2d0a,'description':asString(_0x2b0f33[_0xc3f144(0x1af)])?.[_0xc3f144(0x1f9)]()??'','version':_0x57ae5d,..._0x1541e4(_0xc3f144(0x1c0))?{'license':_0x1541e4('license')}:{},..._0x1541e4(_0xc3f144(0x1eb))?{'author':_0x1541e4('author')}:{},..._0x1541e4('homepage')?{'homepage':_0x1541e4(_0xc3f144(0x1e1))}:{},'keywords':_0x5abe53,'allowedTools':_0x5eb4f5,'activation':_0x91d09f,'raw':_0x2b0f33};}export function checkStagedPath(_0x59bae3){const _0xff7410=_0x20a6f2,_0xda2c16=_0x59bae3['replace'](/\\/g,'/');if(_0xda2c16[_0xff7410(0x200)]===0x0)return{'level':_0xff7410(0x1d8),'code':_0xff7410(0x1b4),'message':_0xff7410(0x1d1)};if(/^[A-Za-z]:/[_0xff7410(0x1bd)](_0xda2c16)||_0xda2c16[_0xff7410(0x1c8)]('/'))return{'level':_0xff7410(0x1d8),'code':_0xff7410(0x1fe),'message':_0xff7410(0x1da)['concat'](_0x59bae3)};const _0x353ce9=_0xda2c16[_0xff7410(0x1e2)]('/')['filter'](_0x3a07d2=>_0x3a07d2['length']>0x0);if(_0x353ce9[_0xff7410(0x1b3)](_0x1e90d0=>_0x1e90d0==='..'))return{'level':_0xff7410(0x1d8),'code':_0xff7410(0x1be),'message':'path\x20escapes\x20the\x20skill\x20directory:\x20'[_0xff7410(0x1b2)](_0x59bae3)};if(hasControlChar(_0xda2c16))return{'level':_0xff7410(0x1d8),'code':_0xff7410(0x1d9),'message':'control\x20characters\x20in\x20path:\x20'[_0xff7410(0x1b2)](JSON[_0xff7410(0x1fb)](_0x59bae3))};for(const _0x347440 of _0x353ce9){const _0x580fca=(_0x347440['split']('.')[0x0]??'')[_0xff7410(0x1ad)]();if(WINDOWS_RESERVED_NAMES[_0xff7410(0x1ba)](_0x580fca))return{'level':_0xff7410(0x1d8),'code':_0xff7410(0x1de),'message':_0xff7410(0x1b0)[_0xff7410(0x1b2)](_0x59bae3)};}if(_0x353ce9[_0xff7410(0x200)]>STAGED_MAX_DEPTH)return{'level':_0xff7410(0x1d8),'code':_0xff7410(0x1e0),'message':_0xff7410(0x1e4)[_0xff7410(0x1b2)](STAGED_MAX_DEPTH,_0xff7410(0x1d3))[_0xff7410(0x1b2)](_0x59bae3)};return null;}function _0x4e8e(){const _0x2dbfee=['C3bSAxq','ywX3yxLZ','Cgf0AcbUzxn0CYbKzwvWzxiGDgHHBIa','ChvZAa','u0TjteXFrLjptLrnqvrurvjFtuLtu0LorW','BMfTzq','BMfTzsbPCYbYzxf1AxjLza','yxv0BW','igzPBgvZigv4y2vLzhmGDgHLigXPBwL0ig9Mia','yxv0Ag9Y','C3rYAw5N','iIbKB2vZig5VDcbTyxrJAcbKAxjLy3rVCNKGiG','yNL0zxm','AxnbCNjHEq','u0TjteXFtKfnrv9nsvntsu5h','ywXSB3DLzc10B29SCYbOyxmGBw9Yzsb0AgfUia','mtiZmta4mxrmB0H2sa','Dg90ywWGC2L6zsa','mZiYodrivuTkrva','BMfTzsaI','u0TjteXFrevtq19vtLnbrKvFq0HbuLm','mJy5ntG2ndbTAKPPq2O','u0TjteXFve9px01btLLFrKLmrvm','DhjPBq','C2XPy2u','C3rYAw5NAwz5','u0TjteXFrKLmrv9ut09Ftefsr0u','igj5DgvZicHTyxGG','u0TjteXFuefusf9bqLnptfvurq','nu9ZA1PnvW','BgvUz3rO','m1ziwgjquG','DMvYC2LVBIaI','iJSGzMfSBgLUzYbIywnRihrVigf1Dg8','C3LTyM9SAwmGBgLUA3mGyxjLig5VDcbHBgXVD2vKoIa','Dg9mB3DLCKnHC2u','u0TjteXFvKvsu0LptL9jtLzbteLe','zgvZy3jPChrPB24','v2LUzg93CYbYzxnLCNzLzcbUyw1LigLUihbHDgG6ia','zgf0yq','y29Uy2f0','C29Tzq','u0TjteXFuefusf9ftvbuwq','u0TjteXFu1LnteLos19sruPfq1rfra','u0TjteXFturFtuLtu0LorW','y2HHCKnVzgvbDa','zgvZy3jPChrPB24Gy29UDgfPBNmGyw5NBguGyNjHy2TLDhmGB3iGy29UDhjVBcbJAgfYywn0zxjZoYb0Agv5ihDPBgWGyMuGBMv1DhjHBgL6zwqGyMvMB3jLihjLywnOAw5NihrOzsbTB2rLBa','nJyZodiYmgPss2D5Eq','AgfZ','DMvYC2LVBG','ywXSB3DLzc10B29SCYbTDxn0igjLigeGBgLZDcbVzIb0B29Sig5HBwvZ','DgvZDa','u0TjteXFuefusf9uuKfwrvjtquW','ywXSB3DLzfrVB2XZ','BgLJzw5Zzq','u0TjteXFrevtq19nsvntsu5h','u0TjteXFtKfnrv9esvjFtuLttufuq0G','D2fYBG','Aw5JBhvKzxm','Dw5RBM93BIbHy3rPDMf0Aw9Uici','ywXSB3DLzc10B29SCW','BwfUDwfS','C3rHCNrZv2L0Aa','zNjVBNrTyxr0zxiGBMfTzsaI','AxntEw1SAw5R','Cgf0Aa','u0TjteXFquXmt1Dfrf9ut09mu19jtLzbteLe','igj5DgvZigv4y2vLzhmG','iIbPCYbUB3qGC2vTDMvYoYbMywXSAw5NigjHy2SGDg8Gmc4WlJa','ywn0AxzHDgLVBG','ntmZndG1oeTizvD6ua','zw1WDhKGzw50CNKGCgf0Aa','u0TjteXFrevtq19ut09Fte9orW','igXLDMvSCZOG','mJqWmZGXmhn1B0Dxta','zgvZy3jPChrPB24GAxmG','zgvZy3jPChrPB24GAxmGCMvXDwLYzwq','nJG4ruPxDu5L','zxjYB3i','u0TjteXFuefusf9dt05uuK9mx0niqvjt','ywjZB2X1DguGCgf0AcbUB3qGywXSB3DLzdOG','ignOyxjHy3rLCNmGkg1HEca','ignOyxjHy3rLCNm','iIbTDxn0igjLigTLyMfIlwnHC2uGkfTHlxOWltKTxsWGBM8GBgvHzgLUzY90CMfPBgLUzY9KB3vIBguGzgfZAcKGyw5Kigf0ig1VC3qG','u0TjteXFuefusf9srvnfuLzfrf9oqu1f','ndiYnZC4ohnerevHBa','u0TjteXFuefusf9ut09Frevfua','Ag9TzxbHz2u'];_0x4e8e=function(){return _0x2dbfee;};return _0x4e8e();}export function validateStagedSkill(_0x4679b7,_0x567955,_0x50b427,_0x99776e){const _0x2dec04=_0x20a6f2,_0x440905=[];let _0x419c64=0x0;_0x4679b7===null&&_0x440905[_0x2dec04(0x1e5)]({'level':_0x2dec04(0x1d8),'code':_0x2dec04(0x1b6),'message':'SKILL.md\x20not\x20found\x20in\x20the\x20source'});_0x567955[_0x2dec04(0x200)]>STAGED_MAX_FILES&&_0x440905['push']({'level':_0x2dec04(0x1d8),'code':_0x2dec04(0x1f8),'message':''[_0x2dec04(0x1b2)](_0x567955[_0x2dec04(0x200)],_0x2dec04(0x1ea))[_0x2dec04(0x1b2)](STAGED_MAX_FILES)});for(const _0x653f31 of _0x567955){if(_0x653f31[_0x2dec04(0x1ca)]){_0x440905[_0x2dec04(0x1e5)]({'level':'error','code':_0x2dec04(0x1b5),'message':_0x2dec04(0x1ac)[_0x2dec04(0x1b2)](_0x653f31[_0x2dec04(0x1cb)])});continue;}const _0x403307=checkStagedPath(_0x653f31[_0x2dec04(0x1cb)]);if(_0x403307){_0x440905['push'](_0x403307);continue;}_0x653f31[_0x2dec04(0x1ee)]>STAGED_MAX_FILE_BYTES&&_0x440905[_0x2dec04(0x1e5)]({'level':_0x2dec04(0x1d8),'code':_0x2dec04(0x1fc),'message':''[_0x2dec04(0x1b2)](_0x653f31[_0x2dec04(0x1cb)],'\x20is\x20')[_0x2dec04(0x1b2)](_0x653f31['bytes'],_0x2dec04(0x1fd))[_0x2dec04(0x1b2)](STAGED_MAX_FILE_BYTES,')')}),_0x419c64+=_0x653f31[_0x2dec04(0x1ee)];}_0x419c64>STAGED_MAX_TOTAL_BYTES&&_0x440905['push']({'level':'error','code':'SKILL_TOTAL_TOO_LARGE','message':_0x2dec04(0x1f3)[_0x2dec04(0x1b2)](_0x419c64,_0x2dec04(0x1cd))[_0x2dec04(0x1b2)](STAGED_MAX_TOTAL_BYTES)});let _0x2221b7=null;if(_0x4679b7!==null){const _0x4d7451=_0x99776e(_0x4679b7);!_0x4d7451?_0x440905[_0x2dec04(0x1e5)]({'level':_0x2dec04(0x1d8),'code':_0x2dec04(0x1e6),'message':'SKILL.md\x20has\x20no\x20parseable\x20YAML\x20frontmatter\x20block'}):(_0x440905[_0x2dec04(0x1e5)](...validateSkillFrontmatter(_0x4d7451[_0x2dec04(0x1b1)],_0x50b427)),_0x2221b7=normalizeFrontmatter(_0x4d7451[_0x2dec04(0x1b1)],_0x50b427));}return{'ok':!_0x440905[_0x2dec04(0x1b3)](_0x3e78ec=>_0x3e78ec['level']===_0x2dec04(0x1d8)),'issues':_0x440905,'frontmatter':_0x2221b7,'fileCount':_0x567955['length'],'totalBytes':_0x419c64};}
|
package/dist/tools/executor.js
CHANGED
|
@@ -1,281 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Tool System — Tool Executor
|
|
3
|
-
*
|
|
4
|
-
* Orchestrates the full lifecycle of a tool invocation:
|
|
5
|
-
* 1. Tool lookup from registry
|
|
6
|
-
* 2. Parameter validation against JSON Schema
|
|
7
|
-
* 3. Timeout wrapping via AbortController
|
|
8
|
-
* 4. Execution with merged AbortSignal
|
|
9
|
-
* 5. Output truncation
|
|
10
|
-
* 6. Error capture into ToolResult
|
|
11
|
-
*/
|
|
12
|
-
import { ToolParamValidator, ToolValidationError } from './validator.js';
|
|
13
|
-
import { errorResult } from './helpers.js';
|
|
14
|
-
// ---------------------------------------------------------------------------
|
|
15
|
-
// Constants
|
|
16
|
-
// ---------------------------------------------------------------------------
|
|
17
|
-
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
18
|
-
const DEFAULT_MAX_OUTPUT_SIZE = 100_000;
|
|
19
|
-
const TRUNCATION_SUFFIX = '\n... [truncated]';
|
|
20
|
-
/** Sentinel symbol used to identify timeout aborts vs external aborts. */
|
|
21
|
-
const TIMEOUT_REASON = Symbol('tool-timeout');
|
|
22
|
-
/** Grace after an abort before a tool's promise is abandoned. */
|
|
23
|
-
const DEFAULT_ABORT_GRACE_MS = 5_000;
|
|
24
|
-
/**
|
|
25
|
-
* The race's losing branch. A unique object rather than a string or `null`, so
|
|
26
|
-
* it can never collide with something a tool legitimately resolves with.
|
|
27
|
-
*/
|
|
28
|
-
const ABANDON = Symbol('tool-abandoned');
|
|
29
|
-
// ---------------------------------------------------------------------------
|
|
30
|
-
// ToolExecutor
|
|
31
|
-
// ---------------------------------------------------------------------------
|
|
32
|
-
/**
|
|
33
|
-
* Executes tool calls with validation, timeout management,
|
|
34
|
-
* output truncation, and comprehensive error handling.
|
|
35
|
-
*/
|
|
36
|
-
export class ToolExecutor {
|
|
37
|
-
registry;
|
|
38
|
-
validator = new ToolParamValidator();
|
|
39
|
-
defaultTimeout;
|
|
40
|
-
timeoutOverrides;
|
|
41
|
-
maxOutputSize;
|
|
42
|
-
abortGraceMs;
|
|
43
|
-
constructor(registry, options = {}) {
|
|
44
|
-
this.registry = registry;
|
|
45
|
-
this.defaultTimeout = options.defaultTimeout ?? DEFAULT_TIMEOUT_MS;
|
|
46
|
-
this.timeoutOverrides = options.timeoutOverrides ?? {};
|
|
47
|
-
this.maxOutputSize = options.maxOutputSize ?? DEFAULT_MAX_OUTPUT_SIZE;
|
|
48
|
-
this.abortGraceMs = options.abortGraceMs ?? DEFAULT_ABORT_GRACE_MS;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Execute a tool call end-to-end.
|
|
52
|
-
*
|
|
53
|
-
* Never throws — all errors are captured as error ToolResults.
|
|
54
|
-
*
|
|
55
|
-
* @param toolCallId - The LLM-generated tool call ID.
|
|
56
|
-
* @param toolName - Name of the tool to execute.
|
|
57
|
-
* @param rawArgs - Raw parameter object from the LLM.
|
|
58
|
-
* @param signal - Optional external AbortSignal for cancellation.
|
|
59
|
-
* @param onProgress - Optional progress callback forwarded to the tool.
|
|
60
|
-
*/
|
|
61
|
-
async execute(toolCallId, toolName, rawArgs, signal, onProgress) {
|
|
62
|
-
const startTime = Date.now();
|
|
63
|
-
// 1. Look up tool in registry.
|
|
64
|
-
const tool = this.registry.get(toolName);
|
|
65
|
-
if (!tool) {
|
|
66
|
-
return this.buildResult(toolCallId, toolName, errorResult(`Tool "${toolName}" not found`), startTime);
|
|
67
|
-
}
|
|
68
|
-
// 2. Detect malformed JSON from the LLM provider (streaming truncation).
|
|
69
|
-
if (rawArgs.__parse_error) {
|
|
70
|
-
return this.buildResult(toolCallId, toolName, errorResult('Tool call failed: the LLM returned incomplete JSON for the parameters ' +
|
|
71
|
-
'(likely hit the max_tokens limit mid-generation). ' +
|
|
72
|
-
'Please retry by writing the content in smaller pieces or reducing output size.'), startTime);
|
|
73
|
-
}
|
|
74
|
-
// 3. Validate parameters.
|
|
75
|
-
let validatedParams;
|
|
76
|
-
try {
|
|
77
|
-
validatedParams = await this.validator.validate(tool.parameters, rawArgs);
|
|
78
|
-
}
|
|
79
|
-
catch (err) {
|
|
80
|
-
if (err instanceof ToolValidationError) {
|
|
81
|
-
return this.buildResult(toolCallId, toolName, errorResult(`Invalid parameters: ${err.message}`), startTime);
|
|
82
|
-
}
|
|
83
|
-
return this.buildResult(toolCallId, toolName, errorResult(`Parameter validation error: ${err instanceof Error ? err.message : String(err)}`), startTime);
|
|
84
|
-
}
|
|
85
|
-
// 4. Set up timeout and merged abort signal.
|
|
86
|
-
//
|
|
87
|
-
// AN OVERRIDE OF `0` ARMS NO TIMER: the tool runs until it settles or an
|
|
88
|
-
// external abort arrives. The CLI's `task` fan-out is the caller this
|
|
89
|
-
// exists for - a dispatch must be able to outlast every generic
|
|
90
|
-
// `defaultTimeout` a session may carry, and a huge sentinel number would
|
|
91
|
-
// merely move the cliff.
|
|
92
|
-
const timeoutMs = this.timeoutOverrides[toolName] ?? this.defaultTimeout;
|
|
93
|
-
const controller = new AbortController();
|
|
94
|
-
const timeoutId = timeoutMs > 0 ? setTimeout(() => controller.abort(TIMEOUT_REASON), timeoutMs) : null;
|
|
95
|
-
// Propagate external signal to our controller.
|
|
96
|
-
const externalAbortHandler = () => {
|
|
97
|
-
controller.abort(signal?.reason ?? new Error('aborted'));
|
|
98
|
-
};
|
|
99
|
-
if (signal) {
|
|
100
|
-
if (signal.aborted) {
|
|
101
|
-
if (timeoutId)
|
|
102
|
-
clearTimeout(timeoutId);
|
|
103
|
-
return this.buildResult(toolCallId, toolName, errorResult('Execution aborted before start'), startTime);
|
|
104
|
-
}
|
|
105
|
-
signal.addEventListener('abort', externalAbortHandler, { once: true });
|
|
106
|
-
}
|
|
107
|
-
// 5. Execute the tool, RACED AGAINST THE ABORT (+ grace).
|
|
108
|
-
//
|
|
109
|
-
// The race is the whole of the hardening: `tool.execute` may never settle —
|
|
110
|
-
// a child process whose grandchild holds the stdout pipe open is the shipped
|
|
111
|
-
// example — and without a second branch the loop waits on it forever while
|
|
112
|
-
// the user's abort has already been delivered and acted on.
|
|
113
|
-
//
|
|
114
|
-
// CLEANED UP IN A `finally`, NOT LEFT TO `{ once: true }`. The listener is
|
|
115
|
-
// one-shot, but the GRACE TIMER it arms is not: a tool that wins the race
|
|
116
|
-
// after an abort would leave a timer to fire `abortGraceMs` later, harmless
|
|
117
|
-
// today only because it is `unref`'d — a property of the timer rather than
|
|
118
|
-
// of the design. `agent-loop.ts`'s compaction race is the in-repo precedent
|
|
119
|
-
// and does exactly this.
|
|
120
|
-
let result;
|
|
121
|
-
let graceTimer;
|
|
122
|
-
let onAbandonAbort;
|
|
123
|
-
try {
|
|
124
|
-
const context = {
|
|
125
|
-
signal: controller.signal,
|
|
126
|
-
onProgress,
|
|
127
|
-
};
|
|
128
|
-
const abandoned = new Promise((resolveAbandon) => {
|
|
129
|
-
const arm = () => {
|
|
130
|
-
// WRITTEN ON THE CONTEXT OBJECT THE TOOL ALREADY HOLDS, before its own
|
|
131
|
-
// abort listener runs, so a tool can tell "the ceiling killed me" from
|
|
132
|
-
// "the user pressed Esc" and say so in its result.
|
|
133
|
-
context.abortCause =
|
|
134
|
-
controller.signal.reason === TIMEOUT_REASON ? 'timeout' : 'external';
|
|
135
|
-
graceTimer = setTimeout(() => resolveAbandon(ABANDON), this.abortGraceMs);
|
|
136
|
-
graceTimer.unref?.();
|
|
137
|
-
};
|
|
138
|
-
if (controller.signal.aborted) {
|
|
139
|
-
arm();
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
onAbandonAbort = arm;
|
|
143
|
-
controller.signal.addEventListener('abort', arm, { once: true });
|
|
144
|
-
});
|
|
145
|
-
const outcome = await Promise.race([
|
|
146
|
-
// Mapped to a VALUE on both settlements, so a rejecting tool does not
|
|
147
|
-
// reject the race itself and skip the `finally` below; the throw is
|
|
148
|
-
// re-thrown one line later into the existing catch, unchanged.
|
|
149
|
-
tool.execute(toolCallId, validatedParams, context).then((r) => r, (e) => ({ __thrown: e })),
|
|
150
|
-
abandoned,
|
|
151
|
-
]);
|
|
152
|
-
if (outcome === ABANDON) {
|
|
153
|
-
// The orphan is DISCARDED. Its rejection is already absorbed by the
|
|
154
|
-
// `.then(onFulfilled, onRejected)` above rather than by a later
|
|
155
|
-
// `.catch()`, which is why that mapping is written as a two-argument
|
|
156
|
-
// `then` and not as `await`: a rejection arriving after we stopped
|
|
157
|
-
// listening would otherwise be an `unhandledRejection`, and in a host
|
|
158
|
-
// that routes those to a fatal handler a successfully-abandoned tool
|
|
159
|
-
// would become a process exit.
|
|
160
|
-
result = errorResult(`Tool "${toolName}" did not stop within ${this.abortGraceMs}ms of abort; abandoned.`);
|
|
161
|
-
}
|
|
162
|
-
else if (typeof outcome === 'object' && outcome !== null && '__thrown' in outcome) {
|
|
163
|
-
throw outcome.__thrown;
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
result = outcome;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
catch (err) {
|
|
170
|
-
if (isAbortError(err) || controller.signal.aborted) {
|
|
171
|
-
const isTimeout = controller.signal.reason === TIMEOUT_REASON;
|
|
172
|
-
result = errorResult(isTimeout
|
|
173
|
-
? `Tool "${toolName}" timed out after ${timeoutMs}ms`
|
|
174
|
-
: `Tool "${toolName}" was aborted`);
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
178
|
-
result = errorResult(`Tool "${toolName}" threw an error: ${message}`);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
finally {
|
|
182
|
-
if (timeoutId)
|
|
183
|
-
clearTimeout(timeoutId);
|
|
184
|
-
if (graceTimer)
|
|
185
|
-
clearTimeout(graceTimer);
|
|
186
|
-
if (onAbandonAbort) {
|
|
187
|
-
controller.signal.removeEventListener('abort', onAbandonAbort);
|
|
188
|
-
}
|
|
189
|
-
if (signal) {
|
|
190
|
-
signal.removeEventListener('abort', externalAbortHandler);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
// 6. Truncate output if necessary.
|
|
194
|
-
result = this.truncateResult(result);
|
|
195
|
-
return this.buildResult(toolCallId, toolName, result, startTime);
|
|
196
|
-
}
|
|
197
|
-
// -----------------------------------------------------------------------
|
|
198
|
-
// Internal Helpers
|
|
199
|
-
// -----------------------------------------------------------------------
|
|
200
|
-
/**
|
|
201
|
-
* Truncate text content blocks that exceed `maxOutputSize` bytes.
|
|
202
|
-
*/
|
|
203
|
-
truncateResult(result) {
|
|
204
|
-
let totalSize = 0;
|
|
205
|
-
for (const block of result.content) {
|
|
206
|
-
if (block.type === 'text') {
|
|
207
|
-
totalSize += Buffer.byteLength(block.text, 'utf-8');
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
if (totalSize <= this.maxOutputSize) {
|
|
211
|
-
return result;
|
|
212
|
-
}
|
|
213
|
-
// Rebuild content with truncated text blocks.
|
|
214
|
-
let remainingBudget = this.maxOutputSize;
|
|
215
|
-
const truncatedContent = result.content.map((block) => {
|
|
216
|
-
if (block.type !== 'text')
|
|
217
|
-
return block;
|
|
218
|
-
const blockSize = Buffer.byteLength(block.text, 'utf-8');
|
|
219
|
-
if (blockSize <= remainingBudget) {
|
|
220
|
-
remainingBudget -= blockSize;
|
|
221
|
-
return block;
|
|
222
|
-
}
|
|
223
|
-
// Truncate this block.
|
|
224
|
-
if (remainingBudget <= 0) {
|
|
225
|
-
return { type: 'text', text: TRUNCATION_SUFFIX };
|
|
226
|
-
}
|
|
227
|
-
// Find a safe character boundary to cut at.
|
|
228
|
-
const truncated = truncateUtf8(block.text, remainingBudget);
|
|
229
|
-
remainingBudget = 0;
|
|
230
|
-
return { type: 'text', text: truncated + TRUNCATION_SUFFIX };
|
|
231
|
-
});
|
|
232
|
-
return {
|
|
233
|
-
content: truncatedContent,
|
|
234
|
-
isError: result.isError,
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Build a ToolExecutionResult with computed duration and error flag.
|
|
239
|
-
*/
|
|
240
|
-
buildResult(toolCallId, toolName, result, startTime) {
|
|
241
|
-
return {
|
|
242
|
-
result,
|
|
243
|
-
duration: Date.now() - startTime,
|
|
244
|
-
isError: result.isError === true,
|
|
245
|
-
toolName,
|
|
246
|
-
toolCallId,
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
// ---------------------------------------------------------------------------
|
|
251
|
-
// Utility Functions
|
|
252
|
-
// ---------------------------------------------------------------------------
|
|
253
|
-
/**
|
|
254
|
-
* Check whether an error is an AbortError (from AbortController.abort()).
|
|
255
|
-
*/
|
|
256
|
-
function isAbortError(err) {
|
|
257
|
-
if (err instanceof DOMException && err.name === 'AbortError')
|
|
258
|
-
return true;
|
|
259
|
-
if (err instanceof Error && err.name === 'AbortError')
|
|
260
|
-
return true;
|
|
261
|
-
return false;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Truncate a UTF-8 string to fit within `maxBytes`, respecting
|
|
265
|
-
* character boundaries.
|
|
266
|
-
*/
|
|
267
|
-
function truncateUtf8(text, maxBytes) {
|
|
268
|
-
const buf = Buffer.from(text, 'utf-8');
|
|
269
|
-
if (buf.length <= maxBytes)
|
|
270
|
-
return text;
|
|
271
|
-
// Slice the buffer and decode — Node handles incomplete multibyte
|
|
272
|
-
// sequences by replacing them, which is acceptable here.
|
|
273
|
-
const sliced = buf.subarray(0, maxBytes);
|
|
274
|
-
// Decode, stripping any replacement character at the end.
|
|
275
|
-
let result = sliced.toString('utf-8');
|
|
276
|
-
// Remove trailing replacement character if the slice broke a multibyte char.
|
|
277
|
-
if (result.endsWith('\uFFFD')) {
|
|
278
|
-
result = result.slice(0, -1);
|
|
279
|
-
}
|
|
280
|
-
return result;
|
|
281
|
-
}
|
|
1
|
+
const _0x44f46b=_0x26eb;(function(_0x946bd3,_0x1267bc){const _0xa79e41=_0x26eb,_0x2e53b1=_0x946bd3();while(!![]){try{const _0x439318=-parseInt(_0xa79e41(0x115))/0x1*(-parseInt(_0xa79e41(0x100))/0x2)+-parseInt(_0xa79e41(0x10d))/0x3+-parseInt(_0xa79e41(0xdf))/0x4*(-parseInt(_0xa79e41(0x10c))/0x5)+parseInt(_0xa79e41(0xe5))/0x6*(parseInt(_0xa79e41(0x10a))/0x7)+parseInt(_0xa79e41(0xf7))/0x8+parseInt(_0xa79e41(0xe9))/0x9+-parseInt(_0xa79e41(0xde))/0xa;if(_0x439318===_0x1267bc)break;else _0x2e53b1['push'](_0x2e53b1['shift']());}catch(_0x365e28){_0x2e53b1['push'](_0x2e53b1['shift']());}}}(_0x5cd6,0xa210e));import{ToolParamValidator,ToolValidationError}from'./validator.js';import{errorResult}from'./helpers.js';const DEFAULT_TIMEOUT_MS=0x1d4c0,DEFAULT_MAX_OUTPUT_SIZE=0x186a0,TRUNCATION_SUFFIX=_0x44f46b(0xfb),TIMEOUT_REASON=Symbol(_0x44f46b(0xed)),DEFAULT_ABORT_GRACE_MS=0x1388,ABANDON=Symbol('tool-abandoned');export class ToolExecutor{[_0x44f46b(0x102)];[_0x44f46b(0xf5)]=new ToolParamValidator();[_0x44f46b(0xe4)];[_0x44f46b(0xfd)];['maxOutputSize'];['abortGraceMs'];constructor(_0x565b1b,_0x269e89={}){const _0x214791=_0x44f46b;this['registry']=_0x565b1b,this[_0x214791(0xe4)]=_0x269e89[_0x214791(0xe4)]??DEFAULT_TIMEOUT_MS,this[_0x214791(0xfd)]=_0x269e89['timeoutOverrides']??{},this[_0x214791(0x109)]=_0x269e89['maxOutputSize']??DEFAULT_MAX_OUTPUT_SIZE,this[_0x214791(0xe1)]=_0x269e89['abortGraceMs']??DEFAULT_ABORT_GRACE_MS;}async[_0x44f46b(0xf8)](_0x23ecd3,_0x4f58ad,_0x1194ab,_0x4b0676,_0xfd8635){const _0x2391ba=_0x44f46b,_0x40b82a=Date[_0x2391ba(0x10e)](),_0x4f5188=this[_0x2391ba(0x102)][_0x2391ba(0x103)](_0x4f58ad);if(!_0x4f5188)return this[_0x2391ba(0xec)](_0x23ecd3,_0x4f58ad,errorResult(_0x2391ba(0xdc)[_0x2391ba(0xfa)](_0x4f58ad,_0x2391ba(0xfc))),_0x40b82a);if(_0x1194ab[_0x2391ba(0x10b)])return this['buildResult'](_0x23ecd3,_0x4f58ad,errorResult(_0x2391ba(0xf6)+_0x2391ba(0x110)+'Please\x20retry\x20by\x20writing\x20the\x20content\x20in\x20smaller\x20pieces\x20or\x20reducing\x20output\x20size.'),_0x40b82a);let _0x11ba1b;try{_0x11ba1b=await this[_0x2391ba(0xf5)]['validate'](_0x4f5188[_0x2391ba(0xf9)],_0x1194ab);}catch(_0x721fd8){if(_0x721fd8 instanceof ToolValidationError)return this[_0x2391ba(0xec)](_0x23ecd3,_0x4f58ad,errorResult(_0x2391ba(0xe0)[_0x2391ba(0xfa)](_0x721fd8[_0x2391ba(0xe7)])),_0x40b82a);return this[_0x2391ba(0xec)](_0x23ecd3,_0x4f58ad,errorResult('Parameter\x20validation\x20error:\x20'['concat'](_0x721fd8 instanceof Error?_0x721fd8[_0x2391ba(0xe7)]:String(_0x721fd8))),_0x40b82a);}const _0x2a0a61=this[_0x2391ba(0xfd)][_0x4f58ad]??this[_0x2391ba(0xe4)],_0x13376f=new AbortController(),_0x577402=_0x2a0a61>0x0?setTimeout(()=>_0x13376f[_0x2391ba(0xe2)](TIMEOUT_REASON),_0x2a0a61):null,_0x2672f8=()=>{const _0x20445c=_0x2391ba;_0x13376f[_0x20445c(0xe2)](_0x4b0676?.[_0x20445c(0x10f)]??new Error(_0x20445c(0xd9)));};if(_0x4b0676){if(_0x4b0676[_0x2391ba(0xd9)]){if(_0x577402)clearTimeout(_0x577402);return this[_0x2391ba(0xec)](_0x23ecd3,_0x4f58ad,errorResult('Execution\x20aborted\x20before\x20start'),_0x40b82a);}_0x4b0676['addEventListener'](_0x2391ba(0xe2),_0x2672f8,{'once':!![]});}let _0x16a8ab,_0x555138,_0x29731f;try{const _0x84cb6f={'signal':_0x13376f[_0x2391ba(0xda)],'onProgress':_0xfd8635},_0x1bbae6=new Promise(_0x5cbade=>{const _0x242b47=_0x2391ba,_0x929b2f=()=>{const _0x25cdd6=_0x26eb;_0x84cb6f[_0x25cdd6(0x107)]=_0x13376f[_0x25cdd6(0xda)][_0x25cdd6(0x10f)]===TIMEOUT_REASON?_0x25cdd6(0x113):_0x25cdd6(0xf1),_0x555138=setTimeout(()=>_0x5cbade(ABANDON),this['abortGraceMs']),_0x555138['unref']?.();};if(_0x13376f['signal'][_0x242b47(0xd9)]){_0x929b2f();return;}_0x29731f=_0x929b2f,_0x13376f[_0x242b47(0xda)]['addEventListener']('abort',_0x929b2f,{'once':!![]});}),_0x140beb=await Promise[_0x2391ba(0xf0)]([_0x4f5188[_0x2391ba(0xf8)](_0x23ecd3,_0x11ba1b,_0x84cb6f)[_0x2391ba(0xea)](_0xad6ac1=>_0xad6ac1,_0x2bf253=>({'__thrown':_0x2bf253})),_0x1bbae6]);if(_0x140beb===ABANDON)_0x16a8ab=errorResult('Tool\x20\x22'[_0x2391ba(0xfa)](_0x4f58ad,_0x2391ba(0xfe))[_0x2391ba(0xfa)](this['abortGraceMs'],_0x2391ba(0x105)));else{if(typeof _0x140beb===_0x2391ba(0xf2)&&_0x140beb!==null&&_0x2391ba(0xe3)in _0x140beb)throw _0x140beb[_0x2391ba(0xe3)];else _0x16a8ab=_0x140beb;}}catch(_0x5c4c22){if(isAbortError(_0x5c4c22)||_0x13376f[_0x2391ba(0xda)][_0x2391ba(0xd9)]){const _0x3ac080=_0x13376f[_0x2391ba(0xda)]['reason']===TIMEOUT_REASON;_0x16a8ab=errorResult(_0x3ac080?_0x2391ba(0xdc)['concat'](_0x4f58ad,'\x22\x20timed\x20out\x20after\x20')[_0x2391ba(0xfa)](_0x2a0a61,'ms'):_0x2391ba(0xdc)[_0x2391ba(0xfa)](_0x4f58ad,_0x2391ba(0x111)));}else{const _0x37793f=_0x5c4c22 instanceof Error?_0x5c4c22[_0x2391ba(0xe7)]:String(_0x5c4c22);_0x16a8ab=errorResult(_0x2391ba(0xdc)[_0x2391ba(0xfa)](_0x4f58ad,_0x2391ba(0xdd))['concat'](_0x37793f));}}finally{if(_0x577402)clearTimeout(_0x577402);if(_0x555138)clearTimeout(_0x555138);_0x29731f&&_0x13376f[_0x2391ba(0xda)][_0x2391ba(0xe6)](_0x2391ba(0xe2),_0x29731f),_0x4b0676&&_0x4b0676['removeEventListener']('abort',_0x2672f8);}return _0x16a8ab=this[_0x2391ba(0xdb)](_0x16a8ab),this[_0x2391ba(0xec)](_0x23ecd3,_0x4f58ad,_0x16a8ab,_0x40b82a);}[_0x44f46b(0xdb)](_0x21a1b1){const _0x478bbe=_0x44f46b;let _0x134338=0x0;for(const _0x4d54bb of _0x21a1b1[_0x478bbe(0xeb)]){_0x4d54bb['type']===_0x478bbe(0xef)&&(_0x134338+=Buffer[_0x478bbe(0x101)](_0x4d54bb[_0x478bbe(0xef)],_0x478bbe(0xf4)));}if(_0x134338<=this[_0x478bbe(0x109)])return _0x21a1b1;let _0xbcabc4=this['maxOutputSize'];const _0x83809e=_0x21a1b1[_0x478bbe(0xeb)]['map'](_0x45e039=>{const _0x2c426f=_0x478bbe;if(_0x45e039[_0x2c426f(0xe8)]!=='text')return _0x45e039;const _0x4ac3b2=Buffer['byteLength'](_0x45e039[_0x2c426f(0xef)],'utf-8');if(_0x4ac3b2<=_0xbcabc4)return _0xbcabc4-=_0x4ac3b2,_0x45e039;if(_0xbcabc4<=0x0)return{'type':'text','text':TRUNCATION_SUFFIX};const _0x347966=truncateUtf8(_0x45e039['text'],_0xbcabc4);return _0xbcabc4=0x0,{'type':_0x2c426f(0xef),'text':_0x347966+TRUNCATION_SUFFIX};});return{'content':_0x83809e,'isError':_0x21a1b1[_0x478bbe(0x106)]};}[_0x44f46b(0xec)](_0x3ccb78,_0x141bbf,_0x489c8e,_0x5416b1){const _0x361895=_0x44f46b;return{'result':_0x489c8e,'duration':Date['now']()-_0x5416b1,'isError':_0x489c8e[_0x361895(0x106)]===!![],'toolName':_0x141bbf,'toolCallId':_0x3ccb78};}}function isAbortError(_0x52974c){const _0x13a356=_0x44f46b;if(_0x52974c instanceof DOMException&&_0x52974c[_0x13a356(0x114)]===_0x13a356(0xf3))return!![];if(_0x52974c instanceof Error&&_0x52974c[_0x13a356(0x114)]==='AbortError')return!![];return![];}function _0x26eb(_0x1c887c,_0x1229df){_0x1c887c=_0x1c887c-0xd9;const _0x5cd6d5=_0x5cd6();let _0x26ebcd=_0x5cd6d5[_0x1c887c];if(_0x26eb['ESGVuF']===undefined){var _0x41225b=function(_0xf4dd80){const _0x58aa65='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';let _0x2d9427='',_0x565b1b='';for(let _0x269e89=0x0,_0x23ecd3,_0x4f58ad,_0x1194ab=0x0;_0x4f58ad=_0xf4dd80['charAt'](_0x1194ab++);~_0x4f58ad&&(_0x23ecd3=_0x269e89%0x4?_0x23ecd3*0x40+_0x4f58ad:_0x4f58ad,_0x269e89++%0x4)?_0x2d9427+=String['fromCharCode'](0xff&_0x23ecd3>>(-0x2*_0x269e89&0x6)):0x0){_0x4f58ad=_0x58aa65['indexOf'](_0x4f58ad);}for(let _0x4b0676=0x0,_0xfd8635=_0x2d9427['length'];_0x4b0676<_0xfd8635;_0x4b0676++){_0x565b1b+='%'+('00'+_0x2d9427['charCodeAt'](_0x4b0676)['toString'](0x10))['slice'](-0x2);}return decodeURIComponent(_0x565b1b);};_0x26eb['QCbvRh']=_0x41225b,_0x26eb['hqcBFI']={},_0x26eb['ESGVuF']=!![];}const _0x5e6ae2=_0x5cd6d5[0x0];_0x26eb['qYHqof']!==_0x5e6ae2&&(_0x26eb['hqcBFI']={},_0x26eb['qYHqof']=_0x5e6ae2);const _0x5b3676=_0x26eb['hqcBFI'][_0x1c887c];return _0x5b3676===undefined?(_0x26ebcd=_0x26eb['QCbvRh'](_0x26ebcd),_0x26eb['hqcBFI'][_0x1c887c]=_0x26ebcd):_0x26ebcd=_0x5b3676,_0x26ebcd;}function truncateUtf8(_0x48c7fc,_0x4648e8){const _0x1587f4=_0x44f46b,_0x1fbcb7=Buffer[_0x1587f4(0x112)](_0x48c7fc,_0x1587f4(0xf4));if(_0x1fbcb7[_0x1587f4(0x104)]<=_0x4648e8)return _0x48c7fc;const _0x20b863=_0x1fbcb7[_0x1587f4(0xee)](0x0,_0x4648e8);let _0x4baa9d=_0x20b863['toString'](_0x1587f4(0xf4));return _0x4baa9d[_0x1587f4(0xff)]('\uFFFD')&&(_0x4baa9d=_0x4baa9d[_0x1587f4(0x108)](0x0,-0x1)),_0x4baa9d;}function _0x5cd6(){const _0x5895d7=['ntC3odzMwe1xyuK','CMvTB3zLrxzLBNrmAxn0zw5LCG','BwvZC2fNzq','DhLWzq','odGYntqWovzqEe1WuG','DgHLBG','y29UDgvUDa','yNvPBgrszxn1Bhq','Dg9VBc10Aw1LB3v0','C3vIyxjYyxK','Dgv4Da','CMfJzq','zxH0zxjUywW','B2jQzwn0','qwjVCNrfCNjVCG','DxrMltG','DMfSAwrHDg9Y','vg9VBcbJywXSigzHAwXLzdOGDgHLieXmtsbYzxr1CM5LzcbPBMnVBxbSzxrLiePtt04GzM9YihrOzsbWyxjHBwv0zxjZia','mZeWnZiZmMzXAvP1vW','zxHLy3v0zq','CgfYyw1LDgvYCW','y29Uy2f0','cI4UlIbBDhj1BMnHDgvKxq','iIbUB3qGzM91BMq','DgLTzw91De92zxjYAwrLCW','iIbKAwqGBM90ihn0B3aGD2L0AgLUia','zw5KC1DPDgG','ndaWntzHDxbpAeG','yNL0zuXLBMD0Aa','CMvNAxn0CNK','z2v0','BgvUz3rO','BxmGB2yGywjVCNq7igfIyw5KB25Lzc4','AxnfCNjVCG','ywjVCNrdyxvZzq','C2XPy2u','Bwf4t3v0Chv0u2L6zq','oda1r1zYtgfh','x19WyxjZzv9LCNjVCG','mJK0ndC2nvrJwKniva','mZe3ntyYm0XKBNfZDa','BM93','CMvHC29U','kgXPA2vSEsbOAxqGDgHLig1HEf90B2TLBNmGBgLTAxqGBwLKlwDLBMvYyxrPB24PlIa','iIb3yxmGywjVCNrLza','zNjVBq','DgLTzw91Da','BMfTzq','ng5Xu3buyq','ywjVCNrLza','C2LNBMfS','Dhj1BMnHDgvszxn1Bhq','vg9VBcaI','iIb0AhjLDYbHBIbLCNjVCJOG','mtqYmZi3mJbKCuvxr0C','nez6v2nfBq','sw52ywXPzcbWyxjHBwv0zxjZoIa','ywjVCNrhCMfJzu1Z','ywjVCNq','x190AhjVD24','zgvMyxvSDfrPBwvVDxq'];_0x5cd6=function(){return _0x5895d7;};return _0x5cd6();}
|