From c905d7cc2aa974ad3cdbb21ec308a52516037748 Mon Sep 17 00:00:00 2001 From: xiaohuo Date: Fri, 20 Dec 2024 02:11:38 +0800 Subject: [PATCH] feat: make express payload limit configurable --- .env.example | 3 +++ packages/client-direct/src/api.ts | 43 +++++++++++++++++++------------ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/.env.example b/.env.example index d2d89eda85..ad1bf056df 100644 --- a/.env.example +++ b/.env.example @@ -34,6 +34,9 @@ LARGE_HYPERBOLIC_MODEL= # Default: meta-llama/Meta-Llama-3.1-405-Instruc # Speech Synthesis ELEVENLABS_XI_API_KEY= # API key from elevenlabs +# Direct Client Setting +EXPRESS_MAX_PAYLOAD= # Default: 100kb + # ElevenLabs Settings ELEVENLABS_MODEL_ID=eleven_multilingual_v2 ELEVENLABS_VOICE_ID=21m00Tcm4TlvDq8ikWAM diff --git a/packages/client-direct/src/api.ts b/packages/client-direct/src/api.ts index fe26cee8f3..4221772d1c 100644 --- a/packages/client-direct/src/api.ts +++ b/packages/client-direct/src/api.ts @@ -5,17 +5,27 @@ import cors from "cors"; import { AgentRuntime, elizaLogger, + getEnvVariable, validateCharacterConfig, } from "@ai16z/eliza"; import { REST, Routes } from "discord.js"; +import { DirectClient } from "."; -export function createApiRouter(agents: Map, directClient) { +export function createApiRouter( + agents: Map, + directClient: DirectClient +) { const router = express.Router(); router.use(cors()); router.use(bodyParser.json()); router.use(bodyParser.urlencoded({ extended: true })); + router.use( + express.json({ + limit: getEnvVariable("EXPRESS_MAX_PAYLOAD") || "100kb", + }) + ); router.get("/", (req, res) => { res.send("Welcome, this is the REST API!"); @@ -51,33 +61,33 @@ export function createApiRouter(agents: Map, directClient) router.post("/agents/:agentId/set", async (req, res) => { const agentId = req.params.agentId; - console.log('agentId', agentId) - let agent:AgentRuntime = agents.get(agentId); + console.log("agentId", agentId); + let agent: AgentRuntime = agents.get(agentId); // update character if (agent) { // stop agent - agent.stop() - directClient.unregisterAgent(agent) + agent.stop(); + directClient.unregisterAgent(agent); // if it has a different name, the agentId will change } // load character from body - const character = req.body + const character = req.body; try { - validateCharacterConfig(character) - } catch(e) { - elizaLogger.error(`Error parsing character: ${e}`); - res.status(400).json({ - success: false, - message: e.message, - }); - return; + validateCharacterConfig(character); + } catch (e) { + elizaLogger.error(`Error parsing character: ${e}`); + res.status(400).json({ + success: false, + message: e.message, + }); + return; } // start it up (and register it) - agent = await directClient.startAgent(character) - elizaLogger.log(`${character.name} started`) + agent = await directClient.startAgent(character); + elizaLogger.log(`${character.name} started`); res.json({ id: character.id, @@ -85,7 +95,6 @@ export function createApiRouter(agents: Map, directClient) }); }); - router.get("/agents/:agentId/channels", async (req, res) => { const agentId = req.params.agentId; const runtime = agents.get(agentId);