The code sets up an Express.js server that handles various routes for a chat application, including handling conversations, rendering messages, and serving static files. The startServer
function is exported as a module, allowing it to be used in other parts of the application to start the server.
npm run import -- "resume express chat service"
const path = require('path')
const {doClick, doKeys, getMonitor, serveHomepage} = importer.import("express automation routes")
const handleConversation = importer.import("handle conversation")
const renderMessages = importer.import("render message history")
const renderHistory = importer.import("render history navigation")
const selectModel = importer.import("select llm")
const {OUTPUT_PATH} = importer.import("stable diffusion request")
const {DEFAULT_MODEL} = importer.import("general chit chat")
const {Remarkable} = require('remarkable')
const md = new Remarkable({html: true, xhtmlOut: true, breaks: true})
const express = require('express')
const cors = require('cors')
async function handleRequest(req, res) {
let promptModel = await selectModel(DEFAULT_MODEL)
let result = await handleConversation(promptModel, req.body.session, req.body.content, req.body.image, req.body.otr)
if(req.headers.accept == 'application/json') {
return res.json({ content: result });
}
const mdHtml = md.render(result)
return res.send(mdHtml)
}
async function startServer() {
var app = express()
app.use(cors({
credentials: true,
origin: '*'
}))
app.use(express.json({ limit: '20mb' }))
app.get('/monitors/*', getMonitor)
app.post('/', handleRequest)
app.get('/embed', renderMessages)
app.get('/history', renderHistory)
app.use('/txt2img-images', express.static(path.join(OUTPUT_PATH, 'txt2img-images')))
app.get('/', function (req, res) {
res.send(importer.interpret('chat page').code)
})
let port = process.env.CHAT_PORT || 8181
app.listen(port, () => console.log('Listening on https://localhost:' + port))
}
module.exports = startServer
// Import necessary modules and set up Remarkable markdown parser
const path = require('path');
const { doClick, doKeys, getMonitor, serveHomepage } = require('./express-automation-routes');
const handleConversation = require('./handle-conversation');
const renderMessages = require('./render-message-history');
const renderHistory = require('./render-history-navigation');
const selectModel = require('./select-llm');
const { OUTPUT_PATH } = require('./stable-diffusion-request');
const { DEFAULT_MODEL } = require('./general-chit-chat');
const cors = require('cors');
const express = require('express');
const md = new (require('remarkable'))({ html: true, xhtmlOut: true, breaks: true });
// Set up Express.js application
const app = express();
// Enable CORS with credentials and allow all origins
app.use(cors({ credentials: true, origin: '*' }));
// Enable JSON body parsing with a limit of 20MB
app.use(express.json({ limit: '20mb' }));
// Set up routes
app.get('/monitors/*', getMonitor);
app.post('/', handleRequest);
app.get('/embed', renderMessages);
app.get('/history', renderHistory);
app.use('/txt2img-images', express.static(path.join(OUTPUT_PATH, 'txt2img-images')));
app.get('/', (req, res) => {
const chatPageInterpretation = require('./chat-page');
res.send(chatPageInterpretation.code);
});
// Set up port and start server
const port = process.env.CHAT_PORT || 8181;
app.listen(port, () => console.log(`Listening on https://localhost:${port}`));
// Function to handle incoming requests
async function handleRequest(req, res) {
// Select the default model or the one specified in the prompt
const promptModel = await selectModel(DEFAULT_MODEL);
// Handle the conversation and get the result
const result = await handleConversation(promptModel, req.body.session, req.body.content, req.body.image, req.body.otr);
// Check if the client accepts JSON
if (req.headers.accept === 'application/json') {
// Return the result as JSON
return res.json({ content: result });
} else {
// Render the result as markdown HTML
const mdHtml = md.render(result);
return res.send(mdHtml);
}
}
// Function to start the server
async function startServer() {
// Use the app instance to set up the server
require('./start-server')(app);
}
// Export the startServer function
module.exports = startServer;
Code Breakdown
The code imports various modules and variables using the importer
object, which is not shown in the code snippet. The imported modules are:
express
: a Node.js web frameworkremarkable
: a Markdown parser librarycors
: a middleware for Cross-Origin Resource Sharing (CORS)express.json
: a middleware for parsing JSON dataexpress.static
: a middleware for serving static filespath
: a Node.js module for working with file pathsThe imported variables are:
doClick
, doKeys
, getMonitor
, serveHomepage
: functions from an unknown modulehandleConversation
, renderMessages
, renderHistory
, selectModel
, OUTPUT_PATH
, DEFAULT_MODEL
: functions or variables from various modulesRemarkable
: a constructor function for the remarkable
librarymd
: an instance of the Remarkable
parserhandleRequest
FunctionThe handleRequest
function is an asynchronous function that takes an HTTP request (req
) and response (res
) as arguments. It does the following:
selectModel
function.handleConversation
function, passing in the selected prompt model, and other parameters from the request body.remarkable
library and returns the rendered HTML as the response.handleRequest
function is assigned to the /
endpoint using the app.post()
method.startServer
FunctionThe startServer
function is an asynchronous function that starts an Express.js server. It does the following:
express()
function.cors()
function, allowing requests from any origin.express.json()
middleware./monitors/*
: uses the getMonitor
function/
: uses the handleRequest
function/embed
: uses the renderMessages
function/history
: uses the renderHistory
function/txt2img-images
: serves static files from the specified path using the express.static()
middleware/
: serves the chat page using the serveHomepage
functionstartServer
FunctionThe startServer
function is exported as a module, making it available for use in other parts of the application.