Skip to content

Commit 253868e

Browse files
committed
fixup! ⚡️(frontend) improve prompt of some actions
1 parent 7a3d154 commit 253868e

File tree

3 files changed

+125
-129
lines changed

3 files changed

+125
-129
lines changed

src/frontend/apps/impress/src/features/docs/doc-editor/components/AI/useAI.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
2-
import { createAIExtension, llmFormats } from '@blocknote/xl-ai';
2+
import { createAIExtension } from '@blocknote/xl-ai';
33
import { useMemo } from 'react';
44

55
import { baseApiUrl, fetchAPI } from '@/api';
@@ -38,7 +38,7 @@ export const useAI = (docId: Doc['id'], aiAllowed: boolean) => {
3838
stream: conf.AI_STREAM,
3939
model,
4040
agentCursor: conf?.AI_BOT,
41-
promptBuilder: promptBuilder(llmFormats.html.defaultPromptBuilder),
41+
promptBuilder,
4242
});
4343

4444
return extension;
Lines changed: 113 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Block } from '@blocknote/core';
2+
import { llmFormats } from '@blocknote/xl-ai';
23
import { CoreMessage } from 'ai';
34
import { useCallback } from 'react';
45
import { useTranslation } from 'react-i18next';
@@ -13,11 +14,6 @@ export type PromptBuilderInput = {
1314
previousMessages?: Array<CoreMessage>;
1415
};
1516

