Fix #2, add execute bit to start.sh

This commit is contained in:
CncAnon 2023-08-30 10:52:24 +03:00
parent 52fe62d520
commit af3da23d5d
2 changed files with 13 additions and 4 deletions

17
main.js
View File

@ -3,6 +3,8 @@ require("dotenv").config();
const express = require('express'); const express = require('express');
const axios = require('axios'); const axios = require('axios');
const app = express(); const app = express();
const StringDecoder = require('string_decoder').StringDecoder;
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '100mb' })); app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({limit: '100mb', extended: true})); app.use(bodyParser.urlencoded({limit: '100mb', extended: true}));
@ -55,10 +57,12 @@ app.post('/v1/complete', async (req, res) => {
const response = await sourcegraph.post('', postData); const response = await sourcegraph.post('', postData);
let previousCompletion = ""; let previousCompletion = "";
// GPT-4 told me to use a StringDecoder so that multi-byte characters are correctly handled across chunks
let decoder = new StringDecoder('utf8');
let buffer = ""; // Buffer to hold incomplete lines let buffer = ""; // Buffer to hold incomplete lines
response.data.on('data', (chunk) => { response.data.on('data', (chunk) => {
buffer += chunk.toString(); buffer += decoder.write(chunk);
let lines = buffer.split("\n"); let lines = buffer.split("\n");
buffer = lines.pop(); // Keep the last (potentially incomplete) line in the buffer buffer = lines.pop(); // Keep the last (potentially incomplete) line in the buffer
@ -71,7 +75,8 @@ app.post('/v1/complete', async (req, res) => {
//console.log(resp); //console.log(resp);
if (isStream) { if (isStream) {
// SourceGraph API always returns the full string, but we need the diff // SourceGraph API always returns the full string, but we need the diff
const newPart = parsedData.completion.replace(previousCompletion, ''); // We can use .length because StringDecoder correctly handles multi-byte characters
const newPart = parsedData.completion.substring(previousCompletion.length);
previousCompletion = parsedData.completion; previousCompletion = parsedData.completion;
let resp = { completion: newPart, stop_reason: null }; let resp = { completion: newPart, stop_reason: null };
res.write(`event: completion\r\ndata: ${JSON.stringify(resp)}\r\n\r\n`); res.write(`event: completion\r\ndata: ${JSON.stringify(resp)}\r\n\r\n`);
@ -81,12 +86,16 @@ app.post('/v1/complete', async (req, res) => {
} }
} }
} catch (error) { } catch (error) {
// If an error is thrown, the JSON is not valid console.log("Invalid JSON chunk: ", chunk);
console.error('Invalid JSON:', chunk); // do nothing, the JSON chunk is incomplete
}}) }})
}); });
response.data.on('end', () => { response.data.on('end', () => {
// Since the last char will be a newline char and not a multi-byte one, we're sure that
// the decoder will be empty, so we can just end it.
decoder.end();
if (isStream) { if (isStream) {
let finalResp = {completion: "", stop_reason: "stop_sequence"}; let finalResp = {completion: "", stop_reason: "stop_sequence"};
res.write(`event: completion\r\ndata: ${JSON.stringify(finalResp)}\r\n\r\n`); res.write(`event: completion\r\ndata: ${JSON.stringify(finalResp)}\r\n\r\n`);

0
start.sh Normal file → Executable file
View File