-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ai message content to document (#7041)
* feat: add ai response content to page * chore: apply suggestions from code review Co-authored-by: Lucas <[email protected]> * chore: apply suggestions from code review * chore: reorganize code * chore: i18n * chore: enable opening the document in the sidebar * fix: async await * chore: rename ai message action bar widget * feat: make transactions be reflected in the opened document * chore: don't forget to close the bloc * fix: isLastLineEmpty * chore: code cleanup * fix: sync after EditorState.apply * chore: decrease visibility of DocumentBlocMap * chore: add back missing assert --------- Co-authored-by: Lucas <[email protected]>
- Loading branch information
1 parent
956d2df
commit 200b367
Showing
15 changed files
with
690 additions
and
219 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_edit_document_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:appflowy/generated/locale_keys.g.dart'; | ||
import 'package:appflowy/plugins/document/application/document_data_pb_extension.dart'; | ||
import 'package:appflowy/plugins/document/application/prelude.dart'; | ||
import 'package:appflowy/shared/markdown_to_document.dart'; | ||
import 'package:appflowy/workspace/application/view/view_service.dart'; | ||
import 'package:appflowy_backend/log.dart'; | ||
import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart'; | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:appflowy_result/appflowy_result.dart'; | ||
import 'package:easy_localization/easy_localization.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_chat_core/flutter_chat_core.dart'; | ||
|
||
class ChatEditDocumentService { | ||
static Future<ViewPB?> saveMessagesToNewPage( | ||
String chatPageName, | ||
String parentViewId, | ||
List<TextMessage> messages, | ||
) async { | ||
if (messages.isEmpty) { | ||
return null; | ||
} | ||
|
||
// Convert messages to markdown and trim the last empty newline. | ||
final completeMessage = messages.map((m) => m.text).join('\n').trimRight(); | ||
if (completeMessage.isEmpty) { | ||
return null; | ||
} | ||
|
||
final document = customMarkdownToDocument(completeMessage); | ||
final initialBytes = | ||
DocumentDataPBFromTo.fromDocument(document)?.writeToBuffer(); | ||
if (initialBytes == null) { | ||
Log.error('Failed to convert messages to document'); | ||
return null; | ||
} | ||
|
||
return ViewBackendService.createView( | ||
name: LocaleKeys.chat_addToNewPageName.tr(args: [chatPageName]), | ||
layoutType: ViewLayoutPB.Document, | ||
parentViewId: parentViewId, | ||
initialDataBytes: initialBytes, | ||
).toNullable(); | ||
} | ||
|
||
static Future<void> addMessageToPage( | ||
String documentId, | ||
TextMessage message, | ||
) async { | ||
if (message.text.isEmpty) { | ||
Log.error('Message is empty'); | ||
return; | ||
} | ||
|
||
final bloc = DocumentBloc( | ||
documentId: documentId, | ||
saveToBlocMap: false, | ||
)..add(const DocumentEvent.initial()); | ||
|
||
if (bloc.state.editorState == null) { | ||
await bloc.stream.firstWhere((state) => state.editorState != null); | ||
} | ||
|
||
final editorState = bloc.state.editorState; | ||
if (editorState == null) { | ||
Log.error("Can't get EditorState of document"); | ||
return; | ||
} | ||
|
||
final messageDocument = customMarkdownToDocument(message.text); | ||
if (messageDocument.isEmpty) { | ||
Log.error('Failed to convert message to document'); | ||
return; | ||
} | ||
|
||
final lastNodeOrNull = editorState.document.root.children.lastOrNull; | ||
|
||
final rootIsEmpty = lastNodeOrNull == null; | ||
final isLastLineEmpty = lastNodeOrNull?.children.isNotEmpty == false && | ||
lastNodeOrNull?.delta?.isNotEmpty == false; | ||
|
||
final nodes = [ | ||
if (rootIsEmpty || !isLastLineEmpty) paragraphNode(), | ||
...messageDocument.root.children, | ||
]; | ||
final insertPath = rootIsEmpty || listEquals(lastNodeOrNull.path, const [0]) | ||
? const [0] | ||
: lastNodeOrNull.path.next; | ||
|
||
final transaction = editorState.transaction..insertNodes(insertPath, nodes); | ||
await editorState.apply(transaction); | ||
await bloc.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.