langcode
Version:
A Plugin-Based Framework for Managing and Using LangChain
182 lines (181 loc) • 7.03 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginRunner = void 0;
const types_1 = require("../types");
const Logger_1 = require("./Logger");
const events_1 = require("events");
class PluginRunner extends events_1.EventEmitter {
constructor(debug = false, logFile = null, strict) {
super();
this.activePlugins = new Map();
this.tag = "langcode";
this.histories = [];
this.historyLimit = 1000;
this.logger = new Logger_1.Logger({
debug: debug,
filePath: logFile,
tag: this.tag,
});
this.strict = strict;
types_1.logLevels.forEach((level) => {
const loggerEvent = `${this.tag}:${level}`;
this.logger.on(loggerEvent, (payload) => {
this.emit(loggerEvent, payload);
this.emit(this.tag, payload);
});
});
}
pushHistory(item) {
const itema = Object.assign(Object.assign({}, item), { timestamp: new Date().toISOString() });
if (this.histories.length >= this.historyLimit)
this.histories.shift();
this.histories.push(itema);
// this.logger[item.level](`🔌 history added: ${JSON.stringify(itema,null,2)}`);
}
history() {
return [...this.histories];
}
async initialize(configList) {
for (const { pluginName, config } of configList) {
try {
const pluginModule = await Promise.resolve(`${`../plugins/${pluginName}/${pluginName}Plugin`}`).then(s => __importStar(require(s)));
const pluginInstance = new pluginModule.default();
this.logger.info(`🔌 Başlatılıyor: ${pluginName}`);
this.pushHistory({
level: "info",
phase: "init",
pluginName,
message: `🔌 Başlatılıyor: ${pluginName}`,
});
await pluginInstance.init(config);
this.activePlugins.set(pluginName, pluginInstance);
this.logger.success(`✅ Aktif: ${pluginName}`);
this.pushHistory({
level: "success",
phase: "init",
pluginName,
message: `✅ Aktif: ${pluginName}`,
});
}
catch (err) {
this.logger.error(`❌ Plugin '${pluginName}' yüklenemedi:`, err);
this.pushHistory({
level: "error",
phase: "init",
pluginName,
message: `❌ Plugin '${pluginName}' yüklenemedi:`,
data: err
});
if (this.strict) {
throw { error: err, history: this.histories };
}
}
}
}
async run(pluginName, args) {
const plugin = this.activePlugins.get(pluginName);
if (!plugin) {
this.logger.error(`Plugin '${pluginName}' aktif değil.`);
this.pushHistory({
level: "error",
phase: "run",
pluginName,
message: `Plugin '${pluginName}' aktif değil.`
});
if (this.strict) {
throw { error: `Plugin '${pluginName}' aktif değil.`, history: this.histories };
}
}
this.logger.info(`⏳ Çalıştırılıyor: ${pluginName}`, args);
this.pushHistory({
level: "info",
phase: "run",
pluginName,
data: args,
message: `⏳ Çalıştırılıyor: ${pluginName}`
});
const start = Date.now();
const result = await (plugin === null || plugin === void 0 ? void 0 : plugin.run(args));
this.logger.success(`✅ Tamamlandı (${pluginName}) - ${Date.now() - start}ms`);
this.logger.debug("📤 Cevap:", result);
this.pushHistory({
level: "success",
phase: "run",
pluginName,
data: result,
message: `✅ Tamamlandı (${pluginName}) - ${Date.now() - start}ms`
});
return result;
}
async getExpose(pluginName) {
const plugin = this.activePlugins.get(pluginName);
if (!plugin) {
this.logger.error(`Plugin '${pluginName}' aktif değil.`);
this.pushHistory({
level: "error",
phase: "expose",
pluginName,
message: `Plugin '${pluginName}' aktif değil.`
});
if (this.strict) {
throw { error: `Plugin '${pluginName}' aktif değil.`, history: this.histories };
}
}
if (typeof (plugin === null || plugin === void 0 ? void 0 : plugin.expose) === "function") {
this.pushHistory({
level: "success",
phase: "expose",
pluginName,
message: `Expose başarılı`
});
return plugin.expose();
}
this.logger.error(`Plugin '${pluginName}' expose() sağlamıyor.`);
this.pushHistory({
level: "error",
phase: "expose",
pluginName,
message: `Plugin '${pluginName}' expose() sağlamıyor.`
});
if (this.strict) {
throw { error: `Plugin '${pluginName}' expose() sağlamıyor.`, history: this.histories };
}
}
}
exports.PluginRunner = PluginRunner;
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}