Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chat: smoother api (fixes #7449) #7452

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions chatapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,63 @@ app.get('/', (req: any, res: any) => {
});
});

const isValidData = (data: any) => data && typeof data === 'object' && !Array.isArray(data) && Object.keys(data).length > 0;

// WebSocket connection handling
wss.on('connection', (ws) => {
ws.on('message', async (data) => {
try {
data = JSON.parse(data.toString());
if (!isValidData(data)) {
ws.send(JSON.stringify({ 'error': 'Invalid data format.' }));
return;
}

const chatResponse = await chat(data, true, (response) => {
ws.send(JSON.stringify({ 'type': 'partial', response }));
});

if (data && typeof data === 'object') {
const chatResponse = await chat(data, true, (response) => {
ws.send(JSON.stringify({ 'type': 'partial', response }));
});

if (chatResponse) {
ws.send(JSON.stringify({
'type': 'final',
'completionText': chatResponse.completionText,
'couchDBResponse': chatResponse.couchSaveResponse
}));
}
} else {
ws.send('Error processing input data!');
if (chatResponse) {
ws.send(JSON.stringify({
'type': 'final',
'completionText': chatResponse.completionText,
'couchDBResponse': chatResponse.couchSaveResponse
}));
}
} catch (error: any) {
ws.send(`Error: ${error.message}`);
}
});

ws.on('close', () => {
console.log('WebSocket connection closed'); // eslint-disable-line no-console
});
});

app.post('/', async (req: any, res: any) => {
try {
const { data, save } = req.body;
const { data, save } = req.body;

if (typeof data !== 'object' || Array.isArray(data) || Object.keys(data).length === 0) {
res.status(400).json({ 'error': 'Bad Request', 'message': 'The "data" field must be a non-empty object' });
}
if (!isValidData(data)) {
return res.status(400).json({ 'error': 'Bad Request', 'message': 'The "data" field must be a non-empty object' });
}

try {
if (!save) {
const response = await chatNoSave(data.content, data.aiProvider, false);
res.status(200).json({
return res.status(200).json({
'status': 'Success',
'chat': response
});
} else if (save && data && typeof data === 'object') {
} else {
const response = await chat(data, false);
res.status(201).json({
return res.status(201).json({
'status': 'Success',
'chat': response?.completionText,
'couchDBResponse': response?.couchSaveResponse
});
} else {
res.status(400).json({ 'error': 'Bad Request', 'message': 'Error processing "data" object' });
}
} catch (error: any) {
res.status(500).json({ 'error': 'Internal Server Error', 'message': error.message });
return res.status(500).json({ 'error': 'Internal Server Error', 'message': error.message });
}
});

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "planet",
"license": "AGPL-3.0",
"version": "0.14.26",
"version": "0.14.27",
"myplanet": {
"latest": "v0.14.97",
"latest": "v0.14.98",
"min": "v0.14.0"
},
"scripts": {
Expand Down
Loading