Compare commits

...
Author SHA1 Message Date
Nathan 58e04291c7 chore: run local ai on windows 2025-03-11 21:30:07 +08:00
Nathan.fooo 57a5b38509 Merge pull request #7488 from AppFlowy-IO/ollama
feat: support Ollama
2025-03-11 13:53:35 +08:00
Nathan 83c53188e3 chore: clippy 2025-03-11 13:22:59 +08:00
Nathan 6ba7f93f69 chore: find plugin load 2025-03-11 13:14:47 +08:00
Nathan 702a486cce chore: find windows exe 2025-03-11 12:55:19 +08:00
Richard Shiue eb0ed1ad86 fix: don't allow selection of text in related questions or loading (#7500) 2025-03-11 11:38:20 +08:00
Lucas e264b3a5b8 Merge pull request #7490 from richardshiue/fix/callout-icon-in-markdown
fix: don't include icon while exporting callout to md
2025-03-11 10:38:46 +08:00
Richard Shiue 667d15c627 fix: ai writer gestures (#7499) 2025-03-11 10:31:08 +08:00
Nathan bd06e1d559 chore: clippy 2025-03-11 09:32:20 +08:00
Lucas 459aca5291 Merge pull request #7496 from LucasXu0/ai_writer_position_improvement
feat: ensure the ai writer block visible when generating result
2025-03-11 09:31:24 +08:00
Nathan e7cd90b6ab chore: update commit 2025-03-11 09:14:11 +08:00
Nathan 940db70447 chore: fix build 2025-03-11 00:27:55 +08:00
Nathan 59139ff323 chore: catch panic 2025-03-10 23:40:28 +08:00
Nathan 22fed1bfbc chore: load from env command 2025-03-10 22:48:09 +08:00
Lucas.Xu 5e593bd36e chore: update appflowy editor version 2025-03-10 21:08:28 +08:00
Lucas.Xu ba1dfc6de4 feat: ensure the ai writer block visible when generating result 2025-03-10 19:18:06 +08:00
Lucas c81f87dcdc feat: make the columns block same width width the editor (#7493)
* feat: make the columns block same width width the editor

* chore: turn off column debug mode

* feat: add block selection container in outline block

* feat: use ratio instead of width in simple columns

* fix: document rules

* fix: turn off debug mode

* fix: update the existing columns block data
2025-03-10 18:13:15 +08:00
Nathan a8b55ca3f0 chore: update prompt 2025-03-10 15:56:09 +08:00
Richard Shiue 0cefaf633c fix: fix test 2025-03-10 14:56:01 +08:00
Richard Shiue ba4aebd005 chore: merge remote-tracking branch 'main' into this one 2025-03-10 13:36:08 +08:00
Lucas 7b32a92290 fix: callout block build error (#7491) 2025-03-10 13:04:29 +08:00
Richard Shiue 41b99209f1 fix: retain if is emoji 2025-03-10 12:10:59 +08:00
Morn c1612fe298 feat: add custom icons for callout (#7449) 2025-03-10 11:46:55 +08:00
Lucas e69a09d332 feat: support nested list in callout block and quote block (#7479)
* feat: support nested list in callout block

* chore: update pubspec.yml

* feat: add new quote block

* feat: support nested list in quote block

* feat: refacotr quote block

* feat: optimize quote block align

* feat: support nested list in quote block

* fix: icon and drag menu overlap

* chore: update appflowy editor version

* feat: support trailing action builder for plugin blocks

* chore: update appflowy editor version
2025-03-10 11:46:17 +08:00
Richard Shiue 4e0d9fdb0b fix: don't include icon while exporting callout to md 2025-03-10 11:08:22 +08:00
Nathan 8b2e769fca chore: ai setting ui 2025-03-10 10:24:55 +08:00
Nathan d29a90a472 chore: init ai plugin on separate thread 2025-03-10 08:54:32 +08:00
Nathan 2e4beb0652 chore: enable windows 2025-03-10 00:35:52 +08:00
Nathan addb041816 chore: update exe path 2025-03-10 00:25:19 +08:00
Nathan 4ff71b5dce chore: implement ollama 2025-03-09 23:32:42 +08:00
90 changed files with 2827 additions and 2870 deletions

No files matched your search

@@ -36,36 +36,29 @@ class AIPromptInputBloc extends Bloc<AIPromptInputEvent, AIPromptInputState> {
on<AIPromptInputEvent>(
(event, emit) {
event.when(
updateChatState: (LocalAIChatPB chatState) {
updateAIState: (LocalAIPB localAIState) {
if (localAIState.hasLackOfResource()) {
emit(
state.copyWith(
aiType: AIType.appflowyAI,
supportChatWithFile: false,
localAIState: localAIState,
),
);
return;
}
// Only user enable chat with file and the plugin is already running
final supportChatWithFile = chatState.fileEnabled &&
chatState.pluginState.state == RunningStatePB.Running;
final supportChatWithFile = localAIState.enabled &&
localAIState.state == RunningStatePB.Running;
final aiType = chatState.pluginState.state == RunningStatePB.Running
? AIType.localAI
: AIType.appflowyAI;
final aiType =
localAIState.enabled ? AIType.localAI : AIType.appflowyAI;
emit(
state.copyWith(
aiType: aiType,
supportChatWithFile: supportChatWithFile,
chatState: chatState,
),
);
},
updatePluginState: (LocalAIPluginStatePB chatState) {
final fileEnabled = state.chatState?.fileEnabled ?? false;
final supportChatWithFile =
fileEnabled && chatState.state == RunningStatePB.Running;
final aiType = chatState.state == RunningStatePB.Running
? AIType.localAI
: AIType.appflowyAI;
emit(
state.copyWith(
supportChatWithFile: supportChatWithFile,
aiType: aiType,
localAIState: localAIState,
),
);
},
@@ -130,22 +123,17 @@ class AIPromptInputBloc extends Bloc<AIPromptInputEvent, AIPromptInputState> {
_listener.start(
stateCallback: (pluginState) {
if (!isClosed) {
add(AIPromptInputEvent.updatePluginState(pluginState));
}
},
chatStateCallback: (chatState) {
if (!isClosed) {
add(AIPromptInputEvent.updateChatState(chatState));
add(AIPromptInputEvent.updateAIState(pluginState));
}
},
);
}
void _init() {
AIEventGetLocalAIChatState().send().fold(
(chatState) {
AIEventGetLocalAIState().send().fold(
(localAIState) {
if (!isClosed) {
add(AIPromptInputEvent.updateChatState(chatState));
add(AIPromptInputEvent.updateAIState(localAIState));
}
},
Log.error,
@@ -168,12 +156,8 @@ class AIPromptInputBloc extends Bloc<AIPromptInputEvent, AIPromptInputState> {
@freezed
class AIPromptInputEvent with _$AIPromptInputEvent {
const factory AIPromptInputEvent.updateChatState(
LocalAIChatPB chatState,
) = _UpdateChatState;
const factory AIPromptInputEvent.updatePluginState(
LocalAIPluginStatePB chatState,
) = _UpdatePluginState;
const factory AIPromptInputEvent.updateAIState(LocalAIPB localAIState) =
_UpdateAIState;
const factory AIPromptInputEvent.toggleShowPredefinedFormat() =
_ToggleShowPredefinedFormat;
const factory AIPromptInputEvent.updatePredefinedFormat(
@@ -196,7 +180,7 @@ class AIPromptInputState with _$AIPromptInputState {
required bool supportChatWithFile,
required bool showPredefinedFormats,
required PredefinedFormat? predefinedFormat,
required LocalAIChatPB? chatState,
required LocalAIPB? localAIState,
required List<ChatFile> attachedFiles,
required List<ViewPB> mentionedPages,
}) = _AIPromptInputState;
@@ -207,7 +191,7 @@ class AIPromptInputState with _$AIPromptInputState {
supportChatWithFile: false,
showPredefinedFormats: format != null,
predefinedFormat: format,
chatState: null,
localAIState: null,
attachedFiles: [],
mentionedPages: [],
);
@@ -16,48 +16,50 @@ class AILoadingIndicator extends StatelessWidget {
@override
Widget build(BuildContext context) {
final slice = Duration(milliseconds: duration.inMilliseconds ~/ 5);
return SizedBox(
height: 20,
child: SeparatedRow(
separatorBuilder: () => const HSpace(4),
children: [
Padding(
padding: const EdgeInsetsDirectional.only(end: 4.0),
child: FlowyText(
text,
color: Theme.of(context).hintColor,
return SelectionContainer.disabled(
child: SizedBox(
height: 20,
child: SeparatedRow(
separatorBuilder: () => const HSpace(4),
children: [
Padding(
padding: const EdgeInsetsDirectional.only(end: 4.0),
child: FlowyText(
text,
color: Theme.of(context).hintColor,
),
),
),
buildDot(const Color(0xFF9327FF))
.animate(onPlay: (controller) => controller.repeat())
.slideY(duration: slice, begin: 0, end: -1)
.then()
.slideY(begin: -1, end: 1)
.then()
.slideY(begin: 1, end: 0)
.then()
.slideY(duration: slice * 2, begin: 0, end: 0),
buildDot(const Color(0xFFFB006D))
.animate(onPlay: (controller) => controller.repeat())
.slideY(duration: slice, begin: 0, end: 0)
.then()
.slideY(begin: 0, end: -1)
.then()
.slideY(begin: -1, end: 1)
.then()
.slideY(begin: 1, end: 0)
.then()
.slideY(begin: 0, end: 0),
buildDot(const Color(0xFFFFCE00))
.animate(onPlay: (controller) => controller.repeat())
.slideY(duration: slice * 2, begin: 0, end: 0)
.then()
.slideY(duration: slice, begin: 0, end: -1)
.then()
.slideY(begin: -1, end: 1)
.then()
.slideY(begin: 1, end: 0),
],
buildDot(const Color(0xFF9327FF))
.animate(onPlay: (controller) => controller.repeat())
.slideY(duration: slice, begin: 0, end: -1)
.then()
.slideY(begin: -1, end: 1)
.then()
.slideY(begin: 1, end: 0)
.then()
.slideY(duration: slice * 2, begin: 0, end: 0),
buildDot(const Color(0xFFFB006D))
.animate(onPlay: (controller) => controller.repeat())
.slideY(duration: slice, begin: 0, end: 0)
.then()
.slideY(begin: 0, end: -1)
.then()
.slideY(begin: -1, end: 1)
.then()
.slideY(begin: 1, end: 0)
.then()
.slideY(begin: 0, end: 0),
buildDot(const Color(0xFFFFCE00))
.animate(onPlay: (controller) => controller.repeat())
.slideY(duration: slice * 2, begin: 0, end: 0)
.then()
.slideY(duration: slice, begin: 0, end: -1)
.then()
.slideY(begin: -1, end: 1)
.then()
.slideY(begin: 1, end: 0),
],
),
),
);
}
@@ -572,7 +572,7 @@ class _PromptBottomActions extends StatelessWidget {
margin: DesktopAIChatSizes.inputActionBarMargin,
child: BlocBuilder<AIPromptInputBloc, AIPromptInputState>(
builder: (context, state) {
if (state.chatState == null) {
if (state.localAIState == null) {
return Align(
alignment: AlignmentDirectional.centerEnd,
child: _sendButton(),
@@ -21,33 +21,35 @@ class RelatedQuestionList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListView.separated(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: relatedQuestions.length + 1,
padding:
const EdgeInsets.only(bottom: 8.0) + AIChatUILayout.messageMargin,
separatorBuilder: (context, index) => const VSpace(4.0),
itemBuilder: (context, index) {
if (index == 0) {
return Padding(
padding: const EdgeInsets.only(bottom: 4.0),
child: FlowyText(
LocaleKeys.chat_relatedQuestion.tr(),
color: Theme.of(context).hintColor,
fontWeight: FontWeight.w600,
),
);
} else {
return Align(
alignment: AlignmentDirectional.centerStart,
child: RelatedQuestionItem(
question: relatedQuestions[index - 1],
onQuestionSelected: onQuestionSelected,
),
);
}
},
return SelectionContainer.disabled(
child: ListView.separated(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: relatedQuestions.length + 1,
padding:
const EdgeInsets.only(bottom: 8.0) + AIChatUILayout.messageMargin,
separatorBuilder: (context, index) => const VSpace(4.0),
itemBuilder: (context, index) {
if (index == 0) {
return Padding(
padding: const EdgeInsets.only(bottom: 4.0),
child: FlowyText(
LocaleKeys.chat_relatedQuestion.tr(),
color: Theme.of(context).hintColor,
fontWeight: FontWeight.w600,
),
);
} else {
return Align(
alignment: AlignmentDirectional.centerStart,
child: RelatedQuestionItem(
question: relatedQuestions[index - 1],
onQuestionSelected: onQuestionSelected,
),
);
}
},
),
);
}
}
@@ -13,7 +13,6 @@ import 'package:appflowy_editor/appflowy_editor.dart'
NodeIterator,
NodeExternalValues,
HeadingBlockKeys,
QuoteBlockKeys,
NumberedListBlockKeys,
BulletedListBlockKeys,
blockComponentDelta;
@@ -104,6 +104,28 @@ class DocumentRules {
} else {
// otherwise, delete the column
deleteColumnsTransaction.deleteNode(column);
final deletedColumnRatio =
column.attributes[SimpleColumnBlockKeys.ratio];
if (deletedColumnRatio != null) {
// update the ratio of the columns
final columnsNode = column.columnsParent;
if (columnsNode != null) {
final length = columnsNode.children.length;
for (final columnNode in columnsNode.children) {
final ratio =
columnNode.attributes[SimpleColumnBlockKeys.ratio] ??
1.0 / length;
if (ratio != null) {
deleteColumnsTransaction.updateNode(columnNode, {
...columnNode.attributes,
SimpleColumnBlockKeys.ratio:
ratio + deletedColumnRatio / (length - 1),
});
}
}
}
}
}
}
}
@@ -6,7 +6,8 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/actions/mo
import 'package:appflowy/plugins/document/presentation/editor_plugins/code_block/code_block_copy_button.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy/plugins/document/presentation/editor_style.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide QuoteBlockComponentBuilder, quoteNode, QuoteBlockKeys;
import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
import 'package:easy_localization/easy_localization.dart' hide TextDirection;
import 'package:flowy_infra/theme_extension.dart';
@@ -121,10 +122,18 @@ BlockComponentConfiguration _buildDefaultConfiguration(BuildContext context) {
},
indentPadding: (node, textDirection) {
double padding = 26.0;
// only add indent padding for the top level node to align the children
if (UniversalPlatform.isMobile && node.path.length == 1) {
padding += EditorStyleCustomizer.nodeHorizontalPadding;
}
// in the quote block, we reduce the indent padding for the first level block.
// So we have to add more padding for the second level to avoid the drag menu overlay the quote icon.
if (node.isInQuote && node.level == 2) {
padding += 24;
}
return textDirection == TextDirection.ltr
? EdgeInsets.only(left: padding)
: EdgeInsets.only(right: padding);
@@ -203,6 +212,16 @@ void _customBlockOptionActions(
),
);
builder.actionTrailingBuilder = (context, state) {
if (context.node.parent?.type == QuoteBlockKeys.type) {
return const SizedBox(
width: 24,
height: 24,
);
}
return const SizedBox.shrink();
};
builder.actionBuilder = (context, state) {
double top = builder.configuration.padding(context.node).top;
final type = context.node.type;
@@ -592,6 +611,7 @@ QuoteBlockComponentBuilder _buildQuoteBlockComponentBuilder(
node: node,
configuration: configuration,
),
indentPadding: (node, _) => EdgeInsets.zero,
),
);
}
@@ -798,8 +818,14 @@ CalloutBlockComponentBuilder _buildCalloutBlockComponentBuilder(
configuration: configuration,
textSpan: textSpan,
),
indentPadding: (node, _) => EdgeInsets.only(left: 42),
),
inlinePadding: const EdgeInsets.symmetric(vertical: 8.0),
inlinePadding: (node) {
if (node.children.isEmpty) {
return const EdgeInsets.symmetric(vertical: 8.0);
}
return EdgeInsets.only(top: 8.0, bottom: 2.0);
},
defaultColor: calloutBGColor,
);
}
@@ -1065,3 +1091,16 @@ TextAlign _buildTextAlignInTableCell(
return node.tableAlign.textAlign;
}
extension on Node {
bool get isInQuote {
Node? parent = this.parent;
while (parent != null) {
if (parent.type == QuoteBlockKeys.type) {
return true;
}
parent = parent.parent;
}
return false;
}
}
@@ -154,7 +154,10 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>
InlineMathEquationKeys.formula,
]);
indentableBlockTypes.add(ToggleListBlockKeys.type);
indentableBlockTypes.addAll([
ToggleListBlockKeys.type,
CalloutBlockKeys.type,
]);
convertibleBlockTypes.addAll([
ToggleListBlockKeys.type,
CalloutBlockKeys.type,
@@ -363,7 +366,9 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>
contextMenuItems: customContextMenuItems,
// customize the header and footer.
header: widget.header,
autoScrollEdgeOffset: UniversalPlatform.isDesktopOrWeb
? 250
: appFlowyEditorAutoScrollEdgeOffset,
footer: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () async {
@@ -372,7 +377,7 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>
},
child: SizedBox(
width: double.infinity,
height: UniversalPlatform.isDesktopOrWeb ? 200 : 400,
height: UniversalPlatform.isDesktopOrWeb ? 300 : 400,
),
),
dropTargetStyle: AppFlowyDropTargetStyle(
@@ -10,7 +10,8 @@ import 'package:appflowy/workspace/application/view/prelude.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide QuoteBlockKeys, quoteNode;
import 'package:flutter_bloc/flutter_bloc.dart';
class BlockActionOptionState {}
@@ -89,7 +89,7 @@ class _DraggableOptionButtonState extends State<DraggableOptionButton> {
interceptor: (context, targetNode) {
// if the cursor node is in a columns block or a column block,
// we will return the node's parent instead to support dragging a node to the inside of a columns block or a column block.
final parentColumnNode = targetNode.parentColumn;
final parentColumnNode = targetNode.columnParent;
if (parentColumnNode != null) {
final position = getDragAreaPosition(
context,
@@ -147,7 +147,7 @@ class _DraggableOptionButtonState extends State<DraggableOptionButton> {
interceptor: (context, targetNode) {
// if the cursor node is in a columns block or a column block,
// we will return the node's parent instead to support dragging a node to the inside of a columns block or a column block.
final parentColumnNode = targetNode.parentColumn;
final parentColumnNode = targetNode.columnParent;
if (parentColumnNode != null) {
final position = getDragAreaPosition(
context,
@@ -1,7 +1,7 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/columns/simple_columns_block_constant.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide QuoteBlockKeys, quoteNode;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
@@ -59,38 +59,37 @@ Future<void> dragToMoveNode(
// 1. if the targetNode is a column block, it means we should create a column block to contain the node and insert the column node to the target node's parent
// 2. if the targetNode is not a column block, it means we should create a columns block to contain the target node and the drag node
final transaction = editorState.transaction;
final targetNodeParent = targetNode.parentColumnsBlock;
final targetNodeParent = targetNode.columnsParent;
if (targetNodeParent != null) {
final length = targetNodeParent.children.length;
final ratios = targetNodeParent.children
.map(
(e) =>
e.attributes[SimpleColumnBlockKeys.ratio]?.toDouble() ??
1.0 / length,
)
.map((e) => e * length / (length + 1))
.toList();
final columnNode = simpleColumnNode(
children: [node.deepCopy()],
width: (node.rect.width * 1 / (length + 1)).clamp(
SimpleColumnsBlockConstants.minimumColumnWidth,
double.infinity,
),
ratio: 1.0 / (length + 1),
);
for (final column in targetNodeParent.children) {
final width =
column.attributes[SimpleColumnBlockKeys.width]?.toDouble() ??
SimpleColumnsBlockConstants.minimumColumnWidth;
for (final (index, column) in targetNodeParent.children.indexed) {
transaction.updateNode(column, {
...column.attributes,
SimpleColumnBlockKeys.width: (width * length / (length + 1)).clamp(
SimpleColumnsBlockConstants.minimumColumnWidth,
double.infinity,
),
SimpleColumnBlockKeys.ratio: ratios[index],
});
}
transaction.insertNode(targetNode.path.next, columnNode);
transaction.deleteNode(node);
} else {
final width = targetNode.rect.width / 2 - 16;
final columnsNode = simpleColumnsNode(
children: [
simpleColumnNode(children: [targetNode.deepCopy()], width: width),
simpleColumnNode(children: [node.deepCopy()], width: width),
simpleColumnNode(children: [targetNode.deepCopy()], ratio: 0.5),
simpleColumnNode(children: [node.deepCopy()], ratio: 0.5),
],
);
@@ -108,39 +107,37 @@ Future<void> dragToMoveNode(
// 1. if the target node is a column block, we should create a column block to contain the node and insert the column node to the target node's parent
// 2. if the target node is not a column block, we should create a columns block to contain the target node and the drag node
final transaction = editorState.transaction;
final targetNodeParent = targetNode.parentColumnsBlock;
final targetNodeParent = targetNode.columnsParent;
if (targetNodeParent != null) {
// find the previous sibling node of the target node
final length = targetNodeParent.children.length;
final ratios = targetNodeParent.children
.map(
(e) =>
e.attributes[SimpleColumnBlockKeys.ratio]?.toDouble() ??
1.0 / length,
)
.map((e) => e * length / (length + 1))
.toList();
final columnNode = simpleColumnNode(
children: [node.deepCopy()],
width: (node.rect.width * 1 / (length + 1)).clamp(
SimpleColumnsBlockConstants.minimumColumnWidth,
double.infinity,
),
ratio: 1.0 / (length + 1),
);
for (final column in targetNodeParent.children) {
final width =
column.attributes[SimpleColumnBlockKeys.width]?.toDouble() ??
SimpleColumnsBlockConstants.minimumColumnWidth;
for (final (index, column) in targetNodeParent.children.indexed) {
transaction.updateNode(column, {
...column.attributes,
SimpleColumnBlockKeys.width: (width * length / (length + 1)).clamp(
SimpleColumnsBlockConstants.minimumColumnWidth,
double.infinity,
),
SimpleColumnBlockKeys.ratio: ratios[index],
});
}
transaction.insertNode(targetNode.path.previous, columnNode);
transaction.deleteNode(node);
} else {
final width = targetNode.rect.width / 2 - 16;
final columnsNode = simpleColumnsNode(
children: [
simpleColumnNode(children: [node.deepCopy()], width: width),
simpleColumnNode(children: [targetNode.deepCopy()], width: width),
simpleColumnNode(children: [node.deepCopy()], ratio: 0.5),
simpleColumnNode(children: [targetNode.deepCopy()], ratio: 0.5),
],
);
@@ -1,7 +1,8 @@
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide QuoteBlockKeys, quoteNode;
import 'package:easy_localization/easy_localization.dart' hide TextDirection;
import 'package:easy_localization/easy_localization.dart';
@@ -5,7 +5,8 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.da
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/workspace/presentation/home/menu/menu_shared_state.dart';
import 'package:appflowy/workspace/presentation/widgets/pop_up_action.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide QuoteBlockKeys, quoteNode;
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
@@ -18,6 +18,7 @@ import 'operations/ai_writer_cubit.dart';
import 'operations/ai_writer_entities.dart';
import 'operations/ai_writer_node_extension.dart';
import 'suggestion_action_bar.dart';
import 'widgets/ai_writer_gesture_detector.dart';
class AiWriterBlockKeys {
const AiWriterBlockKeys._();
@@ -66,6 +67,10 @@ class AIWriterBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -83,6 +88,7 @@ class AiWriterBlockComponent extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -163,10 +169,11 @@ class _AIWriterBlockComponentState extends State<AiWriterBlockComponent> {
children: [
BlocBuilder<AiWriterCubit, AiWriterState>(
builder: (context, state) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => onTapOutside(),
onTapDown: (_) => onTapOutside(),
return AiWriterGestureDetector(
behavior: state is GeneratingAiWriterState
? HitTestBehavior.opaque
: HitTestBehavior.translucent,
onPointerEvent: () => onTapOutside(),
);
},
),
@@ -0,0 +1,40 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
class AiWriterGestureDetector extends StatelessWidget {
const AiWriterGestureDetector({
super.key,
required this.behavior,
required this.onPointerEvent,
this.child,
});
final HitTestBehavior behavior;
final void Function() onPointerEvent;
final Widget? child;
@override
Widget build(BuildContext context) {
return RawGestureDetector(
behavior: behavior,
gestures: <Type, GestureRecognizerFactory>{
TapGestureRecognizer:
GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
() => TapGestureRecognizer(),
(instance) {
instance
..onTap = onPointerEvent
..onTapDown = (_) => onPointerEvent();
},
),
ImmediateMultiDragGestureRecognizer:
GestureRecognizerFactoryWithHandlers<
ImmediateMultiDragGestureRecognizer>(
() => ImmediateMultiDragGestureRecognizer(), (instance) {
instance.onStart = (offset) => null;
}),
},
child: child,
);
}
}
@@ -208,7 +208,7 @@ class _MobileEmojiPickerButton extends StatelessWidget {
MobileEmojiPickerScreen.iconSelectedType: emoji.type.name,
MobileEmojiPickerScreen.uploadDocumentId: documentId,
MobileEmojiPickerScreen.selectTabs:
tabs.map((e) => e.name).toList(),
tabs.map((e) => e.name).toList().join('-'),
},
).toString(),
);
@@ -1,6 +1,8 @@
import 'package:appflowy/generated/locale_keys.g.dart' show LocaleKeys;
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/application/document_bloc.dart';
import 'package:appflowy/shared/icon_emoji_picker/flowy_icon_emoji_picker.dart';
import 'package:appflowy/shared/icon_emoji_picker/tab.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:easy_localization/easy_localization.dart'
@@ -80,7 +82,7 @@ class CalloutBlockComponentBuilder extends BlockComponentBuilder {
});
final Color defaultColor;
final EdgeInsets inlinePadding;
final EdgeInsets Function(Node node) inlinePadding;
@override
BlockComponentWidget build(BlockComponentContext blockComponentContext) {
@@ -96,12 +98,15 @@ class CalloutBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@override
BlockComponentValidate get validate =>
(node) => node.delta != null && node.children.isEmpty;
BlockComponentValidate get validate => (node) => node.delta != null;
}
// the main widget for rendering the callout block
@@ -111,13 +116,14 @@ class CalloutBlockComponentWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
required this.defaultColor,
required this.inlinePadding,
});
final Color defaultColor;
final EdgeInsets inlinePadding;
final EdgeInsets Function(Node node) inlinePadding;
@override
State<CalloutBlockComponentWidget> createState() =>
@@ -132,7 +138,8 @@ class _CalloutBlockComponentWidgetState
BlockComponentConfigurable,
BlockComponentTextDirectionMixin,
BlockComponentAlignMixin,
BlockComponentBackgroundColorMixin {
BlockComponentBackgroundColorMixin,
NestedBlockComponentStatefulWidgetMixin {
// the key used to forward focus to the richtext child
@override
final forwardKey = GlobalKey(debugLabel: 'flowy_rich_text');
@@ -170,40 +177,63 @@ class _CalloutBlockComponentWidgetState
try {
result = EmojiIconData(FlowyIconType.values.byName(type), icon);
} catch (e) {
Log.error(
'get emoji error with icon:[$icon], type:[$type] within alloutBlockComponentWidget',
Log.info(
'get emoji error with icon:[$icon], type:[$type] within calloutBlockComponentWidget',
e,
);
}
return result;
}
// get access to the editor state via provider
@override
late final editorState = Provider.of<EditorState>(context, listen: false);
Widget buildComponentWithChildren(BuildContext context) {
return Stack(
children: [
Positioned.fill(
left: cachedLeft,
child: Container(
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(6.0)),
color: backgroundColor,
),
),
),
NestedListWidget(
indentPadding: indentPadding.copyWith(bottom: 8),
child: buildComponent(context, withBackgroundColor: false),
children: editorState.renderer.buildList(
context,
widget.node.children,
),
),
],
);
}
// build the callout block widget
@override
Widget build(BuildContext context) {
Widget buildComponent(
BuildContext context, {
bool withBackgroundColor = true,
}) {
final textDirection = calculateTextDirection(
layoutDirection: Directionality.maybeOf(context),
);
final (emojiSize, emojiButtonSize) = calculateEmojiSize();
final documentId = context.read<DocumentBloc?>()?.documentId;
Widget child = Container(
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(8.0)),
borderRadius: const BorderRadius.all(Radius.circular(6.0)),
color: backgroundColor,
),
padding: widget.inlinePadding,
padding: widget.inlinePadding(widget.node),
width: double.infinity,
alignment: alignment,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
textDirection: textDirection,
children: [
if (UniversalPlatform.isDesktopOrWeb) const HSpace(4.0),
if (UniversalPlatform.isDesktopOrWeb) const HSpace(6.0),
// the emoji picker button for the note
EmojiPickerButton(
// force to refresh the popover state
@@ -217,12 +247,18 @@ class _CalloutBlockComponentWidgetState
emojiSize: emojiSize,
showBorder: false,
buttonSize: emojiButtonSize,
documentId: documentId,
tabs: const [
PickerTabType.emoji,
PickerTabType.icon,
PickerTabType.custom,
],
onSubmitted: (r, controller) {
setEmojiIconData(r.data);
if (!r.keepOpen) controller?.close();
},
),
if (UniversalPlatform.isDesktopOrWeb) const HSpace(4.0),
if (UniversalPlatform.isDesktopOrWeb) const HSpace(6.0),
Flexible(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
@@ -236,7 +272,7 @@ class _CalloutBlockComponentWidgetState
child = Padding(
key: blockComponentKey,
padding: padding,
padding: EdgeInsets.zero,
child: child,
);
@@ -245,6 +281,7 @@ class _CalloutBlockComponentWidgetState
delegate: this,
listenable: editorState.selectionNotifier,
blockColor: editorState.editorStyle.selectionColor,
selectionAboveBlock: true,
supportTypes: const [
BlockSelectionType.block,
],
@@ -255,6 +292,7 @@ class _CalloutBlockComponentWidgetState
child = BlockComponentActionWrapper(
node: widget.node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -33,8 +33,20 @@ CharacterShortcutEventHandler _insertNewLineHandler = (editorState) async {
if (HardwareKeyboard.instance.isShiftPressed) {
await editorState.insertNewLine();
} else {
await editorState.insertTextAtCurrentSelection('\n');
} else if (node.children.isEmpty) {
// insert a new paragraph within the callout block
final path = node.path.child(0);
final transaction = editorState.transaction;
transaction.insertNode(
path,
paragraphNode(),
);
transaction.afterSelection = Selection.collapsed(
Position(
path: path,
),
);
await editorState.apply(transaction);
}
return true;
@@ -7,27 +7,62 @@ import 'package:provider/provider.dart';
Node simpleColumnNode({
List<Node>? children,
double? width,
double? ratio,
}) {
return Node(
type: SimpleColumnBlockKeys.type,
children: children ?? [paragraphNode()],
attributes: {
SimpleColumnBlockKeys.width: width,
SimpleColumnBlockKeys.ratio: ratio,
},
);
}
extension SimpleColumnBlockAttributes on Node {
// get the next column node of the current column node
// if the current column node is the last column node, return null
Node? get nextColumn {
final index = path.last;
final parent = this.parent;
if (parent == null || index == parent.children.length - 1) {
return null;
}
return parent.children[index + 1];
}
// get the previous column node of the current column node
// if the current column node is the first column node, return null
Node? get previousColumn {
final index = path.last;
final parent = this.parent;
if (parent == null || index == 0) {
return null;
}
return parent.children[index - 1];
}
}
class SimpleColumnBlockKeys {
const SimpleColumnBlockKeys._();
static const String type = 'simple_column';
/// @Deprecated Use [SimpleColumnBlockKeys.ratio] instead.
///
/// This field is no longer used since v0.6.9
@Deprecated('Use [SimpleColumnBlockKeys.ratio] instead.')
static const String width = 'width';
/// The ratio of the column width.
///
/// The value is a double number between 0 and 1.
static const String ratio = 'ratio';
}
class SimpleColumnBlockComponentBuilder extends BlockComponentBuilder {
SimpleColumnBlockComponentBuilder({super.configuration});
SimpleColumnBlockComponentBuilder({
super.configuration,
});
@override
BlockComponentWidget build(BlockComponentContext blockComponentContext) {
@@ -51,6 +86,7 @@ class SimpleColumnBlockComponent extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -73,16 +109,6 @@ class SimpleColumnBlockComponentState extends State<SimpleColumnBlockComponent>
late final EditorState editorState = context.read<EditorState>();
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
Widget child = Column(
@@ -120,7 +146,7 @@ class SimpleColumnBlockComponentState extends State<SimpleColumnBlockComponent>
if (SimpleColumnsBlockConstants.enableDebugBorder) {
child = Container(
color: Colors.green.withValues(
alpha: 0.2,
alpha: 0.3,
),
child: child,
);
@@ -1,6 +1,5 @@
import 'package:appflowy/plugins/document/presentation/editor_configuration.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/actions/drag_to_reorder/draggable_option_button.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/columns/simple_columns_block_constant.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
@@ -26,6 +25,13 @@ class _SimpleColumnBlockWidthResizerState
ValueNotifier<bool> isHovering = ValueNotifier(false);
@override
void dispose() {
isHovering.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MouseRegion(
@@ -78,25 +84,47 @@ class _SimpleColumnBlockWidthResizerState
// update the column width in memory
final columnNode = widget.columnNode;
final columnsNode = columnNode.columnsParent;
if (columnsNode == null) {
return;
}
final editorWidth = columnsNode.rect.width;
final rect = columnNode.rect;
final width =
columnNode.attributes[SimpleColumnBlockKeys.width] ?? rect.width;
final width = rect.width;
final originalRatio = columnNode.attributes[SimpleColumnBlockKeys.ratio];
final newWidth = width + details.delta.dx;
final transaction = widget.editorState.transaction;
final newRatio = newWidth / editorWidth;
transaction.updateNode(columnNode, {
...columnNode.attributes,
SimpleColumnBlockKeys.width: newWidth.clamp(
SimpleColumnsBlockConstants.minimumColumnWidth,
double.infinity,
),
SimpleColumnBlockKeys.ratio: newRatio,
});
final columnsNode = columnNode.parent;
if (columnsNode != null) {
transaction.updateNode(columnsNode, {
...columnsNode.attributes,
ColumnsBlockKeys.columnCount: columnsNode.children.length,
if (newRatio < 0.1 && newRatio < originalRatio) {
return;
}
final nextColumn = columnNode.nextColumn;
if (nextColumn != null) {
final nextColumnRect = nextColumn.rect;
final nextColumnWidth = nextColumnRect.width;
final newNextColumnWidth = nextColumnWidth - details.delta.dx;
final newNextColumnRatio = newNextColumnWidth / editorWidth;
if (newNextColumnRatio < 0.1) {
return;
}
transaction.updateNode(nextColumn, {
...nextColumn.attributes,
SimpleColumnBlockKeys.ratio: newNextColumnRatio,
});
}
transaction.updateNode(columnsNode, {
...columnsNode.attributes,
ColumnsBlockKeys.columnCount: columnsNode.children.length,
});
widget.editorState.apply(
transaction,
options: ApplyOptions(inMemoryUpdate: true),
@@ -113,9 +141,15 @@ class _SimpleColumnBlockWidthResizerState
// apply the transaction again to make sure the width is updated
final transaction = widget.editorState.transaction;
transaction.updateNode(widget.columnNode, {
...widget.columnNode.attributes,
});
final columnsNode = widget.columnNode.columnsParent;
if (columnsNode == null) {
return;
}
for (final columnNode in columnsNode.children) {
transaction.updateNode(columnNode, {
...columnNode.attributes,
});
}
widget.editorState.apply(transaction);
isDragging = false;
@@ -3,7 +3,7 @@ import 'package:appflowy_editor/appflowy_editor.dart';
extension SimpleColumnNodeExtension on Node {
/// Returns the parent [Node] of the current node if it is a [SimpleColumnsBlock].
Node? get parentColumnsBlock {
Node? get columnsParent {
Node? currentNode = parent;
while (currentNode != null) {
if (currentNode.type == SimpleColumnsBlockKeys.type) {
@@ -15,7 +15,7 @@ extension SimpleColumnNodeExtension on Node {
}
/// Returns the parent [Node] of the current node if it is a [SimpleColumnBlock].
Node? get parentColumn {
Node? get columnParent {
Node? currentNode = parent;
while (currentNode != null) {
if (currentNode.type == SimpleColumnBlockKeys.type) {
@@ -27,8 +27,8 @@ extension SimpleColumnNodeExtension on Node {
}
/// Returns whether the current node is in a [SimpleColumnsBlock].
bool get isInColumnsBlock => parentColumnsBlock != null;
bool get isInColumnsBlock => columnsParent != null;
/// Returns whether the current node is in a [SimpleColumnBlock].
bool get isInColumnBlock => parentColumn != null;
bool get isInColumnBlock => columnParent != null;
}
@@ -1,3 +1,5 @@
import 'dart:math';
import 'package:appflowy/plugins/document/presentation/editor_plugins/columns/simple_columns_block_constant.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy_backend/log.dart';
@@ -5,20 +7,19 @@ import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:universal_platform/universal_platform.dart';
// if the children is not provided, it will create two columns by default.
// if the columnCount is provided, it will create the specified number of columns.
Node simpleColumnsNode({
List<Node>? children,
int? columnCount,
double? width,
double? ratio,
}) {
columnCount ??= 2;
children ??= List.generate(
columnCount,
(index) => simpleColumnNode(
width: width,
ratio: ratio,
children: [paragraphNode()],
),
);
@@ -67,6 +68,7 @@ class ColumnsBlockComponent extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -90,6 +92,13 @@ class ColumnsBlockComponentState extends State<ColumnsBlockComponent>
final ScrollController scrollController = ScrollController();
@override
void initState() {
super.initState();
_updateColumnsBlock();
}
@override
void dispose() {
scrollController.dispose();
@@ -99,23 +108,12 @@ class ColumnsBlockComponentState extends State<ColumnsBlockComponent>
@override
Widget build(BuildContext context) {
Widget child = SingleChildScrollView(
scrollDirection: Axis.horizontal,
controller: scrollController,
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: _buildChildren(),
),
Widget child = Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: _buildChildren(),
);
if (UniversalPlatform.isDesktop) {
// only show the scrollbar on desktop
child = Scrollbar(
controller: scrollController,
child: child,
);
}
child = Align(
alignment: Alignment.topLeft,
child: IntrinsicHeight(
@@ -147,19 +145,18 @@ class ColumnsBlockComponentState extends State<ColumnsBlockComponent>
}
List<Widget> _buildChildren() {
final length = node.children.length;
final children = <Widget>[];
for (var i = 0; i < node.children.length; i++) {
for (var i = 0; i < length; i++) {
final childNode = node.children[i];
final width =
childNode.attributes[SimpleColumnBlockKeys.width]?.toDouble() ??
SimpleColumnsBlockConstants.minimumColumnWidth;
final double ratio =
childNode.attributes[SimpleColumnBlockKeys.ratio]?.toDouble() ??
1.0 / length;
Widget child = editorState.renderer.build(context, childNode);
child = SizedBox(
width: width.clamp(
SimpleColumnsBlockConstants.minimumColumnWidth,
double.infinity,
),
child = Expanded(
flex: (max(ratio, 0.1) * 10000).toInt(),
child: child,
);
@@ -175,6 +172,26 @@ class ColumnsBlockComponentState extends State<ColumnsBlockComponent>
return children;
}
// Update the existing columns block data
// if the column ratio is not existing, it will be set to 1.0 / columnCount
void _updateColumnsBlock() {
final transaction = editorState.transaction;
final length = node.children.length;
for (int i = 0; i < length; i++) {
final childNode = node.children[i];
final ratio = childNode.attributes[SimpleColumnBlockKeys.ratio];
if (ratio == null) {
transaction.updateNode(childNode, {
...childNode.attributes,
SimpleColumnBlockKeys.ratio: 1.0 / length,
});
}
}
if (transaction.operations.isNotEmpty) {
editorState.apply(transaction);
}
}
@override
Position start() => Position(path: widget.node.path);
@@ -42,6 +42,10 @@ class DatabaseViewBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -58,6 +62,7 @@ class DatabaseBlockComponentWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -30,6 +30,10 @@ class ErrorBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -43,6 +47,7 @@ class ErrorBlockComponentWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -81,6 +86,7 @@ class _ErrorBlockComponentWidgetState extends State<ErrorBlockComponentWidget>
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -141,6 +141,7 @@ class FileBlockComponent extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -303,6 +304,7 @@ class FileBlockComponentState extends State<FileBlockComponent>
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -123,6 +123,7 @@ class CustomImageBlockComponent extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
this.showMenu = false,
this.menuBuilder,
@@ -226,6 +227,7 @@ class CustomImageBlockComponentState extends State<CustomImageBlockComponent>
delegate: this,
listenable: editorState.selectionNotifier,
blockColor: editorState.editorStyle.selectionColor,
selectionAboveBlock: true,
supportTypes: const [BlockSelectionType.block],
child: child,
);
@@ -235,6 +237,7 @@ class CustomImageBlockComponentState extends State<CustomImageBlockComponent>
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -311,7 +314,7 @@ class CustomImageBlockComponentState extends State<CustomImageBlockComponent>
}) {
final imageBox = imageKey.currentContext?.findRenderObject();
if (imageBox is RenderBox) {
return Offset.zero & imageBox.size;
return padding.topLeft & imageBox.size;
}
return Rect.zero;
}
@@ -82,6 +82,7 @@ class MultiImageBlockComponent extends BlockComponentStatefulWidget {
this.menuBuilder,
super.configuration = const BlockComponentConfiguration(),
super.actionBuilder,
super.actionTrailingBuilder,
});
final bool showMenu;
@@ -190,6 +191,7 @@ class MultiImageBlockComponentState extends State<MultiImageBlockComponent>
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -79,6 +79,10 @@ class MathEquationBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -94,6 +98,7 @@ class MathEquationBlockComponentWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -157,6 +162,7 @@ class MathEquationBlockComponentWidgetState
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -7,7 +7,8 @@ import 'package:appflowy/workspace/application/view/view_ext.dart';
import 'package:appflowy/workspace/application/view/view_service.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide QuoteBlockComponentBuilder, quoteNode, QuoteBlockKeys;
import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
import 'package:collection/collection.dart';
import 'package:string_validator/string_validator.dart';
@@ -6,7 +6,8 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/mobile_too
import 'package:appflowy/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/aa_menu/_popup_menu.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/aa_menu/_toolbar_theme.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide QuoteBlockKeys, quoteNode;
import 'package:collection/collection.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
@@ -12,7 +12,8 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.da
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/startup/tasks/app_widget.dart';
import 'package:appflowy/workspace/presentation/home/menu/menu_shared_state.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide QuoteBlockComponentBuilder, quoteNode, QuoteBlockKeys;
import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
@@ -29,7 +29,7 @@ class NumberedListIcon extends StatelessWidget {
);
return Padding(
padding: const EdgeInsets.only(right: 8.0),
padding: const EdgeInsets.only(left: 6.0, right: 10.0),
child: Text(
node.levelString,
style: adjustedTextStyle,
@@ -52,6 +52,10 @@ class OutlineBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -65,6 +69,7 @@ class OutlineBlockWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -76,7 +81,9 @@ class _OutlineBlockWidgetState extends State<OutlineBlockWidget>
with
BlockComponentConfigurable,
BlockComponentTextDirectionMixin,
BlockComponentBackgroundColorMixin {
BlockComponentBackgroundColorMixin,
DefaultSelectableMixin,
SelectableMixin {
// Change the value if the heading block type supports heading levels greater than '3'
static const maxVisibleDepth = 6;
@@ -90,6 +97,17 @@ class _OutlineBlockWidgetState extends State<OutlineBlockWidget>
late EditorState editorState = context.read<EditorState>();
late Stream<EditorTransactionValue> stream = editorState.transactionStream;
@override
GlobalKey<State<StatefulWidget>> blockComponentKey = GlobalKey(
debugLabel: OutlineBlockKeys.type,
);
@override
GlobalKey<State<StatefulWidget>> get containerKey => widget.node.key;
@override
GlobalKey<State<StatefulWidget>> get forwardKey => widget.node.key;
@override
Widget build(BuildContext context) {
return StreamBuilder(
@@ -97,11 +115,25 @@ class _OutlineBlockWidgetState extends State<OutlineBlockWidget>
builder: (context, snapshot) {
Widget child = _buildOutlineBlock();
child = BlockSelectionContainer(
node: node,
delegate: this,
listenable: editorState.selectionNotifier,
remoteSelection: editorState.remoteSelections,
blockColor: editorState.editorStyle.selectionColor,
selectionAboveBlock: true,
supportTypes: const [
BlockSelectionType.block,
],
child: child,
);
if (UniversalPlatform.isDesktopOrWeb) {
if (widget.showActions && widget.actionBuilder != null) {
child = BlockComponentActionWrapper(
node: widget.node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -170,6 +202,7 @@ class _OutlineBlockWidgetState extends State<OutlineBlockWidget>
}
return Container(
key: blockComponentKey,
constraints: const BoxConstraints(
minHeight: 40.0,
),
@@ -25,6 +25,7 @@ class CustomPageBlockComponent extends BlockComponentStatelessWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
this.header,
this.footer,
@@ -1,4 +1,5 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy/shared/icon_emoji_picker/flowy_icon_emoji_picker.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
class CalloutNodeParser extends NodeParser {
@@ -10,7 +11,6 @@ class CalloutNodeParser extends NodeParser {
@override
String transform(Node node, DocumentMarkdownEncoder? encoder) {
assert(node.children.isEmpty);
final icon = node.attributes[CalloutBlockKeys.icon];
final delta = node.delta ?? Delta()
..insert('');
final String markdown = DeltaMarkdownEncoder()
@@ -18,9 +18,15 @@ class CalloutNodeParser extends NodeParser {
.split('\n')
.map((e) => '> $e')
.join('\n');
final type = node.attributes[CalloutBlockKeys.iconType];
final icon = type == FlowyIconType.emoji.name || type == null || type == ""
? node.attributes[CalloutBlockKeys.icon]
: null;
final content = icon == null ? markdown : "> $icon\n$markdown";
return '''
> $icon
$markdown
$content
''';
}
@@ -63,6 +63,7 @@ export 'outline/outline_block_component.dart';
export 'parsers/document_markdown_parsers.dart';
export 'parsers/markdown_parsers.dart';
export 'parsers/markdown_simple_table_parser.dart';
export 'quote/quote_block_component.dart';
export 'quote/quote_block_shortcuts.dart';
export 'shortcuts/character_shortcuts.dart';
export 'shortcuts/command_shortcuts.dart';
@@ -0,0 +1,304 @@
import 'dart:async';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
typedef QuoteBlockIconBuilder = Widget Function(
BuildContext context,
Node node,
);
class QuoteBlockKeys {
const QuoteBlockKeys._();
static const String type = 'quote';
static const String delta = blockComponentDelta;
static const String backgroundColor = blockComponentBackgroundColor;
static const String textDirection = blockComponentTextDirection;
}
Node quoteNode({
Delta? delta,
String? textDirection,
Attributes? attributes,
Iterable<Node>? children,
}) {
attributes ??= {'delta': (delta ?? Delta()).toJson()};
return Node(
type: QuoteBlockKeys.type,
attributes: {
...attributes,
if (textDirection != null) QuoteBlockKeys.textDirection: textDirection,
},
children: children ?? [],
);
}
class QuoteBlockComponentBuilder extends BlockComponentBuilder {
QuoteBlockComponentBuilder({
super.configuration,
this.iconBuilder,
});
final QuoteBlockIconBuilder? iconBuilder;
@override
BlockComponentWidget build(BlockComponentContext blockComponentContext) {
final node = blockComponentContext.node;
return QuoteBlockComponentWidget(
key: node.key,
node: node,
configuration: configuration,
iconBuilder: iconBuilder,
showActions: showActions(node),
actionBuilder: (context, state) => actionBuilder(
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@override
BlockComponentValidate get validate => (node) => node.delta != null;
}
class QuoteBlockComponentWidget extends BlockComponentStatefulWidget {
const QuoteBlockComponentWidget({
super.key,
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
this.iconBuilder,
});
final QuoteBlockIconBuilder? iconBuilder;
@override
State<QuoteBlockComponentWidget> createState() =>
_QuoteBlockComponentWidgetState();
}
class _QuoteBlockComponentWidgetState extends State<QuoteBlockComponentWidget>
with
SelectableMixin,
DefaultSelectableMixin,
BlockComponentConfigurable,
BlockComponentBackgroundColorMixin,
BlockComponentTextDirectionMixin,
BlockComponentAlignMixin,
NestedBlockComponentStatefulWidgetMixin {
@override
final forwardKey = GlobalKey(debugLabel: 'flowy_rich_text');
@override
GlobalKey<State<StatefulWidget>> get containerKey => widget.node.key;
@override
GlobalKey<State<StatefulWidget>> blockComponentKey = GlobalKey(
debugLabel: QuoteBlockKeys.type,
);
@override
BlockComponentConfiguration get configuration => widget.configuration;
@override
Node get node => widget.node;
ValueNotifier<double> quoteBlockHeightNotifier = ValueNotifier(0);
StreamSubscription<EditorTransactionValue>? _transactionSubscription;
final GlobalKey layoutBuilderKey = GlobalKey();
@override
void initState() {
super.initState();
_observerQuoteBlockChanges();
}
@override
void dispose() {
_transactionSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
_updateQuoteBlockHeight();
return KeyedSubtree(
key: layoutBuilderKey,
child: node.children.isEmpty
? buildComponent(context)
: buildComponentWithChildren(context),
);
},
);
}
@override
Widget buildComponent(
BuildContext context, {
bool withBackgroundColor = true,
}) {
final textDirection = calculateTextDirection(
layoutDirection: Directionality.maybeOf(context),
);
Widget child = AppFlowyRichText(
key: forwardKey,
delegate: this,
node: widget.node,
editorState: editorState,
textAlign: alignment?.toTextAlign ?? textAlign,
placeholderText: placeholderText,
textSpanDecorator: (textSpan) => textSpan.updateTextStyle(
textStyleWithTextSpan(textSpan: textSpan),
),
placeholderTextSpanDecorator: (textSpan) => textSpan.updateTextStyle(
placeholderTextStyleWithTextSpan(textSpan: textSpan),
),
textDirection: textDirection,
cursorColor: editorState.editorStyle.cursorColor,
selectionColor: editorState.editorStyle.selectionColor,
cursorWidth: editorState.editorStyle.cursorWidth,
);
child = Container(
width: double.infinity,
alignment: alignment,
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
textDirection: textDirection,
children: [
widget.iconBuilder != null
? widget.iconBuilder!(context, node)
: ValueListenableBuilder<double>(
valueListenable: quoteBlockHeightNotifier,
builder: (context, height, child) {
return QuoteIcon(height: height);
},
),
Flexible(
child: child,
),
],
),
),
);
child = Container(
color: backgroundColor,
child: Padding(
key: blockComponentKey,
padding: padding,
child: child,
),
);
child = BlockSelectionContainer(
node: node,
delegate: this,
listenable: editorState.selectionNotifier,
remoteSelection: editorState.remoteSelections,
blockColor: editorState.editorStyle.selectionColor,
supportTypes: const [
BlockSelectionType.block,
],
child: child,
);
if (widget.showActions && widget.actionBuilder != null) {
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
return child;
}
void _updateQuoteBlockHeight() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
final renderObject = layoutBuilderKey.currentContext?.findRenderObject();
if (renderObject != null && renderObject is RenderBox) {
quoteBlockHeightNotifier.value =
renderObject.size.height - padding.top * 2;
} else {
quoteBlockHeightNotifier.value = 0;
}
});
}
void _observerQuoteBlockChanges() {
_transactionSubscription = editorState.transactionStream.listen((event) {
final time = event.$1;
if (time != TransactionTime.before) {
return;
}
final transaction = event.$2;
final operations = transaction.operations;
for (final operation in operations) {
if (node.path.isAncestorOf(operation.path)) {
_updateQuoteBlockHeight();
}
}
});
}
}
class QuoteIcon extends StatelessWidget {
const QuoteIcon({
super.key,
this.height = 0,
});
final double height;
@override
Widget build(BuildContext context) {
final textScaleFactor =
context.read<EditorState>().editorStyle.textScaleFactor;
return Container(
alignment: Alignment.center,
constraints:
const BoxConstraints(minWidth: 22, minHeight: 22, maxHeight: 22) *
textScaleFactor,
padding: const EdgeInsets.only(right: 6.0),
child: SizedBox(
width: 3 * textScaleFactor,
// use overflow box to ensure the container can overflow the height so that the children of the quote block can have the quote
child: OverflowBox(
alignment: Alignment.topCenter,
maxHeight: height,
child: Container(
width: 3 * textScaleFactor,
height: height,
color: Theme.of(context).colorScheme.primary,
),
),
),
);
}
}
@@ -31,8 +31,20 @@ CharacterShortcutEventHandler _insertNewLineHandler = (editorState) async {
if (HardwareKeyboard.instance.isShiftPressed) {
await editorState.insertNewLine();
} else {
await editorState.insertTextAtCurrentSelection('\n');
} else if (node.children.isEmpty) {
// insert a new paragraph within the callout block
final path = node.path.child(0);
final transaction = editorState.transaction;
transaction.insertNode(
path,
paragraphNode(),
);
transaction.afterSelection = Selection.collapsed(
Position(
path: path,
),
);
await editorState.apply(transaction);
}
return true;
@@ -161,6 +161,10 @@ class SimpleTableBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -174,6 +178,7 @@ class SimpleTableBlockWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
required this.alwaysDistributeColumnWidths,
});
@@ -262,6 +267,7 @@ class _SimpleTableBlockWidgetState extends State<SimpleTableBlockWidget>
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -47,6 +47,10 @@ class SimpleTableCellBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -60,6 +64,7 @@ class SimpleTableCellBlockWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
required this.alwaysDistributeColumnWidths,
});
@@ -39,6 +39,10 @@ class SimpleTableRowBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -52,6 +56,7 @@ class SimpleTableRowBlockWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
required this.alwaysDistributeColumnWidths,
});
@@ -90,13 +90,8 @@ SelectionMenuItem fourColumnsSlashMenuItem = SelectionMenuItem.node(
);
Node _buildColumnsNode(EditorState editorState, int columnCount) {
final selection = editorState.selection;
double? width;
if (selection != null) {
final parentNode = editorState.getNodeAtPath(selection.start.path);
if (parentNode != null) {
width = parentNode.rect.width / columnCount - 16;
}
}
return simpleColumnsNode(columnCount: columnCount, width: width);
return simpleColumnsNode(
columnCount: columnCount,
ratio: 1.0 / columnCount,
);
}
@@ -73,6 +73,7 @@ class SubPageBlockComponent extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
});
@@ -295,6 +296,7 @@ class SubPageBlockComponentState extends State<SubPageBlockComponent>
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -111,6 +111,10 @@ class ToggleListBlockComponentBuilder extends BlockComponentBuilder {
blockComponentContext,
state,
),
actionTrailingBuilder: (context, state) => actionTrailingBuilder(
blockComponentContext,
state,
),
);
}
@@ -124,6 +128,7 @@ class ToggleListBlockComponentWidget extends BlockComponentStatefulWidget {
required super.node,
super.showActions,
super.actionBuilder,
super.actionTrailingBuilder,
super.configuration = const BlockComponentConfiguration(),
this.padding = const EdgeInsets.all(0),
this.textStyleBuilder,
@@ -247,6 +252,7 @@ class _ToggleListBlockComponentWidgetState
child = BlockComponentActionWrapper(
node: node,
actionBuilder: widget.actionBuilder!,
actionTrailingBuilder: widget.actionTrailingBuilder,
child: child,
);
}
@@ -1,167 +0,0 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:isolate';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/ai_chat/application/chat_entity.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';
import 'package:bloc/bloc.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:fixnum/fixnum.dart';
part 'download_model_bloc.freezed.dart';
class DownloadModelBloc extends Bloc<DownloadModelEvent, DownloadModelState> {
DownloadModelBloc(LLMModelPB model)
: super(DownloadModelState.initial(model)) {
on<DownloadModelEvent>(_handleEvent);
}
Future<void> _handleEvent(
DownloadModelEvent event,
Emitter<DownloadModelState> emit,
) async {
await event.when(
started: () async {
final downloadStream = DownloadingStream();
downloadStream.listen(
onModelPercentage: (name, percent) {
if (!isClosed) {
add(
DownloadModelEvent.updatePercent(name, percent),
);
}
},
onPluginPercentage: (percent) {
if (!isClosed) {
add(DownloadModelEvent.updatePercent("AppFlowy Plugin", percent));
}
},
onFinish: () {
add(const DownloadModelEvent.downloadFinish());
},
onError: (err) {
Log.error(err);
},
);
final payload =
DownloadLLMPB(progressStream: Int64(downloadStream.nativePort));
final result = await AIEventDownloadLLMResource(payload).send();
result.fold((_) {
emit(
state.copyWith(
downloadStream: downloadStream,
loadingState: const ChatLoadingState.finish(),
downloadError: null,
),
);
}, (err) {
emit(
state.copyWith(
loadingState: ChatLoadingState.finish(error: err),
),
);
});
},
updatePercent: (String object, double percent) {
emit(state.copyWith(object: object, percent: percent));
},
downloadFinish: () {
emit(state.copyWith(isFinish: true));
},
);
}
@override
Future<void> close() async {
await state.downloadStream?.dispose();
return super.close();
}
}
@freezed
class DownloadModelEvent with _$DownloadModelEvent {
const factory DownloadModelEvent.started() = _Started;
const factory DownloadModelEvent.updatePercent(
String object,
double percent,
) = _UpdatePercent;
const factory DownloadModelEvent.downloadFinish() = _DownloadFinish;
}
@freezed
class DownloadModelState with _$DownloadModelState {
const factory DownloadModelState({
required LLMModelPB model,
DownloadingStream? downloadStream,
String? downloadError,
@Default("") String object,
@Default(0) double percent,
@Default(false) bool isFinish,
String? bigFileDownloadPrompt,
@Default(ChatLoadingState.loading()) ChatLoadingState loadingState,
}) = _DownloadModelState;
factory DownloadModelState.initial(LLMModelPB model) {
// bigger than 1 GB then show download big file prompt
String? bigFileDownloadPrompt;
if (model.fileSize > 1 * 1024 * 1024 * 1024) {
bigFileDownloadPrompt =
LocaleKeys.settings_aiPage_keys_downloadBigFilePrompt.tr();
}
return DownloadModelState(
model: model,
bigFileDownloadPrompt: bigFileDownloadPrompt,
);
}
}
class DownloadingStream {
DownloadingStream() {
_port.handler = _controller.add;
}
final RawReceivePort _port = RawReceivePort();
StreamSubscription<String>? _sub;
final StreamController<String> _controller = StreamController.broadcast();
int get nativePort => _port.sendPort.nativePort;
Future<void> dispose() async {
await _sub?.cancel();
await _controller.close();
_port.close();
}
void listen({
void Function(String modelName, double percent)? onModelPercentage,
void Function(double percent)? onPluginPercentage,
void Function(String data)? onError,
void Function()? onFinish,
}) {
_sub = _controller.stream.listen((text) {
if (text.contains(':progress:')) {
final progressIndex = text.indexOf(':progress:');
final modelName = text.substring(0, progressIndex);
final progressValue = text
.substring(progressIndex + 10); // 10 is the length of ":progress:"
final percent = double.tryParse(progressValue);
if (percent != null) {
onModelPercentage?.call(modelName, percent);
}
} else if (text.startsWith('plugin:progress:')) {
final percent = double.tryParse(text.substring(16));
if (percent != null) {
onPluginPercentage?.call(percent);
}
} else if (text.startsWith('finish')) {
onFinish?.call();
} else if (text.startsWith('error:')) {
// substring 6 to remove "error:"
onError?.call(text.substring(6));
}
});
}
}
@@ -18,7 +18,7 @@ class DownloadOfflineAIBloc
) async {
await event.when(
started: () async {
final result = await AIEventGetOfflineAIAppLink().send();
final result = await AIEventGetLocalAIDownloadLink().send();
await result.fold(
(app) async {
await launchUrl(Uri.parse(app.link));
@@ -52,7 +52,8 @@ class LocalAIToggleBloc extends Bloc<LocalAIToggleEvent, LocalAIToggleState> {
(localAI) {
emit(
state.copyWith(
pageIndicator: LocalAIToggleStateIndicator.ready(localAI.enabled),
pageIndicator:
LocalAIToggleStateIndicator.isEnabled(localAI.enabled),
),
);
},
@@ -88,6 +89,6 @@ class LocalAIToggleState with _$LocalAIToggleState {
class LocalAIToggleStateIndicator with _$LocalAIToggleStateIndicator {
// when start downloading the model
const factory LocalAIToggleStateIndicator.error(FlowyError error) = _OnError;
const factory LocalAIToggleStateIndicator.ready(bool isEnabled) = _Ready;
const factory LocalAIToggleStateIndicator.isEnabled(bool isEnabled) = _Ready;
const factory LocalAIToggleStateIndicator.loading() = _Loading;
}
@@ -1,185 +1,61 @@
import 'dart:async';
import 'package:appflowy/plugins/ai_chat/application/chat_entity.dart';
import 'package:appflowy/workspace/application/settings/ai/local_llm_listener.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'local_ai_chat_bloc.freezed.dart';
class LocalAIChatSettingBloc
extends Bloc<LocalAIChatSettingEvent, LocalAIChatSettingState> {
LocalAIChatSettingBloc()
class LocalAISettingPanelBloc
extends Bloc<LocalAISettingPanelEvent, LocalAISettingPanelState> {
LocalAISettingPanelBloc()
: listener = LocalLLMListener(),
super(const LocalAIChatSettingState()) {
super(const LocalAISettingPanelState()) {
listener.start(
stateCallback: (newState) {
if (!isClosed) {
add(LocalAIChatSettingEvent.updatePluginState(newState));
add(LocalAISettingPanelEvent.updateAIState(newState));
}
},
);
on<LocalAIChatSettingEvent>(_handleEvent);
on<LocalAISettingPanelEvent>(_handleEvent);
}
final LocalLLMListener listener;
/// Handles incoming events and dispatches them to the appropriate handler.
Future<void> _handleEvent(
LocalAIChatSettingEvent event,
Emitter<LocalAIChatSettingState> emit,
LocalAISettingPanelEvent event,
Emitter<LocalAISettingPanelState> emit,
) async {
await event.when(
refreshAISetting: _handleStarted,
didLoadModelInfo: (FlowyResult<LLMModelInfoPB, FlowyError> result) {
result.fold(
(modelInfo) {
_fetchCurremtLLMState();
emit(
state.copyWith(
modelInfo: modelInfo,
models: modelInfo.models,
selectedLLMModel: modelInfo.selectedModel,
aiModelProgress: const AIModelProgress.finish(),
),
);
},
(err) {
emit(
state.copyWith(
aiModelProgress: AIModelProgress.finish(error: err),
),
);
},
);
},
selectLLMConfig: (LLMModelPB llmModel) async {
final result = await AIEventUpdateLocalLLM(llmModel).send();
result.fold(
(llmResource) {
// If all resources are downloaded, show reload plugin
if (llmResource.pendingResources.isNotEmpty) {
emit(
state.copyWith(
selectedLLMModel: llmModel,
progressIndicator: LocalAIProgress.showDownload(
llmResource,
llmModel,
),
selectLLMState: const ChatLoadingState.finish(),
),
);
} else {
emit(
state.copyWith(
selectedLLMModel: llmModel,
selectLLMState: const ChatLoadingState.finish(),
progressIndicator: const LocalAIProgress.checkPluginState(),
),
);
event.when(
started: () {
AIEventGetLocalAIState().send().fold(
(localAIState) {
if (!isClosed) {
add(LocalAISettingPanelEvent.updateAIState(localAIState));
}
},
(err) {
emit(
state.copyWith(
selectLLMState: ChatLoadingState.finish(error: err),
),
);
},
Log.error,
);
},
refreshLLMState: (LocalModelResourcePB llmResource) {
if (state.selectedLLMModel == null) {
Log.error(
'Unexpected null selected config. It should be set already',
);
return;
}
// reload plugin if all resources are downloaded
if (llmResource.pendingResources.isEmpty) {
updateAIState: (LocalAIPB pluginState) {
if (pluginState.isPluginExecutableReady) {
emit(
state.copyWith(
runningState: pluginState.state,
progressIndicator: const LocalAIProgress.checkPluginState(),
),
);
} else {
if (state.selectedLLMModel != null) {
// Go to download page if the selected model is downloading
if (llmResource.isDownloading) {
emit(
state.copyWith(
progressIndicator:
LocalAIProgress.startDownloading(state.selectedLLMModel!),
selectLLMState: const ChatLoadingState.finish(),
),
);
return;
} else {
emit(
state.copyWith(
progressIndicator: LocalAIProgress.showDownload(
llmResource,
state.selectedLLMModel!,
),
selectLLMState: const ChatLoadingState.finish(),
),
);
}
}
}
},
startDownloadModel: (LLMModelPB llmModel) {
emit(
state.copyWith(
progressIndicator: LocalAIProgress.startDownloading(llmModel),
selectLLMState: const ChatLoadingState.finish(),
),
);
},
cancelDownload: () async {
final _ = await AIEventCancelDownloadLLMResource().send();
_fetchCurremtLLMState();
},
finishDownload: () async {
emit(
state.copyWith(
progressIndicator: const LocalAIProgress.finishDownload(),
),
);
},
updatePluginState: (LocalAIPluginStatePB pluginState) {
if (pluginState.offlineAiReady) {
AIEventRefreshLocalAIModelInfo().send().then((result) {
if (!isClosed) {
add(LocalAIChatSettingEvent.didLoadModelInfo(result));
}
});
if (pluginState.state == RunningStatePB.Stopped) {
emit(
state.copyWith(
runningState: pluginState.state,
progressIndicator: const LocalAIProgress.checkPluginState(),
),
);
} else {
emit(
state.copyWith(
runningState: pluginState.state,
),
);
}
} else {
emit(
state.copyWith(
progressIndicator: const LocalAIProgress.startOfflineAIApp(),
progressIndicator: const LocalAIProgress.downloadLocalAIApp(),
),
);
}
@@ -187,39 +63,6 @@ class LocalAIChatSettingBloc
);
}
void _fetchCurremtLLMState() async {
final result = await AIEventGetLocalLLMState().send();
result.fold(
(llmResource) {
if (!isClosed) {
add(LocalAIChatSettingEvent.refreshLLMState(llmResource));
}
},
(err) {
Log.error(err);
},
);
}
/// Handles the event to fetch local AI settings when the application starts.
Future<void> _handleStarted() async {
final result = await AIEventGetLocalAIPluginState().send();
result.fold(
(pluginState) async {
if (!isClosed) {
add(LocalAIChatSettingEvent.updatePluginState(pluginState));
if (pluginState.offlineAiReady) {
final result = await AIEventRefreshLocalAIModelInfo().send();
if (!isClosed) {
add(LocalAIChatSettingEvent.didLoadModelInfo(result));
}
}
}
},
(err) => Log.error(err.toString()),
);
}
@override
Future<void> close() async {
await listener.stop();
@@ -228,60 +71,24 @@ class LocalAIChatSettingBloc
}
@freezed
class LocalAIChatSettingEvent with _$LocalAIChatSettingEvent {
const factory LocalAIChatSettingEvent.refreshAISetting() = _RefreshAISetting;
const factory LocalAIChatSettingEvent.didLoadModelInfo(
FlowyResult<LLMModelInfoPB, FlowyError> result,
) = _ModelInfo;
const factory LocalAIChatSettingEvent.selectLLMConfig(LLMModelPB config) =
_SelectLLMConfig;
const factory LocalAIChatSettingEvent.refreshLLMState(
LocalModelResourcePB llmResource,
) = _RefreshLLMResource;
const factory LocalAIChatSettingEvent.startDownloadModel(
LLMModelPB llmModel,
) = _StartDownloadModel;
const factory LocalAIChatSettingEvent.cancelDownload() = _CancelDownload;
const factory LocalAIChatSettingEvent.finishDownload() = _FinishDownload;
const factory LocalAIChatSettingEvent.updatePluginState(
LocalAIPluginStatePB pluginState,
) = _PluginState;
class LocalAISettingPanelEvent with _$LocalAISettingPanelEvent {
const factory LocalAISettingPanelEvent.started() = _Started;
const factory LocalAISettingPanelEvent.updateAIState(
LocalAIPB aiState,
) = _UpdateAIState;
}
@freezed
class LocalAIChatSettingState with _$LocalAIChatSettingState {
const factory LocalAIChatSettingState({
LLMModelInfoPB? modelInfo,
LLMModelPB? selectedLLMModel,
class LocalAISettingPanelState with _$LocalAISettingPanelState {
const factory LocalAISettingPanelState({
LocalAIProgress? progressIndicator,
@Default(AIModelProgress.init()) AIModelProgress aiModelProgress,
@Default(ChatLoadingState.loading()) ChatLoadingState selectLLMState,
@Default([]) List<LLMModelPB> models,
@Default(RunningStatePB.Connecting) RunningStatePB runningState,
}) = _LocalAIChatSettingState;
}
@freezed
class LocalAIProgress with _$LocalAIProgress {
// when user comes back to the setting page, it will auto detect current llm state
const factory LocalAIProgress.showDownload(
LocalModelResourcePB llmResource,
LLMModelPB llmModel,
) = _DownloadNeeded;
// when start downloading the model
const factory LocalAIProgress.startDownloading(LLMModelPB llmModel) =
_Downloading;
const factory LocalAIProgress.finishDownload() = _Finish;
const factory LocalAIProgress.checkPluginState() = _CheckPluginState;
const factory LocalAIProgress.startOfflineAIApp() = _StartOfflineAIApp;
}
@freezed
class AIModelProgress with _$AIModelProgress {
const factory AIModelProgress.init() = _AIModelProgressInit;
const factory AIModelProgress.loading() = _AIModelDownloading;
const factory AIModelProgress.finish({FlowyError? error}) = _AIModelFinish;
const factory LocalAIProgress.checkPluginState() = _CheckPluginStateProgress;
const factory LocalAIProgress.downloadLocalAIApp() =
_DownloadLocalAIAppProgress;
}
@@ -20,25 +20,9 @@ class LocalAIChatToggleBloc
) async {
await event.when(
started: () async {
final result = await AIEventGetLocalAIChatState().send();
final result = await AIEventGetLocalAIState().send();
_handleResult(emit, result);
},
toggle: () async {
emit(
state.copyWith(
pageIndicator: const LocalAIChatToggleStateIndicator.loading(),
),
);
unawaited(
AIEventToggleLocalAIChat().send().then(
(result) {
if (!isClosed) {
add(LocalAIChatToggleEvent.handleResult(result));
}
},
),
);
},
handleResult: (result) {
_handleResult(emit, result);
},
@@ -47,7 +31,7 @@ class LocalAIChatToggleBloc
void _handleResult(
Emitter<LocalAIChatToggleState> emit,
FlowyResult<LocalAIChatPB, FlowyError> result,
FlowyResult<LocalAIPB, FlowyError> result,
) {
result.fold(
(localAI) {
@@ -72,9 +56,8 @@ class LocalAIChatToggleBloc
@freezed
class LocalAIChatToggleEvent with _$LocalAIChatToggleEvent {
const factory LocalAIChatToggleEvent.started() = _Started;
const factory LocalAIChatToggleEvent.toggle() = _Toggle;
const factory LocalAIChatToggleEvent.handleResult(
FlowyResult<LocalAIChatPB, FlowyError> result,
FlowyResult<LocalAIPB, FlowyError> result,
) = _HandleResult;
}
@@ -8,8 +8,8 @@ import 'package:appflowy_backend/protobuf/flowy-notification/subject.pb.dart';
import 'package:appflowy_backend/rust_stream.dart';
import 'package:appflowy_result/appflowy_result.dart';
typedef PluginStateCallback = void Function(LocalAIPluginStatePB state);
typedef LocalAIChatCallback = void Function(LocalAIChatPB chatState);
typedef PluginStateCallback = void Function(LocalAIPB state);
typedef PluginResourceCallback = void Function(LackOfAIResourcePB data);
class LocalLLMListener {
LocalLLMListener() {
@@ -24,15 +24,14 @@ class LocalLLMListener {
ChatNotificationParser? _parser;
PluginStateCallback? stateCallback;
LocalAIChatCallback? chatStateCallback;
void Function()? finishStreamingCallback;
PluginResourceCallback? resourceCallback;
void start({
PluginStateCallback? stateCallback,
LocalAIChatCallback? chatStateCallback,
PluginResourceCallback? resourceCallback,
}) {
this.stateCallback = stateCallback;
this.chatStateCallback = chatStateCallback;
this.resourceCallback = resourceCallback;
}
void _callback(
@@ -41,11 +40,11 @@ class LocalLLMListener {
) {
result.map((r) {
switch (ty) {
case ChatNotification.UpdateChatPluginState:
stateCallback?.call(LocalAIPluginStatePB.fromBuffer(r));
case ChatNotification.UpdateLocalAIState:
stateCallback?.call(LocalAIPB.fromBuffer(r));
break;
case ChatNotification.UpdateLocalChatAI:
chatStateCallback?.call(LocalAIChatPB.fromBuffer(r));
case ChatNotification.LocalAIResourceUpdated:
resourceCallback?.call(LackOfAIResourcePB.fromBuffer(r));
break;
default:
break;
@@ -0,0 +1,221 @@
import 'dart:async';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:bloc/bloc.dart';
import 'package:collection/collection.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:equatable/equatable.dart';
part 'ollama_setting_bloc.freezed.dart';
class OllamaSettingBloc extends Bloc<OllamaSettingEvent, OllamaSettingState> {
OllamaSettingBloc() : super(const OllamaSettingState()) {
on<OllamaSettingEvent>(_handleEvent);
}
Future<void> _handleEvent(
OllamaSettingEvent event,
Emitter<OllamaSettingState> emit,
) async {
event.when(
started: () {
AIEventGetLocalAISetting().send().fold(
(setting) {
if (!isClosed) {
add(OllamaSettingEvent.didLoadSetting(setting));
}
},
Log.error,
);
},
didLoadSetting: (setting) => _updateSetting(setting, emit),
updateSetting: (setting) => _updateSetting(setting, emit),
onEdit: (content, settingType) {
final updatedSubmittedItems = state.submittedItems
.map(
(item) => item.settingType == settingType
? SubmittedItem(
content: content,
settingType: item.settingType,
)
: item,
)
.toList();
// Convert both lists to maps: {settingType: content}
final updatedMap = {
for (final item in updatedSubmittedItems)
item.settingType: item.content,
};
final inputMap = {
for (final item in state.inputItems) item.settingType: item.content,
};
// Compare maps instead of lists
final isEdited = !const MapEquality<SettingType, String>()
.equals(updatedMap, inputMap);
emit(
state.copyWith(
submittedItems: updatedSubmittedItems,
isEdited: isEdited,
),
);
},
submit: () {
final setting = LocalAISettingPB();
final settingUpdaters = <SettingType, void Function(String)>{
SettingType.serverUrl: (value) => setting.serverUrl = value,
SettingType.chatModel: (value) => setting.chatModelName = value,
SettingType.embeddingModel: (value) =>
setting.embeddingModelName = value,
};
for (final item in state.submittedItems) {
settingUpdaters[item.settingType]?.call(item.content);
}
add(OllamaSettingEvent.updateSetting(setting));
AIEventUpdateLocalAISetting(setting).send().fold(
(_) {
Log.info('AI setting updated successfully');
},
(err) => Log.error("update ai setting failed: $err"),
);
},
);
}
void _updateSetting(
LocalAISettingPB setting,
Emitter<OllamaSettingState> emit,
) {
emit(
state.copyWith(
setting: setting,
inputItems: _createInputItems(setting),
submittedItems: _createSubmittedItems(setting),
isEdited: false, // Reset to false when the setting is loaded/updated.
),
);
}
List<SettingItem> _createInputItems(LocalAISettingPB setting) => [
SettingItem(
content: setting.serverUrl,
hintText: 'http://localhost:11434',
settingType: SettingType.serverUrl,
),
SettingItem(
content: setting.chatModelName,
hintText: 'llama3.1',
settingType: SettingType.chatModel,
),
SettingItem(
content: setting.embeddingModelName,
hintText: 'nomic-embed-text',
settingType: SettingType.embeddingModel,
),
];
List<SubmittedItem> _createSubmittedItems(LocalAISettingPB setting) => [
SubmittedItem(
content: setting.serverUrl,
settingType: SettingType.serverUrl,
),
SubmittedItem(
content: setting.chatModelName,
settingType: SettingType.chatModel,
),
SubmittedItem(
content: setting.embeddingModelName,
settingType: SettingType.embeddingModel,
),
];
}
// Create an enum for setting type.
enum SettingType {
serverUrl,
chatModel,
embeddingModel; // semicolon needed after the enum values
String get title {
switch (this) {
case SettingType.serverUrl:
return 'Ollama server url';
case SettingType.chatModel:
return 'Chat model name';
case SettingType.embeddingModel:
return 'Embedding model name';
}
}
}
class SettingItem extends Equatable {
const SettingItem({
required this.content,
required this.hintText,
required this.settingType,
});
final String content;
final String hintText;
final SettingType settingType;
@override
List<Object?> get props => [content, settingType];
}
class SubmittedItem extends Equatable {
const SubmittedItem({
required this.content,
required this.settingType,
});
final String content;
final SettingType settingType;
@override
List<Object?> get props => [content, settingType];
}
@freezed
class OllamaSettingEvent with _$OllamaSettingEvent {
const factory OllamaSettingEvent.started() = _Started;
const factory OllamaSettingEvent.didLoadSetting(LocalAISettingPB setting) =
_DidLoadSetting;
const factory OllamaSettingEvent.updateSetting(LocalAISettingPB setting) =
_UpdateSetting;
const factory OllamaSettingEvent.onEdit(
String content,
SettingType settingType,
) = _OnEdit;
const factory OllamaSettingEvent.submit() = _OnSubmit;
}
@freezed
class OllamaSettingState with _$OllamaSettingState {
const factory OllamaSettingState({
LocalAISettingPB? setting,
@Default([
SettingItem(
content: 'http://localhost:11434',
hintText: 'http://localhost:11434',
settingType: SettingType.serverUrl,
),
SettingItem(
content: 'llama3.1',
hintText: 'llama3.1',
settingType: SettingType.chatModel,
),
SettingItem(
content: 'nomic-embed-text',
hintText: 'nomic-embed-text',
settingType: SettingType.embeddingModel,
),
])
List<SettingItem> inputItems,
@Default([]) List<SubmittedItem> submittedItems,
@Default(false) bool isEdited,
}) = _PluginStateState;
}
@@ -1,13 +1,11 @@
import 'dart:async';
import 'package:appflowy/core/helpers/url_launcher.dart';
import 'package:appflowy/workspace/application/settings/ai/local_llm_listener.dart';
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:url_launcher/url_launcher.dart' show launchUrl;
part 'plugin_state_bloc.freezed.dart';
@@ -16,13 +14,18 @@ class PluginStateBloc extends Bloc<PluginStateEvent, PluginStateState> {
: listener = LocalLLMListener(),
super(
const PluginStateState(
action: PluginStateAction.init(),
action: PluginStateAction.unknown(),
),
) {
listener.start(
stateCallback: (pluginState) {
if (!isClosed) {
add(PluginStateEvent.updateState(pluginState));
add(PluginStateEvent.updateLocalAIState(pluginState));
}
},
resourceCallback: (data) {
if (!isClosed) {
add(PluginStateEvent.resourceStateChange(data));
}
},
);
@@ -44,66 +47,71 @@ class PluginStateBloc extends Bloc<PluginStateEvent, PluginStateState> {
) async {
await event.when(
started: () async {
final result = await AIEventGetLocalAIPluginState().send();
final result = await AIEventGetLocalAIState().send();
result.fold(
(pluginState) {
if (!isClosed) {
add(PluginStateEvent.updateState(pluginState));
add(PluginStateEvent.updateLocalAIState(pluginState));
}
},
(err) => Log.error(err.toString()),
);
},
updateState: (LocalAIPluginStatePB pluginState) {
updateLocalAIState: (LocalAIPB aiState) {
// if the offline ai is not started, ask user to start it
if (pluginState.offlineAiReady) {
// Chech state of the plugin
switch (pluginState.state) {
case RunningStatePB.Connecting:
emit(
const PluginStateState(
action: PluginStateAction.loadingPlugin(),
),
);
case RunningStatePB.Running:
emit(const PluginStateState(action: PluginStateAction.ready()));
break;
default:
emit(
state.copyWith(action: const PluginStateAction.restartPlugin()),
);
break;
}
} else {
if (aiState.hasLackOfResource()) {
emit(
const PluginStateState(
action: PluginStateAction.startAIOfflineApp(),
PluginStateState(
action: PluginStateAction.lackOfResource(aiState.lackOfResource),
),
);
return;
}
// Chech state of the plugin
switch (aiState.state) {
case RunningStatePB.ReadyToRun:
emit(
const PluginStateState(
action: PluginStateAction.readToRun(),
),
);
case RunningStatePB.Connecting:
emit(
const PluginStateState(
action: PluginStateAction.initializingPlugin(),
),
);
case RunningStatePB.Connected:
emit(
const PluginStateState(
action: PluginStateAction.initializingPlugin(),
),
);
break;
case RunningStatePB.Running:
emit(const PluginStateState(action: PluginStateAction.running()));
break;
case RunningStatePB.Stopped:
emit(
state.copyWith(action: const PluginStateAction.restartPlugin()),
);
default:
break;
}
},
restartLocalAI: () async {
emit(
const PluginStateState(action: PluginStateAction.loadingPlugin()),
const PluginStateState(action: PluginStateAction.restartPlugin()),
);
unawaited(AIEventRestartLocalAIChat().send());
unawaited(AIEventRestartLocalAI().send());
},
openModelDirectory: () async {
final result = await AIEventGetModelStorageDirectory().send();
result.fold(
(data) {
afLaunchUri(Uri.file(data.filePath));
},
(err) => Log.error(err.toString()),
);
},
downloadOfflineAIApp: () async {
final result = await AIEventGetOfflineAIAppLink().send();
await result.fold(
(app) async {
await launchUrl(Uri.parse(app.link));
},
(err) {},
resourceStateChange: (data) {
emit(
PluginStateState(
action: PluginStateAction.lackOfResource(data.resourceDesc),
),
);
},
);
@@ -113,12 +121,11 @@ class PluginStateBloc extends Bloc<PluginStateEvent, PluginStateState> {
@freezed
class PluginStateEvent with _$PluginStateEvent {
const factory PluginStateEvent.started() = _Started;
const factory PluginStateEvent.updateState(LocalAIPluginStatePB pluginState) =
_UpdatePluginState;
const factory PluginStateEvent.updateLocalAIState(LocalAIPB aiState) =
_UpdateLocalAIState;
const factory PluginStateEvent.restartLocalAI() = _RestartLocalAI;
const factory PluginStateEvent.openModelDirectory() =
_OpenModelStorageDirectory;
const factory PluginStateEvent.downloadOfflineAIApp() = _DownloadOfflineAIApp;
const factory PluginStateEvent.resourceStateChange(LackOfAIResourcePB data) =
_ResourceStateChange;
}
@freezed
@@ -130,9 +137,10 @@ class PluginStateState with _$PluginStateState {
@freezed
class PluginStateAction with _$PluginStateAction {
const factory PluginStateAction.init() = _Init;
const factory PluginStateAction.loadingPlugin() = _LoadingPlugin;
const factory PluginStateAction.ready() = _Ready;
const factory PluginStateAction.unknown() = _Unknown;
const factory PluginStateAction.readToRun() = _ReadyToRun;
const factory PluginStateAction.initializingPlugin() = _InitializingPlugin;
const factory PluginStateAction.running() = _PluginRunning;
const factory PluginStateAction.restartPlugin() = _RestartPlugin;
const factory PluginStateAction.startAIOfflineApp() = _StartAIOfflineApp;
const factory PluginStateAction.lackOfResource(String desc) = _LackOfResource;
}
@@ -1,119 +0,0 @@
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/workspace/application/settings/ai/download_model_bloc.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/theme_extension.dart';
import 'package:flowy_infra_ui/style_widget/button.dart';
import 'package:flowy_infra_ui/style_widget/text.dart';
import 'package:flowy_infra_ui/widget/spacing.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';
class DownloadingIndicator extends StatelessWidget {
const DownloadingIndicator({
required this.llmModel,
required this.onCancel,
required this.onFinish,
super.key,
});
final LLMModelPB llmModel;
final VoidCallback onCancel;
final VoidCallback onFinish;
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) =>
DownloadModelBloc(llmModel)..add(const DownloadModelEvent.started()),
child: BlocListener<DownloadModelBloc, DownloadModelState>(
listener: (context, state) {
if (state.isFinish) {
onFinish();
}
},
child: DecoratedBox(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(8),
),
child: BlocBuilder<DownloadModelBloc, DownloadModelState>(
builder: (context, state) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
DownloadingProgressBar(onCancel: onCancel),
if (state.bigFileDownloadPrompt != null) ...[
const VSpace(2),
Opacity(
opacity: 0.6,
child:
FlowyText(state.bigFileDownloadPrompt!, fontSize: 11),
),
],
],
);
},
),
),
),
);
}
}
class DownloadingProgressBar extends StatelessWidget {
const DownloadingProgressBar({required this.onCancel, super.key});
final VoidCallback onCancel;
@override
Widget build(BuildContext context) {
return BlocBuilder<DownloadModelBloc, DownloadModelState>(
builder: (context, state) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Opacity(
opacity: 0.6,
child: FlowyText(
"${LocaleKeys.settings_aiPage_keys_downloadingModel.tr()}: ${state.object}",
fontSize: 11,
),
),
IntrinsicHeight(
child: Row(
children: [
Expanded(
child: LinearPercentIndicator(
lineHeight: 9.0,
percent: state.percent,
padding: EdgeInsets.zero,
progressColor: AFThemeExtension.of(context).success,
backgroundColor:
AFThemeExtension.of(context).progressBarBGColor,
barRadius: const Radius.circular(8),
trailing: FlowyText(
"${(state.percent * 100).toStringAsFixed(0)}%",
fontSize: 11,
color: AFThemeExtension.of(context).success,
),
),
),
const HSpace(12),
FlowyButton(
useIntrinsicWidth: true,
text: FlowyText(
LocaleKeys.button_cancel.tr(),
fontSize: 11,
),
onTap: onCancel,
),
],
),
),
],
);
},
);
}
}
@@ -22,7 +22,7 @@ class InitLocalAIIndicator extends StatelessWidget {
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),
child: BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(
child: BlocBuilder<LocalAISettingPanelBloc, LocalAISettingPanelState>(
builder: (context, state) {
switch (state.runningState) {
case RunningStatePB.Connecting:
@@ -31,7 +31,7 @@ class InitLocalAIIndicator extends StatelessWidget {
children: [
const HSpace(8),
FlowyText(
LocaleKeys.settings_aiPage_keys_localAILoading.tr(),
LocaleKeys.settings_aiPage_keys_localAIInitializing.tr(),
fontSize: 11,
color: const Color(0xFF1E4620),
),
@@ -1,370 +0,0 @@
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/workspace/application/settings/ai/local_ai_chat_bloc.dart';
import 'package:appflowy/workspace/application/settings/ai/local_ai_chat_toggle_bloc.dart';
import 'package:appflowy/workspace/presentation/home/menu/sidebar/space/shared_widget.dart';
import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/downloading_model.dart';
import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart';
import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dart';
import 'package:appflowy/workspace/presentation/widgets/dialogs.dart';
import 'package:appflowy/workspace/presentation/widgets/toggle/toggle.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:expandable/expandable.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/workspace/presentation/settings/shared/af_dropdown_menu_entry.dart';
import 'package:appflowy/workspace/presentation/settings/shared/settings_dropdown.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class LocalAIChatSetting extends StatelessWidget {
const LocalAIChatSetting({super.key});
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => LocalAIChatSettingBloc()),
BlocProvider(
create: (context) => LocalAIChatToggleBloc()
..add(const LocalAIChatToggleEvent.started()),
),
],
child: ExpandableNotifier(
child: BlocListener<LocalAIChatToggleBloc, LocalAIChatToggleState>(
listener: (context, state) {
// Listen to the toggle state and expand the panel if the state is ready.
final controller = ExpandableController.of(
context,
required: true,
)!;
// Neet to wrap with WidgetsBinding.instance.addPostFrameCallback otherwise the
// ExpandablePanel not expanded sometimes. Maybe because the ExpandablePanel is not
// built yet when the listener is called.
WidgetsBinding.instance.addPostFrameCallback(
(_) {
state.pageIndicator.when(
error: (_) => controller.expanded = false,
ready: (enabled) {
controller.expanded = enabled;
context.read<LocalAIChatSettingBloc>().add(
const LocalAIChatSettingEvent.refreshAISetting(),
);
},
loading: () => controller.expanded = false,
);
},
debugLabel: 'LocalAI.showLocalAIChatSetting',
);
},
child: ExpandablePanel(
theme: const ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
tapBodyToCollapse: false,
hasIcon: false,
tapBodyToExpand: false,
tapHeaderToExpand: false,
),
header: const SizedBox.shrink(),
collapsed: const SizedBox.shrink(),
expanded: Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
// child: _LocalLLMInfoWidget(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(
builder: (context, state) {
// If the progress indicator is startOfflineAIApp, then don't show the LLM model.
if (state.progressIndicator ==
const LocalAIProgress.startOfflineAIApp()) {
return const SizedBox.shrink();
} else {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: FlowyText.medium(
LocaleKeys.settings_aiPage_keys_llmModel.tr(),
fontSize: 14,
),
),
const Spacer(),
state.aiModelProgress.when(
init: () => const SizedBox.shrink(),
loading: () {
return const Expanded(
child: Row(
children: [
Spacer(),
CircularProgressIndicator.adaptive(),
],
),
);
},
finish: (err) => (err == null)
? const _SelectLocalModelDropdownMenu()
: const SizedBox.shrink(),
),
],
);
}
},
),
const IntrinsicHeight(child: _LocalAIStateWidget()),
],
),
),
),
),
),
);
}
}
class LocalAIChatSettingHeader extends StatelessWidget {
const LocalAIChatSettingHeader({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<LocalAIChatToggleBloc, LocalAIChatToggleState>(
builder: (context, state) {
return state.pageIndicator.when(
error: (error) {
return const SizedBox.shrink();
},
loading: () {
return Row(
children: [
FlowyText(
LocaleKeys.settings_aiPage_keys_localAIStart.tr(),
),
const Spacer(),
const CircularProgressIndicator.adaptive(),
const HSpace(8),
],
);
},
ready: (isEnabled) {
return Row(
children: [
const FlowyText('Enable Local AI Chat'),
const Spacer(),
Toggle(
value: isEnabled,
onChanged: (_) {
context
.read<LocalAIChatToggleBloc>()
.add(const LocalAIChatToggleEvent.toggle());
},
),
],
);
},
);
},
);
}
}
class _SelectLocalModelDropdownMenu extends StatelessWidget {
const _SelectLocalModelDropdownMenu();
@override
Widget build(BuildContext context) {
return BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(
builder: (context, state) {
return Flexible(
child: SettingsDropdown<LLMModelPB>(
key: const Key('_SelectLocalModelDropdownMenu'),
onChanged: (model) => context.read<LocalAIChatSettingBloc>().add(
LocalAIChatSettingEvent.selectLLMConfig(model),
),
selectedOption: state.selectedLLMModel!,
options: state.models
.map(
(llm) => buildDropdownMenuEntry<LLMModelPB>(
context,
value: llm,
label: llm.chatModel,
),
)
.toList(),
),
);
},
);
}
}
class _LocalAIStateWidget extends StatelessWidget {
const _LocalAIStateWidget();
@override
Widget build(BuildContext context) {
return BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(
builder: (context, state) {
final error = errorFromState(state);
if (error == null) {
// If the error is null, handle selected llm model.
if (state.progressIndicator != null) {
final child = state.progressIndicator!.when(
showDownload: (
LocalModelResourcePB llmResource,
LLMModelPB llmModel,
) =>
_ShowDownloadIndicator(
llmResource: llmResource,
llmModel: llmModel,
),
startDownloading: (llmModel) {
return DownloadingIndicator(
key: UniqueKey(),
llmModel: llmModel,
onFinish: () => context
.read<LocalAIChatSettingBloc>()
.add(const LocalAIChatSettingEvent.finishDownload()),
onCancel: () => context
.read<LocalAIChatSettingBloc>()
.add(const LocalAIChatSettingEvent.cancelDownload()),
);
},
finishDownload: () => const InitLocalAIIndicator(),
checkPluginState: () => const PluginStateIndicator(),
startOfflineAIApp: () => OpenOrDownloadOfflineAIApp(
onRetry: () {
context
.read<LocalAIChatSettingBloc>()
.add(const LocalAIChatSettingEvent.refreshAISetting());
},
),
);
return Padding(
padding: const EdgeInsets.only(top: 8),
child: child,
);
} else {
return const SizedBox.shrink();
}
} else {
return Opacity(
opacity: 0.5,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: FlowyText(
error.msg,
maxLines: 10,
),
),
);
}
},
);
}
FlowyError? errorFromState(LocalAIChatSettingState state) {
final err = state.aiModelProgress.when(
loading: () => null,
finish: (err) => err,
init: () {},
);
if (err == null) {
state.selectLLMState.when(
loading: () => null,
finish: (err) => err,
);
}
return err;
}
}
void _showDownloadDialog(
BuildContext context,
LocalModelResourcePB llmResource,
LLMModelPB llmModel,
) {
if (llmResource.pendingResources.isEmpty) {
return;
}
final res = llmResource.pendingResources.first;
String desc = "";
switch (res.resType) {
case PendingResourceTypePB.AIModel:
desc = LocaleKeys.settings_aiPage_keys_downloadLLMPromptDetail.tr(
args: [
llmResource.pendingResources[0].name,
llmResource.pendingResources[0].fileSize,
],
);
break;
case PendingResourceTypePB.OfflineApp:
desc = LocaleKeys.settings_aiPage_keys_downloadAppFlowyOfflineAI.tr();
break;
}
showConfirmDialog(
context: context,
style: ConfirmPopupStyle.cancelAndOk,
title: LocaleKeys.settings_aiPage_keys_downloadLLMPrompt.tr(
args: [res.name],
),
description: desc,
confirmLabel: LocaleKeys.button_confirm.tr(),
onConfirm: () => context.read<LocalAIChatSettingBloc>().add(
LocalAIChatSettingEvent.startDownloadModel(
llmModel,
),
),
onCancel: () => context.read<LocalAIChatSettingBloc>().add(
const LocalAIChatSettingEvent.cancelDownload(),
),
);
}
class _ShowDownloadIndicator extends StatelessWidget {
const _ShowDownloadIndicator({
required this.llmResource,
required this.llmModel,
});
final LocalModelResourcePB llmResource;
final LLMModelPB llmModel;
@override
Widget build(BuildContext context) {
return BlocBuilder<LocalAIChatSettingBloc, LocalAIChatSettingState>(
builder: (context, state) {
return Row(
children: [
const Spacer(),
IntrinsicWidth(
child: SizedBox(
height: 30,
child: FlowyButton(
text: FlowyText(
LocaleKeys.settings_aiPage_keys_downloadAIModelButton.tr(),
fontSize: 14,
color: const Color(0xFF005483),
),
leftIcon: const FlowySvg(
FlowySvgs.local_model_download_s,
color: Color(0xFF005483),
),
onTap: () {
_showDownloadDialog(context, llmResource, llmModel);
},
),
),
),
],
);
},
);
}
}
@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/workspace/application/settings/ai/local_ai_bloc.dart';
import 'package:appflowy/workspace/application/settings/ai/settings_ai_bloc.dart';
import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/local_ai_chat_setting.dart';
import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart';
import 'package:appflowy/workspace/presentation/widgets/dialogs.dart';
import 'package:appflowy/workspace/presentation/widgets/toggle/toggle.dart';
import 'package:easy_localization/easy_localization.dart';
@@ -38,10 +38,11 @@ class _LocalAISettingState extends State<LocalAISetting> {
listener: (context, state) {
final controller =
ExpandableController.of(context, required: true)!;
state.pageIndicator.when(
error: (_) => controller.expanded = false,
ready: (enabled) => controller.expanded = enabled,
loading: () => controller.expanded = false,
error: (_) => controller.expanded = true,
isEnabled: (enabled) => controller.expanded = enabled,
loading: () => controller.expanded = true,
);
},
child: ExpandablePanel(
@@ -68,7 +69,7 @@ class _LocalAISettingState extends State<LocalAISetting> {
child: const Padding(
padding:
EdgeInsets.symmetric(horizontal: 12, vertical: 6),
child: LocalAIChatSetting(),
child: LocalAISettingPanel(),
),
),
],
@@ -95,9 +96,9 @@ class LocalAISettingHeader extends StatelessWidget {
return const SizedBox.shrink();
},
loading: () {
return const CircularProgressIndicator.adaptive();
return const SizedBox.shrink();
},
ready: (isEnabled) {
isEnabled: (isEnabled) {
return Row(
children: [
FlowyText(
@@ -0,0 +1,78 @@
import 'package:appflowy/workspace/application/settings/ai/local_ai_chat_bloc.dart';
import 'package:appflowy/workspace/application/settings/ai/local_ai_chat_toggle_bloc.dart';
import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dart';
import 'package:expandable/expandable.dart';
import 'package:flowy_infra_ui/widget/spacing.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'plugin_state.dart';
class LocalAISettingPanel extends StatelessWidget {
const LocalAISettingPanel({super.key});
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => LocalAISettingPanelBloc()),
BlocProvider(
create: (context) => LocalAIChatToggleBloc()
..add(const LocalAIChatToggleEvent.started()),
),
],
child: ExpandableNotifier(
initialExpanded: true,
child: ExpandablePanel(
theme: const ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
tapBodyToCollapse: false,
hasIcon: false,
tapBodyToExpand: false,
tapHeaderToExpand: false,
),
header: const SizedBox.shrink(),
collapsed: const SizedBox.shrink(),
expanded: Padding(
padding: const EdgeInsets.symmetric(vertical: 6),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BlocBuilder<LocalAISettingPanelBloc, LocalAISettingPanelState>(
builder: (context, state) {
// If the progress indicator is startLocalAIApp, then don't show the LLM model.
if (state.progressIndicator ==
const LocalAIProgress.downloadLocalAIApp()) {
return const SizedBox.shrink();
} else {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: const [
OllamaSettingPage(),
VSpace(6),
_LocalAIStateWidget(),
],
);
}
},
),
],
),
),
),
),
);
}
}
class _LocalAIStateWidget extends StatelessWidget {
const _LocalAIStateWidget();
@override
Widget build(BuildContext context) {
return BlocBuilder<LocalAISettingPanelBloc, LocalAISettingPanelState>(
builder: (context, state) {
return const PluginStateIndicator();
},
);
}
}
@@ -0,0 +1,167 @@
import 'package:appflowy/core/helpers/url_launcher.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/workspace/application/settings/ai/ollama_setting_bloc.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/size.dart';
import 'package:flowy_infra_ui/style_widget/button.dart';
import 'package:flowy_infra_ui/style_widget/text.dart';
import 'package:flowy_infra_ui/style_widget/text_field.dart';
import 'package:flowy_infra_ui/widget/flowy_tooltip.dart';
import 'package:flowy_infra_ui/widget/spacing.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class OllamaSettingPage extends StatelessWidget {
const OllamaSettingPage({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) =>
OllamaSettingBloc()..add(const OllamaSettingEvent.started()),
child: BlocBuilder<OllamaSettingBloc, OllamaSettingState>(
buildWhen: (previous, current) =>
previous.inputItems != current.inputItems ||
previous.isEdited != current.isEdited,
builder: (context, state) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListView.separated(
shrinkWrap: true,
itemCount: state.inputItems.length,
separatorBuilder: (_, __) => const VSpace(10),
itemBuilder: (context, index) {
final item = state.inputItems[index];
return _SettingItemWidget(item: item);
},
),
const VSpace(6),
Opacity(
opacity: 0.6,
child: _InstallOllamaInstruction(),
),
_SaveButton(isEdited: state.isEdited),
],
);
},
),
);
}
}
class _SettingItemWidget extends StatelessWidget {
const _SettingItemWidget({required this.item});
final SettingItem item;
@override
Widget build(BuildContext context) {
return Column(
key: ValueKey(item.content + item.settingType.title),
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FlowyText(item.settingType.title),
const VSpace(8),
FlowyTextField(
hintText: item.hintText,
text: item.content,
onChanged: (content) {
context.read<OllamaSettingBloc>().add(
OllamaSettingEvent.onEdit(content, item.settingType),
);
},
),
],
);
}
}
class _SaveButton extends StatelessWidget {
const _SaveButton({required this.isEdited});
final bool isEdited;
@override
Widget build(BuildContext context) {
final tooltipMessage = isEdited ? 'Click to apply changes' : 'No changes';
return SizedBox(
height: 50,
child: Row(
children: [
const Spacer(),
SizedBox(
width: 120,
child: FlowyTooltip(
message: tooltipMessage,
child: Opacity(
opacity: isEdited ? 1 : 0.5,
child: FlowyTextButton(
'Apply',
mainAxisAlignment: MainAxisAlignment.center,
onPressed: isEdited
? () {
context
.read<OllamaSettingBloc>()
.add(const OllamaSettingEvent.submit());
}
: null,
),
),
),
),
],
),
);
}
}
class _InstallOllamaInstruction extends StatelessWidget {
const _InstallOllamaInstruction();
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
RichText(
maxLines: 3,
textAlign: TextAlign.left,
text: TextSpan(
children: <TextSpan>[
TextSpan(
text:
"${LocaleKeys.settings_aiPage_keys_localAISetupInstruction1.tr()} ",
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(height: 1.5),
),
TextSpan(
text:
" ${LocaleKeys.settings_aiPage_keys_localAISetupInstruction2.tr()} ",
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
fontSize: FontSizes.s14,
color: Theme.of(context).colorScheme.primary,
height: 1.5,
),
recognizer: TapGestureRecognizer()
..onTap = () => afLaunchUrlString(
"https://docs.appflowy.io/docs/appflowy/product/appflowy-ai-ollama",
),
),
TextSpan(
text:
" ${LocaleKeys.settings_aiPage_keys_localAISetupInstruction3.tr()} ",
style: Theme.of(context)
.textTheme
.bodySmall!
.copyWith(height: 1.5),
),
],
),
),
],
);
}
}
@@ -3,6 +3,7 @@ import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/workspace/application/settings/ai/download_offline_ai_app_bloc.dart';
import 'package:appflowy/workspace/application/settings/ai/plugin_state_bloc.dart';
import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/size.dart';
import 'package:flowy_infra_ui/style_widget/button.dart';
@@ -23,17 +24,12 @@ class PluginStateIndicator extends StatelessWidget {
child: BlocBuilder<PluginStateBloc, PluginStateState>(
builder: (context, state) {
return state.action.when(
init: () => const _InitPlugin(),
ready: () => const _LocalAIReadyToUse(),
restartPlugin: () => const _ReloadButton(),
loadingPlugin: () => const _InitPlugin(),
startAIOfflineApp: () => OpenOrDownloadOfflineAIApp(
onRetry: () {
context
.read<PluginStateBloc>()
.add(const PluginStateEvent.started());
},
),
unknown: () => const SizedBox.shrink(),
readToRun: () => const SizedBox.shrink(),
initializingPlugin: () => const InitLocalAIIndicator(),
running: () => const _LocalAIRunning(),
restartPlugin: () => const _RestartPluginButton(),
lackOfResource: (desc) => _LackOfResource(desc: desc),
);
},
),
@@ -41,26 +37,8 @@ class PluginStateIndicator extends StatelessWidget {
}
}
class _InitPlugin extends StatelessWidget {
const _InitPlugin();
@override
Widget build(BuildContext context) {
return Row(
children: [
FlowyText(LocaleKeys.settings_aiPage_keys_localAIStart.tr()),
const Spacer(),
const SizedBox(
height: 20,
child: CircularProgressIndicator.adaptive(),
),
],
);
}
}
class _ReloadButton extends StatelessWidget {
const _ReloadButton();
class _RestartPluginButton extends StatelessWidget {
const _RestartPluginButton();
@override
Widget build(BuildContext context) {
@@ -91,8 +69,8 @@ class _ReloadButton extends StatelessWidget {
}
}
class _LocalAIReadyToUse extends StatelessWidget {
const _LocalAIReadyToUse();
class _LocalAIRunning extends StatelessWidget {
const _LocalAIRunning();
@override
Widget build(BuildContext context) {
@@ -119,7 +97,7 @@ class _LocalAIReadyToUse extends StatelessWidget {
const HSpace(6),
Flexible(
child: FlowyText(
LocaleKeys.settings_aiPage_keys_localAILoaded.tr(),
LocaleKeys.settings_aiPage_keys_localAIRunning.tr(),
fontSize: 11,
color: const Color(0xFF1E4620),
maxLines: 3,
@@ -128,22 +106,6 @@ class _LocalAIReadyToUse extends StatelessWidget {
],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 6),
child: FlowyButton(
useIntrinsicWidth: true,
text: FlowyText(
LocaleKeys.settings_aiPage_keys_openModelDirectory.tr(),
fontSize: 11,
color: const Color(0xFF1E4620),
),
onTap: () {
context.read<PluginStateBloc>().add(
const PluginStateEvent.openModelDirectory(),
);
},
),
),
],
),
),
@@ -232,20 +194,6 @@ class OpenOrDownloadOfflineAIApp extends StatelessWidget {
],
),
),
// const SizedBox(
// height: 6,
// ), // Replaced VSpace with SizedBox for simplicity
// SizedBox(
// height: 30,
// child: FlowyButton(
// useIntrinsicWidth: true,
// margin: const EdgeInsets.symmetric(horizontal: 12),
// text: FlowyText(
// LocaleKeys.settings_aiPage_keys_activeOfflineAI.tr(),
// ),
// onTap: onRetry,
// ),
// ),
],
);
},
@@ -253,3 +201,30 @@ class OpenOrDownloadOfflineAIApp extends StatelessWidget {
);
}
}
class _LackOfResource extends StatelessWidget {
const _LackOfResource({required this.desc});
final String desc;
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FlowySvg(
FlowySvgs.toast_warning_filled_s,
size: const Size.square(20.0),
blendMode: null,
),
const HSpace(6),
Expanded(
child: FlowyText(
desc,
maxLines: 3,
),
),
],
);
}
}
+23 -23
View File
@@ -144,34 +144,34 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/window_manager/macos
SPEC CHECKSUMS:
app_links: 9028728e32c83a0831d9db8cf91c526d16cc5468
appflowy_backend: 464aeb3e5c6966a41641a2111e5ead72ce2695f7
auto_updater_macos: 3a42f1a06be6981f1a18be37e6e7bf86aa732118
bitsdojo_window_macos: 7959fb0ca65a3ccda30095c181ecb856fae48ea9
connectivity_plus: e74b9f74717d2d99d45751750e266e55912baeb5
desktop_drop: e0b672a7d84c0a6cbc378595e82cdb15f2970a43
device_info_plus: a56e6e74dbbd2bb92f2da12c64ddd4f67a749041
file_selector_macos: 6280b52b459ae6c590af5d78fc35c7267a3c4b31
flowy_infra_ui: 8760ff42a789de40bf5007a5f176b454722a341e
app_links: 10e0a0ab602ffaf34d142cd4862f29d34b303b2a
appflowy_backend: 865496343de667fc8c600e04b9fd05234e130cf9
auto_updater_macos: 3e3462c418fe4e731917eacd8d28eef7af84086d
bitsdojo_window_macos: 44e3b8fe3dd463820e0321f6256c5b1c16bb6a00
connectivity_plus: 18d3c32514c886e046de60e9c13895109866c747
desktop_drop: 69eeff437544aa619c8db7f4481b3a65f7696898
device_info_plus: ce1b7762849d3ec103d0e0517299f2db7ad60720
file_selector_macos: cc3858c981fe6889f364731200d6232dac1d812d
flowy_infra_ui: 03301a39ad118771adbf051a664265c61c507f38
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
HotKey: 400beb7caa29054ea8d864c96f5ba7e5b4852277
hotkey_manager: b443f35f4d772162937aa73fd8995e579f8ac4e2
irondash_engine_context: 893c7d96d20ce361d7e996f39d360c4c2f9869ba
local_notifier: ebf072651e35ae5e47280ad52e2707375cb2ae4e
package_info_plus: f0052d280d17aa382b932f399edf32507174e870
path_provider_foundation: 080d55be775b7414fd5a5ef3ac137b97b097e564
hotkey_manager: c32bf0bfe8f934b7bc17ab4ad5c4c142960b023c
irondash_engine_context: da62996ee25616d2f01bbeb85dc115d813359478
local_notifier: e9506bc66fc70311e8bc7291fb70f743c081e4ff
package_info_plus: 12f1c5c2cfe8727ca46cbd0b26677728972d9a5b
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
ReachabilitySwift: 32793e867593cfc1177f5d16491e3a197d2fccda
screen_retriever_macos: 452e51764a9e1cdb74b3c541238795849f21557f
screen_retriever_macos: 776e0fa5d42c6163d2bf772d22478df4b302b161
Sentry: 1fe34e9c2cbba1e347623610d26db121dcb569f1
sentry_flutter: e24b397f9a61fa5bbefd8279c3b2242ca86faa90
share_plus: 510bf0af1a42cd602274b4629920c9649c52f4cc
shared_preferences_foundation: 9e1978ff2562383bd5676f64ec4e9aa8fa06a6f7
sentry_flutter: a39c2a2d67d5e5b9cb0b94a4985c76dd5b3fc737
share_plus: 1fa619de8392a4398bfaf176d441853922614e89
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
Sparkle: 5f8960a7a119aa7d45dacc0d5837017170bc5675
sqflite_darwin: 20b2a3a3b70e43edae938624ce550a3cbf66a3d0
super_native_extensions: c2795d6d9aedf4a79fae25cb6160b71b50549189
url_launcher_macos: 0fba8ddabfc33ce0a9afe7c5fef5aab3d8d2d673
webview_flutter_wkwebview: 44d4dee7d7056d5ad185d25b38404436d56c547c
window_manager: 1d01fa7ac65a6e6f83b965471b1a7fdd3f06166c
sqflite_darwin: 5a7236e3b501866c1c9befc6771dfd73ffb8702d
super_native_extensions: 85efee3a7495b46b04befcfc86ed12069264ebf3
url_launcher_macos: c82c93949963e55b228a30115bd219499a6fe404
webview_flutter_wkwebview: 0982481e3d9c78fd5c6f62a002fcd24fc791f1e4
window_manager: 3a1844359a6295ab1e47659b1a777e36773cd6e8
PODFILE CHECKSUM: 0532f3f001ca3110b8be345d6491fff690e95823
+2 -2
View File
@@ -90,8 +90,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: e2c9713
resolved-ref: e2c9713104f00658c83ac7d1657e7a0fade171ad
ref: "5841b4c"
resolved-ref: "5841b4c1bab170da66c52c60977690f2817b8fc4"
url: "https://github.com/AppFlowy-IO/appflowy-editor.git"
source: git
version: "5.1.0"
+1 -1
View File
@@ -180,7 +180,7 @@ dependency_overrides:
appflowy_editor:
git:
url: https://github.com/AppFlowy-IO/appflowy-editor.git
ref: "e2c9713"
ref: "5841b4c"
appflowy_editor_plugins:
git:
@@ -1,7 +1,8 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/actions/block_action_option_cubit.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
hide quoteNode, QuoteBlockKeys;
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
+7 -1
View File
@@ -850,6 +850,8 @@
"localAIStart": "Local AI Chat is starting...",
"localAILoading": "Local AI Chat Model is loading...",
"localAIStopped": "Local AI stopped",
"localAIRunning": "Local AI is running",
"localAIInitializing": "Local AI is initializing and may take a few minutes, depending on your device",
"failToLoadLocalAI": "Failed to start local AI",
"restartLocalAI": "Restart Local AI",
"disableLocalAITitle": "Disable local AI",
@@ -863,7 +865,11 @@
"offlineAIDownload3": "it first",
"activeOfflineAI": "Active",
"downloadOfflineAI": "Download",
"openModelDirectory": "Open folder"
"openModelDirectory": "Open folder",
"localAISetupInstruction1": "Follow these",
"localAISetupInstruction2": "instructions",
"localAISetupInstruction3": "to set up Ollama and AppFlowy Local AI. Skip if you've already set it up",
"startLocalAI": "It may take a few seconds to start the local AI"
}
},
"planPage": {
+111 -35
View File
@@ -198,11 +198,12 @@ dependencies = [
[[package]]
name = "appflowy-local-ai"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=6f064efe232268f8d396edbb4b84d57fbb640f13#6f064efe232268f8d396edbb4b84d57fbb640f13"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=87c47edf42e0bec0aecd8ab94de59a1b4be19f12#87c47edf42e0bec0aecd8ab94de59a1b4be19f12"
dependencies = [
"anyhow",
"appflowy-plugin",
"bytes",
"futures",
"reqwest 0.11.27",
"serde",
"serde_json",
@@ -217,7 +218,7 @@ dependencies = [
[[package]]
name = "appflowy-plugin"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=6f064efe232268f8d396edbb4b84d57fbb640f13#6f064efe232268f8d396edbb4b84d57fbb640f13"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-LocalAI?rev=87c47edf42e0bec0aecd8ab94de59a1b4be19f12#87c47edf42e0bec0aecd8ab94de59a1b4be19f12"
dependencies = [
"anyhow",
"cfg-if",
@@ -231,6 +232,7 @@ dependencies = [
"tokio",
"tokio-stream",
"tracing",
"winreg 0.55.0",
"xattr",
]
@@ -870,11 +872,12 @@ dependencies = [
[[package]]
name = "cmd_lib"
version = "1.3.0"
version = "1.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ba0f413777386d37f85afa5242f277a7b461905254c1af3c339d4af06800f62"
checksum = "371c15a3c178d0117091bd84414545309ca979555b1aad573ef591ad58818d41"
dependencies = [
"cmd_lib_macros",
"env_logger 0.10.2",
"faccess",
"lazy_static",
"log",
@@ -883,14 +886,14 @@ dependencies = [
[[package]]
name = "cmd_lib_macros"
version = "1.3.0"
version = "1.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e66605092ff6c6e37e0246601ae6c3f62dc1880e0599359b5f303497c112dc0"
checksum = "cb844bd05be34d91eb67101329aeba9d3337094c04fd8507d821db7ebb488eaf"
dependencies = [
"proc-macro-error",
"proc-macro-error2",
"proc-macro2",
"quote",
"syn 1.0.109",
"syn 2.0.94",
]
[[package]]
@@ -1395,7 +1398,7 @@ dependencies = [
"cssparser-macros",
"dtoa-short",
"itoa",
"phf 0.8.0",
"phf 0.11.2",
"smallvec",
]
@@ -1790,6 +1793,19 @@ dependencies = [
"regex",
]
[[package]]
name = "env_logger"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580"
dependencies = [
"humantime",
"is-terminal",
"log",
"regex",
"termcolor",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@@ -2042,6 +2058,7 @@ dependencies = [
"tracing-subscriber",
"uuid",
"validator 0.18.1",
"winreg 0.55.0",
"zip 2.2.0",
"zip-extensions",
]
@@ -2725,9 +2742,9 @@ dependencies = [
[[package]]
name = "futures"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [
"futures-channel",
"futures-core",
@@ -2740,9 +2757,9 @@ dependencies = [
[[package]]
name = "futures-channel"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
dependencies = [
"futures-core",
"futures-sink",
@@ -2750,15 +2767,15 @@ dependencies = [
[[package]]
name = "futures-core"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-executor"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
dependencies = [
"futures-core",
"futures-task",
@@ -2767,9 +2784,9 @@ dependencies = [
[[package]]
name = "futures-io"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-lite"
@@ -2786,9 +2803,9 @@ dependencies = [
[[package]]
name = "futures-macro"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
@@ -2797,21 +2814,21 @@ dependencies = [
[[package]]
name = "futures-sink"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7"
[[package]]
name = "futures-task"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004"
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
[[package]]
name = "futures-util"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-channel",
"futures-core",
@@ -3077,6 +3094,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b"
[[package]]
name = "hermit-abi"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e"
[[package]]
name = "hex"
version = "0.4.3"
@@ -3647,6 +3670,17 @@ version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6"
[[package]]
name = "is-terminal"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
dependencies = [
"hermit-abi 0.5.0",
"libc",
"windows-sys 0.59.0",
]
[[package]]
name = "itertools"
version = "0.10.5"
@@ -4327,7 +4361,7 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"hermit-abi 0.3.2",
"libc",
]
@@ -4417,12 +4451,12 @@ dependencies = [
[[package]]
name = "os_pipe"
version = "0.9.2"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb233f06c2307e1f5ce2ecad9f8121cffbbee2c95428f44ea85222e460d0d213"
checksum = "5ffd2b0a5634335b135d5728d84c5e0fd726954b87111f7506a61c502280d982"
dependencies = [
"libc",
"winapi",
"windows-sys 0.59.0",
]
[[package]]
@@ -4612,7 +4646,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"
dependencies = [
"phf_macros",
"phf_macros 0.8.0",
"phf_shared 0.8.0",
"proc-macro-hack",
]
@@ -4632,6 +4666,7 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
dependencies = [
"phf_macros 0.11.3",
"phf_shared 0.11.2",
]
@@ -4699,6 +4734,19 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "phf_macros"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f84ac04429c13a7ff43785d75ad27569f2951ce0ffd30a3321230db2fc727216"
dependencies = [
"phf_generator 0.11.2",
"phf_shared 0.11.2",
"proc-macro2",
"quote",
"syn 2.0.94",
]
[[package]]
name = "phf_shared"
version = "0.8.0"
@@ -5140,7 +5188,7 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6"
dependencies = [
"env_logger",
"env_logger 0.8.4",
"log",
"rand 0.8.5",
]
@@ -5205,7 +5253,7 @@ dependencies = [
"once_cell",
"socket2 0.5.5",
"tracing",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -5547,7 +5595,7 @@ dependencies = [
"wasm-streams",
"web-sys",
"webpki-roots 0.25.2",
"winreg",
"winreg 0.50.0",
]
[[package]]
@@ -6720,6 +6768,15 @@ dependencies = [
"unic-segment",
]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "terminal_size"
version = "0.1.17"
@@ -7847,6 +7904,15 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
@@ -7987,6 +8053,16 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "winreg"
version = "0.55.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb5a765337c50e9ec252c2069be9bf91c7df47afb103b642ba3a53bf8101be97"
dependencies = [
"cfg-if",
"windows-sys 0.59.0",
]
[[package]]
name = "write16"
version = "1.0.0"
+3 -3
View File
@@ -79,7 +79,7 @@ diesel = { version = "2.1.0", features = [
] }
uuid = { version = "1.5.0", features = ["serde", "v4", "v5"] }
serde_repr = "0.1"
futures = "0.3.29"
futures = "0.3.31"
tokio = "1.38.0"
tokio-stream = "0.1.14"
async-trait = "0.1.81"
@@ -152,5 +152,5 @@ collab-importer = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFl
# To update the commit ID, run:
# scripts/tool/update_local_ai_rev.sh new_rev_id
# ⚠️⚠️⚠️️
appflowy-local-ai = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "6f064efe232268f8d396edbb4b84d57fbb640f13" }
appflowy-plugin = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "6f064efe232268f8d396edbb4b84d57fbb640f13" }
appflowy-local-ai = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "87c47edf42e0bec0aecd8ab94de59a1b4be19f12" }
appflowy-plugin = { version = "0.1", git = "https://github.com/AppFlowy-IO/AppFlowy-LocalAI", rev = "87c47edf42e0bec0aecd8ab94de59a1b4be19f12" }
@@ -12,7 +12,7 @@ serde_json.workspace = true
flowy-ast.workspace = true
quote = "1.0"
cmd_lib = { version = "1.3.0", optional = true }
cmd_lib = { version = "1.9.5", optional = true }
protoc-rust = { version = "2.28.0", optional = true }
#protobuf-codegen = { version = "3.7.1" }
walkdir = { version = "2", optional = true }
@@ -22,7 +22,7 @@ tracing.workspace = true
async-trait.workspace = true
tokio = { workspace = true, features = ["sync"] }
lib-infra = { workspace = true }
futures = "0.3"
futures = "0.3.31"
arc-swap = "1.7"
flowy-sqlite = { workspace = true }
diesel.workspace = true
+1 -1
View File
@@ -44,7 +44,7 @@ collab-integrate = { workspace = true }
flowy-derive.workspace = true
serde_yaml = "0.9.27"
flowy-error = { workspace = true, features = ["impl_from_sqlite", "impl_from_dispatch_error", "impl_from_appflowy_cloud", "impl_from_reqwest", "impl_from_serde", "dart"] }
futures = "0.3.26"
futures = "0.3.31"
[features]
default = ["dart"]
@@ -55,7 +55,7 @@ tokio-postgres = { version = "0.7.8" }
chrono = "0.4.31"
zip.workspace = true
walkdir = "2.5.0"
futures = "0.3.30"
futures = "0.3.31"
flowy-ai-pub = { workspace = true }
[features]
+6
View File
@@ -47,9 +47,15 @@ pin-project = "1.1.5"
flowy-storage-pub = { workspace = true }
collab-integrate.workspace = true
[target.'cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))'.dependencies]
notify = "6.1.1"
[target.'cfg(target_os = "windows")'.dependencies]
winreg = "0.55"
#cmd_lib = { version = "1.9.5" }
[dev-dependencies]
dotenv = "0.15.0"
uuid.workspace = true
+18 -15
View File
@@ -3,7 +3,7 @@ use crate::entities::{
ChatInfoPB, ChatMessageListPB, ChatMessagePB, ChatSettingsPB, FilePB, PredefinedFormatPB,
RepeatedRelatedQuestionPB, StreamMessageParams,
};
use crate::local_ai::local_llm_chat::LocalAIController;
use crate::local_ai::controller::LocalAIController;
use crate::middleware::chat_service_mw::AICloudServiceMiddleware;
use crate::persistence::{insert_chat, read_chat_metadata, ChatTable};
use std::collections::HashMap;
@@ -58,7 +58,7 @@ pub struct AIManager {
pub user_service: Arc<dyn AIUserService>,
pub external_service: Arc<dyn AIExternalService>,
chats: Arc<DashMap<String, Arc<Chat>>>,
pub local_ai_controller: Arc<LocalAIController>,
pub local_ai: Arc<LocalAIController>,
store_preferences: Arc<KVStorePreferences>,
}
@@ -72,19 +72,23 @@ impl AIManager {
) -> AIManager {
let user_service = Arc::new(user_service);
let plugin_manager = Arc::new(PluginManager::new());
let local_ai_controller = Arc::new(LocalAIController::new(
let local_ai = Arc::new(LocalAIController::new(
plugin_manager.clone(),
store_preferences.clone(),
user_service.clone(),
chat_cloud_service.clone(),
));
let external_service = Arc::new(query_service);
// setup local chat service
let cloned_local_ai = local_ai.clone();
tokio::spawn(async move {
cloned_local_ai.observe_plugin_resource().await;
});
let external_service = Arc::new(query_service);
let cloud_service_wm = Arc::new(AICloudServiceMiddleware::new(
user_service.clone(),
chat_cloud_service,
local_ai_controller.clone(),
local_ai.clone(),
storage_service,
));
@@ -92,15 +96,14 @@ impl AIManager {
cloud_service_wm,
user_service,
chats: Arc::new(DashMap::new()),
local_ai_controller,
local_ai,
external_service,
store_preferences,
}
}
pub async fn initialize(&self, _workspace_id: &str) -> Result<(), FlowyError> {
// Ignore following error
let _ = self.local_ai_controller.refresh().await;
self.local_ai.reload().await?;
Ok(())
}
@@ -113,9 +116,9 @@ impl AIManager {
self.cloud_service_wm.clone(),
))
});
trace!("[AI Plugin] notify open chat: {}", chat_id);
if self.local_ai_controller.is_running() {
self.local_ai_controller.open_chat(chat_id);
if self.local_ai.is_running() {
trace!("[AI Plugin] notify open chat: {}", chat_id);
self.local_ai.open_chat(chat_id);
}
let user_service = self.user_service.clone();
@@ -146,7 +149,7 @@ impl AIManager {
pub async fn close_chat(&self, chat_id: &str) -> Result<(), FlowyError> {
trace!("close chat: {}", chat_id);
self.local_ai_controller.close_chat(chat_id);
self.local_ai.close_chat(chat_id);
Ok(())
}
@@ -154,9 +157,9 @@ impl AIManager {
if let Some((_, chat)) = self.chats.remove(chat_id) {
chat.close();
if self.local_ai_controller.is_running() {
if self.local_ai.is_running() {
info!("[AI Plugin] notify close chat: {}", chat_id);
self.local_ai_controller.close_chat(chat_id);
self.local_ai.close_chat(chat_id);
}
}
Ok(())
+59 -75
View File
@@ -1,8 +1,8 @@
use crate::local_ai::local_llm_chat::LLMModelInfo;
use appflowy_plugin::core::plugin::RunningState;
use std::collections::HashMap;
use crate::local_ai::local_llm_resource::PendingResource;
use crate::local_ai::controller::LocalAISetting;
use crate::local_ai::resource::PendingResource;
use flowy_ai_pub::cloud::{
ChatMessage, ChatMessageMetadata, ChatMessageType, LLMModel, OutputContent, OutputLayout,
RelatedQuestion, RepeatedChatMessage, RepeatedRelatedQuestion, ResponseFormat,
@@ -309,24 +309,6 @@ impl From<RepeatedRelatedQuestion> for RepeatedRelatedQuestionPB {
}
}
#[derive(Debug, Clone, Default, ProtoBuf)]
pub struct LLMModelInfoPB {
#[pb(index = 1)]
pub selected_model: LLMModelPB,
#[pb(index = 2)]
pub models: Vec<LLMModelPB>,
}
impl From<LLMModelInfo> for LLMModelInfoPB {
fn from(value: LLMModelInfo) -> Self {
LLMModelInfoPB {
selected_model: LLMModelPB::from(value.selected_model),
models: value.models.into_iter().map(LLMModelPB::from).collect(),
}
}
}
#[derive(Debug, Clone, Default, ProtoBuf)]
pub struct LLMModelPB {
#[pb(index = 1)]
@@ -423,17 +405,6 @@ pub struct ChatFilePB {
pub chat_id: String,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct DownloadLLMPB {
#[pb(index = 1)]
pub progress_stream: i64,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct DownloadTaskPB {
#[pb(index = 1)]
pub task_id: String,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct LocalModelStatePB {
#[pb(index = 1)]
@@ -452,18 +423,6 @@ pub struct LocalModelStatePB {
pub is_downloading: bool,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct LocalModelResourcePB {
#[pb(index = 1)]
pub is_ready: bool,
#[pb(index = 2)]
pub pending_resources: Vec<PendingResourcePB>,
#[pb(index = 3)]
pub is_downloading: bool,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct PendingResourcePB {
#[pb(index = 1)]
@@ -482,40 +441,33 @@ pub struct PendingResourcePB {
#[derive(Debug, Default, Clone, ProtoBuf_Enum, PartialEq, Eq, Copy)]
pub enum PendingResourceTypePB {
#[default]
OfflineApp = 0,
LocalAIAppRes = 0,
AIModel = 1,
}
impl From<PendingResource> for PendingResourceTypePB {
fn from(value: PendingResource) -> Self {
match value {
PendingResource::OfflineApp { .. } => PendingResourceTypePB::OfflineApp,
PendingResource::ModelInfoRes { .. } => PendingResourceTypePB::AIModel,
PendingResource::PluginExecutableNotReady { .. } => PendingResourceTypePB::LocalAIAppRes,
_ => PendingResourceTypePB::AIModel,
}
}
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct LocalAIPluginStatePB {
#[pb(index = 1)]
pub state: RunningStatePB,
#[pb(index = 2)]
pub offline_ai_ready: bool,
}
#[derive(Debug, Default, Clone, ProtoBuf_Enum, PartialEq, Eq, Copy)]
pub enum RunningStatePB {
#[default]
Connecting = 0,
Connected = 1,
Running = 2,
Stopped = 3,
ReadyToRun = 0,
Connecting = 1,
Connected = 2,
Running = 3,
Stopped = 4,
}
impl From<RunningState> for RunningStatePB {
fn from(value: RunningState) -> Self {
match value {
RunningState::ReadyToConnect => RunningStatePB::ReadyToRun,
RunningState::Connecting => RunningStatePB::Connecting,
RunningState::Connected { .. } => RunningStatePB::Connected,
RunningState::Running { .. } => RunningStatePB::Running,
@@ -529,28 +481,19 @@ impl From<RunningState> for RunningStatePB {
pub struct LocalAIPB {
#[pb(index = 1)]
pub enabled: bool,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct LocalAIChatPB {
#[pb(index = 1)]
pub enabled: bool,
#[pb(index = 2)]
pub file_enabled: bool,
pub is_plugin_executable_ready: bool,
#[pb(index = 3)]
pub plugin_state: LocalAIPluginStatePB,
#[pb(index = 3, one_of)]
pub lack_of_resource: Option<String>,
#[pb(index = 4)]
pub state: RunningStatePB,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct LocalModelStoragePB {
#[pb(index = 1)]
pub file_path: String,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct OfflineAIPB {
pub struct LocalAIAppLinkPB {
#[pb(index = 1)]
pub link: String,
}
@@ -636,3 +579,44 @@ impl From<PredefinedFormatPB> for ResponseFormat {
}
}
}
#[derive(Default, ProtoBuf, Validate, Clone, Debug)]
pub struct LocalAISettingPB {
#[pb(index = 1)]
#[validate(custom(function = "required_not_empty_str"))]
pub server_url: String,
#[pb(index = 2)]
#[validate(custom(function = "required_not_empty_str"))]
pub chat_model_name: String,
#[pb(index = 3)]
#[validate(custom(function = "required_not_empty_str"))]
pub embedding_model_name: String,
}
impl From<LocalAISetting> for LocalAISettingPB {
fn from(value: LocalAISetting) -> Self {
LocalAISettingPB {
server_url: value.ollama_server_url,
chat_model_name: value.chat_model_name,
embedding_model_name: value.embedding_model_name,
}
}
}
impl From<LocalAISettingPB> for LocalAISetting {
fn from(value: LocalAISettingPB) -> Self {
LocalAISetting {
ollama_server_url: value.server_url,
chat_model_name: value.chat_model_name,
embedding_model_name: value.embedding_model_name,
}
}
}
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct LackOfAIResourcePB {
#[pb(index = 1)]
pub resource_desc: String,
}
+35 -168
View File
@@ -4,15 +4,9 @@ use std::path::PathBuf;
use crate::ai_manager::AIManager;
use crate::completion::AICompletion;
use crate::entities::*;
use crate::local_ai::local_llm_chat::LLMModelInfo;
use crate::notification::{
chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,
};
use allo_isolate::Isolate;
use flowy_ai_pub::cloud::{ChatMessageMetadata, ChatMessageType, ChatRAGData, ContextLoader};
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use lib_dispatch::prelude::{data_result_ok, AFPluginData, AFPluginState, DataResult};
use lib_infra::isolate_stream::IsolateSink;
use serde_json::json;
use std::sync::{Arc, Weak};
use tracing::trace;
@@ -109,7 +103,7 @@ pub(crate) async fn regenerate_response_handler(
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn get_available_model_list_handler(
pub(crate) async fn get_model_list_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<ModelConfigPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
@@ -193,47 +187,6 @@ pub(crate) async fn stop_stream_handler(
Ok(())
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn refresh_local_ai_info_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LLMModelInfoPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let model_info = ai_manager.local_ai_controller.refresh_model_info().await;
if model_info.is_err() {
if let Some(llm_model) = ai_manager.local_ai_controller.get_current_model() {
let model_info = LLMModelInfo {
selected_model: llm_model.clone(),
models: vec![llm_model],
};
return data_result_ok(model_info.into());
}
}
data_result_ok(model_info?.into())
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn update_local_llm_model_handler(
data: AFPluginData<LLMModelPB>,
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalModelResourcePB, FlowyError> {
let data = data.into_inner();
let ai_manager = upgrade_ai_manager(ai_manager)?;
let state = ai_manager
.local_ai_controller
.select_local_llm(data.llm_id)
.await?;
data_result_ok(state)
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn get_local_llm_state_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalModelResourcePB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let state = ai_manager.local_ai_controller.get_local_llm_state().await?;
data_result_ok(state)
}
pub(crate) async fn start_complete_text_handler(
data: AFPluginData<CompleteTextPB>,
tools: AFPluginState<Arc<AICompletion>>,
@@ -301,108 +254,11 @@ pub(crate) async fn chat_file_handler(
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn download_llm_resource_handler(
data: AFPluginData<DownloadLLMPB>,
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<DownloadTaskPB, FlowyError> {
let data = data.into_inner();
let ai_manager = upgrade_ai_manager(ai_manager)?;
let text_sink = IsolateSink::new(Isolate::new(data.progress_stream));
let task_id = ai_manager
.local_ai_controller
.start_downloading(text_sink)
.await?;
data_result_ok(DownloadTaskPB { task_id })
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn cancel_download_llm_resource_handler(
pub(crate) async fn restart_local_ai_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> Result<(), FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
ai_manager.local_ai_controller.cancel_download()?;
Ok(())
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn get_plugin_state_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalAIPluginStatePB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let state = ai_manager.local_ai_controller.get_chat_plugin_state();
data_result_ok(state)
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn toggle_local_ai_chat_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalAIChatPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let enabled = ai_manager
.local_ai_controller
.toggle_local_ai_chat()
.await?;
let file_enabled = ai_manager.local_ai_controller.is_rag_enabled();
let plugin_state = ai_manager.local_ai_controller.get_chat_plugin_state();
let pb = LocalAIChatPB {
enabled,
file_enabled,
plugin_state,
};
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::UpdateLocalChatAI,
)
.payload(pb.clone())
.send();
data_result_ok(pb)
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn toggle_local_ai_chat_file_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalAIChatPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let enabled = ai_manager.local_ai_controller.is_chat_enabled();
let file_enabled = ai_manager
.local_ai_controller
.toggle_local_ai_chat_rag()
.await?;
let plugin_state = ai_manager.local_ai_controller.get_chat_plugin_state();
let pb = LocalAIChatPB {
enabled,
file_enabled,
plugin_state,
};
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::UpdateLocalChatAI,
)
.payload(pb.clone())
.send();
data_result_ok(pb)
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn get_local_ai_chat_state_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalAIChatPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let enabled = ai_manager.local_ai_controller.is_chat_enabled();
let file_enabled = ai_manager.local_ai_controller.is_rag_enabled();
let plugin_state = ai_manager.local_ai_controller.get_chat_plugin_state();
data_result_ok(LocalAIChatPB {
enabled,
file_enabled,
plugin_state,
})
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn restart_local_ai_chat_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> Result<(), FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
ai_manager.local_ai_controller.restart_chat_plugin();
ai_manager.local_ai.restart_plugin().await;
Ok(())
}
@@ -411,8 +267,9 @@ pub(crate) async fn toggle_local_ai_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalAIPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let enabled = ai_manager.local_ai_controller.toggle_local_ai().await?;
data_result_ok(LocalAIPB { enabled })
let _ = ai_manager.local_ai.toggle_local_ai().await?;
let state = ai_manager.local_ai.get_local_ai_state().await;
data_result_ok(state)
}
#[tracing::instrument(level = "debug", skip_all, err)]
@@ -420,31 +277,17 @@ pub(crate) async fn get_local_ai_state_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalAIPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let enabled = ai_manager.local_ai_controller.is_enabled();
data_result_ok(LocalAIPB { enabled })
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn get_model_storage_directory_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalModelStoragePB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let file_path = ai_manager
.local_ai_controller
.get_model_storage_directory()?;
data_result_ok(LocalModelStoragePB { file_path })
let state = ai_manager.local_ai.get_local_ai_state().await;
data_result_ok(state)
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn get_offline_app_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<OfflineAIPB, FlowyError> {
) -> DataResult<LocalAIAppLinkPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let link = ai_manager
.local_ai_controller
.get_offline_ai_app_download_link()
.await?;
data_result_ok(OfflineAIPB { link })
let link = ai_manager.local_ai.get_plugin_download_link().await?;
data_result_ok(LocalAIAppLinkPB { link })
}
#[tracing::instrument(level = "debug", skip_all, err)]
@@ -493,3 +336,27 @@ pub(crate) async fn update_chat_settings_handler(
Ok(())
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn get_local_ai_setting_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
) -> DataResult<LocalAISettingPB, FlowyError> {
let ai_manager = upgrade_ai_manager(ai_manager)?;
let setting = ai_manager.local_ai.get_local_ai_setting();
let pb = LocalAISettingPB::from(setting);
data_result_ok(pb)
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn update_local_ai_setting_handler(
ai_manager: AFPluginState<Weak<AIManager>>,
data: AFPluginData<LocalAISettingPB>,
) -> Result<(), FlowyError> {
let data = data.try_into_inner()?;
let ai_manager = upgrade_ai_manager(ai_manager)?;
ai_manager
.local_ai
.update_local_ai_setting(data.into())
.await?;
Ok(())
}
+15 -63
View File
@@ -23,47 +23,24 @@ pub fn init(ai_manager: Weak<AIManager>) -> AFPlugin {
.event(AIEvent::GetRelatedQuestion, get_related_question_handler)
.event(AIEvent::GetAnswerForQuestion, get_answer_handler)
.event(AIEvent::StopStream, stop_stream_handler)
.event(
AIEvent::RefreshLocalAIModelInfo,
refresh_local_ai_info_handler,
)
.event(AIEvent::UpdateLocalLLM, update_local_llm_model_handler)
.event(AIEvent::GetLocalLLMState, get_local_llm_state_handler)
.event(AIEvent::CompleteText, start_complete_text_handler)
.event(AIEvent::StopCompleteText, stop_complete_text_handler)
.event(AIEvent::ChatWithFile, chat_file_handler)
.event(AIEvent::DownloadLLMResource, download_llm_resource_handler)
.event(
AIEvent::CancelDownloadLLMResource,
cancel_download_llm_resource_handler,
)
.event(AIEvent::GetLocalAIPluginState, get_plugin_state_handler)
.event(AIEvent::ToggleLocalAIChat, toggle_local_ai_chat_handler)
.event(
AIEvent::GetLocalAIChatState,
get_local_ai_chat_state_handler,
)
.event(AIEvent::RestartLocalAIChat, restart_local_ai_chat_handler)
.event(AIEvent::RestartLocalAI, restart_local_ai_handler)
.event(AIEvent::ToggleLocalAI, toggle_local_ai_handler)
.event(AIEvent::GetLocalAIState, get_local_ai_state_handler)
.event(AIEvent::GetLocalAIDownloadLink, get_offline_app_handler)
.event(AIEvent::GetLocalAISetting, get_local_ai_setting_handler)
.event(
AIEvent::ToggleChatWithFile,
toggle_local_ai_chat_file_handler,
AIEvent::UpdateLocalAISetting,
update_local_ai_setting_handler,
)
.event(
AIEvent::GetModelStorageDirectory,
get_model_storage_directory_handler,
)
.event(AIEvent::GetOfflineAIAppLink, get_offline_app_handler)
.event(AIEvent::GetAvailableModels, get_model_list_handler)
.event(AIEvent::CreateChatContext, create_chat_context_handler)
.event(AIEvent::GetChatInfo, create_chat_context_handler)
.event(AIEvent::GetChatSettings, get_chat_settings_handler)
.event(AIEvent::UpdateChatSettings, update_chat_settings_handler)
.event(AIEvent::RegenerateResponse, regenerate_response_handler)
.event(
AIEvent::GetAvailableModels,
get_available_model_list_handler,
)
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
@@ -88,15 +65,6 @@ pub enum AIEvent {
#[event(input = "ChatMessageIdPB", output = "ChatMessagePB")]
GetAnswerForQuestion = 5,
#[event(input = "LLMModelPB", output = "LocalModelResourcePB")]
UpdateLocalLLM = 6,
#[event(output = "LocalModelResourcePB")]
GetLocalLLMState = 7,
#[event(output = "LLMModelInfoPB")]
RefreshLocalAIModelInfo = 8,
#[event(input = "CompleteTextPB", output = "CompleteTextTaskPB")]
CompleteText = 9,
@@ -106,26 +74,10 @@ pub enum AIEvent {
#[event(input = "ChatFilePB")]
ChatWithFile = 11,
#[event(input = "DownloadLLMPB", output = "DownloadTaskPB")]
DownloadLLMResource = 12,
#[event()]
CancelDownloadLLMResource = 13,
#[event(output = "LocalAIPluginStatePB")]
GetLocalAIPluginState = 14,
#[event(output = "LocalAIChatPB")]
ToggleLocalAIChat = 15,
/// Return Local AI Chat State
#[event(output = "LocalAIChatPB")]
GetLocalAIChatState = 16,
/// Restart local AI chat. When plugin quit or user terminate in task manager or activity monitor,
/// the plugin will need to restart.
#[event()]
RestartLocalAIChat = 17,
RestartLocalAI = 17,
/// Enable or disable local AI
#[event(output = "LocalAIPB")]
@@ -135,14 +87,8 @@ pub enum AIEvent {
#[event(output = "LocalAIPB")]
GetLocalAIState = 19,
#[event()]
ToggleChatWithFile = 20,
#[event(output = "LocalModelStoragePB")]
GetModelStorageDirectory = 21,
#[event(output = "OfflineAIPB")]
GetOfflineAIAppLink = 22,
#[event(output = "LocalAIAppLinkPB")]
GetLocalAIDownloadLink = 22,
#[event(input = "CreateChatContextPB")]
CreateChatContext = 23,
@@ -161,4 +107,10 @@ pub enum AIEvent {
#[event(output = "ModelConfigPB")]
GetAvailableModels = 28,
#[event(output = "LocalAISettingPB")]
GetLocalAISetting = 29,
#[event(input = "LocalAISettingPB")]
UpdateLocalAISetting = 30,
}
@@ -0,0 +1,558 @@
use crate::ai_manager::AIUserService;
use crate::entities::{LackOfAIResourcePB, LocalAIPB, RunningStatePB};
use crate::local_ai::resource::{LLMResourceService, LocalAIResourceController};
use crate::notification::{
chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,
};
use anyhow::Error;
use appflowy_plugin::manager::PluginManager;
use flowy_ai_pub::cloud::{ChatCloudService, ChatMessageMetadata, ContextLoader, LocalAIConfig};
use flowy_error::{FlowyError, FlowyResult};
use flowy_sqlite::kv::KVStorePreferences;
use futures::Sink;
use lib_infra::async_trait::async_trait;
use std::collections::HashMap;
use crate::local_ai::watch::is_plugin_ready;
use crate::stream_message::StreamMessage;
use appflowy_local_ai::ollama_plugin::OllamaAIPlugin;
use arc_swap::ArcSwapOption;
use futures_util::SinkExt;
use lib_infra::util::get_operating_system;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::select;
use tokio_stream::StreamExt;
use tracing::{debug, error, info, instrument, trace};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LocalAISetting {
pub ollama_server_url: String,
pub chat_model_name: String,
pub embedding_model_name: String,
}
impl Default for LocalAISetting {
fn default() -> Self {
Self {
ollama_server_url: "http://localhost:11434".to_string(),
chat_model_name: "llama3.1".to_string(),
embedding_model_name: "nomic-embed-text".to_string(),
}
}
}
const LOCAL_AI_SETTING_KEY: &str = "appflowy_local_ai_setting:v1";
pub struct LocalAIController {
ai_plugin: Arc<OllamaAIPlugin>,
resource: Arc<LocalAIResourceController>,
current_chat_id: ArcSwapOption<String>,
store_preferences: Arc<KVStorePreferences>,
user_service: Arc<dyn AIUserService>,
#[allow(dead_code)]
cloud_service: Arc<dyn ChatCloudService>,
}
impl Deref for LocalAIController {
type Target = Arc<OllamaAIPlugin>;
fn deref(&self) -> &Self::Target {
&self.ai_plugin
}
}
impl LocalAIController {
pub fn new(
plugin_manager: Arc<PluginManager>,
store_preferences: Arc<KVStorePreferences>,
user_service: Arc<dyn AIUserService>,
cloud_service: Arc<dyn ChatCloudService>,
) -> Self {
let local_ai = Arc::new(OllamaAIPlugin::new(plugin_manager));
let res_impl = LLMResourceServiceImpl {
user_service: user_service.clone(),
cloud_service: cloud_service.clone(),
store_preferences: store_preferences.clone(),
};
let local_ai_resource = Arc::new(LocalAIResourceController::new(
user_service.clone(),
res_impl,
));
let current_chat_id = ArcSwapOption::default();
let mut running_state_rx = local_ai.subscribe_running_state();
let cloned_llm_res = local_ai_resource.clone();
let cloned_store_preferences = store_preferences.clone();
let cloned_user_service = user_service.clone();
tokio::spawn(async move {
while let Some(state) = running_state_rx.next().await {
if let Ok(workspace_id) = cloned_user_service.workspace_id() {
let key = local_ai_enabled_key(&workspace_id);
info!("[AI Plugin] state: {:?}", state);
let ready = is_plugin_ready();
let lack_of_resource = cloned_llm_res.get_lack_of_resource().await;
let new_state = RunningStatePB::from(state);
let enabled = cloned_store_preferences.get_bool(&key).unwrap_or(true);
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::UpdateLocalAIState,
)
.payload(LocalAIPB {
enabled,
is_plugin_executable_ready: ready,
lack_of_resource,
state: new_state,
})
.send();
}
}
});
Self {
ai_plugin: local_ai,
resource: local_ai_resource,
current_chat_id,
store_preferences,
user_service,
cloud_service,
}
}
#[instrument(level = "debug", skip_all)]
pub async fn observe_plugin_resource(&self) {
debug!(
"[AI Plugin] init plugin when first run. thread: {:?}",
std::thread::current().id()
);
let sys = get_operating_system();
if !sys.is_desktop() {
return;
}
async fn try_init_plugin(
resource: &Arc<LocalAIResourceController>,
ai_plugin: &Arc<OllamaAIPlugin>,
) {
if let Err(err) = initialize_ai_plugin(ai_plugin, resource, None).await {
error!("[AI Plugin] failed to setup plugin: {:?}", err);
}
}
// Clone what is needed for the background task.
let resource_clone = self.resource.clone();
let ai_plugin_clone = self.ai_plugin.clone();
let mut resource_notify = self.resource.subscribe_resource_notify();
let mut app_state_watcher = self.resource.subscribe_app_state();
tokio::spawn(async move {
loop {
select! {
_ = app_state_watcher.recv() => {
info!("[AI Plugin] app state changed, try to init plugin");
try_init_plugin(&resource_clone, &ai_plugin_clone).await;
},
_ = resource_notify.recv() => {
info!("[AI Plugin] resource changed, try to init plugin");
try_init_plugin(&resource_clone, &ai_plugin_clone).await;
},
else => break,
}
}
});
}
pub async fn reload(&self) -> FlowyResult<()> {
let is_enabled = self.is_enabled();
self.toggle_plugin(is_enabled).await?;
Ok(())
}
/// Indicate whether the local AI plugin is running.
pub fn is_running(&self) -> bool {
if !self.is_enabled() {
return false;
}
self.ai_plugin.get_plugin_running_state().is_ready()
}
/// Indicate whether the local AI is enabled.
/// AppFlowy store the value in local storage isolated by workspace id. Each workspace can have
/// different settings.
pub fn is_enabled(&self) -> bool {
if let Ok(key) = self
.user_service
.workspace_id()
.map(|workspace_id| local_ai_enabled_key(&workspace_id))
{
self.store_preferences.get_bool(&key).unwrap_or(true)
} else {
false
}
}
pub fn open_chat(&self, chat_id: &str) {
if !self.is_enabled() {
return;
}
// Only keep one chat open at a time. Since loading multiple models at the same time will cause
// memory issues.
if let Some(current_chat_id) = self.current_chat_id.load().as_ref() {
debug!("[AI Plugin] close previous chat: {}", current_chat_id);
self.close_chat(current_chat_id);
}
self
.current_chat_id
.store(Some(Arc::new(chat_id.to_string())));
let chat_id = chat_id.to_string();
let weak_ctrl = Arc::downgrade(&self.ai_plugin);
tokio::spawn(async move {
if let Some(ctrl) = weak_ctrl.upgrade() {
if let Err(err) = ctrl.create_chat(&chat_id).await {
error!("[AI Plugin] failed to open chat: {:?}", err);
}
}
});
}
pub fn close_chat(&self, chat_id: &str) {
if !self.is_running() {
return;
}
info!("[AI Plugin] notify close chat: {}", chat_id);
let weak_ctrl = Arc::downgrade(&self.ai_plugin);
let chat_id = chat_id.to_string();
tokio::spawn(async move {
if let Some(ctrl) = weak_ctrl.upgrade() {
if let Err(err) = ctrl.close_chat(&chat_id).await {
error!("[AI Plugin] failed to close chat: {:?}", err);
}
}
});
}
pub fn get_local_ai_setting(&self) -> LocalAISetting {
self.resource.get_llm_setting()
}
pub async fn update_local_ai_setting(&self, setting: LocalAISetting) -> FlowyResult<()> {
info!(
"[AI Plugin] update local ai setting: {:?}, thread: {:?}",
setting,
std::thread::current().id()
);
self.resource.set_llm_setting(setting).await?;
self.reload().await?;
Ok(())
}
#[instrument(level = "debug", skip_all)]
pub async fn get_local_ai_state(&self) -> LocalAIPB {
let start = std::time::Instant::now();
let enabled = self.is_enabled();
let is_app_downloaded = is_plugin_ready();
let state = self.ai_plugin.get_plugin_running_state();
let lack_of_resource = self.resource.get_lack_of_resource().await;
let elapsed = start.elapsed();
debug!(
"[AI Plugin] get local ai state, elapsed: {:?}, thread: {:?}",
elapsed,
std::thread::current().id()
);
LocalAIPB {
enabled,
is_plugin_executable_ready: is_app_downloaded,
state: RunningStatePB::from(state),
lack_of_resource,
}
}
#[instrument(level = "debug", skip_all)]
pub async fn restart_plugin(&self) {
if let Err(err) = initialize_ai_plugin(&self.ai_plugin, &self.resource, None).await {
error!("[AI Plugin] failed to setup plugin: {:?}", err);
}
}
pub fn get_model_storage_directory(&self) -> FlowyResult<String> {
self
.resource
.user_model_folder()
.map(|path| path.to_string_lossy().to_string())
}
pub async fn get_plugin_download_link(&self) -> FlowyResult<String> {
self.resource.get_plugin_download_link().await
}
pub async fn toggle_local_ai(&self) -> FlowyResult<bool> {
let workspace_id = self.user_service.workspace_id()?;
let key = local_ai_enabled_key(&workspace_id);
let enabled = !self.store_preferences.get_bool(&key).unwrap_or(true);
self.store_preferences.set_bool(&key, enabled)?;
if self.resource.is_resource_ready().await {
self.toggle_plugin(enabled).await?;
}
Ok(enabled)
}
pub async fn index_message_metadata(
&self,
chat_id: &str,
metadata_list: &[ChatMessageMetadata],
index_process_sink: &mut (impl Sink<String> + Unpin),
) -> FlowyResult<()> {
if !self.is_enabled() {
return Ok(());
}
for metadata in metadata_list {
if let Err(err) = metadata.data.validate() {
error!(
"[AI Plugin] invalid metadata: {:?}, error: {:?}",
metadata, err
);
continue;
}
let mut index_metadata = HashMap::new();
index_metadata.insert("id".to_string(), json!(&metadata.id));
index_metadata.insert("name".to_string(), json!(&metadata.name));
index_metadata.insert("at_name".to_string(), json!(format!("@{}", &metadata.name)));
index_metadata.insert("source".to_string(), json!(&metadata.source));
match &metadata.data.content_type {
ContextLoader::Unknown => {
error!(
"[AI Plugin] unsupported content type: {:?}",
metadata.data.content_type
);
},
ContextLoader::Text | ContextLoader::Markdown => {
trace!("[AI Plugin]: index text: {}", metadata.data.content);
self
.process_index_file(
chat_id,
None,
Some(metadata.data.content.clone()),
metadata,
&index_metadata,
index_process_sink,
)
.await?;
},
ContextLoader::PDF => {
trace!("[AI Plugin]: index pdf file: {}", metadata.data.content);
let file_path = Path::new(&metadata.data.content);
if file_path.exists() {
self
.process_index_file(
chat_id,
Some(file_path.to_path_buf()),
None,
metadata,
&index_metadata,
index_process_sink,
)
.await?;
}
},
}
}
Ok(())
}
async fn process_index_file(
&self,
chat_id: &str,
file_path: Option<PathBuf>,
content: Option<String>,
metadata: &ChatMessageMetadata,
index_metadata: &HashMap<String, serde_json::Value>,
index_process_sink: &mut (impl Sink<String> + Unpin),
) -> Result<(), FlowyError> {
let _ = index_process_sink
.send(
StreamMessage::StartIndexFile {
file_name: metadata.name.clone(),
}
.to_string(),
)
.await;
let result = self
.index_file(chat_id, file_path, content, Some(index_metadata.clone()))
.await;
match result {
Ok(_) => {
let _ = index_process_sink
.send(
StreamMessage::EndIndexFile {
file_name: metadata.name.clone(),
}
.to_string(),
)
.await;
},
Err(err) => {
let _ = index_process_sink
.send(
StreamMessage::IndexFileError {
file_name: metadata.name.clone(),
}
.to_string(),
)
.await;
error!("[AI Plugin] failed to index file: {:?}", err);
},
}
Ok(())
}
#[instrument(level = "debug", skip_all)]
async fn toggle_plugin(&self, enabled: bool) -> FlowyResult<()> {
info!(
"[AI Plugin] enable: {}, thread id: {:?}",
enabled,
std::thread::current().id()
);
if enabled {
let (tx, rx) = tokio::sync::oneshot::channel();
if let Err(err) = initialize_ai_plugin(&self.ai_plugin, &self.resource, Some(tx)).await {
error!("[AI Plugin] failed to initialize local ai: {:?}", err);
}
let _ = rx.await;
} else {
if let Err(err) = self.ai_plugin.destroy_chat_plugin().await {
error!("[AI Plugin] failed to destroy plugin: {:?}", err);
}
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::UpdateLocalAIState,
)
.payload(LocalAIPB {
enabled,
is_plugin_executable_ready: true,
state: RunningStatePB::Stopped,
lack_of_resource: None,
})
.send();
}
Ok(())
}
}
#[instrument(level = "debug", skip_all, err)]
async fn initialize_ai_plugin(
plugin: &Arc<OllamaAIPlugin>,
llm_resource: &Arc<LocalAIResourceController>,
ret: Option<tokio::sync::oneshot::Sender<()>>,
) -> FlowyResult<()> {
let plugin = plugin.clone();
if plugin.get_plugin_running_state().is_loading() {
return Ok(());
}
let lack_of_resource = llm_resource.get_lack_of_resource().await;
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::UpdateLocalAIState,
)
.payload(LocalAIPB {
enabled: true,
is_plugin_executable_ready: true,
state: RunningStatePB::ReadyToRun,
lack_of_resource: lack_of_resource.clone(),
})
.send();
if let Some(lack_of_resource) = lack_of_resource {
info!(
"[AI Plugin] lack of resource: {:?} to initialize plugin, thread: {:?}",
lack_of_resource,
std::thread::current().id()
);
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::LocalAIResourceUpdated,
)
.payload(LackOfAIResourcePB {
resource_desc: lack_of_resource,
})
.send();
return Ok(());
}
let cloned_llm_res = llm_resource.clone();
tokio::task::spawn_blocking(move || {
futures::executor::block_on(async move {
match cloned_llm_res.get_plugin_config(true).await {
Ok(config) => {
info!(
"[AI Plugin] initialize plugin with config: {:?}, thread: {:?}",
config,
std::thread::current().id()
);
match plugin.init_plugin(config).await {
Ok(_) => {},
Err(err) => error!("[AI Plugin] failed to setup plugin: {:?}", err),
}
if let Some(ret) = ret {
let _ = ret.send(());
}
},
Err(err) => {
error!("[AI Plugin] failed to get plugin config: {:?}", err);
},
};
})
});
Ok(())
}
pub struct LLMResourceServiceImpl {
user_service: Arc<dyn AIUserService>,
cloud_service: Arc<dyn ChatCloudService>,
store_preferences: Arc<KVStorePreferences>,
}
#[async_trait]
impl LLMResourceService for LLMResourceServiceImpl {
async fn fetch_local_ai_config(&self) -> Result<LocalAIConfig, anyhow::Error> {
let workspace_id = self.user_service.workspace_id()?;
let config = self
.cloud_service
.get_local_ai_config(&workspace_id)
.await?;
Ok(config)
}
fn store_setting(&self, setting: LocalAISetting) -> Result<(), Error> {
self
.store_preferences
.set_object(LOCAL_AI_SETTING_KEY, &setting)?;
Ok(())
}
fn retrieve_setting(&self) -> Option<LocalAISetting> {
self
.store_preferences
.get_object::<LocalAISetting>(LOCAL_AI_SETTING_KEY)
}
}
const APPFLOWY_LOCAL_AI_ENABLED: &str = "appflowy_local_ai_enabled";
fn local_ai_enabled_key(workspace_id: &str) -> String {
format!("{}:{}", APPFLOWY_LOCAL_AI_ENABLED, workspace_id)
}
@@ -1,599 +0,0 @@
use crate::ai_manager::AIUserService;
use crate::entities::{LocalAIPluginStatePB, LocalModelResourcePB, RunningStatePB};
use crate::local_ai::local_llm_resource::{LLMResourceService, LocalAIResourceController};
use crate::notification::{
chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,
};
use anyhow::Error;
use appflowy_local_ai::chat_plugin::{AIPluginConfig, AppFlowyLocalAI};
use appflowy_plugin::manager::PluginManager;
use appflowy_plugin::util::is_apple_silicon;
use flowy_ai_pub::cloud::{
AppFlowyOfflineAI, ChatCloudService, ChatMessageMetadata, ContextLoader, LLMModel, LocalAIConfig,
SubscriptionPlan,
};
use flowy_error::{FlowyError, FlowyResult};
use flowy_sqlite::kv::KVStorePreferences;
use futures::Sink;
use lib_infra::async_trait::async_trait;
use std::collections::HashMap;
use crate::stream_message::StreamMessage;
use arc_swap::ArcSwapOption;
use futures_util::SinkExt;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::select;
use tokio_stream::StreamExt;
use tracing::{debug, error, info, instrument, trace, warn};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LLMSetting {
pub app: AppFlowyOfflineAI,
pub llm_model: LLMModel,
}
pub struct LLMModelInfo {
pub selected_model: LLMModel,
pub models: Vec<LLMModel>,
}
const APPFLOWY_LOCAL_AI_ENABLED: &str = "appflowy_local_ai_enabled";
const APPFLOWY_LOCAL_AI_CHAT_ENABLED: &str = "appflowy_local_ai_chat_enabled";
const APPFLOWY_LOCAL_AI_CHAT_RAG_ENABLED: &str = "appflowy_local_ai_chat_rag_enabled";
const LOCAL_AI_SETTING_KEY: &str = "appflowy_local_ai_setting:v0";
pub struct LocalAIController {
local_ai: Arc<AppFlowyLocalAI>,
local_ai_resource: Arc<LocalAIResourceController>,
current_chat_id: ArcSwapOption<String>,
store_preferences: Arc<KVStorePreferences>,
user_service: Arc<dyn AIUserService>,
cloud_service: Arc<dyn ChatCloudService>,
}
impl Deref for LocalAIController {
type Target = Arc<AppFlowyLocalAI>;
fn deref(&self) -> &Self::Target {
&self.local_ai
}
}
impl LocalAIController {
pub fn new(
plugin_manager: Arc<PluginManager>,
store_preferences: Arc<KVStorePreferences>,
user_service: Arc<dyn AIUserService>,
cloud_service: Arc<dyn ChatCloudService>,
) -> Self {
let local_ai = Arc::new(AppFlowyLocalAI::new(plugin_manager));
let res_impl = LLMResourceServiceImpl {
user_service: user_service.clone(),
cloud_service: cloud_service.clone(),
store_preferences: store_preferences.clone(),
};
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
let llm_res = Arc::new(LocalAIResourceController::new(
user_service.clone(),
res_impl,
tx,
));
let current_chat_id = ArcSwapOption::default();
let mut running_state_rx = local_ai.subscribe_running_state();
let cloned_llm_res = llm_res.clone();
tokio::spawn(async move {
while let Some(state) = running_state_rx.next().await {
info!("[AI Plugin] state: {:?}", state);
let offline_ai_ready = cloned_llm_res.is_offline_app_ready();
let new_state = RunningStatePB::from(state);
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::UpdateChatPluginState,
)
.payload(LocalAIPluginStatePB {
state: new_state,
offline_ai_ready,
})
.send();
}
});
let this = Self {
local_ai,
local_ai_resource: llm_res,
current_chat_id,
store_preferences,
user_service,
cloud_service,
};
let rag_enabled = this.is_rag_enabled();
let cloned_llm_chat = this.local_ai.clone();
let cloned_llm_res = this.local_ai_resource.clone();
let mut offline_ai_watch = this.local_ai_resource.subscribe_offline_app_state();
tokio::spawn(async move {
let init_fn = || {
if let Ok(chat_config) = cloned_llm_res.get_chat_config(rag_enabled) {
if let Err(err) = initialize_ai_plugin(&cloned_llm_chat, chat_config, None) {
error!("[AI Plugin] failed to setup plugin: {:?}", err);
}
}
};
loop {
select! {
_ = offline_ai_watch.recv() => {
init_fn();
},
_ = rx.recv() => {
init_fn();
},
else => { break; }
}
}
});
if this.can_init_plugin() {
let result = this
.local_ai_resource
.get_chat_config(this.is_rag_enabled());
if let Ok(chat_config) = result {
if let Err(err) = initialize_ai_plugin(&this.local_ai, chat_config, None) {
error!("[AI Plugin] failed to setup plugin: {:?}", err);
}
}
}
this
}
pub async fn refresh(&self) -> FlowyResult<()> {
let is_enabled = self.is_enabled();
self.enable_chat_plugin(is_enabled).await?;
if is_enabled {
let local_ai = self.local_ai.clone();
let workspace_id = self.user_service.workspace_id()?;
let cloned_service = self.cloud_service.clone();
let store_preferences = self.store_preferences.clone();
tokio::spawn(async move {
let key = local_ai_enabled_key(&workspace_id);
match cloned_service.get_workspace_plan(&workspace_id).await {
Ok(plans) => {
trace!("[AI Plugin] workspace:{} plans: {:?}", workspace_id, plans);
if !plans.contains(&SubscriptionPlan::AiLocal) {
info!(
"disable local ai plugin for workspace: {}. reason: no plan found",
workspace_id
);
let _ = store_preferences.set_bool(&key, false);
let _ = local_ai.destroy_chat_plugin().await;
}
},
Err(err) => {
warn!("[AI Plugin]: failed to get workspace plan: {:?}", err);
},
}
});
}
Ok(())
}
pub async fn refresh_model_info(&self) -> FlowyResult<LLMModelInfo> {
self.local_ai_resource.refresh_llm_resource().await
}
/// Returns true if the local AI is enabled and ready to use.
pub fn can_init_plugin(&self) -> bool {
self.is_enabled() && self.local_ai_resource.is_resource_ready()
}
/// Indicate whether the local AI plugin is running.
pub fn is_running(&self) -> bool {
if !self.is_enabled() {
return false;
}
self.local_ai.get_plugin_running_state().is_ready()
}
/// Indicate whether the local AI is enabled.
/// AppFlowy store the value in local storage isolated by workspace id. Each workspace can have
/// different settings.
pub fn is_enabled(&self) -> bool {
if let Ok(key) = self
.user_service
.workspace_id()
.map(|workspace_id| local_ai_enabled_key(&workspace_id))
{
self.store_preferences.get_bool(&key).unwrap_or(true)
} else {
false
}
}
/// Indicate whether the local AI chat is enabled. In the future, we can support multiple
/// AI plugin.
pub fn is_chat_enabled(&self) -> bool {
self
.store_preferences
.get_bool(APPFLOWY_LOCAL_AI_CHAT_ENABLED)
.unwrap_or(true)
}
pub fn is_rag_enabled(&self) -> bool {
self
.store_preferences
.get_bool(APPFLOWY_LOCAL_AI_CHAT_RAG_ENABLED)
.unwrap_or(true)
}
pub fn open_chat(&self, chat_id: &str) {
if !self.is_enabled() {
return;
}
// Only keep one chat open at a time. Since loading multiple models at the same time will cause
// memory issues.
if let Some(current_chat_id) = self.current_chat_id.load().as_ref() {
debug!("[AI Plugin] close previous chat: {}", current_chat_id);
self.close_chat(current_chat_id);
}
self
.current_chat_id
.store(Some(Arc::new(chat_id.to_string())));
let chat_id = chat_id.to_string();
let weak_ctrl = Arc::downgrade(&self.local_ai);
tokio::spawn(async move {
if let Some(ctrl) = weak_ctrl.upgrade() {
if let Err(err) = ctrl.create_chat(&chat_id).await {
error!("[AI Plugin] failed to open chat: {:?}", err);
}
}
});
}
pub fn close_chat(&self, chat_id: &str) {
if !self.is_running() {
return;
}
info!("[AI Plugin] notify close chat: {}", chat_id);
let weak_ctrl = Arc::downgrade(&self.local_ai);
let chat_id = chat_id.to_string();
tokio::spawn(async move {
if let Some(ctrl) = weak_ctrl.upgrade() {
if let Err(err) = ctrl.close_chat(&chat_id).await {
error!("[AI Plugin] failed to close chat: {:?}", err);
}
}
});
}
pub async fn select_local_llm(&self, llm_id: i64) -> FlowyResult<LocalModelResourcePB> {
if !self.is_enabled() {
return Err(FlowyError::local_ai_unavailable());
}
if let Some(model) = self.local_ai_resource.get_selected_model() {
if model.llm_id == llm_id {
return self.local_ai_resource.get_local_llm_state();
}
}
let state = self.local_ai_resource.use_local_llm(llm_id)?;
// Re-initialize the plugin if the setting is updated and ready to use
if self.local_ai_resource.is_resource_ready() {
let chat_config = self
.local_ai_resource
.get_chat_config(self.is_rag_enabled())?;
if let Err(err) = initialize_ai_plugin(&self.local_ai, chat_config, None) {
error!("failed to setup plugin: {:?}", err);
}
}
Ok(state)
}
pub async fn get_local_llm_state(&self) -> FlowyResult<LocalModelResourcePB> {
self.local_ai_resource.get_local_llm_state()
}
pub fn get_current_model(&self) -> Option<LLMModel> {
self.local_ai_resource.get_selected_model()
}
pub async fn start_downloading<T>(&self, progress_sink: T) -> FlowyResult<String>
where
T: Sink<String, Error = anyhow::Error> + Unpin + Sync + Send + 'static,
{
let task_id = self
.local_ai_resource
.start_downloading(progress_sink)
.await?;
Ok(task_id)
}
pub fn cancel_download(&self) -> FlowyResult<()> {
self.local_ai_resource.cancel_download()?;
Ok(())
}
pub fn get_chat_plugin_state(&self) -> LocalAIPluginStatePB {
if !self.is_enabled() {
return LocalAIPluginStatePB {
state: RunningStatePB::Stopped,
offline_ai_ready: false,
};
}
let offline_ai_ready = self.local_ai_resource.is_offline_app_ready();
let state = self.local_ai.get_plugin_running_state();
LocalAIPluginStatePB {
state: RunningStatePB::from(state),
offline_ai_ready,
}
}
pub fn restart_chat_plugin(&self) {
let rag_enabled = self.is_rag_enabled();
if let Ok(chat_config) = self.local_ai_resource.get_chat_config(rag_enabled) {
if let Err(err) = initialize_ai_plugin(&self.local_ai, chat_config, None) {
error!("[AI Plugin] failed to setup plugin: {:?}", err);
}
}
}
pub fn get_model_storage_directory(&self) -> FlowyResult<String> {
self
.local_ai_resource
.user_model_folder()
.map(|path| path.to_string_lossy().to_string())
}
pub async fn get_offline_ai_app_download_link(&self) -> FlowyResult<String> {
self
.local_ai_resource
.get_offline_ai_app_download_link()
.await
}
pub async fn toggle_local_ai(&self) -> FlowyResult<bool> {
let workspace_id = self.user_service.workspace_id()?;
let key = local_ai_enabled_key(&workspace_id);
let enabled = !self.store_preferences.get_bool(&key).unwrap_or(true);
self.store_preferences.set_bool(&key, enabled)?;
// when enable local ai. we need to check if chat is enabled, if enabled, we need to init chat plugin
// otherwise, we need to destroy the plugin
if enabled {
let chat_enabled = self
.store_preferences
.get_bool(APPFLOWY_LOCAL_AI_CHAT_ENABLED)
.unwrap_or(true);
if self.local_ai_resource.is_resource_ready() {
self.enable_chat_plugin(chat_enabled).await?;
}
} else {
let _ = self.enable_chat_plugin(false).await;
}
Ok(enabled)
}
pub async fn toggle_local_ai_chat(&self) -> FlowyResult<bool> {
let enabled = !self
.store_preferences
.get_bool(APPFLOWY_LOCAL_AI_CHAT_ENABLED)
.unwrap_or(true);
self
.store_preferences
.set_bool(APPFLOWY_LOCAL_AI_CHAT_ENABLED, enabled)?;
self.enable_chat_plugin(enabled).await?;
Ok(enabled)
}
pub async fn toggle_local_ai_chat_rag(&self) -> FlowyResult<bool> {
let enabled = !self
.store_preferences
.get_bool_or_default(APPFLOWY_LOCAL_AI_CHAT_RAG_ENABLED);
self
.store_preferences
.set_bool(APPFLOWY_LOCAL_AI_CHAT_RAG_ENABLED, enabled)?;
Ok(enabled)
}
pub async fn index_message_metadata(
&self,
chat_id: &str,
metadata_list: &[ChatMessageMetadata],
index_process_sink: &mut (impl Sink<String> + Unpin),
) -> FlowyResult<()> {
if !self.is_enabled() {
return Ok(());
}
for metadata in metadata_list {
if let Err(err) = metadata.data.validate() {
error!(
"[AI Plugin] invalid metadata: {:?}, error: {:?}",
metadata, err
);
continue;
}
let mut index_metadata = HashMap::new();
index_metadata.insert("id".to_string(), json!(&metadata.id));
index_metadata.insert("name".to_string(), json!(&metadata.name));
index_metadata.insert("at_name".to_string(), json!(format!("@{}", &metadata.name)));
index_metadata.insert("source".to_string(), json!(&metadata.source));
match &metadata.data.content_type {
ContextLoader::Unknown => {
error!(
"[AI Plugin] unsupported content type: {:?}",
metadata.data.content_type
);
},
ContextLoader::Text | ContextLoader::Markdown => {
trace!("[AI Plugin]: index text: {}", metadata.data.content);
self
.process_index_file(
chat_id,
None,
Some(metadata.data.content.clone()),
metadata,
&index_metadata,
index_process_sink,
)
.await?;
},
ContextLoader::PDF => {
trace!("[AI Plugin]: index pdf file: {}", metadata.data.content);
let file_path = Path::new(&metadata.data.content);
if file_path.exists() {
self
.process_index_file(
chat_id,
Some(file_path.to_path_buf()),
None,
metadata,
&index_metadata,
index_process_sink,
)
.await?;
}
},
}
}
Ok(())
}
async fn process_index_file(
&self,
chat_id: &str,
file_path: Option<PathBuf>,
content: Option<String>,
metadata: &ChatMessageMetadata,
index_metadata: &HashMap<String, serde_json::Value>,
index_process_sink: &mut (impl Sink<String> + Unpin),
) -> Result<(), FlowyError> {
let _ = index_process_sink
.send(
StreamMessage::StartIndexFile {
file_name: metadata.name.clone(),
}
.to_string(),
)
.await;
let result = self
.index_file(chat_id, file_path, content, Some(index_metadata.clone()))
.await;
match result {
Ok(_) => {
let _ = index_process_sink
.send(
StreamMessage::EndIndexFile {
file_name: metadata.name.clone(),
}
.to_string(),
)
.await;
},
Err(err) => {
let _ = index_process_sink
.send(
StreamMessage::IndexFileError {
file_name: metadata.name.clone(),
}
.to_string(),
)
.await;
error!("[AI Plugin] failed to index file: {:?}", err);
},
}
Ok(())
}
async fn enable_chat_plugin(&self, enabled: bool) -> FlowyResult<()> {
info!("[AI Plugin] enable chat plugin: {}", enabled);
if enabled {
let (tx, rx) = tokio::sync::oneshot::channel();
let chat_config = self
.local_ai_resource
.get_chat_config(self.is_rag_enabled())?;
if let Err(err) = initialize_ai_plugin(&self.local_ai, chat_config, Some(tx)) {
error!("[AI Plugin] failed to initialize local ai: {:?}", err);
}
let _ = rx.await;
} else if let Err(err) = self.local_ai.destroy_chat_plugin().await {
error!("[AI Plugin] failed to destroy plugin: {:?}", err);
}
Ok(())
}
}
#[instrument(level = "debug", skip_all, err)]
fn initialize_ai_plugin(
llm_chat: &Arc<AppFlowyLocalAI>,
mut chat_config: AIPluginConfig,
ret: Option<tokio::sync::oneshot::Sender<()>>,
) -> FlowyResult<()> {
let llm_chat = llm_chat.clone();
tokio::spawn(async move {
info!("[AI Plugin] config: {:?}", chat_config);
if is_apple_silicon().await.unwrap_or(false) {
chat_config = chat_config.with_device("gpu");
}
match llm_chat.init_chat_plugin(chat_config).await {
Ok(_) => {},
Err(err) => error!("[AI Plugin] failed to setup plugin: {:?}", err),
}
if let Some(ret) = ret {
let _ = ret.send(());
}
});
Ok(())
}
pub struct LLMResourceServiceImpl {
user_service: Arc<dyn AIUserService>,
cloud_service: Arc<dyn ChatCloudService>,
store_preferences: Arc<KVStorePreferences>,
}
#[async_trait]
impl LLMResourceService for LLMResourceServiceImpl {
async fn fetch_local_ai_config(&self) -> Result<LocalAIConfig, anyhow::Error> {
let workspace_id = self.user_service.workspace_id()?;
let config = self
.cloud_service
.get_local_ai_config(&workspace_id)
.await?;
Ok(config)
}
fn store_setting(&self, setting: LLMSetting) -> Result<(), Error> {
self
.store_preferences
.set_object(LOCAL_AI_SETTING_KEY, &setting)?;
Ok(())
}
fn retrieve_setting(&self) -> Option<LLMSetting> {
self
.store_preferences
.get_object::<LLMSetting>(LOCAL_AI_SETTING_KEY)
}
}
fn local_ai_enabled_key(workspace_id: &str) -> String {
format!("{}:{}", APPFLOWY_LOCAL_AI_ENABLED, workspace_id)
}
@@ -1,534 +0,0 @@
use crate::ai_manager::AIUserService;
use crate::entities::{LocalModelResourcePB, PendingResourcePB, PendingResourceTypePB};
use crate::local_ai::local_llm_chat::{LLMModelInfo, LLMSetting};
use crate::local_ai::model_request::download_model;
use appflowy_local_ai::chat_plugin::AIPluginConfig;
use flowy_ai_pub::cloud::{LLMModel, LocalAIConfig, ModelInfo};
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use futures::Sink;
use futures_util::SinkExt;
use lib_infra::async_trait::async_trait;
use arc_swap::ArcSwapOption;
use lib_infra::util::{get_operating_system, OperatingSystem};
use std::path::PathBuf;
use std::sync::Arc;
use crate::local_ai::watch::offline_app_path;
#[cfg(target_os = "macos")]
use crate::local_ai::watch::{watch_offline_app, WatchContext};
use tokio::fs::{self};
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, instrument, trace, warn};
#[async_trait]
pub trait LLMResourceService: Send + Sync + 'static {
/// Get local ai configuration from remote server
async fn fetch_local_ai_config(&self) -> Result<LocalAIConfig, anyhow::Error>;
fn store_setting(&self, setting: LLMSetting) -> Result<(), anyhow::Error>;
fn retrieve_setting(&self) -> Option<LLMSetting>;
}
const LLM_MODEL_DIR: &str = "models";
const DOWNLOAD_FINISH: &str = "finish";
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum WatchDiskEvent {
Create,
Remove,
}
pub enum PendingResource {
OfflineApp,
ModelInfoRes(Vec<ModelInfo>),
}
#[derive(Clone)]
pub struct DownloadTask {
cancel_token: CancellationToken,
tx: tokio::sync::broadcast::Sender<String>,
}
impl DownloadTask {
pub fn new() -> Self {
let (tx, _) = tokio::sync::broadcast::channel(100);
let cancel_token = CancellationToken::new();
Self { cancel_token, tx }
}
pub fn cancel(&self) {
self.cancel_token.cancel();
}
}
pub struct LocalAIResourceController {
user_service: Arc<dyn AIUserService>,
resource_service: Arc<dyn LLMResourceService>,
llm_setting: ArcSwapOption<LLMSetting>,
// The ai_config will be set when user try to get latest local ai config from server
ai_config: ArcSwapOption<LocalAIConfig>,
download_task: Arc<ArcSwapOption<DownloadTask>>,
resource_notify: tokio::sync::mpsc::Sender<()>,
#[cfg(target_os = "macos")]
#[allow(dead_code)]
offline_app_disk_watch: Option<WatchContext>,
offline_app_state_sender: tokio::sync::broadcast::Sender<WatchDiskEvent>,
}
impl LocalAIResourceController {
pub fn new(
user_service: Arc<dyn AIUserService>,
resource_service: impl LLMResourceService,
resource_notify: tokio::sync::mpsc::Sender<()>,
) -> Self {
let (offline_app_state_sender, _) = tokio::sync::broadcast::channel(1);
let llm_setting = resource_service.retrieve_setting().map(Arc::new);
#[cfg(target_os = "macos")]
let mut offline_app_disk_watch: Option<WatchContext> = None;
#[cfg(target_os = "macos")]
{
match watch_offline_app() {
Ok((new_watcher, mut rx)) => {
let sender = offline_app_state_sender.clone();
tokio::spawn(async move {
while let Some(event) = rx.recv().await {
if let Err(err) = sender.send(event) {
error!("[LLM Resource] Failed to send offline app state: {:?}", err);
}
}
});
offline_app_disk_watch = Some(new_watcher);
},
Err(err) => {
error!("[LLM Resource] Failed to watch offline app path: {:?}", err);
},
}
}
Self {
user_service,
resource_service: Arc::new(resource_service),
llm_setting: ArcSwapOption::new(llm_setting),
ai_config: Default::default(),
download_task: Default::default(),
resource_notify,
#[cfg(target_os = "macos")]
offline_app_disk_watch,
offline_app_state_sender,
}
}
#[allow(dead_code)]
pub fn subscribe_offline_app_state(&self) -> tokio::sync::broadcast::Receiver<WatchDiskEvent> {
self.offline_app_state_sender.subscribe()
}
fn set_llm_setting(&self, llm_setting: LLMSetting) {
self.llm_setting.store(Some(llm_setting.into()));
}
/// Returns true when all resources are downloaded and ready to use.
pub fn is_resource_ready(&self) -> bool {
match self.calculate_pending_resources() {
Ok(res) => res.is_empty(),
Err(_) => false,
}
}
pub fn is_offline_app_ready(&self) -> bool {
offline_app_path().exists()
}
pub async fn get_offline_ai_app_download_link(&self) -> FlowyResult<String> {
let ai_config = self.fetch_ai_config().await?;
Ok(ai_config.plugin.url)
}
/// Retrieves model information and updates the current model settings.
#[instrument(level = "debug", skip_all, err)]
pub async fn refresh_llm_resource(&self) -> FlowyResult<LLMModelInfo> {
let ai_config = self.fetch_ai_config().await?;
if ai_config.models.is_empty() {
return Err(FlowyError::local_ai().with_context("No model found"));
}
self.ai_config.store(Some(ai_config.clone().into()));
let selected_model = self.select_model(&ai_config)?;
let llm_setting = LLMSetting {
app: ai_config.plugin.clone(),
llm_model: selected_model.clone(),
};
self.set_llm_setting(llm_setting.clone());
self.resource_service.store_setting(llm_setting)?;
Ok(LLMModelInfo {
selected_model,
models: ai_config.models,
})
}
#[instrument(level = "info", skip_all, err)]
pub fn use_local_llm(&self, llm_id: i64) -> FlowyResult<LocalModelResourcePB> {
let (app, llm_model) = self
.ai_config
.load()
.as_ref()
.and_then(|config| {
config
.models
.iter()
.find(|model| model.llm_id == llm_id)
.cloned()
.map(|model| (config.plugin.clone(), model))
})
.ok_or_else(|| FlowyError::local_ai().with_context("No local ai config found"))?;
let llm_setting = LLMSetting {
app,
llm_model: llm_model.clone(),
};
trace!("[LLM Resource] Selected AI setting: {:?}", llm_setting);
self.set_llm_setting(llm_setting.clone());
self.resource_service.store_setting(llm_setting)?;
self.get_local_llm_state()
}
pub fn get_local_llm_state(&self) -> FlowyResult<LocalModelResourcePB> {
let state = self
.check_resource()
.ok_or_else(|| FlowyError::local_ai().with_context("No local ai config found"))?;
Ok(state)
}
#[instrument(level = "debug", skip_all)]
fn check_resource(&self) -> Option<LocalModelResourcePB> {
trace!("[LLM Resource] Checking local ai resources");
let pending_resources = self.calculate_pending_resources().ok()?;
let is_ready = pending_resources.is_empty();
let is_downloading = self.download_task.load().is_some();
let pending_resources: Vec<_> = pending_resources
.into_iter()
.flat_map(|res| match res {
PendingResource::OfflineApp => vec![PendingResourcePB {
name: "AppFlowy Plugin".to_string(),
file_size: "0 GB".to_string(),
requirements: "".to_string(),
res_type: PendingResourceTypePB::OfflineApp,
}],
PendingResource::ModelInfoRes(model_infos) => model_infos
.into_iter()
.map(|model_info| PendingResourcePB {
name: model_info.name,
file_size: bytes_to_readable_format(model_info.file_size as u64),
requirements: model_info.requirements,
res_type: PendingResourceTypePB::AIModel,
})
.collect::<Vec<_>>(),
})
.collect();
let resource = LocalModelResourcePB {
is_ready,
pending_resources,
is_downloading,
};
debug!("[LLM Resource] Local AI resources state: {:?}", resource);
Some(resource)
}
/// Returns true when all resources are downloaded and ready to use.
pub fn calculate_pending_resources(&self) -> FlowyResult<Vec<PendingResource>> {
match self.llm_setting.load().as_ref() {
None => Err(FlowyError::local_ai().with_context("Can't find any llm config")),
Some(llm_setting) => {
let mut resources = vec![];
let app_path = offline_app_path();
if !app_path.exists() {
trace!("[LLM Resource] offline app not found: {:?}", app_path);
resources.push(PendingResource::OfflineApp);
}
let chat_model = self.model_path(&llm_setting.llm_model.chat_model.file_name)?;
if !chat_model.exists() {
resources.push(PendingResource::ModelInfoRes(vec![llm_setting
.llm_model
.chat_model
.clone()]));
}
let embedding_model = self.model_path(&llm_setting.llm_model.embedding_model.file_name)?;
if !embedding_model.exists() {
resources.push(PendingResource::ModelInfoRes(vec![llm_setting
.llm_model
.embedding_model
.clone()]));
}
Ok(resources)
},
}
}
#[instrument(level = "info", skip_all, err)]
pub async fn start_downloading<T>(&self, mut progress_sink: T) -> FlowyResult<String>
where
T: Sink<String, Error = anyhow::Error> + Unpin + Sync + Send + 'static,
{
let task_id = uuid::Uuid::new_v4().to_string();
let weak_download_task = Arc::downgrade(&self.download_task);
let resource_notify = self.resource_notify.clone();
// notify download progress to client.
let progress_notify = |mut rx: tokio::sync::broadcast::Receiver<String>| {
tokio::spawn(async move {
while let Ok(value) = rx.recv().await {
let is_finish = value == DOWNLOAD_FINISH;
if let Err(err) = progress_sink.send(value).await {
warn!("Failed to send progress: {:?}", err);
break;
}
if is_finish {
info!("notify download finish, need to reload resources");
let _ = resource_notify.send(()).await;
if let Some(download_task) = weak_download_task.upgrade() {
if let Some(task) = download_task.swap(None) {
task.cancel();
}
}
break;
}
}
});
};
// return immediately if download task already exists
{
let guard = self.download_task.load();
if let Some(download_task) = &*guard {
trace!(
"Download task already exists, return the task id: {}",
task_id
);
progress_notify(download_task.tx.subscribe());
return Ok(task_id);
}
}
// If download task is not exists, create a new download task.
info!("[LLM Resource] Start new download task");
let llm_setting = self
.llm_setting
.load_full()
.ok_or_else(|| FlowyError::local_ai().with_context("No local ai config found"))?;
let download_task = Arc::new(DownloadTask::new());
self.download_task.store(Some(download_task.clone()));
progress_notify(download_task.tx.subscribe());
let model_dir = self.user_model_folder()?;
if !model_dir.exists() {
fs::create_dir_all(&model_dir).await.map_err(|err| {
FlowyError::local_ai().with_context(format!("Failed to create model dir: {:?}", err))
})?;
}
tokio::spawn(async move {
// After download the plugin, start downloading models
let chat_model_file = (
model_dir.join(&llm_setting.llm_model.chat_model.file_name),
&llm_setting.llm_model.chat_model.file_name,
&llm_setting.llm_model.chat_model.name,
&llm_setting.llm_model.chat_model.download_url,
);
let embedding_model_file = (
model_dir.join(&llm_setting.llm_model.embedding_model.file_name),
&llm_setting.llm_model.embedding_model.file_name,
&llm_setting.llm_model.embedding_model.name,
&llm_setting.llm_model.embedding_model.download_url,
);
for (file_path, file_name, model_name, url) in [chat_model_file, embedding_model_file] {
if file_path.exists() {
continue;
}
info!("[LLM Resource] Downloading model: {:?}", file_name);
let plugin_progress_tx = download_task.tx.clone();
let cloned_model_name = model_name.clone();
let progress = Arc::new(move |downloaded, total_size| {
let progress = (downloaded as f64 / total_size as f64).clamp(0.0, 1.0);
if plugin_progress_tx.receiver_count() == 0 {
return;
}
if let Err(err) =
plugin_progress_tx.send(format!("{}:progress:{}", cloned_model_name, progress))
{
warn!("Failed to send progress: {:?}", err);
}
});
match download_model(
url,
&model_dir,
file_name,
Some(progress),
Some(download_task.cancel_token.clone()),
)
.await
{
Ok(_) => info!("[LLM Resource] Downloaded model: {:?}", file_name),
Err(err) => {
error!(
"[LLM Resource] Failed to download model for given url: {:?}, error: {:?}",
url, err
);
download_task
.tx
.send(format!("error:failed to download {}", model_name))?;
continue;
},
}
}
info!("[LLM Resource] All resources downloaded");
download_task.tx.send(DOWNLOAD_FINISH.to_string())?;
Ok::<_, anyhow::Error>(())
});
Ok(task_id)
}
pub fn cancel_download(&self) -> FlowyResult<()> {
if let Some(cancel_token) = self.download_task.swap(None) {
info!("[LLM Resource] Cancel download");
cancel_token.cancel();
}
Ok(())
}
#[instrument(level = "info", skip_all)]
pub fn get_chat_config(&self, rag_enabled: bool) -> FlowyResult<AIPluginConfig> {
if !self.is_resource_ready() {
return Err(FlowyError::local_ai().with_context("Local AI resources are not ready"));
}
let llm_setting = self
.llm_setting
.load_full()
.ok_or_else(|| FlowyError::local_ai().with_context("No local llm setting found"))?;
let model_dir = self.user_model_folder()?;
let bin_path = match get_operating_system() {
OperatingSystem::MacOS => {
let path = offline_app_path();
if !path.exists() {
return Err(FlowyError::new(
ErrorCode::AIOfflineNotInstalled,
format!("AppFlowy Offline not installed at path: {:?}", path),
));
}
path
},
_ => {
return Err(
FlowyError::local_ai_unavailable()
.with_context("Local AI not available on current platform"),
);
},
};
let chat_model_path = model_dir.join(&llm_setting.llm_model.chat_model.file_name);
let mut config = AIPluginConfig::new(bin_path, chat_model_path)?;
if rag_enabled {
let resource_dir = self.resource_dir()?;
let embedding_model_path = model_dir.join(&llm_setting.llm_model.embedding_model.file_name);
let persist_directory = resource_dir.join("vectorstore");
if !persist_directory.exists() {
std::fs::create_dir_all(&persist_directory)?;
}
config.set_rag_enabled(&embedding_model_path, &persist_directory)?;
}
if cfg!(debug_assertions) {
config = config.with_verbose(true);
}
trace!("[AI Chat] use config: {:?}", config);
Ok(config)
}
/// Fetches the local AI configuration from the resource service.
async fn fetch_ai_config(&self) -> FlowyResult<LocalAIConfig> {
self
.resource_service
.fetch_local_ai_config()
.await
.map_err(|err| {
error!("[LLM Resource] Failed to fetch local ai config: {:?}", err);
FlowyError::local_ai()
.with_context("Can't retrieve model info. Please try again later".to_string())
})
}
pub fn get_selected_model(&self) -> Option<LLMModel> {
let setting = self.llm_setting.load();
Some(setting.as_ref()?.llm_model.clone())
}
/// Selects the appropriate model based on the current settings or defaults to the first model.
fn select_model(&self, ai_config: &LocalAIConfig) -> FlowyResult<LLMModel> {
let llm_setting = self.llm_setting.load();
let selected_model = match &*llm_setting {
None => ai_config.models[0].clone(),
Some(llm_setting) => {
match ai_config
.models
.iter()
.find(|model| model.llm_id == llm_setting.llm_model.llm_id)
{
None => ai_config.models[0].clone(),
Some(llm_model) => {
if llm_model != &llm_setting.llm_model {
info!(
"[LLM Resource] existing model is different from remote, replace with remote model"
);
}
llm_model.clone()
},
}
},
};
Ok(selected_model)
}
pub(crate) fn user_model_folder(&self) -> FlowyResult<PathBuf> {
self.resource_dir().map(|dir| dir.join(LLM_MODEL_DIR))
}
fn model_path(&self, model_file_name: &str) -> FlowyResult<PathBuf> {
self
.user_model_folder()
.map(|dir| dir.join(model_file_name))
}
pub(crate) fn resource_dir(&self) -> FlowyResult<PathBuf> {
let user_data_dir = self.user_service.application_root_dir()?;
Ok(user_data_dir.join("ai"))
}
}
fn bytes_to_readable_format(bytes: u64) -> String {
const BYTES_IN_GIGABYTE: u64 = 1024 * 1024 * 1024;
const BYTES_IN_MEGABYTE: u64 = 1024 * 1024;
if bytes >= BYTES_IN_GIGABYTE {
let gigabytes = (bytes as f64) / (BYTES_IN_GIGABYTE as f64);
format!("{:.1} GB", gigabytes)
} else {
let megabytes = (bytes as f64) / (BYTES_IN_MEGABYTE as f64);
format!("{:.2} MB", megabytes)
}
}
@@ -1,6 +1,6 @@
pub mod local_llm_chat;
pub mod local_llm_resource;
mod model_request;
pub mod controller;
mod request;
pub mod resource;
pub mod stream_util;
pub mod watch;
@@ -12,6 +12,7 @@ use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
use tokio_util::sync::CancellationToken;
use tracing::{instrument, trace};
#[allow(dead_code)]
type ProgressCallback = Arc<dyn Fn(u64, u64) + Send + Sync>;
#[instrument(level = "trace", skip_all, err)]
@@ -95,6 +96,7 @@ pub async fn download_model(
Ok(download_path)
}
#[allow(dead_code)]
async fn make_request(
client: &Client,
url: &str,
@@ -0,0 +1,311 @@
use crate::ai_manager::AIUserService;
use crate::local_ai::controller::LocalAISetting;
use flowy_ai_pub::cloud::LocalAIConfig;
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use lib_infra::async_trait::async_trait;
use crate::entities::LackOfAIResourcePB;
use crate::local_ai::watch::{is_plugin_ready, ollama_plugin_path};
#[cfg(target_os = "macos")]
use crate::local_ai::watch::{watch_offline_app, WatchContext};
use crate::notification::{
chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,
};
use appflowy_local_ai::ollama_plugin::OllamaPluginConfig;
use lib_infra::util::{get_operating_system, OperatingSystem};
use reqwest::Client;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tracing::{error, info, instrument, trace};
#[async_trait]
pub trait LLMResourceService: Send + Sync + 'static {
/// Get local ai configuration from remote server
async fn fetch_local_ai_config(&self) -> Result<LocalAIConfig, anyhow::Error>;
fn store_setting(&self, setting: LocalAISetting) -> Result<(), anyhow::Error>;
fn retrieve_setting(&self) -> Option<LocalAISetting>;
}
const LLM_MODEL_DIR: &str = "models";
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum WatchDiskEvent {
Create,
Remove,
}
pub enum PendingResource {
PluginExecutableNotReady,
OllamaServerNotReady,
MissingModel(String),
}
impl PendingResource {
pub fn desc(self) -> String {
match self {
PendingResource::PluginExecutableNotReady => "The Local AI app was not installed correctly. Please follow the instructions to install the Local AI application".to_string(),
PendingResource::OllamaServerNotReady => "Ollama is not ready. Please follow the instructions to install Ollama".to_string(),
PendingResource::MissingModel(model) => format!("Cannot find the model: {}. Please use the ollama pull command to install the model", model),
}
}
}
pub struct LocalAIResourceController {
user_service: Arc<dyn AIUserService>,
resource_service: Arc<dyn LLMResourceService>,
resource_notify: tokio::sync::broadcast::Sender<()>,
#[cfg(target_os = "macos")]
#[allow(dead_code)]
app_disk_watch: Option<WatchContext>,
app_state_sender: tokio::sync::broadcast::Sender<WatchDiskEvent>,
}
impl LocalAIResourceController {
pub fn new(
user_service: Arc<dyn AIUserService>,
resource_service: impl LLMResourceService,
) -> Self {
let (resource_notify, _) = tokio::sync::broadcast::channel(1);
let (app_state_sender, _) = tokio::sync::broadcast::channel(1);
#[cfg(target_os = "macos")]
let mut offline_app_disk_watch: Option<WatchContext> = None;
#[cfg(target_os = "macos")]
{
match watch_offline_app() {
Ok((new_watcher, mut rx)) => {
let sender = app_state_sender.clone();
tokio::spawn(async move {
while let Some(event) = rx.recv().await {
if let Err(err) = sender.send(event) {
error!("[LLM Resource] Failed to send offline app state: {:?}", err);
}
}
});
offline_app_disk_watch = Some(new_watcher);
},
Err(err) => {
error!("[LLM Resource] Failed to watch offline app path: {:?}", err);
},
}
}
Self {
user_service,
resource_service: Arc::new(resource_service),
#[cfg(target_os = "macos")]
app_disk_watch: offline_app_disk_watch,
app_state_sender,
resource_notify,
}
}
pub fn subscribe_resource_notify(&self) -> tokio::sync::broadcast::Receiver<()> {
self.resource_notify.subscribe()
}
pub fn subscribe_app_state(&self) -> tokio::sync::broadcast::Receiver<WatchDiskEvent> {
self.app_state_sender.subscribe()
}
/// Returns true when all resources are downloaded and ready to use.
pub async fn is_resource_ready(&self) -> bool {
let sys = get_operating_system();
if !sys.is_desktop() {
return false;
}
match self.calculate_pending_resources().await {
Ok(res) => res.is_empty(),
Err(_) => false,
}
}
pub async fn get_plugin_download_link(&self) -> FlowyResult<String> {
let ai_config = self.get_local_ai_configuration().await?;
Ok(ai_config.plugin.url)
}
/// Retrieves model information and updates the current model settings.
pub fn get_llm_setting(&self) -> LocalAISetting {
self.resource_service.retrieve_setting().unwrap_or_default()
}
#[instrument(level = "info", skip_all, err)]
pub async fn set_llm_setting(&self, setting: LocalAISetting) -> FlowyResult<()> {
self.resource_service.store_setting(setting)?;
let mut resources = self.calculate_pending_resources().await?;
if let Some(resource) = resources.pop() {
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::LocalAIResourceUpdated,
)
.payload(LackOfAIResourcePB {
resource_desc: resource.desc(),
})
.send();
}
Ok(())
}
pub async fn get_lack_of_resource(&self) -> Option<String> {
let mut resources = self.calculate_pending_resources().await.ok()?;
resources.pop().map(|r| r.desc())
}
/// Returns true when all resources are downloaded and ready to use.
pub async fn calculate_pending_resources(&self) -> FlowyResult<Vec<PendingResource>> {
let mut resources = vec![];
let app_path = ollama_plugin_path();
if !is_plugin_ready() {
trace!("[LLM Resource] offline app not found: {:?}", app_path);
resources.push(PendingResource::PluginExecutableNotReady);
return Ok(resources);
}
let setting = self.get_llm_setting();
let client = Client::builder().timeout(Duration::from_secs(5)).build()?;
match client.get(&setting.ollama_server_url).send().await {
Ok(resp) if resp.status().is_success() => {
info!(
"[LLM Resource] Ollama server is running at {}",
setting.ollama_server_url
);
},
_ => {
info!(
"[LLM Resource] Ollama server is not responding at {}",
setting.ollama_server_url
);
resources.push(PendingResource::OllamaServerNotReady);
return Ok(resources);
},
}
let required_models = vec![
setting.chat_model_name,
setting.embedding_model_name,
// Add any additional required models here.
];
match tokio::process::Command::new("ollama")
.arg("list")
.output()
.await
{
Ok(output) if output.status.success() => {
let stdout = String::from_utf8_lossy(&output.stdout);
for model in &required_models {
if !stdout.contains(model.as_str()) {
log::trace!(
"[LLM Resource] required model '{}' not found in ollama list",
model
);
resources.push(PendingResource::MissingModel(model.clone()));
return Ok(resources);
}
}
},
Ok(output) => {
error!(
"[LLM Resource] 'ollama list' command failed with status: {:?}",
output.status
);
resources.push(PendingResource::OllamaServerNotReady);
},
Err(e) => {
error!("[LLM Resource] failed to execute 'ollama list': {:?}", e);
resources.push(PendingResource::OllamaServerNotReady);
},
}
Ok(resources)
}
#[instrument(level = "info", skip_all)]
pub async fn get_plugin_config(&self, rag_enabled: bool) -> FlowyResult<OllamaPluginConfig> {
if !self.is_resource_ready().await {
return Err(FlowyError::local_ai().with_context("Local AI resources are not ready"));
}
let llm_setting = self.get_llm_setting();
let bin_path = match get_operating_system() {
OperatingSystem::MacOS | OperatingSystem::Windows => {
if !is_plugin_ready() {
return Err(FlowyError::new(
ErrorCode::AppFlowyLAINotReady,
"AppFlowyLAI not found",
));
}
ollama_plugin_path()
},
_ => {
return Err(
FlowyError::local_ai_unavailable()
.with_context("Local AI not available on current platform"),
);
},
};
let mut config = OllamaPluginConfig::new(
bin_path,
"ollama_ai_plugin".to_string(),
llm_setting.chat_model_name.clone(),
llm_setting.embedding_model_name.clone(),
Some(llm_setting.ollama_server_url.clone()),
)?;
if rag_enabled {
let resource_dir = self.resource_dir()?;
let persist_directory = resource_dir.join("vectorstore");
if !persist_directory.exists() {
std::fs::create_dir_all(&persist_directory)?;
}
config.set_rag_enabled(&persist_directory)?;
}
if cfg!(debug_assertions) {
config = config.with_verbose(true);
}
trace!("[AI Chat] config: {:?}", config);
Ok(config)
}
/// Fetches the local AI configuration from the resource service.
async fn get_local_ai_configuration(&self) -> FlowyResult<LocalAIConfig> {
self
.resource_service
.fetch_local_ai_config()
.await
.map_err(|err| {
error!("[LLM Resource] Failed to fetch local ai config: {:?}", err);
FlowyError::local_ai()
.with_context("Can't retrieve model info. Please try again later".to_string())
})
}
pub(crate) fn user_model_folder(&self) -> FlowyResult<PathBuf> {
self.resource_dir().map(|dir| dir.join(LLM_MODEL_DIR))
}
pub(crate) fn resource_dir(&self) -> FlowyResult<PathBuf> {
let user_data_dir = self.user_service.application_root_dir()?;
Ok(user_data_dir.join("ai"))
}
}
#[allow(dead_code)]
fn bytes_to_readable_format(bytes: u64) -> String {
const BYTES_IN_GIGABYTE: u64 = 1024 * 1024 * 1024;
const BYTES_IN_MEGABYTE: u64 = 1024 * 1024;
if bytes >= BYTES_IN_GIGABYTE {
let gigabytes = (bytes as f64) / (BYTES_IN_GIGABYTE as f64);
format!("{:.1} GB", gigabytes)
} else {
let megabytes = (bytes as f64) / (BYTES_IN_MEGABYTE as f64);
format!("{:.2} MB", megabytes)
}
}
@@ -1,9 +1,13 @@
use crate::local_ai::local_llm_resource::WatchDiskEvent;
use crate::local_ai::resource::WatchDiskEvent;
use flowy_error::{FlowyError, FlowyResult};
use std::path::PathBuf;
use std::process::Command;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
use tracing::{error, trace};
#[cfg(windows)]
use winreg::{enums::*, RegKey};
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
#[allow(dead_code)]
pub struct WatchContext {
@@ -20,7 +24,7 @@ pub fn watch_offline_app() -> FlowyResult<(WatchContext, UnboundedReceiver<Watch
FlowyError::internal().with_context("Unsupported platform for offline app watching")
})?;
let (tx, rx) = unbounded_channel();
let app_path = offline_app_path();
let app_path = ollama_plugin_path();
let mut watcher = notify::recommended_watcher(move |res: Result<Event, _>| match res {
Ok(event) => {
if event.paths.iter().any(|path| path == &app_path) {
@@ -80,15 +84,100 @@ pub(crate) fn offline_app_path() -> PathBuf {
PathBuf::new()
}
pub fn is_plugin_ready() -> bool {
ollama_plugin_path().exists() || ollama_plugin_command_available()
}
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
pub(crate) fn offline_app_path() -> PathBuf {
let offline_app = "appflowy_ai_plugin";
pub(crate) fn ollama_plugin_path() -> std::path::PathBuf {
#[cfg(target_os = "windows")]
return PathBuf::from(format!("/usr/local/bin/{}", offline_app));
{
// Use LOCALAPPDATA for a user-specific installation path on Windows.
let local_appdata =
std::env::var("LOCALAPPDATA").unwrap_or_else(|_| "C:\\Program Files".to_string());
std::path::PathBuf::from(local_appdata).join("Programs\\appflowy_plugin\\ollama_ai_plugin.exe")
}
#[cfg(target_os = "macos")]
return PathBuf::from(format!("/usr/local/bin/{}", offline_app));
{
let offline_app = "ollama_ai_plugin";
std::path::PathBuf::from(format!("/usr/local/bin/{}", offline_app))
}
#[cfg(target_os = "linux")]
return PathBuf::from(format!("/usr/local/bin/{}", offline_app));
{
let offline_app = "ollama_ai_plugin";
std::path::PathBuf::from(format!("/usr/local/bin/{}", offline_app))
}
}
pub(crate) fn ollama_plugin_command_available() -> bool {
if cfg!(windows) {
#[cfg(windows)]
{
// 1. Try "where" command first
let output = Command::new("cmd")
.args(["/C", "where", "ollama_ai_plugin"])
.output();
if let Ok(output) = output {
if !output.stdout.is_empty() {
return true;
}
}
// 2. Fallback: Check registry PATH for the executable
let path_dirs = get_windows_path_dirs();
let plugin_exe = "ollama_ai_plugin.exe"; // Adjust name if needed
path_dirs.iter().any(|dir| {
let full_path = std::path::Path::new(dir).join(plugin_exe);
full_path.exists()
})
}
#[cfg(not(windows))]
false
} else {
let output = Command::new("command")
.args(["-v", "ollama_ai_plugin"])
.output();
match output {
Ok(o) => !o.stdout.is_empty(),
_ => false,
}
}
}
#[cfg(windows)]
fn get_windows_path_dirs() -> Vec<String> {
let mut paths = Vec::new();
// Check HKEY_CURRENT_USER\Environment
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
if let Ok(env) = hkcu.open_subkey("Environment") {
if let Ok(path) = env.get_value::<String, _>("Path") {
paths.extend(path.split(';').map(|s| s.trim().to_string()));
}
}
// Check HKEY_LOCAL_MACHINE\SYSTEM\...\Environment
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
if let Ok(env) = hklm.open_subkey(r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment")
{
if let Ok(path) = env.get_value::<String, _>("Path") {
paths.extend(path.split(';').map(|s| s.trim().to_string()));
}
}
paths
}
#[cfg(test)]
mod tests {
use crate::local_ai::watch::ollama_plugin_command_available;
#[test]
fn test_command_import() {
let result = ollama_plugin_command_available();
println!("ollama plugin exist: {:?}", result);
}
}
@@ -1,6 +1,6 @@
use crate::ai_manager::AIUserService;
use crate::entities::{ChatStatePB, ModelTypePB};
use crate::local_ai::local_llm_chat::LocalAIController;
use crate::local_ai::controller::LocalAIController;
use crate::notification::{
chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY,
};
@@ -30,7 +30,7 @@ use tracing::trace;
pub struct AICloudServiceMiddleware {
cloud_service: Arc<dyn ChatCloudService>,
user_service: Arc<dyn AIUserService>,
local_llm_controller: Arc<LocalAIController>,
local_ai: Arc<LocalAIController>,
storage_service: Weak<dyn StorageService>,
}
@@ -38,19 +38,19 @@ impl AICloudServiceMiddleware {
pub fn new(
user_service: Arc<dyn AIUserService>,
cloud_service: Arc<dyn ChatCloudService>,
local_llm_controller: Arc<LocalAIController>,
local_ai: Arc<LocalAIController>,
storage_service: Weak<dyn StorageService>,
) -> Self {
Self {
user_service,
cloud_service,
local_llm_controller,
local_ai,
storage_service,
}
}
pub fn is_local_ai_enabled(&self) -> bool {
self.local_llm_controller.is_enabled()
self.local_ai.is_enabled()
}
pub async fn index_message_metadata(
@@ -68,7 +68,7 @@ impl AICloudServiceMiddleware {
.await;
self
.local_llm_controller
.local_ai
.index_message_metadata(chat_id, metadata_list, index_process_sink)
.await?;
let _ = index_process_sink
@@ -97,7 +97,7 @@ impl AICloudServiceMiddleware {
) {
chat_notification_builder(
APPFLOWY_AI_NOTIFICATION_KEY,
ChatNotification::UpdateChatPluginState,
ChatNotification::UpdateLocalAIState,
)
.payload(ChatStatePB {
model_type: ModelTypePB::LocalAI,
@@ -158,11 +158,11 @@ impl ChatCloudService for AICloudServiceMiddleware {
question_id: i64,
format: ResponseFormat,
) -> Result<StreamAnswer, FlowyError> {
if self.local_llm_controller.is_running() {
if self.local_ai.is_running() {
let row = self.get_message_record(question_id)?;
match self
.local_llm_controller
.stream_question(chat_id, &row.content, json!([]))
.local_ai
.stream_question(chat_id, &row.content, json!({}))
.await
{
Ok(stream) => Ok(QuestionStream::new(stream).boxed()),
@@ -185,13 +185,9 @@ impl ChatCloudService for AICloudServiceMiddleware {
chat_id: &str,
question_message_id: i64,
) -> Result<ChatMessage, FlowyError> {
if self.local_llm_controller.is_running() {
if self.local_ai.is_running() {
let content = self.get_message_record(question_message_id)?.content;
match self
.local_llm_controller
.ask_question(chat_id, &content)
.await
{
match self.local_ai.ask_question(chat_id, &content).await {
Ok(answer) => {
// TODO(nathan): metadata
let message = self
@@ -244,9 +240,9 @@ impl ChatCloudService for AICloudServiceMiddleware {
chat_id: &str,
message_id: i64,
) -> Result<RepeatedRelatedQuestion, FlowyError> {
if self.local_llm_controller.is_running() {
if self.local_ai.is_running() {
let questions = self
.local_llm_controller
.local_ai
.get_related_question(chat_id)
.await
.map_err(|err| FlowyError::local_ai().with_context(err))?;
@@ -274,9 +270,9 @@ impl ChatCloudService for AICloudServiceMiddleware {
workspace_id: &str,
params: CompleteTextParams,
) -> Result<StreamComplete, FlowyError> {
if self.local_llm_controller.is_running() {
if self.local_ai.is_running() {
match self
.local_llm_controller
.local_ai
.complete_text(&params.text, params.completion_type.unwrap() as u8)
.await
{
@@ -305,9 +301,9 @@ impl ChatCloudService for AICloudServiceMiddleware {
chat_id: &str,
metadata: Option<HashMap<String, Value>>,
) -> Result<(), FlowyError> {
if self.local_llm_controller.is_running() {
if self.local_ai.is_running() {
self
.local_llm_controller
.local_ai
.index_file(chat_id, Some(file_path.to_path_buf()), None, metadata)
.await
.map_err(|err| FlowyError::local_ai().with_context(err))?;
@@ -12,9 +12,9 @@ pub enum ChatNotification {
DidReceiveChatMessage = 3,
StreamChatMessageError = 4,
FinishStreaming = 5,
UpdateChatPluginState = 6,
UpdateLocalChatAI = 7,
DidUpdateChatSettings = 8,
UpdateLocalAIState = 6,
DidUpdateChatSettings = 7,
LocalAIResourceUpdated = 8,
}
impl std::convert::From<ChatNotification> for i32 {
@@ -30,8 +30,9 @@ impl std::convert::From<i32> for ChatNotification {
3 => ChatNotification::DidReceiveChatMessage,
4 => ChatNotification::StreamChatMessageError,
5 => ChatNotification::FinishStreaming,
6 => ChatNotification::UpdateChatPluginState,
7 => ChatNotification::UpdateLocalChatAI,
6 => ChatNotification::UpdateLocalAIState,
7 => ChatNotification::DidUpdateChatSettings,
8 => ChatNotification::LocalAIResourceUpdated,
_ => ChatNotification::Unknown,
}
}
@@ -51,10 +51,10 @@ impl DatabaseAIService for DatabaseAIServiceMiddleware {
object_id: &str,
summary_row: SummaryRowContent,
) -> Result<String, FlowyError> {
if self.ai_manager.local_ai_controller.is_running() {
if self.ai_manager.local_ai.is_running() {
self
.ai_manager
.local_ai_controller
.local_ai
.summary_database_row(summary_row)
.await
.map_err(|err| FlowyError::local_ai().with_context(err))
@@ -72,7 +72,7 @@ impl DatabaseAIService for DatabaseAIServiceMiddleware {
translate_row: TranslateRowContent,
language: &str,
) -> Result<TranslateRowResponse, FlowyError> {
if self.ai_manager.local_ai_controller.is_running() {
if self.ai_manager.local_ai.is_running() {
let data = LocalAITranslateRowData {
cells: translate_row
.into_iter()
@@ -86,7 +86,7 @@ impl DatabaseAIService for DatabaseAIServiceMiddleware {
};
let resp = self
.ai_manager
.local_ai_controller
.local_ai
.translate_database_row(data)
.await
.map_err(|err| FlowyError::local_ai().with_context(err))?;
+1
View File
@@ -255,6 +255,7 @@ impl AppFlowyCore {
server_provider: server_provider.clone(),
storage_manager: storage_manager.clone(),
ai_manager: ai_manager.clone(),
runtime: runtime.clone(),
};
let collab_interact_impl = CollabInteractImpl {
@@ -2,7 +2,7 @@ use std::sync::Arc;
use anyhow::Context;
use client_api::entity::billing_dto::SubscriptionPlan;
use tracing::{event, info};
use tracing::{error, event, info};
use collab_entity::CollabType;
use collab_integrate::collab_builder::AppFlowyCollabBuilder;
@@ -15,6 +15,7 @@ use flowy_storage::manager::StorageManager;
use flowy_user::event_map::UserStatusCallback;
use flowy_user_pub::cloud::{UserCloudConfig, UserCloudServiceProvider};
use flowy_user_pub::entities::{Authenticator, UserProfile, UserWorkspace};
use lib_dispatch::runtime::AFPluginRuntime;
use lib_infra::async_trait::async_trait;
use crate::server_layer::{Server, ServerProvider};
@@ -27,6 +28,20 @@ pub(crate) struct UserStatusCallbackImpl {
pub(crate) server_provider: Arc<ServerProvider>,
pub(crate) storage_manager: Arc<StorageManager>,
pub(crate) ai_manager: Arc<AIManager>,
// By default, all callback will run on the caller thread. If you don't want to block the caller
// thread, you can use runtime to spawn a new task.
pub(crate) runtime: Arc<AFPluginRuntime>,
}
impl UserStatusCallbackImpl {
fn init_ai_component(&self, workspace_id: String) {
let cloned_ai_manager = self.ai_manager.clone();
self.runtime.spawn(async move {
if let Err(err) = cloned_ai_manager.initialize(&workspace_id).await {
error!("Failed to initialize AIManager: {:?}", err);
}
});
}
}
#[async_trait]
@@ -70,7 +85,9 @@ impl UserStatusCallback for UserStatusCallbackImpl {
.initialize(user_id, authenticator == &Authenticator::Local)
.await?;
self.document_manager.initialize(user_id).await?;
self.ai_manager.initialize(&user_workspace.id).await?;
let workspace_id = user_workspace.id.clone();
self.init_ai_component(workspace_id);
Ok(())
}
@@ -97,6 +114,9 @@ impl UserStatusCallback for UserStatusCallbackImpl {
.initialize(user_id, authenticator.is_local())
.await?;
self.document_manager.initialize(user_id).await?;
let workspace_id = user_workspace.id.clone();
self.init_ai_component(workspace_id);
Ok(())
}
@@ -175,6 +195,9 @@ impl UserStatusCallback for UserStatusCallbackImpl {
.initialize_with_new_user(user_profile.uid)
.await
.context("DocumentManager error")?;
let workspace_id = user_workspace.id.clone();
self.init_ai_component(workspace_id);
Ok(())
}
+2 -2
View File
@@ -302,8 +302,8 @@ pub enum ErrorCode {
#[error("Unsupported file format")]
UnsupportedFileFormat = 104,
#[error("AI offline not started")]
AIOfflineNotInstalled = 105,
#[error("AppFlowy LAI not ready")]
AppFlowyLAINotReady = 105,
#[error("Invalid Request")]
InvalidRequest = 106,
+1 -1
View File
@@ -41,7 +41,7 @@ validator.workspace = true
async-trait.workspace = true
client-api = { workspace = true }
regex = "1.9.5"
futures = "0.3.30"
futures = "0.3.31"
dashmap.workspace = true
+2 -2
View File
@@ -22,7 +22,7 @@ validator = { workspace = true, features = ["derive"] }
tracing.workspace = true
atomic_refcell = "0.1"
allo-isolate = { version = "^0.1", features = ["catch-unwind"], optional = true }
futures = "0.3.30"
futures = "0.3.31"
cfg-if = "1.0.0"
futures-util = "0.3.30"
@@ -36,7 +36,7 @@ base64 = { version = "0.22.1" }
[dev-dependencies]
rand = "0.8.5"
futures = "0.3.30"
futures = "0.3.31"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
zip = { version = "2.2.0", features = ["deflate"] }