16-
type PromptBuilder = (
17-
editor: DocsBlockNoteEditor,
18-
opts: PromptBuilderInput,
19-
) => Promise<Array<CoreMessage>>;
20-
2117
/**
2218
* Custom implementation of the PromptBuilder that allows for using predefined prompts.
2319
*
@@ -28,131 +24,131 @@ export const usePromptAI = () => {
2824
const { t } = useTranslation();
2925

3026
return useCallback(
31-
(defaultPromptBuilder: PromptBuilder) =>
32-
async (
33-
editor: DocsBlockNoteEditor,
34-
opts: PromptBuilderInput,
35-
): Promise<Array<CoreMessage>> => {
36-
const systemPrompts: Record<
37-
| 'add-edit-instruction'
38-
| 'add-formatting'
39-
| 'add-markdown'
40-
| 'assistant'
41-
| 'language'
42-
| 'referenceId',
43-
CoreMessage
44-
> = {
45-
assistant: {
46-
role: 'system',
47-
content: t(`You are an AI assistant that edits user documents.`),
48-
},
49-
referenceId: {
50-
role: 'system',
51-
content: t(
52-
`Keep block IDs exactly as provided when referencing them (including the trailing "$").`,
53-
),
54-
},
55-
'add-markdown': {
56-
role: 'system',
57-
content: t(`Answer the user prompt in markdown format.`),
58-
},
59-
'add-formatting': {
60-
role: 'system',
61-
content: t(`Add formatting to the text to make it more readable.`),
62-
},
63-
'add-edit-instruction': {
64-
role: 'system',
65-
content: t(
66-
`Add content; do not delete or alter existing blocks unless explicitly told.`,
67-
),
68-
},
69-
language: {
70-
role: 'system',
71-
content: t(
72-
`Detect the dominant language inside the provided blocks. YOU MUST PROVIDE A ANSWER IN THE DETECTED LANGUAGE.`,
73-
),
74-
},
75-
};
76-
77-
const userPrompts: Record<string, string> = {
78-
'continue writing': t(
79-
'Keep writing about the content send in the prompt, expanding on the ideas.',
27+
async (
28+
editor: DocsBlockNoteEditor,
29+
opts: PromptBuilderInput,
30+
): Promise<Array<CoreMessage>> => {
31+
const systemPrompts: Record<
32+
| 'add-edit-instruction'
33+
| 'add-formatting'
34+
| 'add-markdown'
35+
| 'assistant'
36+
| 'language'
37+
| 'referenceId',
38+
CoreMessage
39+
> = {
40+
assistant: {
41+
role: 'system',
42+
content: t(`You are an AI assistant that edits user documents.`),
43+
},
44+
referenceId: {
45+
role: 'system',
46+
content: t(
47+
`Keep block IDs exactly as provided when referencing them (including the trailing "$").`,
8048
),
81-
'improve writing': t(
82-
'Improve the writing of the selected text. Make it more professional and clear.',
49+
},
50+
'add-markdown': {
51+
role: 'system',
52+
content: t(`Answer the user prompt in markdown format.`),
53+
},
54+
'add-formatting': {
55+
role: 'system',
56+
content: t(`Add formatting to the text to make it more readable.`),
57+
},
58+
'add-edit-instruction': {
59+
role: 'system',
60+
content: t(
61+
`Add content; do not delete or alter existing blocks unless explicitly told.`,
8362
),
84-
summarize: t('Summarize the document into a concise paragraph.'),
85-
'fix spelling': t(
86-
'Fix the spelling and grammar mistakes in the selected text.',
63+
},
64+
language: {
65+
role: 'system',
66+
content: t(
67+
`Detect the dominant language inside the provided blocks. YOU MUST PROVIDE AN ANSWER IN THE DETECTED LANGUAGE.`,
8768
),
88-
};
89-
90-
// Modify userPrompt if it matches a custom prompt
91-
const customPromptMatch = opts.userPrompt.match(/^([^:]+)(?=[:]|$)/);
92-
let modifiedOpts = opts;
93-
const promptKey = customPromptMatch?.[0].trim().toLowerCase();
94-
if (promptKey) {
95-
if (userPrompts[promptKey]) {
96-
modifiedOpts = {
97-
...opts,
98-
userPrompt: userPrompts[promptKey],
99-
};
100-
}
69+
},
70+
};
71+
72+
const userPrompts: Record<string, string> = {
73+
'continue writing': t(
74+
'Keep writing about the content sent in the prompt, expanding on the ideas.',
75+
),
76+
'improve writing': t(
77+
'Improve the writing of the selected text. Make it more professional and clear.',
78+
),
79+
summarize: t('Summarize the document into a concise paragraph.'),
80+
'fix spelling': t(
81+
'Fix the spelling and grammar mistakes in the selected text.',
82+
),
83+
};
84+
85+
// Modify userPrompt if it matches a custom prompt
86+
const customPromptMatch = opts.userPrompt.match(/^([^:]+)(?=[:]|$)/);
87+
let modifiedOpts = opts;
88+
const promptKey = customPromptMatch?.[0].trim().toLowerCase();
89+
if (promptKey) {
90+
if (userPrompts[promptKey]) {
91+
modifiedOpts = {
92+
...opts,
93+
userPrompt: userPrompts[promptKey],
94+
};
10195
}
96+
}
97+
let prompts = await llmFormats.html.defaultPromptBuilder(
98+
editor,
99+
modifiedOpts,
100+
);
101+
const isTransformExistingContent = !!opts.selectedBlocks?.length;
102+
if (!isTransformExistingContent) {
103+
prompts = prompts.map((prompt) => {
104+
if (!prompt.content || typeof prompt.content !== 'string') {
105+
return prompt;
106+
}
102107

103-
let prompts = await defaultPromptBuilder(editor, modifiedOpts);
104-
const isTransformExistingContent = !!opts.selectedBlocks?.length;
105-
if (!isTransformExistingContent) {
106-
prompts = prompts.map((prompt) => {
107-
if (!prompt.content || typeof prompt.content !== 'string') {
108-
return prompt;
109-
}
110-
111-
/**
112-
* Fix a bug when the initial content is empty
113-
* TODO: Remove this when the bug is fixed in BlockNote
114-
*/
115-
if (prompt.content === '[]') {
116-
const lastBlockId =
117-
editor.document[editor.document.length - 1].id;
108+
/**
109+
* Fix a bug when the initial content is empty
110+
* TODO: Remove this when the bug is fixed in BlockNote
111+
*/
112+
if (prompt.content === '[]') {
113+
const lastBlockId = editor.document[editor.document.length - 1].id;
118114

119-
prompt.content = `[{\"id\":\"${lastBlockId}$\",\"block\":\"<p></p>\"}]`;
120-
return prompt;
121-
}
115+
prompt.content = `[{\"id\":\"${lastBlockId}$\",\"block\":\"<p></p>\"}]`;
116+
return prompt;
117+
}
122118

123-
if (
124-
prompt.content.includes(
125-
"You're manipulating a text document using HTML blocks.",
126-
)
127-
) {
128-
prompt = systemPrompts['add-markdown'];
129-
return prompt;
130-
}
119+
if (
120+
prompt.content.includes(
121+
"You're manipulating a text document using HTML blocks.",
122+
)
123+
) {
124+
prompt = systemPrompts['add-markdown'];
125+
return prompt;
126+
}
131127

132-
if (
133-
prompt.content.includes(
134-
'First, determine what part of the document the user is talking about.',
135-
)
136-
) {
137-
prompt = systemPrompts['add-edit-instruction'];
138-
}
128+
if (
129+
prompt.content.includes(
130+
'First, determine what part of the document the user is talking about.',
131+
)
132+
) {
133+
prompt = systemPrompts['add-edit-instruction'];
134+
}
139135

140-
return prompt;
141-
});
136+
return prompt;
137+
});
142138

143-
prompts.push(systemPrompts['add-formatting']);
144-
}
139+
prompts.push(systemPrompts['add-formatting']);
140+
}
145141

