langpong
Version:
langcode api server version
188 lines (187 loc) • 7.14 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Langpong = void 0;
// Langpong.ts
const express_1 = __importDefault(require("express"));
const ManagerStore_1 = require("./ManagerStore");
/**
* Langpong
* - Express.js uygulamasını bir class olarak sarar
* - Port ve host dışarıdan alınır
* - ManagerStore'u kullanıp endpoint'leri tanımlar
*/
class Langpong {
constructor(options = {}) {
this.app = (0, express_1.default)();
// Varsayılan değerler
this.port = options.port || (process.env.PORT ? parseInt(process.env.PORT) : 3000);
this.host = options.host || process.env.HOST || "127.0.0.1";
// eğer store dışarıdan verilmemişse singleton alır
this.store = options.managerStore || ManagerStore_1.ManagerStore.getInstance({
logLevel: "debug",
// vb.
});
// Express middleware
this.app.use(express_1.default.json());
// Yönlendirmeleri kuralım
this.initializeRoutes();
}
/**
* Tüm API uç noktalarını tanımlayalım
*/
initializeRoutes() {
// 1) Manager init
this.app.post("/init", async (req, res) => {
try {
const { pluginConfigs } = req.body;
const sid = await this.store.createManagerAsync(pluginConfigs);
res.json({ success: true, sessionID: sid, message: "Manager created or initializing." });
}
catch (err) {
res.status(400).json({ success: false, error: err.error.message, history: err.history });
}
});
// 2) runPlugin
this.app.post("/runPlugin", async (req, res) => {
try {
const { sessionId, pluginName, pluginParams } = req.body;
const result = await this.store.runPlugin(sessionId, pluginName, pluginParams);
res.json({ success: true, result });
}
catch (err) {
res.status(400).json({ success: false, error: err.error || err.message, history: err.history });
}
});
this.app.post("/autorunPlugin", async (req, res) => {
try {
const { pluginConfig, pluginParams } = req.body;
if (!pluginConfig) {
res.status(400).json({ success: false, error: "pluginConfig gereklidir." });
}
const sessionId = await this.store.createManagerAsync([pluginConfig]);
const result = await this.store.runPlugin(sessionId, pluginConfig.pluginName, pluginParams);
this.store.removeManager(sessionId);
res.json({ success: true, result });
}
catch (err) {
res.status(400).json({ success: false, error: err.error || err.message, history: err.history });
}
});
this.app.post("/getPlugin", async (req, res) => {
try {
const { pluginName } = req.body;
const result = await this.store.getPlugin(pluginName);
res.json({ success: true, result });
}
catch (err) {
res.status(400).json({ success: false, error: err.error, history: err.history });
}
});
this.app.post("/getPlugins", async (req, res) => {
try {
const result = await this.store.getPlugins();
res.json({ success: true, result });
}
catch (err) {
res.status(400).json({ success: false, error: err.error, history: err.history });
}
});
// 3) createChain
this.app.post("/createChain", (req, res) => {
try {
const { sessionId, chainId, chainConfig } = req.body;
this.store.createChain(sessionId, chainId, chainConfig);
res.json({ success: true });
}
catch (err) {
res.status(400).json({ success: false, error: err.message });
}
});
// 4) runChain
this.app.post("/runChain", async (req, res) => {
try {
const { sessionId, chainId, input } = req.body;
const result = await this.store.runChain(sessionId, chainId, input);
res.json({ success: true, result });
}
catch (err) {
res.status(400).json({ success: false, error: err.message });
}
});
// 5) removeManager
this.app.post("/removeManager", (req, res) => {
try {
const { sessionId } = req.body;
const removed = this.store.removeManager(sessionId);
res.json({ success: removed });
}
catch (err) {
res.status(400).json({ success: false, error: err.message });
}
});
// 6) sharedMemory
this.app.post("/putSharedData", (req, res) => {
try {
const { sessionId, key, data } = req.body;
this.store.putSharedData(sessionId, key, data);
res.json({ success: true });
}
catch (err) {
res.status(400).json({ success: false, error: err.message });
}
});
this.app.post("/getSharedData", (req, res) => {
try {
const { sessionId, key } = req.body;
const value = this.store.getSharedData(sessionId, key);
res.json({ success: true, value });
}
catch (err) {
res.status(400).json({ success: false, error: err.message });
}
});
// 7) listManagers
this.app.post("/listManagers", (req, res) => {
const data = this.store.listActiveManagers();
res.json({ success: true, data });
});
// 8) shutdownAll
this.app.post("/shutdownAll", (req, res) => {
this.store.shutdownAllManagers();
res.json({ success: true });
});
}
/**
* Sunucuyu başlat
*/
start() {
return new Promise((resolve) => {
this.serverInstance = this.app.listen(this.port, this.host, () => {
console.log(`Langpong running at http://${this.host}:${this.port}`);
resolve();
});
});
}
/**
* Sunucuyu kapat (opsiyonel)
*/
stop() {
return new Promise((resolve, reject) => {
if (this.serverInstance) {
this.serverInstance.close((err) => {
if (err)
return reject(err);
console.log("Langpong stopped.");
resolve();
});
}
else {
resolve();
}
});
}
}
exports.Langpong = Langpong;