n8n
Version:
n8n Workflow Automation Tool
106 lines • 3.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalGateway = void 0;
const node_events_1 = require("node:events");
const nanoid_1 = require("nanoid");
const REQUEST_TIMEOUT_MS = 60_000;
class LocalGateway {
constructor() {
this.pendingRequests = new Map();
this.emitter = new node_events_1.EventEmitter();
this._connected = false;
this._connectedAt = null;
this._rootPath = null;
this._hostIdentifier = null;
this._toolCategories = [];
this._availableTools = [];
}
get isConnected() {
return this._connected;
}
get connectedAt() {
return this._connectedAt;
}
get rootPath() {
return this._rootPath;
}
getAvailableTools() {
return this._availableTools;
}
getToolsByCategory(category) {
return this._availableTools.filter((t) => t.annotations?.category === category);
}
onRequest(listener) {
this.emitter.on('filesystem-request', listener);
return () => this.emitter.off('filesystem-request', listener);
}
onDisconnect(listener) {
this.emitter.on('gateway-disconnect', listener);
return () => this.emitter.off('gateway-disconnect', listener);
}
init(data) {
this._rootPath = data.rootPath;
this._hostIdentifier = data.hostIdentifier ?? null;
this._toolCategories = data.toolCategories ?? [];
this._availableTools = data.tools;
this._connected = true;
this._connectedAt = new Date().toISOString();
}
resolveRequest(requestId, result, error) {
const pending = this.pendingRequests.get(requestId);
if (!pending)
return false;
clearTimeout(pending.timer);
this.pendingRequests.delete(requestId);
if (error) {
pending.reject(new Error(error));
return true;
}
pending.resolve(result ?? { content: [] });
return true;
}
disconnect() {
this.emitter.emit('gateway-disconnect', {
type: 'gateway-disconnect',
});
this._connected = false;
this._connectedAt = null;
this._rootPath = null;
this._hostIdentifier = null;
this._toolCategories = [];
this._availableTools = [];
for (const [id, pending] of this.pendingRequests) {
clearTimeout(pending.timer);
pending.reject(new Error('Local gateway disconnected'));
this.pendingRequests.delete(id);
}
}
getStatus() {
return {
connected: this._connected,
connectedAt: this._connectedAt,
directory: this._rootPath,
hostIdentifier: this._hostIdentifier,
toolCategories: this._toolCategories,
};
}
async callTool(toolCall) {
if (!this._connected) {
throw new Error('Local gateway is not connected');
}
const requestId = `gw_${(0, nanoid_1.nanoid)()}`;
return await new Promise((resolve, reject) => {
const timer = setTimeout(() => {
this.pendingRequests.delete(requestId);
reject(new Error(`Local gateway request timed out after ${REQUEST_TIMEOUT_MS}ms`));
}, REQUEST_TIMEOUT_MS);
this.pendingRequests.set(requestId, { resolve, reject, timer, toolCall });
this.emitter.emit('filesystem-request', {
type: 'filesystem-request',
payload: { requestId, toolCall },
});
});
}
}
exports.LocalGateway = LocalGateway;
//# sourceMappingURL=local-gateway.js.map