Skip to content

Commit 8187da5

Browse files
committed
Fix issues with completion after losing focus
1 parent 8c02e09 commit 8187da5

File tree

3 files changed

+16
-92
lines changed

3 files changed

+16
-92
lines changed

backend/src/routes/page/complete.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import OpenAIClient from '../../helpers/clients/OpenAIClient';
55

66
const router = express.Router();
77

8-
const AUTOCOMPLETE_PROMPT = `You are a helpful AI assistant. Use the following pieces of context to complete the text at the end.
9-
DO NOT repeat the context. Only complete it.
10-
11-
Context: {{context}}`;
12-
138
router.get(
149
'/complete',
1510
verifySession(),
@@ -34,23 +29,17 @@ router.get(
3429
return;
3530
}
3631

37-
const prompt = AUTOCOMPLETE_PROMPT.replace('{{context}}', context);
38-
39-
const response = await OpenAIClient!.createChatCompletion({
40-
model: 'gpt-3.5-turbo',
41-
messages: [
42-
{
43-
role: 'user',
44-
content: prompt,
45-
}
46-
],
32+
const response = await OpenAIClient!.createCompletion({
33+
model: 'text-davinci-003',
34+
prompt: context,
4735
max_tokens: 10,
36+
n: 1,
4837
});
4938

5039
res.statusCode = 200;
5140
res.json({
5241
status: 'success',
53-
message: response.data.choices[0].message?.content,
42+
message: response.data.choices[0].text || '',
5443
});
5544
},
5645
);

web/src/components/Editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const Editor = () => {
4343
.map((node) => node.textContent)
4444
.join('\n');
4545

46-
const completionEndpoint = `${process.env.NEXT_PUBLIC_API_URL}/page/complete?context=${textBefore}`;
46+
const completionEndpoint = `${process.env.NEXT_PUBLIC_API_URL}/page/complete?context="${textBefore}"`;
4747

4848
const completionRequest = await fetch(completionEndpoint);
4949

web/src/components/blocks/TextBlock.tsx

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import getCursorOffset from '../../lib/helpers/caret/getCursorOffset';
1515
import ContentEditable from 'react-contenteditable';
1616
import { sanitize } from 'dompurify';
1717
import focusElement from '../../lib/helpers/focusElement';
18+
import isElementFocused from '../../lib/helpers/isElementFocused';
1819

1920
const TextBlock = (props: EditableText) => {
2021
const {
@@ -359,7 +360,13 @@ const TextBlock = (props: EditableText) => {
359360
* Ensure the cursor is never past the completion
360361
*/
361362
useEffect(() => {
362-
if (!editableRef.current) return;
363+
if (!editableRef.current || !state.value) return;
364+
365+
if (!isElementFocused(editableRef.current)) {
366+
setCompletion(null);
367+
setCompletionTimeout(null);
368+
return;
369+
}
363370

364371
const caretOffset = getCursorOffset(editableRef.current);
365372

@@ -414,6 +421,7 @@ const TextBlock = (props: EditableText) => {
414421
}
415422

416423
setCompletion(null);
424+
setCompletionTimeout(null);
417425

418426
const value = saveBlock(editableRef.current, completion);
419427

@@ -453,80 +461,7 @@ const TextBlock = (props: EditableText) => {
453461
/>
454462
{slashMenu}
455463
</>
456-
)
457-
458-
// return (
459-
// <ContentEditable
460-
// className={`min-h-[1.2em] outline-none relative whitespace-pre-wrap w-full ${TextStyles[type]}`}
461-
// role="textbox"
462-
// tabIndex={0}
463-
// contentEditable={isAllowedToEdit}
464-
// suppressContentEditableWarning
465-
// ref={isAllowedToEdit ? editableRef as unknown as React.RefObject<typeof ContentEditable> : null}
466-
// id={`block-${blockID}`}
467-
// data-block-index={index}
468-
// onInput={(_) => {
469-
// if (!editableRef.current) return;
470-
471-
// handlePotentialTypeChange(editableRef.current);
472-
// handlePotentialInlineBlocks(editableRef.current);
473-
474-
// if (completionTimeout) {
475-
// clearTimeout(completionTimeout);
476-
// }
477-
478-
// setCompletion(null);
479-
480-
// if (
481-
// getCursorOffset(editableRef.current) < (editableRef.current.innerText.length - 2)
482-
// || editableRef.current.innerText.length <= 1
483-
// ) return;
484-
485-
// setCompletionTimeout(
486-
// setTimeout(
487-
// createCompletion,
488-
// 500
489-
// )
490-
// );
491-
// }}
492-
// onBlur={
493-
// (e) => {
494-
// if (completionTimeout) {
495-
// clearTimeout(completionTimeout);
496-
// }
497-
498-
// setCompletion(null);
499-
// setCompletionTimeout(null);
500-
// saveBlock(e.currentTarget);
501-
// }
502-
// }
503-
// onKeyDown={
504-
// (e) => {
505-
// if (!editableRef.current) return;
506-
507-
// if (e.code === 'Enter' && !e.shiftKey) {
508-
// e.preventDefault();
509-
// e.currentTarget.blur();
510-
// addBlockAtIndex(index + 1, page, pageData, setPageData);
511-
// } else if (e.code === 'Backspace' && type !== 'text' && getCursorOffset(editableRef.current) === 0) {
512-
// setCurrentBlockType('text');
513-
// editBlock([blockID], 'text', undefined, page);
514-
// } else if (e.code === 'Backspace' && type === 'text' && (editableRef.current.innerText === '' || editableRef.current.innerText === '\n')) {
515-
// removeBlock(index, [blockID], page, pageData, setPageData, true);
516-
// }
517-
// }
518-
// }
519-
// >
520-
// {
521-
// renderInlineBlocks(
522-
// properties.value,
523-
// properties.style
524-
// )
525-
// }
526-
// {completion}
527-
// {slashMenu}
528-
// </ContentEditable>
529-
// );
464+
);
530465
};
531466

532467
export default TextBlock;

0 commit comments

Comments
 (0)