@enclave-vm/broker 2.11.1 → 2.13.0
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/broker-session.d.ts +24 -1
- package/broker.d.ts +27 -0
- package/esm/index.mjs +1110 -31
- package/index.d.ts +2 -0
- package/index.js +1107 -30
- package/openapi/catalog-handler.d.ts +63 -0
- package/openapi/hash-utils.d.ts +12 -0
- package/openapi/index.d.ts +13 -0
- package/openapi/openapi-source.d.ts +93 -0
- package/openapi/openapi-spec-poller.d.ts +100 -0
- package/openapi/openapi-tool-loader.d.ts +107 -0
- package/package.json +5 -5
- package/tool-registry.d.ts +5 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog Handler
|
|
3
|
+
*
|
|
4
|
+
* HTTP handler for GET /code/actions endpoint.
|
|
5
|
+
* Returns the current action catalog derived from OpenAPI sources.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import type { ToolRegistry } from '../tool-registry';
|
|
10
|
+
import type { OpenApiSource } from './openapi-source';
|
|
11
|
+
import type { BrokerRequest, BrokerResponse } from '../http/types';
|
|
12
|
+
/**
|
|
13
|
+
* Action descriptor in the catalog.
|
|
14
|
+
*/
|
|
15
|
+
export interface CatalogAction {
|
|
16
|
+
name: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
inputSchema?: Record<string, unknown>;
|
|
19
|
+
service: string;
|
|
20
|
+
tags?: string[];
|
|
21
|
+
deprecated?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Service descriptor in the catalog.
|
|
25
|
+
*/
|
|
26
|
+
export interface CatalogService {
|
|
27
|
+
name: string;
|
|
28
|
+
specUrl: string;
|
|
29
|
+
lastUpdated: string;
|
|
30
|
+
actionCount: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Full catalog response.
|
|
34
|
+
*/
|
|
35
|
+
export interface CatalogResponse {
|
|
36
|
+
actions: CatalogAction[];
|
|
37
|
+
services: CatalogService[];
|
|
38
|
+
version: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* HTTP handler for the action catalog endpoint.
|
|
42
|
+
*/
|
|
43
|
+
export declare class CatalogHandler {
|
|
44
|
+
private readonly toolRegistry;
|
|
45
|
+
private readonly sources;
|
|
46
|
+
constructor(toolRegistry: ToolRegistry, sources: OpenApiSource[]);
|
|
47
|
+
/**
|
|
48
|
+
* Get route definitions for the catalog endpoint.
|
|
49
|
+
*/
|
|
50
|
+
getRoutes(): Array<{
|
|
51
|
+
method: string;
|
|
52
|
+
path: string;
|
|
53
|
+
handler: (req: BrokerRequest, res: BrokerResponse) => Promise<void>;
|
|
54
|
+
}>;
|
|
55
|
+
/**
|
|
56
|
+
* Handle GET /code/actions request.
|
|
57
|
+
*/
|
|
58
|
+
private handleGetActions;
|
|
59
|
+
/**
|
|
60
|
+
* Build the full catalog response.
|
|
61
|
+
*/
|
|
62
|
+
buildCatalog(): CatalogResponse;
|
|
63
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-platform SHA-256 hashing utility.
|
|
3
|
+
*
|
|
4
|
+
* Uses Web Crypto API (crypto.subtle) which is available in both
|
|
5
|
+
* Node.js 18+ and modern browsers.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Compute SHA-256 hex digest of a string.
|
|
11
|
+
*/
|
|
12
|
+
export declare function sha256Hex(data: string): Promise<string>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI integration for the Enclave broker.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
export { OpenApiSpecPoller } from './openapi-spec-poller';
|
|
7
|
+
export type { OpenApiPollerConfig, ChangeDetectionMode, PollerRetryConfig, OpenApiPollerEvents, } from './openapi-spec-poller';
|
|
8
|
+
export { OpenApiToolLoader } from './openapi-tool-loader';
|
|
9
|
+
export type { LoaderOptions, UpstreamAuth } from './openapi-tool-loader';
|
|
10
|
+
export { OpenApiSource } from './openapi-source';
|
|
11
|
+
export type { OpenApiSourceConfig, OpenApiSourceStats, ToolsUpdatedEvent, SourceHealthStatus } from './openapi-source';
|
|
12
|
+
export { CatalogHandler } from './catalog-handler';
|
|
13
|
+
export type { CatalogAction, CatalogService, CatalogResponse } from './catalog-handler';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI Source
|
|
3
|
+
*
|
|
4
|
+
* Orchestrator that ties together the spec poller, tool loader, and broker.
|
|
5
|
+
* Handles diff-based tool updates when specs change.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
import type { OpenApiPollerConfig } from './openapi-spec-poller';
|
|
11
|
+
import type { LoaderOptions, UpstreamAuth } from './openapi-tool-loader';
|
|
12
|
+
import type { ToolRegistry } from '../tool-registry';
|
|
13
|
+
/**
|
|
14
|
+
* Health status for the OpenAPI source.
|
|
15
|
+
*/
|
|
16
|
+
export type SourceHealthStatus = 'healthy' | 'unhealthy' | 'unknown';
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for an OpenAPI source.
|
|
19
|
+
*/
|
|
20
|
+
export interface OpenApiSourceConfig extends OpenApiPollerConfig {
|
|
21
|
+
/** Service name (e.g., 'user-service') */
|
|
22
|
+
name: string;
|
|
23
|
+
/** Upstream API base URL */
|
|
24
|
+
baseUrl: string;
|
|
25
|
+
/** Tool loader options */
|
|
26
|
+
loaderOptions?: LoaderOptions;
|
|
27
|
+
/** Upstream authentication */
|
|
28
|
+
auth?: UpstreamAuth;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Statistics for the OpenAPI source.
|
|
32
|
+
*/
|
|
33
|
+
export interface OpenApiSourceStats {
|
|
34
|
+
toolCount: number;
|
|
35
|
+
lastUpdate: string;
|
|
36
|
+
specHash: string;
|
|
37
|
+
pollHealth: SourceHealthStatus;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Tool update event data.
|
|
41
|
+
*/
|
|
42
|
+
export interface ToolsUpdatedEvent {
|
|
43
|
+
added: string[];
|
|
44
|
+
removed: string[];
|
|
45
|
+
updated: string[];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Orchestrator that ties together spec polling, tool loading, and broker registration.
|
|
49
|
+
*/
|
|
50
|
+
export declare class OpenApiSource extends EventEmitter {
|
|
51
|
+
private readonly toolRegistry;
|
|
52
|
+
private readonly config;
|
|
53
|
+
private poller;
|
|
54
|
+
private previousToolNames;
|
|
55
|
+
private lastUpdate;
|
|
56
|
+
private specHash;
|
|
57
|
+
private health;
|
|
58
|
+
private disposed;
|
|
59
|
+
private _syncLock;
|
|
60
|
+
constructor(toolRegistry: ToolRegistry, config: OpenApiSourceConfig);
|
|
61
|
+
/**
|
|
62
|
+
* Initial load + start polling.
|
|
63
|
+
*/
|
|
64
|
+
start(): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Stop polling.
|
|
67
|
+
*/
|
|
68
|
+
stop(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Manual refresh (for webhook triggers).
|
|
71
|
+
*/
|
|
72
|
+
refresh(): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Sync tools based on a new spec (diff-based update).
|
|
75
|
+
*/
|
|
76
|
+
private syncTools;
|
|
77
|
+
/**
|
|
78
|
+
* Get the set of tool names managed by this source.
|
|
79
|
+
*/
|
|
80
|
+
getToolNames(): Set<string>;
|
|
81
|
+
/**
|
|
82
|
+
* Get source statistics.
|
|
83
|
+
*/
|
|
84
|
+
getStats(): OpenApiSourceStats;
|
|
85
|
+
/**
|
|
86
|
+
* Get the source name.
|
|
87
|
+
*/
|
|
88
|
+
getName(): string;
|
|
89
|
+
/**
|
|
90
|
+
* Dispose the source and release resources.
|
|
91
|
+
*/
|
|
92
|
+
dispose(): void;
|
|
93
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI Spec Poller
|
|
3
|
+
*
|
|
4
|
+
* Polls an OpenAPI specification URL for changes using ETag/content-hash detection.
|
|
5
|
+
* Follows the HealthChecker pattern (EventEmitter, start/stop, setInterval).
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
/**
|
|
11
|
+
* Change detection strategy.
|
|
12
|
+
*/
|
|
13
|
+
export type ChangeDetectionMode = 'content-hash' | 'etag' | 'auto';
|
|
14
|
+
/**
|
|
15
|
+
* Retry configuration for failed polls.
|
|
16
|
+
*/
|
|
17
|
+
export interface PollerRetryConfig {
|
|
18
|
+
/** Maximum number of retries per poll cycle @default 3 */
|
|
19
|
+
maxRetries?: number;
|
|
20
|
+
/** Initial delay before first retry in ms @default 1000 */
|
|
21
|
+
initialDelayMs?: number;
|
|
22
|
+
/** Maximum delay between retries in ms @default 10000 */
|
|
23
|
+
maxDelayMs?: number;
|
|
24
|
+
/** Backoff multiplier @default 2 */
|
|
25
|
+
backoffMultiplier?: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Configuration for the OpenAPI spec poller.
|
|
29
|
+
*/
|
|
30
|
+
export interface OpenApiPollerConfig {
|
|
31
|
+
/** URL of the OpenAPI specification */
|
|
32
|
+
url: string;
|
|
33
|
+
/** Poll interval in milliseconds @default 60000 */
|
|
34
|
+
intervalMs?: number;
|
|
35
|
+
/** Fetch timeout in milliseconds @default 10000 */
|
|
36
|
+
fetchTimeoutMs?: number;
|
|
37
|
+
/** Change detection strategy @default 'auto' */
|
|
38
|
+
changeDetection?: ChangeDetectionMode;
|
|
39
|
+
/** Retry configuration */
|
|
40
|
+
retry?: PollerRetryConfig;
|
|
41
|
+
/** Number of consecutive failures before marking unhealthy @default 3 */
|
|
42
|
+
unhealthyThreshold?: number;
|
|
43
|
+
/** Additional headers for fetch requests */
|
|
44
|
+
headers?: Record<string, string>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Events emitted by the poller.
|
|
48
|
+
*/
|
|
49
|
+
export interface OpenApiPollerEvents {
|
|
50
|
+
changed: [spec: string, hash: string];
|
|
51
|
+
unchanged: [];
|
|
52
|
+
pollError: [error: Error];
|
|
53
|
+
unhealthy: [consecutiveFailures: number];
|
|
54
|
+
recovered: [];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Polls an OpenAPI spec URL for changes using ETag and content-hash detection.
|
|
58
|
+
*/
|
|
59
|
+
export declare class OpenApiSpecPoller extends EventEmitter {
|
|
60
|
+
private readonly config;
|
|
61
|
+
private intervalTimer;
|
|
62
|
+
private lastHash;
|
|
63
|
+
private lastEtag;
|
|
64
|
+
private lastModified;
|
|
65
|
+
private consecutiveFailures;
|
|
66
|
+
private isPolling;
|
|
67
|
+
private wasUnhealthy;
|
|
68
|
+
/** Per-run abort controller — aborted on stop(), recreated on start() */
|
|
69
|
+
private _runAbort;
|
|
70
|
+
private _fetchController;
|
|
71
|
+
private _activePoll;
|
|
72
|
+
constructor(config: OpenApiPollerConfig);
|
|
73
|
+
/**
|
|
74
|
+
* Start polling for changes.
|
|
75
|
+
*/
|
|
76
|
+
start(): void;
|
|
77
|
+
/**
|
|
78
|
+
* Stop polling.
|
|
79
|
+
*/
|
|
80
|
+
stop(): void;
|
|
81
|
+
/**
|
|
82
|
+
* Get the current content hash.
|
|
83
|
+
*/
|
|
84
|
+
getHash(): string | null;
|
|
85
|
+
/**
|
|
86
|
+
* Get the number of consecutive failures.
|
|
87
|
+
*/
|
|
88
|
+
getConsecutiveFailures(): number;
|
|
89
|
+
/**
|
|
90
|
+
* Perform a single poll cycle.
|
|
91
|
+
*/
|
|
92
|
+
poll(): Promise<void>;
|
|
93
|
+
private pollWithSignal;
|
|
94
|
+
private fetchWithRetry;
|
|
95
|
+
private doFetch;
|
|
96
|
+
/**
|
|
97
|
+
* Dispose the poller and release resources.
|
|
98
|
+
*/
|
|
99
|
+
dispose(): void;
|
|
100
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI Tool Loader
|
|
3
|
+
*
|
|
4
|
+
* Converts OpenAPI specifications into Enclave ToolDefinitions.
|
|
5
|
+
* Uses mcp-from-openapi as an optional peer dependency.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import type { ToolDefinition } from '../tool-registry';
|
|
10
|
+
/**
|
|
11
|
+
* Options for the OpenAPI tool loader.
|
|
12
|
+
*/
|
|
13
|
+
export interface LoaderOptions {
|
|
14
|
+
/** Base URL for the upstream API */
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
/** Additional headers for API requests */
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
/** Custom naming strategy for tool names */
|
|
19
|
+
namingStrategy?: (method: string, path: string, operationId?: string) => string;
|
|
20
|
+
/** Only include these operation IDs */
|
|
21
|
+
includeOperations?: string[];
|
|
22
|
+
/** Exclude these operation IDs */
|
|
23
|
+
excludeOperations?: string[];
|
|
24
|
+
/** Default timeout per API call in ms @default 30000 */
|
|
25
|
+
perToolDeadlineMs?: number;
|
|
26
|
+
/** Source/service name prefix for default tool names (prevents collisions across sources) */
|
|
27
|
+
sourceName?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Authentication configuration for upstream API requests.
|
|
31
|
+
*/
|
|
32
|
+
export interface UpstreamAuth {
|
|
33
|
+
type: 'bearer' | 'api-key' | 'basic';
|
|
34
|
+
token?: string;
|
|
35
|
+
header?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Converts OpenAPI specs into Enclave ToolDefinitions.
|
|
39
|
+
*/
|
|
40
|
+
export declare class OpenApiToolLoader {
|
|
41
|
+
private tools;
|
|
42
|
+
private toolNames;
|
|
43
|
+
private specHash;
|
|
44
|
+
private readonly options;
|
|
45
|
+
private readonly auth?;
|
|
46
|
+
private constructor();
|
|
47
|
+
/**
|
|
48
|
+
* Create a loader from a URL.
|
|
49
|
+
*/
|
|
50
|
+
static fromURL(url: string, options?: LoaderOptions, auth?: UpstreamAuth): Promise<OpenApiToolLoader>;
|
|
51
|
+
/**
|
|
52
|
+
* Create a loader from a spec object.
|
|
53
|
+
*/
|
|
54
|
+
static fromSpec(spec: Record<string, unknown>, options?: LoaderOptions, auth?: UpstreamAuth): Promise<OpenApiToolLoader>;
|
|
55
|
+
/**
|
|
56
|
+
* Get all loaded tool definitions.
|
|
57
|
+
*/
|
|
58
|
+
getTools(): ToolDefinition[];
|
|
59
|
+
/**
|
|
60
|
+
* Get all tool names.
|
|
61
|
+
*/
|
|
62
|
+
getToolNames(): Set<string>;
|
|
63
|
+
/**
|
|
64
|
+
* Get the hash of the loaded spec.
|
|
65
|
+
*/
|
|
66
|
+
getSpecHash(): string;
|
|
67
|
+
/**
|
|
68
|
+
* Parse the OpenAPI spec and generate tool definitions.
|
|
69
|
+
*/
|
|
70
|
+
private loadFromSpec;
|
|
71
|
+
/**
|
|
72
|
+
* Extract operations from OpenAPI paths.
|
|
73
|
+
*/
|
|
74
|
+
private extractOperations;
|
|
75
|
+
/**
|
|
76
|
+
* Merge path-level and operation-level parameters, de-duplicating by (name, in).
|
|
77
|
+
* Operation-level entries override path-level ones.
|
|
78
|
+
*/
|
|
79
|
+
private mergeParameters;
|
|
80
|
+
/**
|
|
81
|
+
* Build a Zod args schema from an OpenAPI operation.
|
|
82
|
+
*/
|
|
83
|
+
private buildArgsSchema;
|
|
84
|
+
/**
|
|
85
|
+
* Serialize a parameter value for use in URLs/headers.
|
|
86
|
+
* Arrays and objects are JSON-stringified; primitives use String().
|
|
87
|
+
*/
|
|
88
|
+
private static serializeParam;
|
|
89
|
+
/**
|
|
90
|
+
* Check if a content-type string represents a JSON media type.
|
|
91
|
+
* Matches 'application/json' and structured syntax suffix '+json' (RFC 6838).
|
|
92
|
+
*/
|
|
93
|
+
private static isJsonContentType;
|
|
94
|
+
/**
|
|
95
|
+
* Map an OpenAPI schema type to the corresponding Zod type.
|
|
96
|
+
*/
|
|
97
|
+
private mapOpenApiType;
|
|
98
|
+
/**
|
|
99
|
+
* Build a Zod schema for the request body based on the media type.
|
|
100
|
+
* Returns both the schema and the chosen media type for correct serialization.
|
|
101
|
+
*/
|
|
102
|
+
private buildBodySchema;
|
|
103
|
+
/**
|
|
104
|
+
* Create a handler function for an OpenAPI operation.
|
|
105
|
+
*/
|
|
106
|
+
private createHandler;
|
|
107
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enclave-vm/broker",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"description": "Tool broker and session management for the EnclaveJS streaming runtime",
|
|
5
5
|
"author": "AgentFront <info@agentfront.dev>",
|
|
6
6
|
"homepage": "https://github.com/agentfront/enclave",
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
}
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@enclave-vm/types": "2.
|
|
38
|
-
"@enclave-vm/stream": "2.
|
|
39
|
-
"@enclave-vm/core": "2.
|
|
40
|
-
"minimatch": "^10.
|
|
37
|
+
"@enclave-vm/types": "2.13.0",
|
|
38
|
+
"@enclave-vm/stream": "2.13.0",
|
|
39
|
+
"@enclave-vm/core": "2.13.0",
|
|
40
|
+
"minimatch": "^10.2.5",
|
|
41
41
|
"zod": "^4.3.6"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/tool-registry.d.ts
CHANGED
|
@@ -106,6 +106,11 @@ export declare class ToolRegistry {
|
|
|
106
106
|
* @returns true if tool was removed, false if not found
|
|
107
107
|
*/
|
|
108
108
|
unregister(name: string): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Replace an existing tool with a new definition.
|
|
111
|
+
* If the tool doesn't exist, it registers it as new.
|
|
112
|
+
*/
|
|
113
|
+
replace<TArgs = unknown, TResult = unknown>(definition: ToolDefinition<TArgs, TResult>): void;
|
|
109
114
|
/**
|
|
110
115
|
* Check if a tool is registered
|
|
111
116
|
*/
|