Skip to content

Commit

Permalink
feat(api): allow CORS all origins and handle empty message param
Browse files Browse the repository at this point in the history
  • Loading branch information
waylaidwanderer committed Feb 6, 2023
1 parent ca80ce5 commit 188565a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,26 @@ To start a conversation with ChatGPT, send a POST request to the server's `/conv
```
The server will return a JSON object containing ChatGPT's response:
```JSON
// HTTP/1.1 200 OK
{
"response": "I'm doing well, thank you! How are you?",
"conversationId": "your-conversation-id",
"messageId": "response-message-id"
}
```

If the request is unsuccessful, the server will return a JSON object with an error message and a status code of 503.
If the request is unsuccessful, the server will return a JSON object with an error message.

If the request object is missing a required property (e.g. `message`):
```JSON
// HTTP/1.1 400 Bad Request
{
"error": "The message parameter is required."
}
```
If there was an error sending the message to ChatGPT:
```JSON
// HTTP/1.1 503 Service Unavailable
{
"error": "There was an error communicating with ChatGPT."
}
Expand Down
29 changes: 24 additions & 5 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
import fastify from 'fastify';
import cors from '@fastify/cors';
import { FastifySSEPlugin } from "fastify-sse-v2";
import fs from 'fs';
import { pathToFileURL } from 'url'
Expand Down Expand Up @@ -45,7 +46,10 @@ const chatGptClient = new ChatGPTClient(settings.openaiApiKey, settings.chatGptC

const server = fastify();

server.register(FastifySSEPlugin);
await server.register(FastifySSEPlugin);
await server.register(cors, {
origin: '*',
});

server.post('/conversation', async (request, reply) => {
const body = request.body || {};
Expand All @@ -67,6 +71,15 @@ server.post('/conversation', async (request, reply) => {
let result;
let error;
try {
if (!body.message) {
const invalidError = new Error();
invalidError.data = {
code: 400,
message: 'The message parameter is required.',
};
// noinspection ExceptionCaughtLocallyJS
throw invalidError;
}
const parentMessageId = body.parentMessageId ? body.parentMessageId.toString() : undefined;
result = await chatGptClient.sendMessage(body.message, {
conversationId,
Expand All @@ -87,18 +100,24 @@ server.post('/conversation', async (request, reply) => {
console.debug(result);
}
} else {
console.error(error);
const code = error?.data?.code || 503;
if (code === 503) {
console.error(error);
} else if (settings.apiOptions?.debug) {
console.debug(error);
}
const message = error?.data?.message || 'There was an error communicating with ChatGPT.';
if (body.stream === true) {
reply.sse({
id: '',
event: 'error',
data: JSON.stringify({
code: 503,
error: 'There was an error communicating with ChatGPT.',
code,
error: message,
}),
});
} else {
reply.code(503).send({ error: 'There was an error communicating with ChatGPT.' });
reply.code(code).send({ error: message });
}
}
});
Expand Down
23 changes: 23 additions & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"api-wrapper"
],
"dependencies": {
"@fastify/cors": "^8.2.0",
"@fortaine/fetch-event-source": "^3.0.6",
"boxen": "^7.0.1",
"clipboardy": "^3.0.0",
Expand Down

0 comments on commit 188565a

Please sign in to comment.