* fix: [Bug] Code block jump skip blank line

* fix: auto completion interaction

* Revert "fix: [Bug] Code block jump skip blank line"

This reverts commit 5a252bcb18.

* fix: [Bug] Code block jump skip blank line

* fix: number list parse error
This commit is contained in:
Lucas.Xu
2023-03-09 10:10:31 +08:00
committed by GitHub
parent d3823eb076
commit 21199c04ac
7 changed files with 75 additions and 26 deletions

View File

@ -2,7 +2,6 @@ import 'dart:convert';
import 'package:appflowy/plugins/document/presentation/plugins/openai/service/text_edit.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'text_completion.dart';
import 'package:dartz/dartz.dart';
@ -39,7 +38,7 @@ abstract class OpenAIRepository {
Future<Either<OpenAIError, TextCompletionResponse>> getCompletions({
required String prompt,
String? suffix,
int maxTokens = 500,
int maxTokens = 2048,
double temperature = .3,
});
@ -47,10 +46,10 @@ abstract class OpenAIRepository {
required String prompt,
required Future<void> Function() onStart,
required Future<void> Function(TextCompletionResponse response) onProcess,
required VoidCallback onEnd,
required Future<void> Function() onEnd,
required void Function(OpenAIError error) onError,
String? suffix,
int maxTokens = 500,
int maxTokens = 2048,
double temperature = 0.3,
});
@ -85,7 +84,7 @@ class HttpOpenAIRepository implements OpenAIRepository {
Future<Either<OpenAIError, TextCompletionResponse>> getCompletions({
required String prompt,
String? suffix,
int maxTokens = 500,
int maxTokens = 2048,
double temperature = 0.3,
}) async {
final parameters = {
@ -121,10 +120,10 @@ class HttpOpenAIRepository implements OpenAIRepository {
required String prompt,
required Future<void> Function() onStart,
required Future<void> Function(TextCompletionResponse response) onProcess,
required VoidCallback onEnd,
required Future<void> Function() onEnd,
required void Function(OpenAIError error) onError,
String? suffix,
int maxTokens = 500,
int maxTokens = 2048,
double temperature = 0.3,
}) async {
final parameters = {
@ -159,21 +158,23 @@ class HttpOpenAIRepository implements OpenAIRepository {
continue;
}
final data = chunk.trim().split('data: ');
if (data.length > 1 && data[1] != '[DONE]') {
final response = TextCompletionResponse.fromJson(
json.decode(data[1]),
);
if (response.choices.isNotEmpty) {
final text = response.choices.first.text;
if (text == previousSyntax && text == '\n') {
continue;
Log.editor.info(data.toString());
if (data.length > 1) {
if (data[1] != '[DONE]') {
final response = TextCompletionResponse.fromJson(
json.decode(data[1]),
);
if (response.choices.isNotEmpty) {
final text = response.choices.first.text;
if (text == previousSyntax && text == '\n') {
continue;
}
await onProcess(response);
previousSyntax = response.choices.first.text;
}
await onProcess(response);
previousSyntax = response.choices.first.text;
Log.editor.info(response.choices.first.text);
} else {
onEnd();
}
} else {
onEnd();
}
}
} else {

View File

@ -69,6 +69,8 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
void dispose() {
controller.dispose();
textFieldFocusNode.removeListener(_onFocusChanged);
widget.editorState.service.selectionService.currentSelection
.removeListener(_onCancelWhenSelectionChanged);
super.dispose();
}
@ -239,6 +241,7 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
final result = await UserBackendService.getCurrentUserProfile();
result.fold((userProfile) async {
BarrierDialog? barrierDialog;
final openAIRepository = HttpOpenAIRepository(
client: http.Client(),
apiKey: userProfile.openaiKey,
@ -247,6 +250,8 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
prompt: controller.text,
onStart: () async {
loading.stop();
barrierDialog = BarrierDialog(context);
barrierDialog?.show();
await _makeSurePreviousNodeIsEmptyTextNode();
},
onProcess: (response) async {
@ -255,10 +260,15 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
await widget.editorState.autoInsertText(
text,
inputType: TextRobotInputType.word,
delay: Duration.zero,
);
}
},
onEnd: () {},
onEnd: () async {
await barrierDialog?.dismiss();
widget.editorState.service.selectionService.currentSelection
.addListener(_onCancelWhenSelectionChanged);
},
onError: (error) async {
loading.stop();
await _showError(error.message);
@ -353,4 +363,6 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
widget.editorState.service.keyboardService?.enable();
}
}
void _onCancelWhenSelectionChanged() {}
}

View File

@ -32,3 +32,28 @@ class Loading {
return Navigator.of(loadingContext).pop();
}
}
class BarrierDialog {
BarrierDialog(
this.context,
);
late BuildContext loadingContext;
final BuildContext context;
Future<void> show() async {
return showDialog<void>(
context: context,
barrierDismissible: false,
barrierColor: Colors.transparent,
builder: (BuildContext context) {
loadingContext = context;
return Container();
},
);
}
Future<void> dismiss() async {
return Navigator.of(loadingContext).pop();
}
}