Add synchronization support for guidelines and AI guidelines

- Implemented syncing for `GuideLine` and `AIGuideLine` entities with encryption and decryption workflows.
- Refined `GuidelineRepo` methods to check existence and update/insert records for both guideline types.
- Updated timestamp handling in `GuideLine` and `AIGuideLine` models for accurate tracking.
- Corrected QuillSense localization keys for labels and descriptions.
- Improved database queries and modularized logic for consistency.
This commit is contained in:
natreex
2026-01-14 09:17:56 -05:00
parent 707da73551
commit fc42269c8c
5 changed files with 183 additions and 52 deletions

View File

@@ -66,13 +66,13 @@ const QuillSenseSetting = forwardRef(function QuillSenseSetting(props, ref) {
lang lang
); );
if (updateResult) { if (updateResult) {
successMessage(t('quillsenseSetting.saveSuccess')); successMessage(t('quillSenseSetting.successSave'));
// Mettre a jour le contexte du livre // Mettre a jour le contexte du livre
if (setBook && book) { if (setBook && book) {
setBook({...book, quillsenseEnabled: quillsenseEnabled}); setBook({...book, quillsenseEnabled: quillsenseEnabled});
} }
} else { } else {
errorMessage(t('quillsenseSetting.saveError')); errorMessage(t('quillSenseSetting.errorSave'));
} }
} catch (e: unknown) { } catch (e: unknown) {
if (e instanceof Error) { if (e instanceof Error) {
@@ -94,7 +94,7 @@ const QuillSenseSetting = forwardRef(function QuillSenseSetting(props, ref) {
<div className="bg-secondary/20 rounded-xl p-5 shadow-inner border border-secondary/30"> <div className="bg-secondary/20 rounded-xl p-5 shadow-inner border border-secondary/30">
<InputField <InputField
icon={faToggleOn} icon={faToggleOn}
fieldName={t('quillsenseSetting.enableQuillsense')} fieldName={t('quillSenseSetting.enableLabel')}
input={ input={
<ToggleSwitch <ToggleSwitch
checked={quillsenseEnabled} checked={quillsenseEnabled}
@@ -103,24 +103,24 @@ const QuillSenseSetting = forwardRef(function QuillSenseSetting(props, ref) {
} }
/> />
<p className="text-muted text-sm mt-2"> <p className="text-muted text-sm mt-2">
{t('quillsenseSetting.enableDescription')} {t('quillSenseSetting.enableDescription')}
</p> </p>
</div> </div>
<div className="bg-secondary/20 rounded-xl p-5 shadow-inner border border-secondary/30"> <div className="bg-secondary/20 rounded-xl p-5 shadow-inner border border-secondary/30">
<InputField <InputField
icon={faMagicWandSparkles} icon={faMagicWandSparkles}
fieldName={t('quillsenseSetting.advancedPrompt')} fieldName={t('quillSenseSetting.advancedPromptLabel')}
input={ input={
<TexteAreaInput <TexteAreaInput
value={advancedPrompt} value={advancedPrompt}
setValue={(e: ChangeEvent<HTMLTextAreaElement>): void => setAdvancedPrompt(e.target.value)} setValue={(e: ChangeEvent<HTMLTextAreaElement>): void => setAdvancedPrompt(e.target.value)}
placeholder={t('quillsenseSetting.advancedPromptPlaceholder')} placeholder={t('quillSenseSetting.advancedPromptPlaceholder')}
/> />
} }
/> />
<p className="text-muted text-sm mt-2"> <p className="text-muted text-sm mt-2">
{t('quillsenseSetting.advancedPromptDescription')} {t('quillSenseSetting.advancedPromptHint')}
</p> </p>
</div> </div>
</div> </div>

View File

@@ -174,10 +174,8 @@ export default class Act {
for (const chapter of mainChapters) { for (const chapter of mainChapters) {
const chapterId: string = chapter.chapterId; const chapterId: string = chapter.chapterId;
const chapterTitle: string = chapter.title; const chapterTitle: string = chapter.title;
const hashedChapterTitle: string = System.hashElement(chapterTitle);
const encryptedChapterTitle: string = System.encryptDataWithUserKey(chapterTitle, userEncryptionKey);
const chapterOrder: number = chapter.chapterOrder; const chapterOrder: number = chapter.chapterOrder;
ChapterRepo.updateChapter(userId, chapterId, encryptedChapterTitle, hashedChapterTitle, chapterOrder, System.timeStampInSeconds(), lang); Chapter.updateChapter(userId, chapterId, chapterTitle, chapterOrder, lang);
} }
return true; return true;

View File

@@ -120,6 +120,7 @@ export default class GuideLine {
encryptedPacing, encryptedPacing,
encryptedKeyMessages, encryptedKeyMessages,
encryptedIntendedAudience, encryptedIntendedAudience,
System.timeStampInSeconds(),
lang lang
); );
} }
@@ -210,6 +211,7 @@ export default class GuideLine {
verbTense, verbTense,
language, language,
encryptedThemes, encryptedThemes,
System.timeStampInSeconds(),
lang lang
); );
} }

View File

@@ -303,6 +303,41 @@ export default class Sync {
} }
} }
if (syncCompareData.guideLine) {
const guidelineResults: BookGuideLineTable[] = await GuidelineRepo.fetchBookGuideLineTable(userId, syncCompareData.id, lang);
if (guidelineResults.length > 0) {
const guidelineRecord: BookGuideLineTable = guidelineResults[0];
decryptedGuideLines.push({
...guidelineRecord,
tone: guidelineRecord.tone ? System.decryptDataWithUserKey(guidelineRecord.tone, userEncryptionKey) : null,
atmosphere: guidelineRecord.atmosphere ? System.decryptDataWithUserKey(guidelineRecord.atmosphere, userEncryptionKey) : null,
writing_style: guidelineRecord.writing_style ? System.decryptDataWithUserKey(guidelineRecord.writing_style, userEncryptionKey) : null,
themes: guidelineRecord.themes ? System.decryptDataWithUserKey(guidelineRecord.themes, userEncryptionKey) : null,
symbolism: guidelineRecord.symbolism ? System.decryptDataWithUserKey(guidelineRecord.symbolism, userEncryptionKey) : null,
motifs: guidelineRecord.motifs ? System.decryptDataWithUserKey(guidelineRecord.motifs, userEncryptionKey) : null,
narrative_voice: guidelineRecord.narrative_voice ? System.decryptDataWithUserKey(guidelineRecord.narrative_voice, userEncryptionKey) : null,
pacing: guidelineRecord.pacing ? System.decryptDataWithUserKey(guidelineRecord.pacing, userEncryptionKey) : null,
intended_audience: guidelineRecord.intended_audience ? System.decryptDataWithUserKey(guidelineRecord.intended_audience, userEncryptionKey) : null,
key_messages: guidelineRecord.key_messages ? System.decryptDataWithUserKey(guidelineRecord.key_messages, userEncryptionKey) : null
});
}
}
if (syncCompareData.aiGuideLine) {
const aiGuidelineResults: BookAIGuideLineTable[] = await GuidelineRepo.fetchBookAIGuideLine(userId, syncCompareData.id, lang);
if (aiGuidelineResults.length > 0) {
const aiGuidelineRecord: BookAIGuideLineTable = aiGuidelineResults[0];
decryptedAIGuideLines.push({
...aiGuidelineRecord,
global_resume: aiGuidelineRecord.global_resume ? System.decryptDataWithUserKey(aiGuidelineRecord.global_resume, userEncryptionKey) : null,
themes: aiGuidelineRecord.themes ? System.decryptDataWithUserKey(aiGuidelineRecord.themes, userEncryptionKey) : null,
tone: aiGuidelineRecord.tone ? System.decryptDataWithUserKey(aiGuidelineRecord.tone, userEncryptionKey) : null,
atmosphere: aiGuidelineRecord.atmosphere ? System.decryptDataWithUserKey(aiGuidelineRecord.atmosphere, userEncryptionKey) : null,
current_resume: aiGuidelineRecord.current_resume ? System.decryptDataWithUserKey(aiGuidelineRecord.current_resume, userEncryptionKey) : null
});
}
}
const bookResults: EritBooksTable[] = await BookRepo.fetchCompleteBookById(syncCompareData.id, lang); const bookResults: EritBooksTable[] = await BookRepo.fetchCompleteBookById(syncCompareData.id, lang);
if (bookResults.length > 0) { if (bookResults.length > 0) {
const bookRecord: EritBooksTable = bookResults[0]; const bookRecord: EritBooksTable = bookResults[0];
@@ -361,6 +396,8 @@ export default class Sync {
const serverWorlds: BookWorldTable[] = completeBook.worlds; const serverWorlds: BookWorldTable[] = completeBook.worlds;
const serverWorldElements: BookWorldElementsTable[] = completeBook.worldElements; const serverWorldElements: BookWorldElementsTable[] = completeBook.worldElements;
const serverIssues: BookIssuesTable[] = completeBook.issues; const serverIssues: BookIssuesTable[] = completeBook.issues;
const serverGuideLines: BookGuideLineTable[] = completeBook.guideLine;
const serverAIGuideLines: BookAIGuideLineTable[] = completeBook.aiGuideLine;
const bookId: string = completeBook.eritBooks.length > 0 ? completeBook.eritBooks[0].book_id : ''; const bookId: string = completeBook.eritBooks.length > 0 ? completeBook.eritBooks[0].book_id : '';
@@ -638,6 +675,55 @@ export default class Sync {
} }
} }
if (serverGuideLines && serverGuideLines.length > 0) {
for (const serverGuideLine of serverGuideLines) {
const guideLineExists: boolean = GuidelineRepo.guideLineExist(userId, bookId, lang);
const encryptedTone: string | null = serverGuideLine.tone ? System.encryptDataWithUserKey(serverGuideLine.tone, userEncryptionKey) : null;
const encryptedAtmosphere: string | null = serverGuideLine.atmosphere ? System.encryptDataWithUserKey(serverGuideLine.atmosphere, userEncryptionKey) : null;
const encryptedWritingStyle: string | null = serverGuideLine.writing_style ? System.encryptDataWithUserKey(serverGuideLine.writing_style, userEncryptionKey) : null;
const encryptedThemes: string | null = serverGuideLine.themes ? System.encryptDataWithUserKey(serverGuideLine.themes, userEncryptionKey) : null;
const encryptedSymbolism: string | null = serverGuideLine.symbolism ? System.encryptDataWithUserKey(serverGuideLine.symbolism, userEncryptionKey) : null;
const encryptedMotifs: string | null = serverGuideLine.motifs ? System.encryptDataWithUserKey(serverGuideLine.motifs, userEncryptionKey) : null;
const encryptedNarrativeVoice: string | null = serverGuideLine.narrative_voice ? System.encryptDataWithUserKey(serverGuideLine.narrative_voice, userEncryptionKey) : null;
const encryptedPacing: string | null = serverGuideLine.pacing ? System.encryptDataWithUserKey(serverGuideLine.pacing, userEncryptionKey) : null;
const encryptedKeyMessages: string | null = serverGuideLine.key_messages ? System.encryptDataWithUserKey(serverGuideLine.key_messages, userEncryptionKey) : null;
const encryptedIntendedAudience: string | null = serverGuideLine.intended_audience ? System.encryptDataWithUserKey(serverGuideLine.intended_audience, userEncryptionKey) : null;
if (guideLineExists) {
const updateSuccessful: boolean = GuidelineRepo.updateGuideLine(userId, bookId, encryptedTone, encryptedAtmosphere, encryptedWritingStyle, encryptedThemes, encryptedSymbolism, encryptedMotifs, encryptedNarrativeVoice, encryptedPacing, encryptedKeyMessages, encryptedIntendedAudience, serverGuideLine.last_update, lang);
if (!updateSuccessful) {
return false;
}
} else {
const insertSuccessful: boolean = GuidelineRepo.insertSyncGuideLine(userId, bookId, encryptedTone, encryptedAtmosphere, encryptedWritingStyle, encryptedThemes, encryptedSymbolism, encryptedMotifs, encryptedNarrativeVoice, encryptedPacing, encryptedIntendedAudience, encryptedKeyMessages, serverGuideLine.last_update, lang);
if (!insertSuccessful) {
return false;
}
}
}
}
if (serverAIGuideLines && serverAIGuideLines.length > 0) {
for (const serverAIGuideLine of serverAIGuideLines) {
const aiGuideLineExists: boolean = GuidelineRepo.aiGuideLineExist(userId, bookId, lang);
const encryptedGlobalResume: string | null = serverAIGuideLine.global_resume ? System.encryptDataWithUserKey(serverAIGuideLine.global_resume, userEncryptionKey) : null;
const encryptedThemes: string | null = serverAIGuideLine.themes ? System.encryptDataWithUserKey(serverAIGuideLine.themes, userEncryptionKey) : null;
const encryptedTone: string | null = serverAIGuideLine.tone ? System.encryptDataWithUserKey(serverAIGuideLine.tone, userEncryptionKey) : null;
const encryptedAtmosphere: string | null = serverAIGuideLine.atmosphere ? System.encryptDataWithUserKey(serverAIGuideLine.atmosphere, userEncryptionKey) : null;
const encryptedCurrentResume: string | null = serverAIGuideLine.current_resume ? System.encryptDataWithUserKey(serverAIGuideLine.current_resume, userEncryptionKey) : null;
if (aiGuideLineExists) {
const updateSuccessful: boolean = GuidelineRepo.insertAIGuideLine(userId, bookId, serverAIGuideLine.narrative_type, serverAIGuideLine.dialogue_type, encryptedGlobalResume, encryptedAtmosphere, serverAIGuideLine.verbe_tense, serverAIGuideLine.langue, encryptedThemes, serverAIGuideLine.last_update, lang);
if (!updateSuccessful) {
return false;
}
} else {
const insertSuccessful: boolean = GuidelineRepo.insertSyncAIGuideLine(userId, bookId, encryptedGlobalResume, encryptedThemes, serverAIGuideLine.verbe_tense, serverAIGuideLine.narrative_type, serverAIGuideLine.langue, serverAIGuideLine.dialogue_type, encryptedTone, encryptedAtmosphere, encryptedCurrentResume, serverAIGuideLine.last_update, lang);
if (!insertSuccessful) {
return false;
}
}
}
}
return true; return true;
} }
@@ -703,8 +789,8 @@ export default class Sync {
PlotPointRepository.fetchSyncedPlotPoints(userId, lang), PlotPointRepository.fetchSyncedPlotPoints(userId, lang),
IssueRepository.fetchSyncedIssues(userId, lang), IssueRepository.fetchSyncedIssues(userId, lang),
ActRepository.fetchSyncedActSummaries(userId, lang), ActRepository.fetchSyncedActSummaries(userId, lang),
GuidelineRepo.fetchSyncedAIGuideLine(userId, lang), GuidelineRepo.fetchSyncedGuideLine(userId, lang),
GuidelineRepo.fetchSyncedGuideLine(userId, lang) GuidelineRepo.fetchSyncedAIGuideLine(userId, lang)
]); ]);
return allBooks.map((bookRecord: SyncedBookResult): SyncedBook => { return allBooks.map((bookRecord: SyncedBookResult): SyncedBook => {

View File

@@ -117,24 +117,30 @@ export default class GuidelineRepo {
* @returns True if the operation was successful * @returns True if the operation was successful
* @throws Error if the guideline cannot be updated or inserted * @throws Error if the guideline cannot be updated or inserted
*/ */
static updateGuideLine( /**
userId: string, * Updates or inserts a guideline for a specific book.
bookId: string, * If the guideline exists, it updates it; otherwise, it inserts a new one.
encryptedTone: string, * @param userId - The user identifier
encryptedAtmosphere: string, * @param bookId - The book identifier
encryptedWritingStyle: string, * @param encryptedTone - The encrypted tone value
encryptedThemes: string, * @param encryptedAtmosphere - The encrypted atmosphere value
encryptedSymbolism: string, * @param encryptedWritingStyle - The encrypted writing style value
encryptedMotifs: string, * @param encryptedThemes - The encrypted themes value
encryptedNarrativeVoice: string, * @param encryptedSymbolism - The encrypted symbolism value
encryptedPacing: string, * @param encryptedMotifs - The encrypted motifs value
encryptedKeyMessages: string, * @param encryptedNarrativeVoice - The encrypted narrative voice value
encryptedIntendedAudience: string, * @param encryptedPacing - The encrypted pacing value
lang: 'fr' | 'en' * @param encryptedKeyMessages - The encrypted key messages value
): boolean { * @param encryptedIntendedAudience - The encrypted intended audience value
* @param lastUpdate - The timestamp of the last update
* @param lang - The language for error messages ('fr' or 'en')
* @returns True if the operation was successful
* @throws Error if the guideline cannot be updated or inserted
*/
static updateGuideLine(userId: string, bookId: string, encryptedTone: string | null, encryptedAtmosphere: string | null, encryptedWritingStyle: string | null, encryptedThemes: string | null, encryptedSymbolism: string | null, encryptedMotifs: string | null, encryptedNarrativeVoice: string | null, encryptedPacing: string | null, encryptedKeyMessages: string | null, encryptedIntendedAudience: string | null, lastUpdate: number, lang: 'fr' | 'en'): boolean {
try { try {
const db: Database = System.getDb(); const db: Database = System.getDb();
const updateQuery: string = 'UPDATE book_guide_line SET tone=?, atmosphere=?, writing_style=?, themes=?, symbolism=?, motifs=?, narrative_voice=?, pacing=?, key_messages=?, last_update=? WHERE user_id=? AND book_id=?'; const updateQuery: string = 'UPDATE book_guide_line SET tone=?, atmosphere=?, writing_style=?, themes=?, symbolism=?, motifs=?, narrative_voice=?, pacing=?, intended_audience=?, key_messages=?, last_update=? WHERE user_id=? AND book_id=?';
const updateParams: SQLiteValue[] = [ const updateParams: SQLiteValue[] = [
encryptedTone, encryptedTone,
encryptedAtmosphere, encryptedAtmosphere,
@@ -144,8 +150,9 @@ export default class GuidelineRepo {
encryptedMotifs, encryptedMotifs,
encryptedNarrativeVoice, encryptedNarrativeVoice,
encryptedPacing, encryptedPacing,
encryptedIntendedAudience,
encryptedKeyMessages, encryptedKeyMessages,
System.timeStampInSeconds(), lastUpdate,
userId, userId,
bookId bookId
]; ];
@@ -168,7 +175,7 @@ export default class GuidelineRepo {
encryptedPacing, encryptedPacing,
encryptedIntendedAudience, encryptedIntendedAudience,
encryptedKeyMessages, encryptedKeyMessages,
System.timeStampInSeconds() lastUpdate
]; ];
const insertResult: RunResult = db.run(insertQuery, insertParams); const insertResult: RunResult = db.run(insertQuery, insertParams);
return insertResult.changes > 0; return insertResult.changes > 0;
@@ -196,34 +203,24 @@ export default class GuidelineRepo {
* @param verbTense - The verb tense identifier * @param verbTense - The verb tense identifier
* @param language - The language identifier * @param language - The language identifier
* @param encryptedThemes - The encrypted themes value * @param encryptedThemes - The encrypted themes value
* @param lastUpdate - The timestamp of the last update
* @param lang - The language for error messages ('fr' or 'en') * @param lang - The language for error messages ('fr' or 'en')
* @returns True if the operation was successful * @returns True if the operation was successful
* @throws Error if the AI guideline cannot be inserted or updated * @throws Error if the AI guideline cannot be inserted or updated
*/ */
static insertAIGuideLine( static insertAIGuideLine(userId: string, bookId: string, narrativeType: number | null, dialogueType: number | null, encryptedPlotSummary: string | null, encryptedToneAtmosphere: string | null, verbTense: number | null, language: number | null, encryptedThemes: string | null, lastUpdate: number, lang: 'fr' | 'en'): boolean {
userId: string,
bookId: string,
narrativeType: number,
dialogueType: number,
encryptedPlotSummary: string,
encryptedToneAtmosphere: string,
verbTense: number,
language: number,
encryptedThemes: string,
lang: 'fr' | 'en'
): boolean {
try { try {
const db: Database = System.getDb(); const db: Database = System.getDb();
const updateQuery: string = 'UPDATE book_ai_guide_line SET narrative_type=?, dialogue_type=?, global_resume=?, atmosphere=?, verbe_tense=?, langue=?, themes=?, last_update=? WHERE user_id=? AND book_id=?'; const updateQuery: string = 'UPDATE book_ai_guide_line SET narrative_type=?, dialogue_type=?, global_resume=?, atmosphere=?, verbe_tense=?, langue=?, themes=?, last_update=? WHERE user_id=? AND book_id=?';
const updateParams: SQLiteValue[] = [ const updateParams: SQLiteValue[] = [
narrativeType ? narrativeType : null, narrativeType,
dialogueType ? dialogueType : null, dialogueType,
encryptedPlotSummary, encryptedPlotSummary,
encryptedToneAtmosphere, encryptedToneAtmosphere,
verbTense ? verbTense : null, verbTense,
language ? language : null, language,
encryptedThemes, encryptedThemes,
System.timeStampInSeconds(), lastUpdate,
userId, userId,
bookId bookId
]; ];
@@ -238,14 +235,14 @@ export default class GuidelineRepo {
bookId, bookId,
encryptedPlotSummary, encryptedPlotSummary,
encryptedThemes, encryptedThemes,
verbTense ? verbTense : null, verbTense,
narrativeType ? narrativeType : null, narrativeType,
language ? language : null, language,
dialogueType ? dialogueType : null, dialogueType,
encryptedToneAtmosphere, encryptedToneAtmosphere,
encryptedToneAtmosphere, encryptedToneAtmosphere,
encryptedPlotSummary, encryptedPlotSummary,
System.timeStampInSeconds() lastUpdate
]; ];
const insertResult: RunResult = db.run(insertQuery, insertParams); const insertResult: RunResult = db.run(insertQuery, insertParams);
return insertResult.changes > 0; return insertResult.changes > 0;
@@ -391,6 +388,54 @@ export default class GuidelineRepo {
} }
} }
/**
* Checks if a guideline exists for a specific book.
* @param userId - The unique identifier of the user.
* @param bookId - The unique identifier of the book.
* @param lang - The language for error messages ('fr' or 'en').
* @returns True if the guideline exists, false otherwise.
*/
static guideLineExist(userId: string, bookId: string, lang: 'fr' | 'en'): boolean {
try {
const db: Database = System.getDb();
const query: string = 'SELECT 1 FROM book_guide_line WHERE user_id=? AND book_id=?';
const params: SQLiteValue[] = [userId, bookId];
const result: Record<string, SQLiteValue> | undefined = db.get(query, params) as Record<string, SQLiteValue> | undefined;
return result !== undefined;
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`DB Error: ${error.message}`);
throw new Error(lang === 'fr' ? `Impossible de vérifier l'existence de la ligne directrice.` : `Unable to check guideline existence.`);
} else {
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
/**
* Checks if an AI guideline exists for a specific book.
* @param userId - The unique identifier of the user.
* @param bookId - The unique identifier of the book.
* @param lang - The language for error messages ('fr' or 'en').
* @returns True if the AI guideline exists, false otherwise.
*/
static aiGuideLineExist(userId: string, bookId: string, lang: 'fr' | 'en'): boolean {
try {
const db: Database = System.getDb();
const query: string = 'SELECT 1 FROM book_ai_guide_line WHERE user_id=? AND book_id=?';
const params: SQLiteValue[] = [userId, bookId];
const result: Record<string, SQLiteValue> | undefined = db.get(query, params) as Record<string, SQLiteValue> | undefined;
return result !== undefined;
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`DB Error: ${error.message}`);
throw new Error(lang === 'fr' ? `Impossible de vérifier l'existence de la ligne directrice IA.` : `Unable to check AI guideline existence.`);
} else {
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
/** /**
* Inserts a synced AI guideline for a specific book. * Inserts a synced AI guideline for a specific book.
* @param userId - The user identifier * @param userId - The user identifier