Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

fix: ai writer launch review 0.8.5 (#7445)

Richard Shiue637c043f5b58
diff --git a/frontend/appflowy_flutter/lib/ai/service/ai_prompt_input_bloc.dart b/frontend/appflowy_flutter/lib/ai/service/ai_prompt_input_bloc.dart
index e80b196db6..23d8879275 100644
--- a/frontend/appflowy_flutter/lib/ai/service/ai_prompt_input_bloc.dart
+++ b/frontend/appflowy_flutter/lib/ai/service/ai_prompt_input_bloc.dart
@@ -44,6 +44,7 @@ class AIPromptInputBloc extends Bloc<AIPromptInputEvent, AIPromptInputState> {
final aiType = chatState.pluginState.state == RunningStatePB.Running
? AIType.localAI
: AIType.appflowyAI;
+
emit(
state.copyWith(
aiType: aiType,
@@ -69,9 +70,17 @@ class AIPromptInputBloc extends Bloc<AIPromptInputEvent, AIPromptInputState> {
);
},
toggleShowPredefinedFormat: () {
+ final predefinedFormat =
+ !state.showPredefinedFormats && state.predefinedFormat == null
+ ? PredefinedFormat(
+ imageFormat: ImageFormat.text,
+ textFormat: TextFormat.paragraph,
+ )
+ : null;
emit(
state.copyWith(
showPredefinedFormats: !state.showPredefinedFormats,
+ predefinedFormat: predefinedFormat,
),
);
},
diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/chat_related_question.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/chat_related_question.dart
index deba1cb96d..9e4cc603b0 100644
--- a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/chat_related_question.dart
+++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/chat_related_question.dart
@@ -70,7 +70,7 @@ class RelatedQuestionItem extends StatelessWidget {
child: FlowyText(
question,
lineHeight: 1.4,
- overflow: TextOverflow.ellipsis,
+ maxLines: null,
),
),
expandText: false,
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart
index fd1d0c4c82..a6a4c80b5d 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart
@@ -169,6 +169,7 @@ class _AIWriterBlockComponentState extends State<AiWriterBlockComponent> {
return GestureDetector(
behavior: hitTestBehavior,
onTap: () => onTapOutside(),
+ onTapDown: (_) => onTapOutside(),
);
},
),
@@ -300,46 +301,59 @@ class OverlayContent extends StatelessWidget {
child: Container(
constraints: BoxConstraints(maxHeight: 140),
width: double.infinity,
- child: SingleChildScrollView(
- padding: EdgeInsets.all(8.0),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Container(
- height: 24.0,
- padding: EdgeInsets.symmetric(horizontal: 6.0),
- alignment: AlignmentDirectional.centerStart,
- child: FlowyText(
- state.command.i18n,
- fontSize: 12,
- fontWeight: FontWeight.w600,
- color: Color(0xFF666D76),
+ padding: EdgeInsets.symmetric(horizontal: 8.0),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Expanded(
+ child: SingleChildScrollView(
+ physics: ClampingScrollPhysics(),
+ padding: EdgeInsets.only(top: 8.0),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Container(
+ height: 24.0,
+ padding:
+ EdgeInsets.symmetric(horizontal: 6.0),
+ alignment:
+ AlignmentDirectional.centerStart,
+ child: FlowyText(
+ state.command.i18n,
+ fontSize: 12,
+ fontWeight: FontWeight.w600,
+ color: Color(0xFF666D76),
+ ),
+ ),
+ const VSpace(4.0),
+ Padding(
+ padding:
+ EdgeInsets.symmetric(horizontal: 6.0),
+ child: AIMarkdownText(
+ markdown: markdownText,
+ ),
+ ),
+ ],
),
),
+ ),
+ if (showSuggestionPopup) ...[
const VSpace(4.0),
- Padding(
- padding: EdgeInsets.symmetric(horizontal: 6.0),
- child: AIMarkdownText(
- markdown: markdownText,
+ SuggestionActionBar(
+ actions: _getSuggestedActions(
+ currentCommand: state.command,
+ hasSelection: hasSelection,
),
+ onTap: (action) {
+ context
+ .read<AiWriterCubit>()
+ .runResponseAction(action);
+ },
),
- if (showSuggestionPopup) ...[
- const VSpace(4.0),
- SuggestionActionBar(
- actions: _getSuggestedActions(
- currentCommand: state.command,
- hasSelection: hasSelection,
- ),
- onTap: (action) {
- context
- .read<AiWriterCubit>()
- .runResponseAction(action);
- },
- ),
- ],
],
- ),
+ const VSpace(8.0),
+ ],
),
),
),
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_toolbar_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_toolbar_item.dart
index 96fb8289d3..714bd5f4b8 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_toolbar_item.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_toolbar_item.dart
@@ -187,6 +187,7 @@ class ImproveWritingButton extends StatelessWidget {
onPressed: () {
if (_isAIEnabled(editorState)) {
keepEditorFocusNotifier.increase();
+ _insertAiNode(editorState, AiWriterCommand.improveWriting);
} else {
showToastNotification(
context,
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart
index e1c6efb337..bf46dd0e9a 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_cubit.dart
@@ -29,7 +29,9 @@ class AiWriterCubit extends Cubit<AiWriterState> {
initialCommand,
isFirstRun: true,
),
- );
+ ) {
+ editorState.service.keyboardService?.disable();
+ }
final String documentId;
final EditorState editorState;
@@ -40,10 +42,12 @@ class AiWriterCubit extends Cubit<AiWriterState> {
final ValueNotifier<List<String>> selectedSourcesNotifier;
(String, PredefinedFormat?)? _previousPrompt;
+ bool acceptReplacesOriginal = false;
@override
Future<void> close() async {
selectedSourcesNotifier.dispose();
+ editorState.service.keyboardService?.enable();
await super.close();
}
@@ -212,7 +216,7 @@ class AiWriterCubit extends Cubit<AiWriterState> {
if (action case SuggestionAction.accept || SuggestionAction.keep) {
await _textRobot.persist();
- if (state.command.acceptWillReplace) {
+ if (acceptReplacesOriginal) {
final nodes = editorState.getNodesInSelection(selection);
final transaction = editorState.transaction..deleteNodes(nodes);
await editorState.apply(
@@ -339,7 +343,6 @@ class AiWriterCubit extends Cubit<AiWriterState> {
);
},
onEnd: () async {
- editorState.service.keyboardService?.enable();
if (state case GeneratingAiWriterState _) {
await _textRobot.stop(
attributes: ApplySuggestionFormatType.replace.attributes,
@@ -348,7 +351,6 @@ class AiWriterCubit extends Cubit<AiWriterState> {
}
},
onError: (error) async {
- editorState.service.keyboardService?.enable();
emit(ErrorAiWriterState(command, error: error));
},
);
@@ -369,6 +371,8 @@ class AiWriterCubit extends Cubit<AiWriterState> {
return;
}
+ acceptReplacesOriginal = true;
+
final stream = await _aiService.streamCompletion(
objectId: documentId,
text: await editorState.getMarkdownInSelection(selection),
@@ -413,7 +417,6 @@ class AiWriterCubit extends Cubit<AiWriterState> {
}
},
onError: (error) async {
- editorState.service.keyboardService?.enable();
emit(ErrorAiWriterState(command, error: error));
},
);
@@ -451,7 +454,6 @@ class AiWriterCubit extends Cubit<AiWriterState> {
}
},
onEnd: () async {
- editorState.service.keyboardService?.enable();
if (state case final GeneratingAiWriterState generatingState) {
emit(
ReadyAiWriterState(
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_entities.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_entities.dart
index 349d7cb419..e0cc944b3f 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_entities.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/operations/ai_writer_entities.dart
@@ -108,15 +108,6 @@ enum AiWriterCommand {
makeShorter => CompletionTypePB.MakeShorter,
makeLonger => CompletionTypePB.MakeLonger,
};
-
- bool get acceptWillReplace => switch (this) {
- AiWriterCommand.fixSpellingAndGrammar ||
- AiWriterCommand.improveWriting ||
- AiWriterCommand.makeLonger ||
- AiWriterCommand.makeShorter =>
- true,
- _ => false,
- };
}
enum ApplySuggestionFormatType {
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/markdown_text_robot.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/markdown_text_robot.dart
index ac170fef00..36936f36a9 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/markdown_text_robot.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/markdown_text_robot.dart
@@ -162,12 +162,6 @@ class MarkdownTextRobot {
return;
}
- final node = editorState.getNodeAtPath(position.path);
- if (node == null) {
- Log.error("Cannot find node at position: ${position.path}");
- return;
- }
-
// Convert markdown and deep copy the nodes, prevent ing the linked
// entities from being changed
final documentNodes = customMarkdownToDocument(
@@ -184,29 +178,40 @@ class MarkdownTextRobot {
if (newNodes.isEmpty) {
return;
}
- final transaction = editorState.transaction
- ..insertNodes(position.path, newNodes)
+
+ final deleteTransaction = editorState.transaction
..deleteNodes(getInsertedNodes());
+ await editorState.apply(
+ deleteTransaction,
+ options: ApplyOptions(
+ inMemoryUpdate: inMemoryUpdate,
+ recordUndo: false,
+ ),
+ );
+
+ final insertTransaction = editorState.transaction
+ ..insertNodes(position.path, newNodes);
+
final lastDelta = newNodes.lastOrNull?.delta;
if (lastDelta != null) {
- transaction.afterSelection = Selection.collapsed(
+ insertTransaction.afterSelection = Selection.collapsed(
Position(
path: position.path.nextNPath(newNodes.length - 1),
offset: lastDelta.length,
),
);
- }
- if (!updateSelection) {
- transaction.afterSelection = null;
+ if (!updateSelection) {
+ insertTransaction.afterSelection = null;
+ }
}
await editorState.apply(
- transaction,
+ insertTransaction,
options: ApplyOptions(
inMemoryUpdate: inMemoryUpdate,
- recordUndo: false,
+ recordUndo: !inMemoryUpdate,
),
);
diff --git a/frontend/rust-lib/flowy-ai/src/completion.rs b/frontend/rust-lib/flowy-ai/src/completion.rs
index 28c64d8a8f..38f4f565e6 100644
--- a/frontend/rust-lib/flowy-ai/src/completion.rs
+++ b/frontend/rust-lib/flowy-ai/src/completion.rs
@@ -104,7 +104,7 @@ impl CompletionTask {
custom_prompt: None,
metadata: Some(CompletionMetadata {
object_id: self.context.object_id,
- workspace_id: None,
+ workspace_id: Some(self.workspace_id.clone()),
rag_ids: Some(self.context.rag_ids),
}),
format: self.context.format.map(Into::into).unwrap_or_default(),