@easynet/agent-tool-hub 1.0.6
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/README.md +132 -0
- package/dist/N8nLocalAdapter-6RKQJXJP.js +4 -0
- package/dist/N8nLocalAdapter-6RKQJXJP.js.map +1 -0
- package/dist/N8nLocalAdapter-MDWME5ZG.cjs +13 -0
- package/dist/N8nLocalAdapter-MDWME5ZG.cjs.map +1 -0
- package/dist/chunk-57LVNNHL.js +19 -0
- package/dist/chunk-57LVNNHL.js.map +1 -0
- package/dist/chunk-6QTWECRD.cjs +23 -0
- package/dist/chunk-6QTWECRD.cjs.map +1 -0
- package/dist/chunk-HPDQEW2P.js +5251 -0
- package/dist/chunk-HPDQEW2P.js.map +1 -0
- package/dist/chunk-MHEFFZBB.js +134 -0
- package/dist/chunk-MHEFFZBB.js.map +1 -0
- package/dist/chunk-NTTBDQUF.cjs +118 -0
- package/dist/chunk-NTTBDQUF.cjs.map +1 -0
- package/dist/chunk-TIKHPRMB.cjs +5313 -0
- package/dist/chunk-TIKHPRMB.cjs.map +1 -0
- package/dist/chunk-X53WXBKX.cjs +136 -0
- package/dist/chunk-X53WXBKX.cjs.map +1 -0
- package/dist/chunk-YSYEED4K.js +114 -0
- package/dist/chunk-YSYEED4K.js.map +1 -0
- package/dist/cli.cjs +194 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +10 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +186 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +592 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +552 -0
- package/dist/index.d.ts +552 -0
- package/dist/index.js +358 -0
- package/dist/index.js.map +1 -0
- package/dist/toolhub-runtime-CQkP4QVW.d.cts +1564 -0
- package/dist/toolhub-runtime-CQkP4QVW.d.ts +1564 -0
- package/dist/toolhub-runtime.cjs +30 -0
- package/dist/toolhub-runtime.cjs.map +1 -0
- package/dist/toolhub-runtime.d.cts +4 -0
- package/dist/toolhub-runtime.d.ts +4 -0
- package/dist/toolhub-runtime.js +5 -0
- package/dist/toolhub-runtime.js.map +1 -0
- package/package.json +77 -0
|
@@ -0,0 +1,1564 @@
|
|
|
1
|
+
import { N8nLocal } from '@easynet/n8n-local';
|
|
2
|
+
import { ErrorObject } from 'ajv';
|
|
3
|
+
import { BulkheadPolicy, CircuitBreakerPolicy } from 'cockatiel';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Budget constraints for a tool invocation.
|
|
7
|
+
*/
|
|
8
|
+
interface BudgetConfig {
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
maxRetries?: number;
|
|
11
|
+
maxToolCalls?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Execution context passed from agent-orchestra.
|
|
15
|
+
* Contains permissions, budget, and observability context.
|
|
16
|
+
*/
|
|
17
|
+
interface ExecContext {
|
|
18
|
+
requestId: string;
|
|
19
|
+
taskId: string;
|
|
20
|
+
/** Allowed capabilities for this invocation */
|
|
21
|
+
permissions: Capability[];
|
|
22
|
+
budget?: BudgetConfig;
|
|
23
|
+
/** OpenTelemetry-compatible trace ID */
|
|
24
|
+
traceId?: string;
|
|
25
|
+
userId?: string;
|
|
26
|
+
/** Optional: enable dry-run mode for two-phase commit */
|
|
27
|
+
dryRun?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Tool invocation intent from agent-orchestra.
|
|
31
|
+
* Represents what the agent wants to do (untrusted input).
|
|
32
|
+
*/
|
|
33
|
+
interface ToolIntent {
|
|
34
|
+
/** ToolSpec.name reference */
|
|
35
|
+
tool: string;
|
|
36
|
+
/** Untrusted input arguments */
|
|
37
|
+
args: unknown;
|
|
38
|
+
/** Human-readable purpose for audit trail */
|
|
39
|
+
purpose: string;
|
|
40
|
+
/** Idempotency key: recommended format requestId:taskId:tool */
|
|
41
|
+
idempotencyKey?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Unified tool kinds supported by the tools package.
|
|
46
|
+
*/
|
|
47
|
+
type ToolKind = "mcp" | "langchain" | "n8n" | "comfyui" | "skill" | "core";
|
|
48
|
+
/**
|
|
49
|
+
* Capability declarations for tools.
|
|
50
|
+
* Used by PolicyEngine for permission gating.
|
|
51
|
+
*/
|
|
52
|
+
type Capability = "read:web" | "read:fs" | "write:fs" | "read:db" | "write:db" | "network" | "gpu" | "workflow" | "danger:destructive";
|
|
53
|
+
/**
|
|
54
|
+
* Cost hints for tools, used by Budget and routing.
|
|
55
|
+
*/
|
|
56
|
+
interface CostHints {
|
|
57
|
+
latencyMsP50?: number;
|
|
58
|
+
latencyMsP95?: number;
|
|
59
|
+
isAsync?: boolean;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Unified tool specification.
|
|
63
|
+
* All tool types (MCP, LangChain, n8n, ComfyUI, SKILL) are described by this interface.
|
|
64
|
+
*/
|
|
65
|
+
interface ToolSpec {
|
|
66
|
+
/** Globally unique name, recommended format: namespace/name */
|
|
67
|
+
name: string;
|
|
68
|
+
/** Semver version */
|
|
69
|
+
version: string;
|
|
70
|
+
/** Tool kind determines which adapter handles execution */
|
|
71
|
+
kind: ToolKind;
|
|
72
|
+
description?: string;
|
|
73
|
+
tags?: string[];
|
|
74
|
+
/** JSON Schema for input validation */
|
|
75
|
+
inputSchema: object;
|
|
76
|
+
/** JSON Schema for output validation */
|
|
77
|
+
outputSchema: object;
|
|
78
|
+
/** Required capabilities for this tool */
|
|
79
|
+
capabilities: Capability[];
|
|
80
|
+
costHints?: CostHints;
|
|
81
|
+
/** Adapter-specific: endpoint URL (MCP/n8n/ComfyUI) */
|
|
82
|
+
endpoint?: string;
|
|
83
|
+
/** Adapter-specific: resource identifier (workflowId, promptId, etc.) */
|
|
84
|
+
resourceId?: string;
|
|
85
|
+
/** Adapter-specific: implementation reference (LangChain Tool instance, skill handler) */
|
|
86
|
+
impl?: unknown;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Unified adapter interface.
|
|
90
|
+
* Each protocol adapter (MCP, LangChain, n8n, ComfyUI, SKILL) implements this.
|
|
91
|
+
*/
|
|
92
|
+
interface ToolAdapter {
|
|
93
|
+
kind: ToolKind;
|
|
94
|
+
/** Optional: supports dynamic tool discovery */
|
|
95
|
+
listTools?(): Promise<ToolSpec[]>;
|
|
96
|
+
/** Execute the tool with validated args */
|
|
97
|
+
invoke(spec: ToolSpec, args: unknown, ctx: ExecContext): Promise<{
|
|
98
|
+
result: unknown;
|
|
99
|
+
raw?: unknown;
|
|
100
|
+
}>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Evidence attached to a tool result for audit trail.
|
|
105
|
+
*/
|
|
106
|
+
interface Evidence {
|
|
107
|
+
type: "tool" | "file" | "url" | "text" | "metric";
|
|
108
|
+
ref: string;
|
|
109
|
+
summary: string;
|
|
110
|
+
createdAt: string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Error information in a tool result.
|
|
114
|
+
*/
|
|
115
|
+
interface ToolError {
|
|
116
|
+
kind?: "TOOL_NOT_FOUND" | "INPUT_SCHEMA_INVALID" | "POLICY_DENIED" | "BUDGET_EXCEEDED" | "TIMEOUT" | "UPSTREAM_ERROR" | "OUTPUT_SCHEMA_INVALID" | "PATH_OUTSIDE_SANDBOX" | "FILE_TOO_LARGE" | "HTTP_DISALLOWED_HOST" | "HTTP_TIMEOUT" | "HTTP_TOO_LARGE";
|
|
117
|
+
message: string;
|
|
118
|
+
details?: unknown;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Unified tool result returned to agent-orchestra.
|
|
122
|
+
* Always structured, never throws raw exceptions.
|
|
123
|
+
*/
|
|
124
|
+
interface ToolResult {
|
|
125
|
+
ok: boolean;
|
|
126
|
+
result?: unknown;
|
|
127
|
+
evidence: Evidence[];
|
|
128
|
+
error?: ToolError;
|
|
129
|
+
/** Raw response for debugging (can be disabled in production) */
|
|
130
|
+
raw?: unknown;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Event types emitted by PTCRuntime and observability layer.
|
|
135
|
+
*/
|
|
136
|
+
type ToolEventType = "TOOL_CALLED" | "TOOL_RESULT" | "POLICY_DENIED" | "RETRY" | "TIMEOUT" | "BUDGET_EXCEEDED" | "JOB_SUBMITTED" | "JOB_COMPLETED" | "JOB_FAILED";
|
|
137
|
+
/**
|
|
138
|
+
* Base event structure for all tool events.
|
|
139
|
+
*/
|
|
140
|
+
interface ToolEvent {
|
|
141
|
+
type: ToolEventType;
|
|
142
|
+
timestamp: string;
|
|
143
|
+
requestId: string;
|
|
144
|
+
taskId: string;
|
|
145
|
+
toolName: string;
|
|
146
|
+
traceId?: string;
|
|
147
|
+
userId?: string;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Emitted when a tool is called.
|
|
151
|
+
*/
|
|
152
|
+
interface ToolCalledEvent extends ToolEvent {
|
|
153
|
+
type: "TOOL_CALLED";
|
|
154
|
+
argsSummary: string;
|
|
155
|
+
purpose: string;
|
|
156
|
+
idempotencyKey?: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Emitted when a tool returns a result.
|
|
160
|
+
*/
|
|
161
|
+
interface ToolResultEvent extends ToolEvent {
|
|
162
|
+
type: "TOOL_RESULT";
|
|
163
|
+
ok: boolean;
|
|
164
|
+
durationMs: number;
|
|
165
|
+
resultSummary: string;
|
|
166
|
+
evidence: Evidence[];
|
|
167
|
+
error?: ToolError;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Emitted when policy denies a tool invocation.
|
|
171
|
+
*/
|
|
172
|
+
interface PolicyDeniedEvent extends ToolEvent {
|
|
173
|
+
type: "POLICY_DENIED";
|
|
174
|
+
reason: string;
|
|
175
|
+
missingCapabilities?: string[];
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Emitted on retry.
|
|
179
|
+
*/
|
|
180
|
+
interface RetryEvent extends ToolEvent {
|
|
181
|
+
type: "RETRY";
|
|
182
|
+
attempt: number;
|
|
183
|
+
maxRetries: number;
|
|
184
|
+
reason: string;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Async job events.
|
|
188
|
+
*/
|
|
189
|
+
interface JobSubmittedEvent extends ToolEvent {
|
|
190
|
+
type: "JOB_SUBMITTED";
|
|
191
|
+
jobId: string;
|
|
192
|
+
}
|
|
193
|
+
interface JobCompletedEvent extends ToolEvent {
|
|
194
|
+
type: "JOB_COMPLETED";
|
|
195
|
+
jobId: string;
|
|
196
|
+
durationMs: number;
|
|
197
|
+
}
|
|
198
|
+
interface JobFailedEvent extends ToolEvent {
|
|
199
|
+
type: "JOB_FAILED";
|
|
200
|
+
jobId: string;
|
|
201
|
+
error: string;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Union type of all tool events.
|
|
205
|
+
*/
|
|
206
|
+
type AnyToolEvent = ToolCalledEvent | ToolResultEvent | PolicyDeniedEvent | RetryEvent | JobSubmittedEvent | JobCompletedEvent | JobFailedEvent;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Search query for tools.
|
|
210
|
+
*/
|
|
211
|
+
interface ToolSearchQuery {
|
|
212
|
+
/** Text search in name/description/tags */
|
|
213
|
+
text?: string;
|
|
214
|
+
/** Filter by tool kind */
|
|
215
|
+
kind?: ToolKind;
|
|
216
|
+
/** Filter by required capabilities */
|
|
217
|
+
capabilities?: Capability[];
|
|
218
|
+
/** Filter by tags */
|
|
219
|
+
tags?: string[];
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Tool Registry: manages tool registration, lookup, and search.
|
|
223
|
+
* Supports both static registration and dynamic discovery via adapters.
|
|
224
|
+
*/
|
|
225
|
+
declare class ToolRegistry {
|
|
226
|
+
private readonly tools;
|
|
227
|
+
private readonly tagIndex;
|
|
228
|
+
private readonly kindIndex;
|
|
229
|
+
/**
|
|
230
|
+
* Register a single tool spec.
|
|
231
|
+
* Overwrites if same name already exists.
|
|
232
|
+
*/
|
|
233
|
+
register(spec: ToolSpec): void;
|
|
234
|
+
/**
|
|
235
|
+
* Register multiple tool specs at once.
|
|
236
|
+
*/
|
|
237
|
+
bulkRegister(specs: ToolSpec[]): void;
|
|
238
|
+
/**
|
|
239
|
+
* Unregister a tool by name.
|
|
240
|
+
*/
|
|
241
|
+
unregister(name: string): boolean;
|
|
242
|
+
/**
|
|
243
|
+
* Get a tool spec by name.
|
|
244
|
+
*/
|
|
245
|
+
get(name: string): ToolSpec | undefined;
|
|
246
|
+
/**
|
|
247
|
+
* Check if a tool exists.
|
|
248
|
+
*/
|
|
249
|
+
has(name: string): boolean;
|
|
250
|
+
/**
|
|
251
|
+
* Search tools by query.
|
|
252
|
+
*/
|
|
253
|
+
search(query: ToolSearchQuery): ToolSpec[];
|
|
254
|
+
/**
|
|
255
|
+
* List all registered tool names.
|
|
256
|
+
*/
|
|
257
|
+
list(): string[];
|
|
258
|
+
/**
|
|
259
|
+
* Get count of registered tools.
|
|
260
|
+
*/
|
|
261
|
+
get size(): number;
|
|
262
|
+
/**
|
|
263
|
+
* Export a snapshot of all registered tools (for debugging/routing).
|
|
264
|
+
*/
|
|
265
|
+
snapshot(): ToolSpec[];
|
|
266
|
+
/**
|
|
267
|
+
* Clear all registered tools.
|
|
268
|
+
*/
|
|
269
|
+
clear(): void;
|
|
270
|
+
private validateSpec;
|
|
271
|
+
private indexTool;
|
|
272
|
+
private deindexTool;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Schema validation result.
|
|
277
|
+
*/
|
|
278
|
+
interface ValidationResult {
|
|
279
|
+
valid: boolean;
|
|
280
|
+
errors?: ErrorObject[];
|
|
281
|
+
data?: unknown;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* AJV-based JSON Schema validator with coercion and default enrichment.
|
|
285
|
+
*/
|
|
286
|
+
declare class SchemaValidator {
|
|
287
|
+
private readonly ajv;
|
|
288
|
+
private readonly cache;
|
|
289
|
+
constructor();
|
|
290
|
+
/**
|
|
291
|
+
* Validate data against a JSON Schema.
|
|
292
|
+
* Coerces types and applies defaults in-place.
|
|
293
|
+
*/
|
|
294
|
+
validate(schema: object, data: unknown): ValidationResult;
|
|
295
|
+
/**
|
|
296
|
+
* Validate and return coerced data, or throw a descriptive error.
|
|
297
|
+
*/
|
|
298
|
+
validateOrThrow(schema: object, data: unknown, context: string): unknown;
|
|
299
|
+
/**
|
|
300
|
+
* Apply default values from schema to data without full validation.
|
|
301
|
+
*/
|
|
302
|
+
enrichDefaults(schema: object, data: unknown): unknown;
|
|
303
|
+
private getOrCompile;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Error thrown on schema validation failure.
|
|
307
|
+
*/
|
|
308
|
+
declare class SchemaValidationError extends Error {
|
|
309
|
+
readonly errors: ErrorObject[];
|
|
310
|
+
constructor(message: string, errors: ErrorObject[]);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Policy configuration for the engine.
|
|
315
|
+
*/
|
|
316
|
+
interface PolicyConfig {
|
|
317
|
+
/** File system sandbox paths (allowed write directories) */
|
|
318
|
+
sandboxPaths?: string[];
|
|
319
|
+
/** Allowed URL patterns (regex strings) */
|
|
320
|
+
urlAllowlist?: string[];
|
|
321
|
+
/** Denied URL patterns (regex strings) */
|
|
322
|
+
urlDenylist?: string[];
|
|
323
|
+
/** SQL patterns that are denied (e.g., DROP, TRUNCATE) */
|
|
324
|
+
deniedSqlPatterns?: string[];
|
|
325
|
+
/** Network allowed domains */
|
|
326
|
+
allowedDomains?: string[];
|
|
327
|
+
/** Whether to require explicit permission for danger:destructive */
|
|
328
|
+
requireExplicitDangerPermission?: boolean;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Result of a policy check.
|
|
332
|
+
*/
|
|
333
|
+
interface PolicyCheckResult {
|
|
334
|
+
allowed: boolean;
|
|
335
|
+
reason?: string;
|
|
336
|
+
missingCapabilities?: Capability[];
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Policy engine for capability gating, parameter security, and access control.
|
|
340
|
+
*/
|
|
341
|
+
declare class PolicyEngine {
|
|
342
|
+
private readonly config;
|
|
343
|
+
constructor(config?: PolicyConfig);
|
|
344
|
+
/**
|
|
345
|
+
* Enforce all policy checks. Throws PolicyDeniedError if denied.
|
|
346
|
+
*/
|
|
347
|
+
enforce(spec: ToolSpec, args: unknown, ctx: ExecContext): void;
|
|
348
|
+
/**
|
|
349
|
+
* Check all policies without throwing.
|
|
350
|
+
*/
|
|
351
|
+
check(spec: ToolSpec, args: unknown, ctx: ExecContext): PolicyCheckResult;
|
|
352
|
+
/**
|
|
353
|
+
* Check that context permissions cover tool capabilities.
|
|
354
|
+
*/
|
|
355
|
+
private checkCapabilities;
|
|
356
|
+
/**
|
|
357
|
+
* Check parameter-level security constraints.
|
|
358
|
+
*/
|
|
359
|
+
private checkParameters;
|
|
360
|
+
private checkFilePaths;
|
|
361
|
+
private checkUrls;
|
|
362
|
+
private checkSql;
|
|
363
|
+
private checkDomains;
|
|
364
|
+
/**
|
|
365
|
+
* Extract string values from args matching given key patterns.
|
|
366
|
+
*/
|
|
367
|
+
private extractStringValues;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Error thrown when policy denies execution.
|
|
371
|
+
*/
|
|
372
|
+
declare class PolicyDeniedError extends Error {
|
|
373
|
+
readonly missingCapabilities?: Capability[] | undefined;
|
|
374
|
+
readonly kind = "POLICY_DENIED";
|
|
375
|
+
constructor(message: string, missingCapabilities?: Capability[] | undefined);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Budget configuration for a tool or global scope.
|
|
380
|
+
*/
|
|
381
|
+
interface BudgetOptions {
|
|
382
|
+
/** Default timeout in ms for tool invocations */
|
|
383
|
+
defaultTimeoutMs?: number;
|
|
384
|
+
/** Max concurrent invocations per tool */
|
|
385
|
+
maxConcurrency?: number;
|
|
386
|
+
/** Rate limit: max calls per window */
|
|
387
|
+
rateLimit?: {
|
|
388
|
+
maxCalls: number;
|
|
389
|
+
windowMs: number;
|
|
390
|
+
};
|
|
391
|
+
/** Circuit breaker config */
|
|
392
|
+
circuitBreaker?: {
|
|
393
|
+
/** Number of consecutive failures before opening */
|
|
394
|
+
threshold: number;
|
|
395
|
+
/** Half-open reset time in ms */
|
|
396
|
+
halfOpenAfterMs: number;
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Budget manager that provides timeout, rate limiting, concurrency control,
|
|
401
|
+
* and circuit breaker per tool.
|
|
402
|
+
*/
|
|
403
|
+
declare class BudgetManager {
|
|
404
|
+
private readonly defaultTimeoutMs;
|
|
405
|
+
private readonly bulkheads;
|
|
406
|
+
private readonly circuitBreakers;
|
|
407
|
+
private readonly rateLimiters;
|
|
408
|
+
private readonly options;
|
|
409
|
+
constructor(options?: BudgetOptions);
|
|
410
|
+
/**
|
|
411
|
+
* Get effective timeout for a tool invocation.
|
|
412
|
+
*/
|
|
413
|
+
getTimeout(_toolName: string, contextTimeoutMs?: number): number;
|
|
414
|
+
/**
|
|
415
|
+
* Check rate limit for a tool. Returns true if allowed.
|
|
416
|
+
*/
|
|
417
|
+
checkRateLimit(toolName: string): boolean;
|
|
418
|
+
/**
|
|
419
|
+
* Get or create a bulkhead (concurrency limiter) for a tool.
|
|
420
|
+
*/
|
|
421
|
+
getBulkhead(toolName: string): BulkheadPolicy | undefined;
|
|
422
|
+
/**
|
|
423
|
+
* Get or create a circuit breaker for a tool.
|
|
424
|
+
*/
|
|
425
|
+
getCircuitBreaker(toolName: string): CircuitBreakerPolicy | undefined;
|
|
426
|
+
/**
|
|
427
|
+
* Execute a function within budget constraints (bulkhead + circuit breaker).
|
|
428
|
+
*/
|
|
429
|
+
execute<T>(toolName: string, fn: () => Promise<T>): Promise<T>;
|
|
430
|
+
/**
|
|
431
|
+
* Reset all policies for a tool (useful for testing).
|
|
432
|
+
*/
|
|
433
|
+
reset(toolName: string): void;
|
|
434
|
+
/**
|
|
435
|
+
* Reset all policies globally.
|
|
436
|
+
*/
|
|
437
|
+
resetAll(): void;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Event log entry with sequence number.
|
|
442
|
+
*/
|
|
443
|
+
interface LogEntry {
|
|
444
|
+
seq: number;
|
|
445
|
+
event: AnyToolEvent;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Event log listener type.
|
|
449
|
+
*/
|
|
450
|
+
type EventListener = (entry: LogEntry) => void;
|
|
451
|
+
/**
|
|
452
|
+
* Append-only event log for tool invocations.
|
|
453
|
+
* Supports in-memory storage with configurable max size and event subscriptions.
|
|
454
|
+
*/
|
|
455
|
+
declare class EventLog {
|
|
456
|
+
private readonly entries;
|
|
457
|
+
private seq;
|
|
458
|
+
private readonly maxEntries;
|
|
459
|
+
private readonly emitter;
|
|
460
|
+
constructor(options?: {
|
|
461
|
+
maxEntries?: number;
|
|
462
|
+
});
|
|
463
|
+
/**
|
|
464
|
+
* Append an event to the log.
|
|
465
|
+
*/
|
|
466
|
+
append(event: AnyToolEvent): LogEntry;
|
|
467
|
+
/**
|
|
468
|
+
* Subscribe to all events.
|
|
469
|
+
*/
|
|
470
|
+
on(listener: EventListener): () => void;
|
|
471
|
+
/**
|
|
472
|
+
* Subscribe to events of a specific type.
|
|
473
|
+
*/
|
|
474
|
+
onType(type: ToolEventType, listener: EventListener): () => void;
|
|
475
|
+
/**
|
|
476
|
+
* Query events by filter.
|
|
477
|
+
*/
|
|
478
|
+
query(filter: {
|
|
479
|
+
type?: ToolEventType;
|
|
480
|
+
toolName?: string;
|
|
481
|
+
requestId?: string;
|
|
482
|
+
since?: number;
|
|
483
|
+
limit?: number;
|
|
484
|
+
}): LogEntry[];
|
|
485
|
+
/**
|
|
486
|
+
* Get all entries.
|
|
487
|
+
*/
|
|
488
|
+
getAll(): readonly LogEntry[];
|
|
489
|
+
/**
|
|
490
|
+
* Get entry count.
|
|
491
|
+
*/
|
|
492
|
+
get size(): number;
|
|
493
|
+
/**
|
|
494
|
+
* Clear all entries (for testing).
|
|
495
|
+
*/
|
|
496
|
+
clear(): void;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
type LogLevel = "silent" | "error" | "warn" | "info" | "debug" | "trace";
|
|
500
|
+
interface DebugOptions {
|
|
501
|
+
enabled?: boolean;
|
|
502
|
+
level?: LogLevel;
|
|
503
|
+
includeArgs?: boolean;
|
|
504
|
+
includeResults?: boolean;
|
|
505
|
+
includeRaw?: boolean;
|
|
506
|
+
logEvents?: boolean;
|
|
507
|
+
prefix?: string;
|
|
508
|
+
}
|
|
509
|
+
interface ResolvedDebugOptions {
|
|
510
|
+
enabled: boolean;
|
|
511
|
+
level: LogLevel;
|
|
512
|
+
includeArgs: boolean;
|
|
513
|
+
includeResults: boolean;
|
|
514
|
+
includeRaw: boolean;
|
|
515
|
+
logEvents: boolean;
|
|
516
|
+
prefix: string;
|
|
517
|
+
}
|
|
518
|
+
interface Logger {
|
|
519
|
+
options: ResolvedDebugOptions;
|
|
520
|
+
isEnabled(level: LogLevel): boolean;
|
|
521
|
+
error(message: string, meta?: Record<string, unknown>): void;
|
|
522
|
+
warn(message: string, meta?: Record<string, unknown>): void;
|
|
523
|
+
info(message: string, meta?: Record<string, unknown>): void;
|
|
524
|
+
debug(message: string, meta?: Record<string, unknown>): void;
|
|
525
|
+
trace(message: string, meta?: Record<string, unknown>): void;
|
|
526
|
+
}
|
|
527
|
+
declare function createLogger(options?: DebugOptions): Logger;
|
|
528
|
+
declare function sanitizeForLog(value: unknown, maxLen?: number): string;
|
|
529
|
+
declare function summarizeForLog(value: unknown, maxLen?: number): string;
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Simple counter metric.
|
|
533
|
+
*/
|
|
534
|
+
interface CounterValue {
|
|
535
|
+
name: string;
|
|
536
|
+
labels: Record<string, string>;
|
|
537
|
+
value: number;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Histogram bucket.
|
|
541
|
+
*/
|
|
542
|
+
interface HistogramValue {
|
|
543
|
+
name: string;
|
|
544
|
+
labels: Record<string, string>;
|
|
545
|
+
count: number;
|
|
546
|
+
sum: number;
|
|
547
|
+
buckets: Map<number, number>;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Lightweight metrics collector for tool invocations.
|
|
551
|
+
* Provides counters and histograms with label support.
|
|
552
|
+
*/
|
|
553
|
+
declare class Metrics {
|
|
554
|
+
private readonly counters;
|
|
555
|
+
private readonly histograms;
|
|
556
|
+
private readonly defaultBuckets;
|
|
557
|
+
/**
|
|
558
|
+
* Increment a counter.
|
|
559
|
+
*/
|
|
560
|
+
increment(name: string, labels?: Record<string, string>, value?: number): void;
|
|
561
|
+
/**
|
|
562
|
+
* Record a value in a histogram.
|
|
563
|
+
*/
|
|
564
|
+
observe(name: string, labels: Record<string, string>, value: number): void;
|
|
565
|
+
/**
|
|
566
|
+
* Get a counter value.
|
|
567
|
+
*/
|
|
568
|
+
getCounter(name: string, labels?: Record<string, string>): number;
|
|
569
|
+
/**
|
|
570
|
+
* Get histogram stats.
|
|
571
|
+
*/
|
|
572
|
+
getHistogram(name: string, labels: Record<string, string>): HistogramValue | undefined;
|
|
573
|
+
/**
|
|
574
|
+
* Get all counter values.
|
|
575
|
+
*/
|
|
576
|
+
getAllCounters(): CounterValue[];
|
|
577
|
+
/**
|
|
578
|
+
* Get all histogram values.
|
|
579
|
+
*/
|
|
580
|
+
getAllHistograms(): HistogramValue[];
|
|
581
|
+
/**
|
|
582
|
+
* Record standard tool invocation metrics.
|
|
583
|
+
*/
|
|
584
|
+
recordInvocation(toolName: string, ok: boolean, durationMs: number): void;
|
|
585
|
+
/**
|
|
586
|
+
* Record a retry event.
|
|
587
|
+
*/
|
|
588
|
+
recordRetry(toolName: string): void;
|
|
589
|
+
/**
|
|
590
|
+
* Record a policy denial.
|
|
591
|
+
*/
|
|
592
|
+
recordPolicyDenied(toolName: string, reason: string): void;
|
|
593
|
+
/**
|
|
594
|
+
* Reset all metrics (for testing).
|
|
595
|
+
*/
|
|
596
|
+
reset(): void;
|
|
597
|
+
private makeKey;
|
|
598
|
+
private parseKey;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* A trace span representing a unit of work.
|
|
603
|
+
*/
|
|
604
|
+
interface Span {
|
|
605
|
+
spanId: string;
|
|
606
|
+
traceId: string;
|
|
607
|
+
parentSpanId?: string;
|
|
608
|
+
name: string;
|
|
609
|
+
startTime: number;
|
|
610
|
+
endTime?: number;
|
|
611
|
+
durationMs?: number;
|
|
612
|
+
status: "ok" | "error" | "in_progress";
|
|
613
|
+
attributes: Record<string, string | number | boolean>;
|
|
614
|
+
events: SpanEvent[];
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* An event within a span.
|
|
618
|
+
*/
|
|
619
|
+
interface SpanEvent {
|
|
620
|
+
name: string;
|
|
621
|
+
timestamp: number;
|
|
622
|
+
attributes?: Record<string, string | number | boolean>;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Lightweight tracing system for tool invocation spans.
|
|
626
|
+
* Compatible with OpenTelemetry trace/span ID format.
|
|
627
|
+
*/
|
|
628
|
+
declare class Tracing {
|
|
629
|
+
private readonly spans;
|
|
630
|
+
private readonly traceSpans;
|
|
631
|
+
/**
|
|
632
|
+
* Start a new span.
|
|
633
|
+
*/
|
|
634
|
+
startSpan(options: {
|
|
635
|
+
name: string;
|
|
636
|
+
traceId?: string;
|
|
637
|
+
parentSpanId?: string;
|
|
638
|
+
attributes?: Record<string, string | number | boolean>;
|
|
639
|
+
}): Span;
|
|
640
|
+
/**
|
|
641
|
+
* End a span and calculate duration.
|
|
642
|
+
*/
|
|
643
|
+
endSpan(spanId: string, status?: "ok" | "error"): Span | undefined;
|
|
644
|
+
/**
|
|
645
|
+
* Add an event to a span.
|
|
646
|
+
*/
|
|
647
|
+
addEvent(spanId: string, name: string, attributes?: Record<string, string | number | boolean>): void;
|
|
648
|
+
/**
|
|
649
|
+
* Set attributes on a span.
|
|
650
|
+
*/
|
|
651
|
+
setAttributes(spanId: string, attributes: Record<string, string | number | boolean>): void;
|
|
652
|
+
/**
|
|
653
|
+
* Get a span by ID.
|
|
654
|
+
*/
|
|
655
|
+
getSpan(spanId: string): Span | undefined;
|
|
656
|
+
/**
|
|
657
|
+
* Get all spans for a trace.
|
|
658
|
+
*/
|
|
659
|
+
getTrace(traceId: string): Span[];
|
|
660
|
+
/**
|
|
661
|
+
* Create a child span from a parent.
|
|
662
|
+
*/
|
|
663
|
+
createChildSpan(parentSpanId: string, name: string, attributes?: Record<string, string | number | boolean>): Span | undefined;
|
|
664
|
+
/**
|
|
665
|
+
* Clear all traces (for testing).
|
|
666
|
+
*/
|
|
667
|
+
clear(): void;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* PTC Runtime configuration.
|
|
672
|
+
*/
|
|
673
|
+
interface PTCRuntimeConfig {
|
|
674
|
+
policy?: PolicyConfig;
|
|
675
|
+
budget?: BudgetOptions;
|
|
676
|
+
/** Include raw response in ToolResult (default: true, disable in production) */
|
|
677
|
+
includeRaw?: boolean;
|
|
678
|
+
/** Maximum retries if not specified in context (default: 2) */
|
|
679
|
+
defaultMaxRetries?: number;
|
|
680
|
+
/** Debug/logging configuration */
|
|
681
|
+
debug?: DebugOptions;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* PTC Runtime: the unified execution kernel for all tool invocations.
|
|
685
|
+
*
|
|
686
|
+
* Enforces the mandatory 9-step pipeline:
|
|
687
|
+
* 1. Resolve (Registry lookup)
|
|
688
|
+
* 2. Input Validate (AJV)
|
|
689
|
+
* 3. Defaults Enrich
|
|
690
|
+
* 4. Policy Gate
|
|
691
|
+
* 5. Budget check
|
|
692
|
+
* 6. Execute (adapter.invoke())
|
|
693
|
+
* 7. Output Validate (AJV)
|
|
694
|
+
* 8. Evidence Build
|
|
695
|
+
* 9. Audit & Metrics
|
|
696
|
+
*
|
|
697
|
+
* Never throws to callers - always returns ToolResult.
|
|
698
|
+
*/
|
|
699
|
+
declare class PTCRuntime {
|
|
700
|
+
private readonly registry;
|
|
701
|
+
private readonly adapters;
|
|
702
|
+
private readonly validator;
|
|
703
|
+
private readonly policy;
|
|
704
|
+
private readonly budget;
|
|
705
|
+
private readonly eventLog;
|
|
706
|
+
private readonly metrics;
|
|
707
|
+
private readonly tracing;
|
|
708
|
+
private readonly config;
|
|
709
|
+
private readonly logger;
|
|
710
|
+
constructor(options?: {
|
|
711
|
+
registry?: ToolRegistry;
|
|
712
|
+
validator?: SchemaValidator;
|
|
713
|
+
policy?: PolicyEngine;
|
|
714
|
+
budget?: BudgetManager;
|
|
715
|
+
eventLog?: EventLog;
|
|
716
|
+
metrics?: Metrics;
|
|
717
|
+
tracing?: Tracing;
|
|
718
|
+
config?: PTCRuntimeConfig;
|
|
719
|
+
});
|
|
720
|
+
/**
|
|
721
|
+
* Register an adapter for a tool kind.
|
|
722
|
+
*/
|
|
723
|
+
registerAdapter(adapter: ToolAdapter): void;
|
|
724
|
+
/**
|
|
725
|
+
* Get the tool registry.
|
|
726
|
+
*/
|
|
727
|
+
getRegistry(): ToolRegistry;
|
|
728
|
+
/**
|
|
729
|
+
* Get the event log.
|
|
730
|
+
*/
|
|
731
|
+
getEventLog(): EventLog;
|
|
732
|
+
/**
|
|
733
|
+
* Get the metrics collector.
|
|
734
|
+
*/
|
|
735
|
+
getMetrics(): Metrics;
|
|
736
|
+
/**
|
|
737
|
+
* Get the tracing system.
|
|
738
|
+
*/
|
|
739
|
+
getTracing(): Tracing;
|
|
740
|
+
/**
|
|
741
|
+
* Invoke a tool through the PTC pipeline.
|
|
742
|
+
* Never throws - always returns a structured ToolResult.
|
|
743
|
+
*/
|
|
744
|
+
invoke(intent: ToolIntent, ctx: ExecContext): Promise<ToolResult>;
|
|
745
|
+
/**
|
|
746
|
+
* Search for tools in the registry.
|
|
747
|
+
*/
|
|
748
|
+
searchTools(query: string, filters?: {
|
|
749
|
+
kind?: string;
|
|
750
|
+
capabilities?: string[];
|
|
751
|
+
tags?: string[];
|
|
752
|
+
}): ToolSpec[];
|
|
753
|
+
/**
|
|
754
|
+
* Get the schema for a tool.
|
|
755
|
+
*/
|
|
756
|
+
getToolSchema(toolName: string): {
|
|
757
|
+
input: object;
|
|
758
|
+
output: object;
|
|
759
|
+
} | undefined;
|
|
760
|
+
private getPipelineDeps;
|
|
761
|
+
private getObservabilityDeps;
|
|
762
|
+
private buildDryRunResult;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* Interface for LangChain-compatible tool instances.
|
|
767
|
+
* Matches @langchain/core BaseTool.invoke() signature.
|
|
768
|
+
*/
|
|
769
|
+
interface LangChainToolLike {
|
|
770
|
+
invoke(input: unknown, config?: unknown): Promise<unknown>;
|
|
771
|
+
name?: string;
|
|
772
|
+
description?: string;
|
|
773
|
+
schema?: object;
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Options for creating a LangChainAdapter.
|
|
777
|
+
*/
|
|
778
|
+
interface LangChainAdapterOptions {
|
|
779
|
+
/** Map of tool names to their implementations */
|
|
780
|
+
tools?: Map<string, LangChainToolLike>;
|
|
781
|
+
/** Debug/logging configuration */
|
|
782
|
+
debug?: DebugOptions;
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Adapter for LangChain-compatible tools.
|
|
786
|
+
* Wraps local tool instances with the unified ToolAdapter interface.
|
|
787
|
+
*/
|
|
788
|
+
declare class LangChainAdapter implements ToolAdapter {
|
|
789
|
+
readonly kind: "langchain";
|
|
790
|
+
private readonly tools;
|
|
791
|
+
private readonly logger;
|
|
792
|
+
constructor(options?: LangChainAdapterOptions);
|
|
793
|
+
/**
|
|
794
|
+
* Register a LangChain tool instance.
|
|
795
|
+
*/
|
|
796
|
+
registerTool(name: string, tool: LangChainToolLike): void;
|
|
797
|
+
/**
|
|
798
|
+
* Unregister a tool.
|
|
799
|
+
*/
|
|
800
|
+
unregisterTool(name: string): boolean;
|
|
801
|
+
/**
|
|
802
|
+
* List available tools (from registered instances).
|
|
803
|
+
*/
|
|
804
|
+
listTools(): Promise<ToolSpec[]>;
|
|
805
|
+
/**
|
|
806
|
+
* Invoke a LangChain tool.
|
|
807
|
+
*/
|
|
808
|
+
invoke(spec: ToolSpec, args: unknown, ctx: ExecContext): Promise<{
|
|
809
|
+
result: unknown;
|
|
810
|
+
raw?: unknown;
|
|
811
|
+
}>;
|
|
812
|
+
private resolveTool;
|
|
813
|
+
private normalizeResult;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* MCP tool definition as returned by MCP server.
|
|
818
|
+
*/
|
|
819
|
+
interface MCPToolDefinition {
|
|
820
|
+
name: string;
|
|
821
|
+
description?: string;
|
|
822
|
+
inputSchema?: object;
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* MCP call result from MCP server.
|
|
826
|
+
*/
|
|
827
|
+
interface MCPCallResult {
|
|
828
|
+
content: Array<{
|
|
829
|
+
type: string;
|
|
830
|
+
text?: string;
|
|
831
|
+
data?: unknown;
|
|
832
|
+
}>;
|
|
833
|
+
isError?: boolean;
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Injectable MCP client interface.
|
|
837
|
+
* Matches the core methods of @modelcontextprotocol/sdk Client.
|
|
838
|
+
*/
|
|
839
|
+
interface MCPClientLike {
|
|
840
|
+
listTools(): Promise<{
|
|
841
|
+
tools: MCPToolDefinition[];
|
|
842
|
+
}>;
|
|
843
|
+
callTool(params: {
|
|
844
|
+
name: string;
|
|
845
|
+
arguments?: Record<string, unknown>;
|
|
846
|
+
}): Promise<MCPCallResult>;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Options for creating an MCPAdapter.
|
|
850
|
+
*/
|
|
851
|
+
interface MCPAdapterOptions {
|
|
852
|
+
/** Injectable MCP client instance */
|
|
853
|
+
client?: MCPClientLike;
|
|
854
|
+
/** Endpoint URL for the MCP server (used in ToolSpec) */
|
|
855
|
+
endpoint?: string;
|
|
856
|
+
/** Tool name prefix for namespacing */
|
|
857
|
+
prefix?: string;
|
|
858
|
+
/** Auth token for MCP server */
|
|
859
|
+
authToken?: string;
|
|
860
|
+
/** Debug/logging configuration */
|
|
861
|
+
debug?: DebugOptions;
|
|
862
|
+
}
|
|
863
|
+
/**
|
|
864
|
+
* Adapter for MCP (Model Context Protocol) servers.
|
|
865
|
+
* Supports tool discovery and invocation via injectable MCP client.
|
|
866
|
+
*/
|
|
867
|
+
declare class MCPAdapter implements ToolAdapter {
|
|
868
|
+
readonly kind: "mcp";
|
|
869
|
+
private client;
|
|
870
|
+
private readonly endpoint;
|
|
871
|
+
private readonly prefix;
|
|
872
|
+
private readonly _authToken?;
|
|
873
|
+
private cachedTools;
|
|
874
|
+
private cacheExpiry;
|
|
875
|
+
private readonly cacheTtlMs;
|
|
876
|
+
private readonly logger;
|
|
877
|
+
constructor(options?: MCPAdapterOptions);
|
|
878
|
+
/**
|
|
879
|
+
* Set or replace the MCP client.
|
|
880
|
+
*/
|
|
881
|
+
setClient(client: MCPClientLike): void;
|
|
882
|
+
/**
|
|
883
|
+
* Discover tools from the MCP server.
|
|
884
|
+
*/
|
|
885
|
+
listTools(): Promise<ToolSpec[]>;
|
|
886
|
+
/**
|
|
887
|
+
* Invoke an MCP tool.
|
|
888
|
+
*/
|
|
889
|
+
invoke(spec: ToolSpec, args: unknown, _ctx: ExecContext): Promise<{
|
|
890
|
+
result: unknown;
|
|
891
|
+
raw?: unknown;
|
|
892
|
+
}>;
|
|
893
|
+
/**
|
|
894
|
+
* Invalidate the tool cache.
|
|
895
|
+
*/
|
|
896
|
+
invalidateCache(): void;
|
|
897
|
+
private mapToToolSpec;
|
|
898
|
+
private extractMCPName;
|
|
899
|
+
private parseResult;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* HTTP client interface for n8n API/webhook calls.
|
|
904
|
+
*/
|
|
905
|
+
interface HttpClient {
|
|
906
|
+
fetch(url: string, options: {
|
|
907
|
+
method: string;
|
|
908
|
+
headers?: Record<string, string>;
|
|
909
|
+
body?: string;
|
|
910
|
+
signal?: AbortSignal;
|
|
911
|
+
}): Promise<{
|
|
912
|
+
status: number;
|
|
913
|
+
json(): Promise<unknown>;
|
|
914
|
+
text(): Promise<string>;
|
|
915
|
+
}>;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* n8n invocation mode.
|
|
919
|
+
*/
|
|
920
|
+
type N8nInvokeMode = "webhook" | "api";
|
|
921
|
+
/**
|
|
922
|
+
* n8n adapter configuration.
|
|
923
|
+
*/
|
|
924
|
+
interface N8nAdapterOptions {
|
|
925
|
+
/** Injectable HTTP client */
|
|
926
|
+
httpClient?: HttpClient;
|
|
927
|
+
/** n8n API base URL (for API mode) */
|
|
928
|
+
apiBaseUrl?: string;
|
|
929
|
+
/** n8n API key */
|
|
930
|
+
apiKey?: string;
|
|
931
|
+
/** Default invocation mode */
|
|
932
|
+
defaultMode?: N8nInvokeMode;
|
|
933
|
+
/** Threshold in ms: if workflow takes longer, treat as async */
|
|
934
|
+
asyncThresholdMs?: number;
|
|
935
|
+
/** Debug/logging configuration */
|
|
936
|
+
debug?: DebugOptions;
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Adapter for n8n workflows.
|
|
940
|
+
* Supports webhook trigger and API invocation modes.
|
|
941
|
+
* Handles idempotency keys and sync/async detection.
|
|
942
|
+
*/
|
|
943
|
+
declare class N8nAdapter implements ToolAdapter {
|
|
944
|
+
readonly kind: "n8n";
|
|
945
|
+
private readonly httpClient;
|
|
946
|
+
private readonly apiBaseUrl;
|
|
947
|
+
private readonly apiKey?;
|
|
948
|
+
private readonly defaultMode;
|
|
949
|
+
private readonly _asyncThresholdMs;
|
|
950
|
+
private readonly idempotencyStore;
|
|
951
|
+
private readonly logger;
|
|
952
|
+
constructor(options?: N8nAdapterOptions);
|
|
953
|
+
/**
|
|
954
|
+
* Invoke an n8n workflow.
|
|
955
|
+
*/
|
|
956
|
+
invoke(spec: ToolSpec, args: unknown, ctx: ExecContext): Promise<{
|
|
957
|
+
result: unknown;
|
|
958
|
+
raw?: unknown;
|
|
959
|
+
}>;
|
|
960
|
+
private invokeWebhook;
|
|
961
|
+
private invokeApi;
|
|
962
|
+
private getInvokeMode;
|
|
963
|
+
private buildIdempotencyKey;
|
|
964
|
+
private normalizeResult;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
interface N8nLocalAdapterOptions {
|
|
968
|
+
/** Reuse an existing N8nLocal instance */
|
|
969
|
+
instance?: N8nLocal;
|
|
970
|
+
/** Auto-start n8n-local on first invoke (default: true) */
|
|
971
|
+
autoStart?: boolean;
|
|
972
|
+
/** Whether to start the internal HTTP server (default: false) */
|
|
973
|
+
startHttpServer?: boolean;
|
|
974
|
+
/** SQLite database file path (default: "database.sqlite") */
|
|
975
|
+
sqliteDatabase?: string;
|
|
976
|
+
/** Optional data folder for workflow sync */
|
|
977
|
+
dataFolder?: string;
|
|
978
|
+
/** Debug/logging configuration */
|
|
979
|
+
debug?: DebugOptions;
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Adapter for n8n-local (embedded) workflow execution.
|
|
983
|
+
* Uses in-process instance, no HTTP API calls.
|
|
984
|
+
*/
|
|
985
|
+
declare class N8nLocalAdapter implements ToolAdapter {
|
|
986
|
+
readonly kind: "n8n";
|
|
987
|
+
private readonly instance;
|
|
988
|
+
private readonly autoStart;
|
|
989
|
+
private startPromise?;
|
|
990
|
+
private readonly workflowMap;
|
|
991
|
+
private readonly logger;
|
|
992
|
+
constructor(options?: N8nLocalAdapterOptions);
|
|
993
|
+
listTools(): Promise<ToolSpec[]>;
|
|
994
|
+
invoke(spec: ToolSpec, args: unknown, _ctx: ExecContext): Promise<{
|
|
995
|
+
result: unknown;
|
|
996
|
+
raw?: unknown;
|
|
997
|
+
}>;
|
|
998
|
+
stop(): Promise<void>;
|
|
999
|
+
start(): Promise<void>;
|
|
1000
|
+
syncWorkflows(specs: ToolSpec[]): Promise<void>;
|
|
1001
|
+
private ensureStarted;
|
|
1002
|
+
private ensureWorkflowImported;
|
|
1003
|
+
private getWorkflowDefinition;
|
|
1004
|
+
private normalizeWorkflow;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* ComfyUI queue prompt response.
|
|
1009
|
+
*/
|
|
1010
|
+
interface ComfyUIQueueResponse {
|
|
1011
|
+
prompt_id: string;
|
|
1012
|
+
number: number;
|
|
1013
|
+
node_errors?: Record<string, unknown>;
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* ComfyUI history entry.
|
|
1017
|
+
*/
|
|
1018
|
+
interface ComfyUIHistoryEntry {
|
|
1019
|
+
status: {
|
|
1020
|
+
status_str: string;
|
|
1021
|
+
completed: boolean;
|
|
1022
|
+
};
|
|
1023
|
+
outputs: Record<string, {
|
|
1024
|
+
images?: Array<{
|
|
1025
|
+
filename: string;
|
|
1026
|
+
subfolder: string;
|
|
1027
|
+
type: string;
|
|
1028
|
+
}>;
|
|
1029
|
+
}>;
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* Injectable HTTP client for ComfyUI API.
|
|
1033
|
+
*/
|
|
1034
|
+
interface ComfyUIHttpClient {
|
|
1035
|
+
fetch(url: string, options: {
|
|
1036
|
+
method: string;
|
|
1037
|
+
headers?: Record<string, string>;
|
|
1038
|
+
body?: string;
|
|
1039
|
+
}): Promise<{
|
|
1040
|
+
status: number;
|
|
1041
|
+
json(): Promise<unknown>;
|
|
1042
|
+
text(): Promise<string>;
|
|
1043
|
+
}>;
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* ComfyUI adapter options.
|
|
1047
|
+
*/
|
|
1048
|
+
interface ComfyUIAdapterOptions {
|
|
1049
|
+
/** Injectable HTTP client */
|
|
1050
|
+
httpClient?: ComfyUIHttpClient;
|
|
1051
|
+
/** ComfyUI server base URL */
|
|
1052
|
+
baseUrl?: string;
|
|
1053
|
+
/** Polling interval in ms for async jobs */
|
|
1054
|
+
pollIntervalMs?: number;
|
|
1055
|
+
/** Max poll attempts before timeout */
|
|
1056
|
+
maxPollAttempts?: number;
|
|
1057
|
+
/** Client ID for ComfyUI WebSocket */
|
|
1058
|
+
clientId?: string;
|
|
1059
|
+
/** Debug/logging configuration */
|
|
1060
|
+
debug?: DebugOptions;
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
* Adapter for ComfyUI image generation workflows.
|
|
1064
|
+
* Submits prompts to the queue, polls for completion, and retrieves artifacts.
|
|
1065
|
+
*/
|
|
1066
|
+
declare class ComfyUIAdapter implements ToolAdapter {
|
|
1067
|
+
readonly kind: "comfyui";
|
|
1068
|
+
private readonly httpClient;
|
|
1069
|
+
private readonly baseUrl;
|
|
1070
|
+
private readonly pollIntervalMs;
|
|
1071
|
+
private readonly maxPollAttempts;
|
|
1072
|
+
private readonly clientId;
|
|
1073
|
+
private readonly logger;
|
|
1074
|
+
constructor(options?: ComfyUIAdapterOptions);
|
|
1075
|
+
/**
|
|
1076
|
+
* Invoke a ComfyUI workflow.
|
|
1077
|
+
* Queues the prompt and polls for completion.
|
|
1078
|
+
*/
|
|
1079
|
+
invoke(spec: ToolSpec, args: unknown, _ctx: ExecContext): Promise<{
|
|
1080
|
+
result: unknown;
|
|
1081
|
+
raw?: unknown;
|
|
1082
|
+
}>;
|
|
1083
|
+
/**
|
|
1084
|
+
* Get the status of a queued prompt.
|
|
1085
|
+
*/
|
|
1086
|
+
getStatus(promptId: string): Promise<{
|
|
1087
|
+
completed: boolean;
|
|
1088
|
+
status: string;
|
|
1089
|
+
}>;
|
|
1090
|
+
/**
|
|
1091
|
+
* Get the result of a completed prompt.
|
|
1092
|
+
*/
|
|
1093
|
+
getResult(promptId: string): Promise<unknown>;
|
|
1094
|
+
private buildPrompt;
|
|
1095
|
+
private mergeWorkflowArgs;
|
|
1096
|
+
private deepMerge;
|
|
1097
|
+
private queuePrompt;
|
|
1098
|
+
private getHistory;
|
|
1099
|
+
private pollForCompletion;
|
|
1100
|
+
private extractResult;
|
|
1101
|
+
private sleep;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
/**
|
|
1105
|
+
* Parsed SKILL.md manifest following Anthropic's Agent Skills specification.
|
|
1106
|
+
*
|
|
1107
|
+
* A SKILL.md file has:
|
|
1108
|
+
* - YAML frontmatter with `name` and `description` (Level 1: metadata, always loaded)
|
|
1109
|
+
* - Markdown body with instructions (Level 2: loaded when triggered)
|
|
1110
|
+
* - Bundled resource files referenced from the body (Level 3: loaded as needed)
|
|
1111
|
+
*
|
|
1112
|
+
* @see https://platform.claude.com/docs/en/agents-and-tools/agent-skills/overview
|
|
1113
|
+
*/
|
|
1114
|
+
/**
|
|
1115
|
+
* YAML frontmatter from SKILL.md.
|
|
1116
|
+
* This is Level 1 (metadata) — always loaded at startup for discovery.
|
|
1117
|
+
*/
|
|
1118
|
+
interface SkillFrontmatter {
|
|
1119
|
+
/**
|
|
1120
|
+
* Skill name identifier.
|
|
1121
|
+
* - Max 64 characters
|
|
1122
|
+
* - Lowercase letters, numbers, and hyphens only
|
|
1123
|
+
*/
|
|
1124
|
+
name: string;
|
|
1125
|
+
/**
|
|
1126
|
+
* What the skill does and when to use it.
|
|
1127
|
+
* - Max 1024 characters
|
|
1128
|
+
* - Should include triggers/contexts for activation
|
|
1129
|
+
* - Written in third person
|
|
1130
|
+
*/
|
|
1131
|
+
description: string;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* A resource file bundled with the skill.
|
|
1135
|
+
* Resources are Level 3 — loaded only as needed during execution.
|
|
1136
|
+
*/
|
|
1137
|
+
interface SkillResource {
|
|
1138
|
+
/** Relative path from the skill directory */
|
|
1139
|
+
relativePath: string;
|
|
1140
|
+
/** Absolute path on disk */
|
|
1141
|
+
absolutePath: string;
|
|
1142
|
+
/** File extension (e.g., ".md", ".py", ".json") */
|
|
1143
|
+
extension: string;
|
|
1144
|
+
/** Resource type inferred from extension */
|
|
1145
|
+
type: "instructions" | "code" | "data";
|
|
1146
|
+
}
|
|
1147
|
+
/**
|
|
1148
|
+
* Full parsed SKILL.md with progressive disclosure levels.
|
|
1149
|
+
*/
|
|
1150
|
+
interface SkillDefinition {
|
|
1151
|
+
/** Level 1: Metadata from YAML frontmatter (always loaded, ~100 tokens) */
|
|
1152
|
+
frontmatter: SkillFrontmatter;
|
|
1153
|
+
/** Level 2: Markdown body instructions (loaded when skill triggered, <5k tokens recommended) */
|
|
1154
|
+
instructions: string;
|
|
1155
|
+
/** Level 3: Bundled resource files (loaded as needed, effectively unlimited) */
|
|
1156
|
+
resources: SkillResource[];
|
|
1157
|
+
/** Absolute path to the skill directory */
|
|
1158
|
+
dirPath: string;
|
|
1159
|
+
/** Absolute path to the SKILL.md file */
|
|
1160
|
+
skillMdPath: string;
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Validation error for SKILL.md parsing.
|
|
1164
|
+
*/
|
|
1165
|
+
declare class SkillManifestError extends Error {
|
|
1166
|
+
readonly path: string;
|
|
1167
|
+
readonly field: string;
|
|
1168
|
+
constructor(path: string, field: string, message: string);
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* Validate a SkillFrontmatter object.
|
|
1172
|
+
* Throws SkillManifestError if invalid.
|
|
1173
|
+
*/
|
|
1174
|
+
declare function validateFrontmatter(fm: Partial<SkillFrontmatter>, filePath: string): asserts fm is SkillFrontmatter;
|
|
1175
|
+
|
|
1176
|
+
/**
|
|
1177
|
+
* Skill handler function signature.
|
|
1178
|
+
* A skill handler provides programmatic execution when bundled scripts need
|
|
1179
|
+
* to run as part of skill invocation.
|
|
1180
|
+
*/
|
|
1181
|
+
type SkillHandler = (args: unknown, ctx: SkillContext) => Promise<SkillOutput>;
|
|
1182
|
+
/**
|
|
1183
|
+
* Context passed to skill handlers.
|
|
1184
|
+
* Provides access to the skill's progressive disclosure levels and sub-tool invocation.
|
|
1185
|
+
*/
|
|
1186
|
+
interface SkillContext {
|
|
1187
|
+
requestId: string;
|
|
1188
|
+
taskId: string;
|
|
1189
|
+
traceId?: string;
|
|
1190
|
+
userId?: string;
|
|
1191
|
+
/** The skill definition with all three disclosure levels */
|
|
1192
|
+
skill: SkillInvocationContext;
|
|
1193
|
+
/** Invoke a sub-tool (if needed by the skill) */
|
|
1194
|
+
invokeTool?: (toolName: string, args: unknown) => Promise<unknown>;
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Skill invocation context providing progressive disclosure access.
|
|
1198
|
+
* Mirrors the three-level loading model from the Anthropic spec.
|
|
1199
|
+
*/
|
|
1200
|
+
interface SkillInvocationContext {
|
|
1201
|
+
/** Level 1: Metadata (name + description) — always available */
|
|
1202
|
+
name: string;
|
|
1203
|
+
description: string;
|
|
1204
|
+
/** Level 2: Instructions from SKILL.md body — loaded when triggered */
|
|
1205
|
+
instructions: string;
|
|
1206
|
+
/** Level 3: Resource access — loaded as needed */
|
|
1207
|
+
resources: SkillResource[];
|
|
1208
|
+
/** Read a resource file by relative path */
|
|
1209
|
+
readResource: (relativePath: string) => Promise<string>;
|
|
1210
|
+
/** Get resources filtered by type */
|
|
1211
|
+
getResourcesByType: (type: "instructions" | "code" | "data") => SkillResource[];
|
|
1212
|
+
/** Absolute path to the skill directory (for script execution) */
|
|
1213
|
+
dirPath: string;
|
|
1214
|
+
}
|
|
1215
|
+
/**
|
|
1216
|
+
* Structured output from a skill handler.
|
|
1217
|
+
*/
|
|
1218
|
+
interface SkillOutput {
|
|
1219
|
+
result: unknown;
|
|
1220
|
+
evidence?: Evidence[];
|
|
1221
|
+
metadata?: Record<string, unknown>;
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Result returned when a skill is invoked in instruction-only mode
|
|
1225
|
+
* (no handler function, just SKILL.md content for an agent to consume).
|
|
1226
|
+
*/
|
|
1227
|
+
interface SkillInstructionResult {
|
|
1228
|
+
/** The skill name */
|
|
1229
|
+
name: string;
|
|
1230
|
+
/** The skill description */
|
|
1231
|
+
description: string;
|
|
1232
|
+
/** The full instructions from SKILL.md body */
|
|
1233
|
+
instructions: string;
|
|
1234
|
+
/** List of available resources with paths and types */
|
|
1235
|
+
resources: Array<{
|
|
1236
|
+
path: string;
|
|
1237
|
+
type: "instructions" | "code" | "data";
|
|
1238
|
+
}>;
|
|
1239
|
+
/** The skill directory path for resource access */
|
|
1240
|
+
dirPath: string;
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* Options for creating a SkillAdapter.
|
|
1244
|
+
*/
|
|
1245
|
+
interface SkillAdapterOptions {
|
|
1246
|
+
/** Map of skill names to their SkillDefinitions (from SKILL.md) */
|
|
1247
|
+
definitions?: Map<string, SkillDefinition>;
|
|
1248
|
+
/** Map of skill names to their handler functions (optional per skill) */
|
|
1249
|
+
handlers?: Map<string, SkillHandler>;
|
|
1250
|
+
/** Optional sub-tool invoker for skills that need to call other tools */
|
|
1251
|
+
toolInvoker?: (toolName: string, args: unknown, ctx: ExecContext) => Promise<unknown>;
|
|
1252
|
+
/** Debug/logging configuration */
|
|
1253
|
+
debug?: DebugOptions;
|
|
1254
|
+
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Adapter for SKILL type tools following Anthropic's Agent Skills specification.
|
|
1257
|
+
*
|
|
1258
|
+
* Implements the three-level progressive disclosure model:
|
|
1259
|
+
* - Level 1 (metadata): name + description, used for discovery (~100 tokens)
|
|
1260
|
+
* - Level 2 (instructions): SKILL.md body, loaded when skill is triggered (<5k tokens)
|
|
1261
|
+
* - Level 3 (resources): bundled files, loaded as needed (unlimited)
|
|
1262
|
+
*
|
|
1263
|
+
* Skills can operate in two modes:
|
|
1264
|
+
* 1. **Instruction-only**: Returns SKILL.md content for an agent/model to consume
|
|
1265
|
+
* 2. **Handler mode**: Executes a bundled handler function with full context
|
|
1266
|
+
*
|
|
1267
|
+
* This implementation is model-agnostic — any model can consume the skill instructions.
|
|
1268
|
+
*
|
|
1269
|
+
* @see https://platform.claude.com/docs/en/agents-and-tools/agent-skills/overview
|
|
1270
|
+
*/
|
|
1271
|
+
declare class SkillAdapter implements ToolAdapter {
|
|
1272
|
+
readonly kind: "skill";
|
|
1273
|
+
private readonly definitions;
|
|
1274
|
+
private readonly handlers;
|
|
1275
|
+
private readonly toolInvoker?;
|
|
1276
|
+
private readonly logger;
|
|
1277
|
+
constructor(options?: SkillAdapterOptions);
|
|
1278
|
+
/**
|
|
1279
|
+
* Register a skill definition (from SKILL.md parsing).
|
|
1280
|
+
*/
|
|
1281
|
+
registerSkill(name: string, definition: SkillDefinition, handler?: SkillHandler): void;
|
|
1282
|
+
/**
|
|
1283
|
+
* Unregister a skill.
|
|
1284
|
+
*/
|
|
1285
|
+
unregisterSkill(name: string): boolean;
|
|
1286
|
+
/**
|
|
1287
|
+
* List registered skills with Level 1 metadata.
|
|
1288
|
+
* Returns ToolSpecs with name and description from YAML frontmatter.
|
|
1289
|
+
*/
|
|
1290
|
+
listTools(): Promise<ToolSpec[]>;
|
|
1291
|
+
/**
|
|
1292
|
+
* Get Level 1 metadata for all registered skills.
|
|
1293
|
+
* This is what gets loaded at startup (~100 tokens per skill).
|
|
1294
|
+
*/
|
|
1295
|
+
getMetadata(): Array<{
|
|
1296
|
+
name: string;
|
|
1297
|
+
description: string;
|
|
1298
|
+
}>;
|
|
1299
|
+
/**
|
|
1300
|
+
* Get Level 2 instructions for a specific skill.
|
|
1301
|
+
* This is loaded when the skill is triggered.
|
|
1302
|
+
*/
|
|
1303
|
+
getInstructions(name: string): string | undefined;
|
|
1304
|
+
/**
|
|
1305
|
+
* Get Level 3 resources for a specific skill.
|
|
1306
|
+
*/
|
|
1307
|
+
getResources(name: string): SkillResource[];
|
|
1308
|
+
/**
|
|
1309
|
+
* Read a specific resource file from a skill.
|
|
1310
|
+
*/
|
|
1311
|
+
readResource(skillName: string, relativePath: string): Promise<string>;
|
|
1312
|
+
/**
|
|
1313
|
+
* Invoke a skill.
|
|
1314
|
+
*
|
|
1315
|
+
* If the skill has a handler function, executes it with full context.
|
|
1316
|
+
* Otherwise, returns the skill's instruction content (Level 2 + Level 3 manifest)
|
|
1317
|
+
* for an agent/model to consume and act upon.
|
|
1318
|
+
*/
|
|
1319
|
+
invoke(spec: ToolSpec, args: unknown, ctx: ExecContext): Promise<{
|
|
1320
|
+
result: unknown;
|
|
1321
|
+
raw?: unknown;
|
|
1322
|
+
}>;
|
|
1323
|
+
private invokeWithHandler;
|
|
1324
|
+
private invokeInstructionOnly;
|
|
1325
|
+
private buildInvocationContext;
|
|
1326
|
+
private resolveDefinition;
|
|
1327
|
+
private resolveHandler;
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
/**
|
|
1331
|
+
* Configuration for core tools runtime.
|
|
1332
|
+
*/
|
|
1333
|
+
interface CoreToolsConfig {
|
|
1334
|
+
/** Absolute path. All FS operations are confined within this root. */
|
|
1335
|
+
sandboxRoot: string;
|
|
1336
|
+
/** Only these hosts may be fetched. Supports wildcard prefix (e.g. "*.github.com"). */
|
|
1337
|
+
allowedHosts: string[];
|
|
1338
|
+
/** Maximum bytes for fs.readText (default: 5MB) */
|
|
1339
|
+
maxReadBytes: number;
|
|
1340
|
+
/** Maximum bytes for HTTP response body (default: 5MB) */
|
|
1341
|
+
maxHttpBytes: number;
|
|
1342
|
+
/** Maximum bytes for http.downloadFile (default: 100MB) */
|
|
1343
|
+
maxDownloadBytes: number;
|
|
1344
|
+
/** CIDR ranges to block. Defaults include RFC1918 + loopback + link-local. */
|
|
1345
|
+
blockedCidrs: string[];
|
|
1346
|
+
/** Default HTTP timeout in ms (default: 15000) */
|
|
1347
|
+
defaultTimeoutMs: number;
|
|
1348
|
+
/** User-Agent header for HTTP requests */
|
|
1349
|
+
httpUserAgent: string;
|
|
1350
|
+
/** If true, large HTTP responses are auto-written to sandbox and a file ref is returned */
|
|
1351
|
+
enableAutoWriteLargeResponses: boolean;
|
|
1352
|
+
}
|
|
1353
|
+
/**
|
|
1354
|
+
* Default configuration values for core tools.
|
|
1355
|
+
*/
|
|
1356
|
+
declare const DEFAULT_CORE_TOOLS_CONFIG: Omit<CoreToolsConfig, "sandboxRoot" | "allowedHosts">;
|
|
1357
|
+
/**
|
|
1358
|
+
* Context passed to each core tool handler.
|
|
1359
|
+
*/
|
|
1360
|
+
interface CoreToolContext {
|
|
1361
|
+
execCtx: ExecContext;
|
|
1362
|
+
config: CoreToolsConfig;
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* Structured result from a core tool handler.
|
|
1366
|
+
*/
|
|
1367
|
+
interface CoreToolResult {
|
|
1368
|
+
result: unknown;
|
|
1369
|
+
evidence: Evidence[];
|
|
1370
|
+
}
|
|
1371
|
+
/**
|
|
1372
|
+
* A core tool handler function.
|
|
1373
|
+
*/
|
|
1374
|
+
type CoreToolHandler = (args: Record<string, unknown>, ctx: CoreToolContext) => Promise<CoreToolResult>;
|
|
1375
|
+
|
|
1376
|
+
/**
|
|
1377
|
+
* Adapter for core tools (kind="core").
|
|
1378
|
+
* Dispatches to registered handler functions by tool name.
|
|
1379
|
+
*
|
|
1380
|
+
* Core tools are local, atomic operations (filesystem, HTTP, utilities)
|
|
1381
|
+
* that enforce their own security constraints (sandbox, SSRF) in addition
|
|
1382
|
+
* to the PolicyEngine capability gating.
|
|
1383
|
+
*/
|
|
1384
|
+
declare class CoreAdapter implements ToolAdapter {
|
|
1385
|
+
readonly kind: "core";
|
|
1386
|
+
private readonly handlers;
|
|
1387
|
+
private readonly config;
|
|
1388
|
+
constructor(config: CoreToolsConfig);
|
|
1389
|
+
/**
|
|
1390
|
+
* Register a handler for a specific core tool name.
|
|
1391
|
+
*/
|
|
1392
|
+
registerHandler(toolName: string, handler: CoreToolHandler): void;
|
|
1393
|
+
/**
|
|
1394
|
+
* Unregister a handler.
|
|
1395
|
+
*/
|
|
1396
|
+
unregisterHandler(toolName: string): boolean;
|
|
1397
|
+
/**
|
|
1398
|
+
* List registered core tool names.
|
|
1399
|
+
*/
|
|
1400
|
+
getRegisteredTools(): string[];
|
|
1401
|
+
/**
|
|
1402
|
+
* Invoke dispatches to the appropriate handler by spec.name.
|
|
1403
|
+
*/
|
|
1404
|
+
invoke(spec: ToolSpec, args: unknown, ctx: ExecContext): Promise<{
|
|
1405
|
+
result: unknown;
|
|
1406
|
+
raw?: unknown;
|
|
1407
|
+
}>;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* User-provided config for registerCoreTools.
|
|
1412
|
+
* `sandboxRoot` and `allowedHosts` are required; the rest have defaults.
|
|
1413
|
+
*/
|
|
1414
|
+
type CoreToolsUserConfig = Pick<CoreToolsConfig, "sandboxRoot" | "allowedHosts"> & Partial<Omit<CoreToolsConfig, "sandboxRoot" | "allowedHosts">>;
|
|
1415
|
+
/**
|
|
1416
|
+
* Register all core tools with a ToolRegistry and return the configured CoreAdapter.
|
|
1417
|
+
*
|
|
1418
|
+
* Usage:
|
|
1419
|
+
* ```ts
|
|
1420
|
+
* const registry = new ToolRegistry();
|
|
1421
|
+
* const coreAdapter = registerCoreTools(registry, {
|
|
1422
|
+
* sandboxRoot: "/var/tool-hub/sandbox",
|
|
1423
|
+
* allowedHosts: ["api.github.com", "*.example.com"],
|
|
1424
|
+
* });
|
|
1425
|
+
* runtime.registerAdapter(coreAdapter);
|
|
1426
|
+
* ```
|
|
1427
|
+
*/
|
|
1428
|
+
declare function registerCoreTools(registry: ToolRegistry, userConfig: CoreToolsUserConfig): CoreAdapter;
|
|
1429
|
+
|
|
1430
|
+
interface ToolMetadata {
|
|
1431
|
+
name: string;
|
|
1432
|
+
description: string;
|
|
1433
|
+
}
|
|
1434
|
+
type ToolDescription = SkillInstructionResult | {
|
|
1435
|
+
name: string;
|
|
1436
|
+
description?: string;
|
|
1437
|
+
kind: ToolSpec["kind"];
|
|
1438
|
+
version: string;
|
|
1439
|
+
tags?: string[];
|
|
1440
|
+
capabilities: Capability[];
|
|
1441
|
+
inputSchema: object;
|
|
1442
|
+
outputSchema: object;
|
|
1443
|
+
costHints?: ToolSpec["costHints"];
|
|
1444
|
+
endpoint?: string;
|
|
1445
|
+
resourceId?: string;
|
|
1446
|
+
};
|
|
1447
|
+
interface ToolHubInitOptions {
|
|
1448
|
+
roots: Array<string | {
|
|
1449
|
+
path: string;
|
|
1450
|
+
namespace?: string;
|
|
1451
|
+
} | {
|
|
1452
|
+
path: "coreTools";
|
|
1453
|
+
namespace?: string;
|
|
1454
|
+
config?: CoreToolsUserConfig;
|
|
1455
|
+
}>;
|
|
1456
|
+
namespace?: string;
|
|
1457
|
+
extensions?: string[];
|
|
1458
|
+
onDiscoverError?: (dir: string, err: Error) => void;
|
|
1459
|
+
includeCoreTools?: boolean;
|
|
1460
|
+
coreTools?: CoreToolsUserConfig;
|
|
1461
|
+
runtimeConfig?: PTCRuntimeConfig;
|
|
1462
|
+
debug?: DebugOptions;
|
|
1463
|
+
watch?: {
|
|
1464
|
+
enabled?: boolean;
|
|
1465
|
+
debounceMs?: number;
|
|
1466
|
+
persistent?: boolean;
|
|
1467
|
+
};
|
|
1468
|
+
langchain?: LangChainAdapterOptions;
|
|
1469
|
+
mcp?: MCPAdapterOptions;
|
|
1470
|
+
n8n?: N8nAdapterOptions;
|
|
1471
|
+
n8nLocal?: N8nLocalAdapterOptions;
|
|
1472
|
+
n8nMode?: "local" | "api";
|
|
1473
|
+
comfyui?: ComfyUIAdapterOptions;
|
|
1474
|
+
skill?: SkillAdapterOptions;
|
|
1475
|
+
}
|
|
1476
|
+
interface InvokeOptions {
|
|
1477
|
+
purpose?: string;
|
|
1478
|
+
requestId?: string;
|
|
1479
|
+
taskId?: string;
|
|
1480
|
+
traceId?: string;
|
|
1481
|
+
userId?: string;
|
|
1482
|
+
permissions?: Capability[];
|
|
1483
|
+
budget?: BudgetConfig;
|
|
1484
|
+
dryRun?: boolean;
|
|
1485
|
+
idempotencyKey?: string;
|
|
1486
|
+
}
|
|
1487
|
+
declare class ToolHub {
|
|
1488
|
+
private readonly registry;
|
|
1489
|
+
private readonly runtime;
|
|
1490
|
+
private readonly logger;
|
|
1491
|
+
private scanner;
|
|
1492
|
+
private readonly scannerOptions;
|
|
1493
|
+
private readonly skillAdapter;
|
|
1494
|
+
private n8nLocalAdapter?;
|
|
1495
|
+
private readonly n8nLocalOptions?;
|
|
1496
|
+
private readonly n8nMode;
|
|
1497
|
+
private readonly includeCoreTools;
|
|
1498
|
+
private readonly coreToolsConfig?;
|
|
1499
|
+
private readonly watchConfig?;
|
|
1500
|
+
private readonly watchers;
|
|
1501
|
+
private readonly watchTimers;
|
|
1502
|
+
constructor(options: ToolHubInitOptions);
|
|
1503
|
+
/**
|
|
1504
|
+
* Initialize all tools by scanning the configured roots.
|
|
1505
|
+
*/
|
|
1506
|
+
initAllTools(): Promise<ToolSpec[]>;
|
|
1507
|
+
/**
|
|
1508
|
+
* Refresh tools by re-scanning current roots.
|
|
1509
|
+
*/
|
|
1510
|
+
refreshTools(): Promise<ToolSpec[]>;
|
|
1511
|
+
/**
|
|
1512
|
+
* Add additional roots and optionally refresh.
|
|
1513
|
+
*/
|
|
1514
|
+
addRoots(roots: Array<string | {
|
|
1515
|
+
path: string;
|
|
1516
|
+
namespace?: string;
|
|
1517
|
+
}>, refresh?: boolean): Promise<ToolSpec[] | void>;
|
|
1518
|
+
/**
|
|
1519
|
+
* Replace roots and optionally refresh.
|
|
1520
|
+
*/
|
|
1521
|
+
setRoots(roots: Array<string | {
|
|
1522
|
+
path: string;
|
|
1523
|
+
namespace?: string;
|
|
1524
|
+
}>, refresh?: boolean): Promise<ToolSpec[] | void>;
|
|
1525
|
+
/**
|
|
1526
|
+
* Watch all current roots and auto-refresh on changes.
|
|
1527
|
+
*/
|
|
1528
|
+
watchRoots(options?: {
|
|
1529
|
+
debounceMs?: number;
|
|
1530
|
+
persistent?: boolean;
|
|
1531
|
+
}): void;
|
|
1532
|
+
/**
|
|
1533
|
+
* Stop watching all roots.
|
|
1534
|
+
*/
|
|
1535
|
+
unwatchRoots(): void;
|
|
1536
|
+
/**
|
|
1537
|
+
* Return tool metadata in SKILL-like format (name + description).
|
|
1538
|
+
*/
|
|
1539
|
+
listToolMetadata(): ToolMetadata[];
|
|
1540
|
+
/**
|
|
1541
|
+
* Get a tool's full description. For skills, returns full SKILL content.
|
|
1542
|
+
*/
|
|
1543
|
+
getToolDescription(toolName: string): ToolDescription;
|
|
1544
|
+
/**
|
|
1545
|
+
* Invoke a tool through PTC Runtime.
|
|
1546
|
+
*/
|
|
1547
|
+
invokeTool(toolName: string, args: unknown, options?: InvokeOptions): Promise<ToolResult>;
|
|
1548
|
+
/**
|
|
1549
|
+
* Invoke a tool using a pre-built ToolIntent and ExecContext.
|
|
1550
|
+
*/
|
|
1551
|
+
invokeIntent(intent: ToolIntent, ctx: ExecContext): Promise<ToolResult>;
|
|
1552
|
+
getRegistry(): ToolRegistry;
|
|
1553
|
+
getRuntime(): PTCRuntime;
|
|
1554
|
+
shutdown(): Promise<void>;
|
|
1555
|
+
private ensureN8nLocalAdapter;
|
|
1556
|
+
private extractSkillDefinition;
|
|
1557
|
+
}
|
|
1558
|
+
declare function createToolHub(options: ToolHubInitOptions): ToolHub;
|
|
1559
|
+
|
|
1560
|
+
declare function createToolHubAndInit(options: ToolHubInitOptions): Promise<ToolHub>;
|
|
1561
|
+
declare function createToolHubAndInitFromConfig(configPath: string): Promise<ToolHub>;
|
|
1562
|
+
declare function createAgentToolHub(configPath: string): Promise<ToolHub>;
|
|
1563
|
+
|
|
1564
|
+
export { N8nLocalAdapter as $, type AnyToolEvent as A, type BudgetConfig as B, type Capability as C, DEFAULT_CORE_TOOLS_CONFIG as D, type ExecContext as E, type JobFailedEvent as F, type JobSubmittedEvent as G, type HistogramValue as H, type InvokeOptions as I, type JobCompletedEvent as J, type LangChainAdapterOptions as K, LangChainAdapter as L, type LangChainToolLike as M, type LogEntry as N, type LogLevel as O, type Logger as P, MCPAdapter as Q, type MCPAdapterOptions as R, type SkillDefinition as S, type ToolSpec as T, type MCPCallResult as U, type MCPClientLike as V, type MCPToolDefinition as W, Metrics as X, N8nAdapter as Y, type N8nAdapterOptions as Z, type N8nInvokeMode as _, type Evidence as a, type N8nLocalAdapterOptions as a0, PTCRuntime as a1, type PTCRuntimeConfig as a2, type PolicyCheckResult as a3, type PolicyConfig as a4, PolicyDeniedError as a5, type PolicyDeniedEvent as a6, PolicyEngine as a7, type ResolvedDebugOptions as a8, type RetryEvent as a9, createLogger as aA, createToolHub as aB, createToolHubAndInitFromConfig as aC, registerCoreTools as aD, sanitizeForLog as aE, summarizeForLog as aF, validateFrontmatter as aG, createToolHubAndInit as aH, SchemaValidationError as aa, SchemaValidator as ab, SkillAdapter as ac, type SkillAdapterOptions as ad, type SkillContext as ae, type SkillHandler as af, type SkillInstructionResult as ag, type SkillInvocationContext as ah, SkillManifestError as ai, type SkillOutput as aj, type Span as ak, type SpanEvent as al, type ToolCalledEvent as am, type ToolDescription as an, type ToolError as ao, type ToolEvent as ap, type ToolEventType as aq, ToolHub as ar, type ToolIntent as as, type ToolMetadata as at, type ToolResult as au, type ToolResultEvent as av, type ToolSearchQuery as aw, Tracing as ax, type ValidationResult as ay, createAgentToolHub as az, ToolRegistry as b, type ToolAdapter as c, type SkillFrontmatter as d, type SkillResource as e, type ToolKind as f, type CostHints as g, type ToolHubInitOptions as h, BudgetManager as i, type BudgetOptions as j, ComfyUIAdapter as k, type ComfyUIAdapterOptions as l, type ComfyUIHistoryEntry as m, type ComfyUIHttpClient as n, type ComfyUIQueueResponse as o, CoreAdapter as p, type CoreToolContext as q, type CoreToolHandler as r, type CoreToolResult as s, type CoreToolsConfig as t, type CoreToolsUserConfig as u, type CounterValue as v, type DebugOptions as w, type EventListener as x, EventLog as y, type HttpClient as z };
|