diff --git a/bin/web/index.js b/bin/web/index.js index 0867174..9019b2d 100644 --- a/bin/web/index.js +++ b/bin/web/index.js @@ -17,21 +17,46 @@ const start = (port, home) => { res.render("index") }) app.get("/prompts", (req, res) => { - const promptsDir = path.join(__dirname, "prompts"); - fs.readdir(promptsDir, (err, files) => { - if (err) { - console.error(err); - return res.status(500).json({ error: "Internal server error" }); + const promptsDirs = [ + path.join(__dirname, "prompts"), + path.join(__dirname, "prompts", "custom") + ]; + const allPrompts = []; + let numDirsChecked = 0; + promptsDirs.forEach(promptsDir => { + if (fs.existsSync(promptsDir) && fs.statSync(promptsDir).isDirectory()) { + fs.readdir(promptsDir, (err, files) => { + if (err) { + console.error(err); + return res.status(500).json({ error: "Internal server error" }); + } + const prompts = files.filter(file => !fs.statSync(path.join(promptsDir, file)).isDirectory()) + .map((file) => { + const promptName = path.basename(file, ".txt"); + const promptValue = fs.readFileSync(path.join(promptsDir, file), "utf8"); + return { name: promptName, value: promptValue }; + }); + allPrompts.push(...prompts); + console.log(`Added ${prompts.length} prompts from ${promptsDir}`); + numDirsChecked++; + if (numDirsChecked === promptsDirs.length) { + res.json(allPrompts); + console.log(`Returning ${allPrompts.length} prompts`); + } + }); + } else { + console.error(`Directory not found: ${promptsDir}`); + numDirsChecked++; + if (numDirsChecked === promptsDirs.length) { + res.json(allPrompts); + console.log(`Returning ${allPrompts.length} prompts`); + } } - const prompts = files.map((file) => { - const promptName = path.basename(file, ".txt"); - const promptValue = fs.readFileSync(path.join(promptsDir, file), "utf8"); - return { name: promptName, value: promptValue }; - }); - res.json(prompts); }); }); + + httpServer.listen(port, () => { console.log(`Server running on http://localhost:${port}/`) }) diff --git a/bin/web/prompts/chatbot.txt b/bin/web/prompts/chatbot.txt index 774a787..ae822d0 100644 --- a/bin/web/prompts/chatbot.txt +++ b/bin/web/prompts/chatbot.txt @@ -1 +1,2 @@ -The expected response for a highly intelligent chatbot to "" is " \ No newline at end of file +The expected response for a highly intelligent chatbot to "" is +" \ No newline at end of file diff --git a/bin/web/prompts/default.txt b/bin/web/prompts/custom/default.txt similarity index 100% rename from bin/web/prompts/default.txt rename to bin/web/prompts/custom/default.txt