2023-05-23 06:55:23 -04:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
import { useRecoilValue, useResetRecoilState, useSetRecoilState } from 'recoil';
|
2023-04-06 14:09:13 -07:00
|
|
|
import { SSE } from '~/data-provider/sse.mjs';
|
|
|
|
|
import createPayload from '~/data-provider/createPayload';
|
2023-03-28 22:39:27 +08:00
|
|
|
import store from '~/store';
|
feat: Auth and User System (#205)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* fix: issue with api error watcher (#12)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Update README.md
* Update LOCAL_INSTALL structure
* Add local testing instructions
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Fixup: LOCAL_INSTALL.md PS instructions (#200) (#30)
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
* feat: send user with completion to protect against abuse (#31)
* Fixup: LOCAL_INSTALL.md PS instructions (#200)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Send user id to openai to protect against abuse
* add meilisearch to gitignore
* Remove webpack
---------
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
---------
Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
Co-authored-by: Alfredo Fomitchenko <alfredo.fomitchenko@mail.polimi.it>
2023-05-07 10:04:51 -07:00
|
|
|
import { useAuthContext } from '~/hooks/AuthContext';
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
export default function MessageHandler() {
|
|
|
|
|
const submission = useRecoilValue(store.submission);
|
|
|
|
|
const setIsSubmitting = useSetRecoilState(store.isSubmitting);
|
2023-03-28 20:36:21 +08:00
|
|
|
const setMessages = useSetRecoilState(store.messages);
|
|
|
|
|
const setConversation = useSetRecoilState(store.conversation);
|
|
|
|
|
const resetLatestMessage = useResetRecoilState(store.latestMessage);
|
feat: Auth and User System (#205)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* fix: issue with api error watcher (#12)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Update README.md
* Update LOCAL_INSTALL structure
* Add local testing instructions
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Fixup: LOCAL_INSTALL.md PS instructions (#200) (#30)
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
* feat: send user with completion to protect against abuse (#31)
* Fixup: LOCAL_INSTALL.md PS instructions (#200)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Send user id to openai to protect against abuse
* add meilisearch to gitignore
* Remove webpack
---------
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
---------
Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
Co-authored-by: Alfredo Fomitchenko <alfredo.fomitchenko@mail.polimi.it>
2023-05-07 10:04:51 -07:00
|
|
|
const { token } = useAuthContext();
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
const { refreshConversations } = store.useConversations();
|
|
|
|
|
|
|
|
|
|
const messageHandler = (data, submission) => {
|
2023-03-28 22:39:27 +08:00
|
|
|
const { messages, message, initialResponse, isRegenerate = false } = submission;
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-04-10 18:27:06 -04:00
|
|
|
if (isRegenerate) {
|
2023-03-28 20:36:21 +08:00
|
|
|
setMessages([
|
|
|
|
|
...messages,
|
|
|
|
|
{
|
|
|
|
|
...initialResponse,
|
|
|
|
|
text: data,
|
|
|
|
|
parentMessageId: message?.overrideParentMessageId,
|
2023-03-28 22:39:27 +08:00
|
|
|
messageId: message?.overrideParentMessageId + '_',
|
2023-05-18 11:09:31 -07:00
|
|
|
submitting: true
|
2023-04-10 18:27:06 -04:00
|
|
|
// unfinished: true
|
2023-03-28 22:39:27 +08:00
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
]);
|
2023-04-10 18:27:06 -04:00
|
|
|
} else {
|
2023-03-28 20:36:21 +08:00
|
|
|
setMessages([
|
|
|
|
|
...messages,
|
|
|
|
|
message,
|
|
|
|
|
{
|
|
|
|
|
...initialResponse,
|
|
|
|
|
text: data,
|
|
|
|
|
parentMessageId: message?.messageId,
|
2023-03-28 22:39:27 +08:00
|
|
|
messageId: message?.messageId + '_',
|
2023-05-18 11:09:31 -07:00
|
|
|
submitting: true
|
2023-04-10 18:27:06 -04:00
|
|
|
// unfinished: true
|
2023-03-28 22:39:27 +08:00
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
]);
|
2023-04-10 18:27:06 -04:00
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const cancelHandler = (data, submission) => {
|
2023-04-11 03:26:38 +08:00
|
|
|
const { messages, isRegenerate = false } = submission;
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-04-11 03:26:38 +08:00
|
|
|
const { requestMessage, responseMessage, conversation } = data;
|
|
|
|
|
|
|
|
|
|
// update the messages
|
2023-04-10 18:27:06 -04:00
|
|
|
if (isRegenerate) {
|
|
|
|
|
setMessages([...messages, responseMessage]);
|
|
|
|
|
} else {
|
|
|
|
|
setMessages([...messages, requestMessage, responseMessage]);
|
|
|
|
|
}
|
2023-04-11 03:26:38 +08:00
|
|
|
setIsSubmitting(false);
|
|
|
|
|
|
|
|
|
|
// refresh title
|
|
|
|
|
if (requestMessage.parentMessageId == '00000000-0000-0000-0000-000000000000') {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
refreshConversations();
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
|
|
// in case it takes too long.
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
refreshConversations();
|
|
|
|
|
}, 5000);
|
2023-04-08 23:19:29 -04:00
|
|
|
}
|
2023-04-11 03:26:38 +08:00
|
|
|
|
2023-05-18 11:44:07 -07:00
|
|
|
setConversation((prevState) => ({
|
2023-04-11 03:26:38 +08:00
|
|
|
...prevState,
|
|
|
|
|
...conversation
|
|
|
|
|
}));
|
2023-03-28 20:36:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createdHandler = (data, submission) => {
|
2023-03-28 22:39:27 +08:00
|
|
|
const { messages, message, initialResponse, isRegenerate = false } = submission;
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
if (isRegenerate)
|
|
|
|
|
setMessages([
|
|
|
|
|
...messages,
|
|
|
|
|
{
|
|
|
|
|
...initialResponse,
|
|
|
|
|
parentMessageId: message?.overrideParentMessageId,
|
2023-03-28 22:39:27 +08:00
|
|
|
messageId: message?.overrideParentMessageId + '_',
|
|
|
|
|
submitting: true
|
|
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
]);
|
|
|
|
|
else
|
|
|
|
|
setMessages([
|
|
|
|
|
...messages,
|
|
|
|
|
message,
|
|
|
|
|
{
|
|
|
|
|
...initialResponse,
|
|
|
|
|
parentMessageId: message?.messageId,
|
2023-03-28 22:39:27 +08:00
|
|
|
messageId: message?.messageId + '_',
|
|
|
|
|
submitting: true
|
|
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const { conversationId } = message;
|
2023-05-18 11:44:07 -07:00
|
|
|
setConversation((prevState) => ({
|
2023-03-28 20:36:21 +08:00
|
|
|
...prevState,
|
2023-03-28 22:39:27 +08:00
|
|
|
conversationId
|
2023-03-28 20:36:21 +08:00
|
|
|
}));
|
|
|
|
|
resetLatestMessage();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const finalHandler = (data, submission) => {
|
2023-03-31 03:22:57 +08:00
|
|
|
const { messages, isRegenerate = false } = submission;
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
const { requestMessage, responseMessage, conversation } = data;
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
// update the messages
|
2023-05-23 06:55:23 -04:00
|
|
|
if (isRegenerate) {
|
|
|
|
|
setMessages([...messages, responseMessage]);
|
|
|
|
|
} else {
|
|
|
|
|
setMessages([...messages, requestMessage, responseMessage]);
|
|
|
|
|
}
|
2023-03-28 20:36:21 +08:00
|
|
|
setIsSubmitting(false);
|
|
|
|
|
|
|
|
|
|
// refresh title
|
2023-03-28 22:39:27 +08:00
|
|
|
if (requestMessage.parentMessageId == '00000000-0000-0000-0000-000000000000') {
|
2023-03-28 20:36:21 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
refreshConversations();
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
|
|
// in case it takes too long.
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
refreshConversations();
|
|
|
|
|
}, 5000);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 11:44:07 -07:00
|
|
|
setConversation((prevState) => ({
|
2023-03-31 03:22:57 +08:00
|
|
|
...prevState,
|
|
|
|
|
...conversation
|
|
|
|
|
}));
|
2023-03-28 20:36:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const errorHandler = (data, submission) => {
|
2023-03-31 03:22:57 +08:00
|
|
|
const { messages, message } = submission;
|
2023-03-28 22:39:27 +08:00
|
|
|
|
|
|
|
|
console.log('Error:', data);
|
2023-03-28 20:36:21 +08:00
|
|
|
const errorResponse = {
|
|
|
|
|
...data,
|
|
|
|
|
error: true,
|
2023-03-28 22:39:27 +08:00
|
|
|
parentMessageId: message?.messageId
|
2023-03-28 20:36:21 +08:00
|
|
|
};
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
setMessages([...messages, message, errorResponse]);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-18 11:09:31 -07:00
|
|
|
const abortConversation = (conversationId) => {
|
2023-04-11 03:26:38 +08:00
|
|
|
console.log(submission);
|
|
|
|
|
const { endpoint } = submission?.conversation || {};
|
|
|
|
|
|
|
|
|
|
fetch(`/api/ask/${endpoint}/abort`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
feat: Auth and User System (#205)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* fix: issue with api error watcher (#12)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Update README.md
* Update LOCAL_INSTALL structure
* Add local testing instructions
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Fixup: LOCAL_INSTALL.md PS instructions (#200) (#30)
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
* feat: send user with completion to protect against abuse (#31)
* Fixup: LOCAL_INSTALL.md PS instructions (#200)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Send user id to openai to protect against abuse
* add meilisearch to gitignore
* Remove webpack
---------
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
---------
Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
Co-authored-by: Alfredo Fomitchenko <alfredo.fomitchenko@mail.polimi.it>
2023-05-07 10:04:51 -07:00
|
|
|
'Content-Type': 'application/json',
|
2023-05-18 11:09:31 -07:00
|
|
|
Authorization: `Bearer ${token}`
|
2023-04-11 03:26:38 +08:00
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
abortKey: conversationId
|
|
|
|
|
})
|
|
|
|
|
})
|
2023-05-18 11:44:07 -07:00
|
|
|
.then((response) => response.json())
|
2023-05-18 11:09:31 -07:00
|
|
|
.then((data) => {
|
2023-04-11 03:26:38 +08:00
|
|
|
console.log('aborted', data);
|
|
|
|
|
cancelHandler(data, submission);
|
|
|
|
|
})
|
2023-05-18 11:09:31 -07:00
|
|
|
.catch((error) => {
|
2023-04-11 03:26:38 +08:00
|
|
|
console.error('Error aborting request');
|
|
|
|
|
console.error(error);
|
|
|
|
|
// errorHandler({ text: 'Error aborting request' }, { ...submission, message });
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (submission === null) return;
|
|
|
|
|
if (Object.keys(submission).length === 0) return;
|
|
|
|
|
|
2023-04-11 03:26:38 +08:00
|
|
|
let { message } = submission;
|
2023-04-08 23:19:29 -04:00
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const { server, payload } = createPayload(submission);
|
|
|
|
|
|
|
|
|
|
const events = new SSE(server, {
|
|
|
|
|
payload: JSON.stringify(payload),
|
2023-05-18 11:09:31 -07:00
|
|
|
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }
|
2023-03-28 20:36:21 +08:00
|
|
|
});
|
|
|
|
|
|
2023-05-18 11:09:31 -07:00
|
|
|
events.onmessage = (e) => {
|
2023-03-28 20:36:21 +08:00
|
|
|
const data = JSON.parse(e.data);
|
|
|
|
|
|
|
|
|
|
if (data.final) {
|
|
|
|
|
finalHandler(data, { ...submission, message });
|
2023-03-28 22:39:27 +08:00
|
|
|
console.log('final', data);
|
2023-03-28 20:36:21 +08:00
|
|
|
}
|
|
|
|
|
if (data.created) {
|
|
|
|
|
message = {
|
|
|
|
|
...data.message,
|
2023-03-28 22:39:27 +08:00
|
|
|
overrideParentMessageId: message?.overrideParentMessageId
|
2023-03-28 20:36:21 +08:00
|
|
|
};
|
|
|
|
|
createdHandler(data, { ...submission, message });
|
2023-03-28 22:39:27 +08:00
|
|
|
console.log('created', message);
|
2023-03-28 20:36:21 +08:00
|
|
|
} else {
|
|
|
|
|
let text = data.text || data.response;
|
|
|
|
|
if (data.initial) console.log(data);
|
|
|
|
|
|
|
|
|
|
if (data.message) {
|
|
|
|
|
messageHandler(text, { ...submission, message });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-28 22:39:27 +08:00
|
|
|
events.onopen = () => console.log('connection is opened');
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-05-18 11:09:31 -07:00
|
|
|
events.oncancel = () =>
|
|
|
|
|
abortConversation(message?.conversationId || submission?.conversationId);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
events.onerror = function (e) {
|
2023-03-28 22:39:27 +08:00
|
|
|
console.log('error in opening conn.');
|
2023-03-28 20:36:21 +08:00
|
|
|
events.close();
|
|
|
|
|
|
|
|
|
|
const data = JSON.parse(e.data);
|
|
|
|
|
|
|
|
|
|
errorHandler(data, { ...submission, message });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
|
events.stream();
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
const isCancelled = events.readyState <= 1;
|
|
|
|
|
events.close();
|
2023-04-11 03:26:38 +08:00
|
|
|
// setSource(null);
|
2023-03-28 20:36:21 +08:00
|
|
|
if (isCancelled) {
|
2023-03-28 22:39:27 +08:00
|
|
|
const e = new Event('cancel');
|
2023-03-28 20:36:21 +08:00
|
|
|
events.dispatchEvent(e);
|
|
|
|
|
}
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
};
|
|
|
|
|
}, [submission]);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|