Skip to content

Commit

Permalink
Merge pull request #854 from ManishMadan2882/main
Browse files Browse the repository at this point in the history
Message Streaming with the Mock Server
  • Loading branch information
dartpain authored Feb 14, 2024
2 parents 5dcde67 + 70ad1fb commit 0e38c67
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions mock-backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mock-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"json-server": "^0.17.4",
"uuid": "^9.0.1"
},
Expand Down
46 changes: 35 additions & 11 deletions mock-backend/src/server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import jsonServer from "json-server";
import routes from "./mocks/routes.json" assert { type: "json" };
import { v4 as uuid } from "uuid";

import cors from 'cors'
const server = jsonServer.create();
const router = jsonServer.router("./src/mocks/db.json");
const middlewares = jsonServer.defaults();

const localStorage = [];

server.use(middlewares);

server.use(cors({ origin: '*' }))
server.use(jsonServer.rewriter(routes));

server.use((req, res, next) => {
Expand Down Expand Up @@ -49,16 +49,40 @@ router.render = (req, res) => {
} else {
res.status(404).jsonp({});
}
} else if (req.url === "/stream") {
res.status(200).jsonp({
data: "The answer is 42",
sources: [
"https://en.wikipedia.org/wiki/42_(number)",
"https://en.wikipedia.org/wiki/42_(number)",
],
conversation_id: "1234",
} else if (req.url === "/stream" && req.method === "POST") {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
} else {
const message = ('Hi, How are you today?').split(' ');
let index = 0;
const interval = setInterval(() => {
if (index < message.length) {
res.write(`data: {"answer": "${message[index++]} "}\n`);
} else {
res.write(`data: {"type": "id", "id": "65cbc39d11f077b9eeb06d26"}\n`)
res.write(`data: {"type": "end"}\n`)
clearInterval(interval); // Stop the interval once the message is fully streamed
res.end(); // End the response
}
}, 500); // Send a word every 1 second
}
else if (req.url === '/search' && req.method === 'POST') {
res.status(200).json(
[
{
"text": "\n\n/api/answer\nIt's a POST request that sends a JSON in body with 4 values. It will receive an answer for a user provided question.\n",
"title": "API-docs.md"
},
{
"text": "\n\nOur Standards\n\nExamples of behavior that contribute to a positive environment for our\ncommunity include:\n* Demonstrating empathy and kindness towards other people\n",
"title": "How-to-use-different-LLM.md"
}
]
)
}
else {
res.status(res.statusCode).jsonp(res.locals.data);
}
};
Expand Down

0 comments on commit 0e38c67

Please sign in to comment.