146-
prompts.unshift(systemPrompts['assistant']);
147-
prompts.push(systemPrompts['referenceId']);
142+
prompts.unshift(systemPrompts['assistant']);
143+
prompts.push(systemPrompts['referenceId']);
148144

149-
// Try to keep the language of the document except when we are translating
150-
if (!promptKey?.includes('Translate into')) {
151-
prompts.push(systemPrompts['language']);
152-
}
145+
// Try to keep the language of the document except when we are translating
146+
if (!promptKey?.includes('Translate into')) {
147+
prompts.push(systemPrompts['language']);
148+
}
153149

154-
return prompts;
155-
},
150+
return prompts;
151+
},
156152
[t],
157153
);
158154
};

src/frontend/apps/impress/src/i18n/translations.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,16 @@
590590
"Warning": "Attention",
591591
"Why can't I edit?": "Pourquoi ne puis-je pas éditer ?",
592592
"Write": "Écrire",
593-
"You are an AI assistant that helps users to edit their documents.": "Vous êtes un assistant IA qui aide les utilisateurs à éditer leurs documents.",
594-
"Answer the user prompt in markdown format.": "Répondez à la demande de l'utilisateur au format markdown.",
595-
"Add formatting to the text to make it more readable.": "Ajoutez du formatage au texte pour le rendre plus lisible.",
596-
"Keep adding to the document, do not delete or modify existing blocks.": "Continuez à ajouter au document, ne supprimez ni ne modifiez les blocs existants.",
597-
"Your answer must be in the same language as the document.": "Votre réponse doit être dans la même langue que le document.",
598-
"Fix the spelling and grammar mistakes in the selected text.": "Corrigez les fautes d'orthographe et de grammaire dans le texte sélectionné.",
599-
"Improve the writing of the selected text. Make it more professional and clear.": "Améliorez l'écriture du texte sélectionné. Rendez-le plus professionnel et clair.",
600-
"Summarize the document into a concise paragraph.": "Résumez le document en un paragraphe concis.",
601-
"Keep writing about the content send in the prompt, expanding on the ideas.": "Continuez à écrire sur le contenu envoyé dans la demande, en développant les idées.",
602-
"Important, verified the language of the document! Your answer MUST be in the same language as the document. If the document is in English, your answer MUST be in English. If the document is in Spanish, your answer MUST be in Spanish, etc.": "Important, vérifiez la langue du document ! Votre réponse DOIT être dans la même langue que le document. Si le document est en anglais, votre réponse DOIT être en anglais. Si le document est en espagnol, votre réponse DOIT être en espagnol, etc.",
593+
"You are an AI assistant that helps users to edit their documents.": "Tu es un assistant IA qui aide les utilisateurs à éditer leurs documents.",
594+
"Answer the user prompt in markdown format.": "Réponds à la demande de l'utilisateur au format markdown.",
595+
"Add formatting to the text to make it more readable.": "Ajoute du formatage au texte pour le rendre plus lisible.",
596+
"Keep adding to the document, do not delete or modify existing blocks.": "Continue d'ajouter au document, ne supprime ni ne modifie les blocs existants.",
597+
"Your answer must be in the same language as the document.": "Ta réponse doit être dans la même langue que le document.",
598+
"Fix the spelling and grammar mistakes in the selected text.": "Corrige les fautes d'orthographe et de grammaire dans le texte sélectionné.",
599+
"Improve the writing of the selected text. Make it more professional and clear.": "Améliore l'écriture du texte sélectionné. Rends-le plus professionnel et clair.",
600+
"Summarize the document into a concise paragraph.": "Résume le document en un paragraphe concis.",
601+
"Keep writing about the content sent in the prompt, expanding on the ideas.": "Continue à écrire sur le contenu envoyé dans la demande, en développant les idées.",
602+
"Important, verified the language of the document! Your answer MUST be in the same language as the document. If the document is in English, your answer MUST be in English. If the document is in Spanish, your answer MUST be in Spanish, etc.": "Important, vérifie la langue du document ! Ta réponse DOIT être dans la même langue que le document. Si le document est en anglais, ta réponse DOIT être en anglais. Si le document est en espagnol, ta réponse DOIT être en espagnol, etc.",
603603
"You are the sole owner of this group, make another member the group owner before you can change your own role or be removed from your document.": "Vous êtes le seul propriétaire de ce groupe, faites d'un autre membre le propriétaire du groupe, avant de pouvoir modifier votre propre rôle ou vous supprimer du document.",
604604
"You do not have permission to view this document.": "Vous n'avez pas la permission de voir ce document.",
605605
"You do not have permission to view users sharing this document or modify link settings.": "Vous n'avez pas la permission de voir les utilisateurs partageant ce document ou de modifier les paramètres du lien.",

0 commit comments

Comments
 (0)