Skip to content

Commit

Permalink
Merge pull request #31 from memori-ai/fix_open_session_error
Browse files Browse the repository at this point in the history
Fix open session error
  • Loading branch information
nzambello authored Dec 2, 2024
2 parents fe11692 + 3e26519 commit 3df1af2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/components/ChatBubble/ChatBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ marked.use({
renderer: {
link: ({ href, title, text }) => {
const cleanHref = cleanUrl(href);

if (cleanHref === null) {
return text;
}
Expand Down
70 changes: 63 additions & 7 deletions src/components/MemoriWidget/MemoriWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1086,12 +1086,23 @@ const MemoriWidget = ({
}
});
};


/**
* Opening Session
*/
/**
* Fetches a new session with the given parameters
* @param params OpenSession parameters
* @returns Promise resolving to dialog state and session ID if successful, void otherwise
*/
const fetchSession = async (
params: OpenSession
): Promise<{
dialogState: DialogState;
sessionID: string;
} | void> => {
// Check if age verification is needed
let storageBirthDate = getLocalConfig<string | undefined>(
'birthDate',
undefined
Expand All @@ -1101,6 +1112,7 @@ const MemoriWidget = ({
return;
}

// Check if authentication is needed for private Memori
if (
memori.privacyType !== 'PUBLIC' &&
!memori.secretToken &&
Expand All @@ -1112,6 +1124,7 @@ const MemoriWidget = ({
}
setLoading(true);
try {
// Check for and set giver invitation if available
if (!memori.giverTag && !!memori.receivedInvitations?.length) {
let giverInvitation = memori.receivedInvitations.find(
(i: Invitation) => i.type === 'GIVER' && i.state === 'ACCEPTED'
Expand All @@ -1123,6 +1136,7 @@ const MemoriWidget = ({
}
}

// Get referral URL
let referral;
try {
referral = (() => {
Expand All @@ -1132,6 +1146,7 @@ const MemoriWidget = ({
console.debug(err);
}

// Initialize session with parameters
const session = await initSession({
...params,
tag: params.tag ?? personification?.tag,
Expand All @@ -1145,6 +1160,8 @@ const MemoriWidget = ({
timeZoneOffset: new Date().getTimezoneOffset().toString(),
},
});

// Handle successful session creation
if (
session?.sessionID &&
session?.currentState &&
Expand All @@ -1159,25 +1176,45 @@ const MemoriWidget = ({
dialogState: session.currentState,
sessionID: session.sessionID,
} as { dialogState: DialogState; sessionID: string };
} else if (
}
// Handle age restriction error
else if (
session?.resultMessage.startsWith('This Memori is aged restricted')
) {
console.warn(session);
toast.error(t('underageTwinSession', { age: minAge }));
setGotErrorInOpening(true);
} else if (session?.resultCode === 403) {
}
// Handle authentication error
else if (session?.resultCode === 403) {
setMemoriPwd(undefined);
setAuthModalState('password');
} else {
}
// Handle other errors
else {
console.warn(session);
toast.error(t(getErrori18nKey(session?.resultCode)));
setGotErrorInOpening(true);
}
} catch (err) {
console.error(err);
new Error('Error fetching session');
toast.error(t('errorFetchingSession'));
throw new Error('Error fetching session');
}
};

/**
* Reopens an existing session with optional parameters
* @param updateDialogState Whether to update dialog state
* @param password Optional password for authentication
* @param recoveryTokens Optional recovery tokens
* @param tag Optional tag
* @param pin Optional PIN
* @param initialContextVars Optional initial context variables
* @param initialQuestion Optional initial question
* @param birthDate Optional birth date for age verification
* @returns Promise resolving to dialog state and session ID if successful, null otherwise
*/
const reopenSession = async (
updateDialogState: boolean = false,
password?: string,
Expand All @@ -1190,6 +1227,7 @@ const MemoriWidget = ({
) => {
setLoading(true);
try {
// Check if age verification is needed
let storageBirthDate = getLocalConfig<string | undefined>(
'birthDate',
undefined
Expand All @@ -1199,6 +1237,7 @@ const MemoriWidget = ({
return;
}

// Check if authentication is needed
if (
memori.privacyType !== 'PUBLIC' &&
!password &&
Expand All @@ -1211,15 +1250,19 @@ const MemoriWidget = ({
return;
}

// Get referral URL
let referral;
try {
referral = (() => {
return window.location.href;
})();
} catch (err) {
console.debug(err);
toast.error(t('errorGettingReferralURL'));
throw new Error('Error getting referral URL');
}

// Initialize session with parameters
const { sessionID, currentState, ...response } = await initSession({
memoriID: memori.engineMemoriID ?? '',
password: password || memoriPwd || memori.secretToken,
Expand All @@ -1243,12 +1286,15 @@ const MemoriWidget = ({
},
});

// Handle successful session creation
if (sessionID && currentState && response.resultCode === 0) {
setSessionId(sessionID);

// Update dialog state if requested
if (updateDialogState) {
setCurrentDialogState(currentState);
if (currentState.emission) {
// Set history based on current length
history.length <= 1
? setHistory([
{
Expand Down Expand Up @@ -1285,6 +1331,7 @@ const MemoriWidget = ({
}
}

// Apply position and date if needed
if (position) applyPosition(position, sessionID);
if (memori.needsDateTime)
sendDateChangedEvent({ sessionID: sessionID, state: currentState });
Expand All @@ -1294,28 +1341,37 @@ const MemoriWidget = ({
dialogState: currentState,
sessionID,
};
} else if (
}
// Handle age restriction error
else if (
response?.resultMessage.startsWith('This Memori is aged restricted')
) {
console.error(response);
toast.error(t('underageTwinSession', { age: minAge }));
setGotErrorInOpening(true);
} else if (response?.resultCode === 403) {
}
// Handle authentication error
else if (response?.resultCode === 403) {
setMemoriPwd(undefined);
setAuthModalState('password');
} else {
}
// Handle other errors
else {
console.error(response);
toast.error(t(getErrori18nKey(response.resultCode)));
setGotErrorInOpening(true);
}
} catch (err) {
console.error(err);
toast.error(t('errorReopeningSession'));
throw new Error('Error reopening session');
}
setLoading(false);

return null;
};


const changeTag = async (
memoriId: string,
sessionId: string,
Expand Down
3 changes: 3 additions & 0 deletions src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"birthDateHelper": "Wir fragen nur nach Ihrem Geburtsdatum, um Funktionen zu aktivieren oder zu deaktivieren, für die Altersbeschränkungen gelten",
"underage": "Das müssen Sie mindestens sein {{age}} Jahre alt, um sich anzumelden.",
"underageTwinSession": "Das müssen Sie mindestens sein {{age}} Jahre alt, um mit diesem Zwilling zu interagieren.",
"errorFetchingSession": "Fehler beim Laden der Sitzung",
"errorGettingReferralURL": "Fehler beim Laden des Referrals",
"errorReopeningSession": "Fehler beim erneuten Öffnen der Sitzung",
"ageVerification": "Altersüberprüfung",
"ageVerificationText": "Um mit diesem Zwilling interagieren zu können, müssen Sie mindestens sein {{minAge}} Jahre alt.",
"nsfw": "NSFW: Dieser Twin enthält Inhalte für Erwachsene",
Expand Down
3 changes: 3 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"birthDateHelper": "We ask for your birth date only to enable or disable functionalities that have age restrictions",
"underage": "You must be at least {{age}} years old to sign up.",
"underageTwinSession": "You must be at least {{age}} years old to interact with this Twin.",
"errorFetchingSession": "Error during session loading",
"errorGettingReferralURL": "Error during referral loading",
"errorReopeningSession": "Error during session reopening",
"ageVerification": "Age verification",
"ageVerificationText": "To interact with this Twin, you must be at least {{minAge}} years old.",
"nsfw": "NSFW: This Twin contains adult contents",
Expand Down
3 changes: 3 additions & 0 deletions src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"birthDateHelper": "Solicitamos tu fecha de nacimiento únicamente para habilitar o deshabilitar funcionalidades que tienen restricciones de edad.",
"underage": "Debes ser al menos {{age}} años para inscribirse.",
"underageTwinSession": "Debes ser al menos {{age}} años para interactuar con este gemelo.",
"errorFetchingSession": "Error durante el cargamento de la sesión",
"errorGettingReferralURL": "Error durante el cargamento del référent",
"errorReopeningSession": "Error durante el re-abrir la sesión",
"ageVerification": "Verificación de edad",
"ageVerificationText": "Para interactuar con este Gemelo, debes tener al menos {{minAge}} años.",
"nsfw": "NSFW: Este gemelo contiene contenido para adultos",
Expand Down
3 changes: 3 additions & 0 deletions src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"birthDateHelper": "Nous demandons votre date de naissance uniquement pour activer ou désactiver les fonctionnalités qui ont des restrictions d'âge",
"underage": "Vous devez être au moins {{age}} ans pour s'inscrire.",
"underageTwinSession": "Vous devez être au moins {{age}} ans pour interagir avec ce Twin.",
"errorFetchingSession": "Erreur lors du chargement de la session",
"errorGettingReferralURL": "Erreur lors du chargement du référent",
"errorReopeningSession": "Erreur lors de la re-ouverture de la session",
"ageVerification": "Vérification de l'âge",
"ageVerificationText": "Pour interagir avec ce Twin, vous devez être au minimum {{minAge}} ans.",
"nsfw": "NSFW : Ce jumeau contient du contenu pour adultes",
Expand Down
3 changes: 3 additions & 0 deletions src/locales/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"birthDateHelper": "Ti chiediamo la data di nascita esclusivamente per abilitare o disabilitare le funzionalità che hanno restrizioni in base all'età",
"underage": "Devi avere almeno {{age}} anni per registrarti.",
"underageTwinSession": "Devi avere almeno {{age}} anni per interagire con questo Twin.",
"errorFetchingSession": "Errore durante il caricamento della sessione",
"errorGettingReferralURL": "Errore durante il caricamento del riferimento",
"errorReopeningSession": "Errore durante il riapertura della sessione",
"ageVerification": "Verifica dell'età",
"ageVerificationText": "Per interagire con questo Twin, devi aver almeno {{minAge}} anni.",
"nsfw": "NSFW: Questo Twin contiene contenuti per adulti",
Expand Down

0 comments on commit 3df1af2

Please sign in to comment.