mirror of
https://github.com/cocktailpeanut/dalai
synced 2025-02-14 20:14:29 +01:00
simplify implementation. default prompts saved in same directory but custom ones in user directory
This commit is contained in:
parent
a0c3f52ac0
commit
4fadbda1db
@ -1,4 +1,5 @@
|
||||
const express = require('express')
|
||||
const os = require("os");
|
||||
const http = require('http')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
@ -16,47 +17,54 @@ const start = (port, home) => {
|
||||
app.get("/", (req, res) => {
|
||||
res.render("index")
|
||||
})
|
||||
|
||||
const defaultDir = path.join(__dirname, "prompts");
|
||||
const customDir = path.join(os.homedir(), "dalai", "config", "prompts");
|
||||
|
||||
if (!fs.existsSync(customDir)) {
|
||||
fs.mkdirSync(customDir, { recursive: true });
|
||||
console.log(`Created custom directory: ${customDir}`);
|
||||
}
|
||||
|
||||
app.get("/prompts", (req, res) => {
|
||||
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");
|
||||
const isCustom = promptsDir.includes("custom");
|
||||
const editable = isCustom ? true : false;
|
||||
return { name: promptName, value: promptValue, editable: editable };
|
||||
});
|
||||
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`);
|
||||
let prompts = [];
|
||||
|
||||
const filesToPrompts = (files, directory, editable) =>
|
||||
files.flatMap((file) => {
|
||||
const filePath = path.join(directory, file);
|
||||
const stats = fs.statSync(filePath, "utf8");
|
||||
|
||||
// Filter out directories and non .txt files
|
||||
if (!stats.isFile() || !file.endsWith('.txt')) {
|
||||
return []
|
||||
}
|
||||
const promptName = path.basename(file, ".txt");
|
||||
const promptValue = fs.readFileSync(filePath, "utf8");
|
||||
return { name: promptName, value: promptValue, editable };
|
||||
});
|
||||
|
||||
// Read default prompts
|
||||
fs.readdir(defaultDir, (err, files) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
prompts = filesToPrompts(files, defaultDir, false);
|
||||
|
||||
// Read custom prompts
|
||||
fs.readdir(customDir, (err, files) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
prompts = prompts.concat(filesToPrompts(files, customDir, true));
|
||||
res.json(prompts);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
httpServer.listen(port, () => {
|
||||
console.log(`Server running on http://localhost:${port}/`)
|
||||
})
|
||||
|
@ -170,11 +170,11 @@
|
||||
n_predict: 200,
|
||||
top_k: 40,
|
||||
top_p: 0.9,
|
||||
temp: 0.8,
|
||||
temp: 0.8,
|
||||
repeat_last_n: 64,
|
||||
repeat_penalty: 1.3,
|
||||
debug: false,
|
||||
html: true,
|
||||
html: true,
|
||||
models: []
|
||||
}
|
||||
const socket = io();
|
||||
@ -306,7 +306,7 @@ ${fields}
|
||||
const say = (msg, id) => {
|
||||
let item = document.createElement('li');
|
||||
if (id) item.setAttribute("data-id", id)
|
||||
item.innerHTML = msg;
|
||||
item.innerHTML = msg;
|
||||
messages.prepend(item);
|
||||
}
|
||||
socket.emit('request', {
|
||||
@ -329,9 +329,9 @@ ${fields}
|
||||
const id = request.id
|
||||
let existing = document.querySelector(`[data-id='${id}']`)
|
||||
if (existing) {
|
||||
existing.innerHTML = existing.innerHTML + response
|
||||
//existing.innerHTML = existing.innerHTML.replaceAll(/\r?\n\x1B\[\d+;\d+H./g, "");
|
||||
//existing.textContent = existing.textContent.replaceAll("\\n", "\n");
|
||||
existing.innerHTML = existing.innerHTML + response
|
||||
//existing.innerHTML = existing.innerHTML.replaceAll(/\r?\n\x1B\[\d+;\d+H./g, "");
|
||||
//existing.textContent = existing.textContent.replaceAll("\\n", "\n");
|
||||
} else {
|
||||
say(response, id)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user