Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
chore: show local ai disable
Nathan2277d7d23436
diff --git a/frontend/appflowy_flutter/lib/ai/service/ai_entities.dart b/frontend/appflowy_flutter/lib/ai/service/ai_entities.dartindex 249a92019a..b08fadb7f8 100644--- a/frontend/appflowy_flutter/lib/ai/service/ai_entities.dart+++ b/frontend/appflowy_flutter/lib/ai/service/ai_entities.dart@@ -4,6 +4,20 @@ import 'package:appflowy_backend/protobuf/flowy-ai/protobuf.dart';import 'package:easy_localization/easy_localization.dart';import 'package:equatable/equatable.dart';+class AIStreamEventPrefix {+ static const data = 'data:';+ static const error = 'error:';+ static const metadata = 'metadata:';+ static const start = 'start:';+ static const finish = 'finish:';+ static const comment = 'comment:';+ static const aiResponseLimit = 'AI_RESPONSE_LIMIT';+ static const aiImageResponseLimit = 'AI_IMAGE_RESPONSE_LIMIT';+ static const aiMaxRequired = 'AI_MAX_REQUIRED:';+ static const localAINotReady = 'LOCAL_AI_NOT_READY';+ static const localAIDisabled = 'LOCAL_AI_DISABLED';+}+enum AiType {cloud,local;diff --git a/frontend/appflowy_flutter/lib/ai/service/appflowy_ai_service.dart b/frontend/appflowy_flutter/lib/ai/service/appflowy_ai_service.dartindex f2d666a586..d96a3f1eed 100644--- a/frontend/appflowy_flutter/lib/ai/service/appflowy_ai_service.dart+++ b/frontend/appflowy_flutter/lib/ai/service/appflowy_ai_service.dart@@ -121,59 +121,7 @@ class AppFlowyCompletionStream extends CompletionStream {_port.handler = _controller.add;_subscription = _controller.stream.listen((event) async {- if (event == "AI_RESPONSE_LIMIT") {- processError(- AIError(- message: LocaleKeys.ai_textLimitReachedDescription.tr(),- code: AIErrorCode.aiResponseLimitExceeded,- ),- );- }-- if (event == "AI_IMAGE_RESPONSE_LIMIT") {- processError(- AIError(- message: LocaleKeys.ai_imageLimitReachedDescription.tr(),- code: AIErrorCode.aiImageResponseLimitExceeded,- ),- );- }-- if (event.startsWith("AI_MAX_REQUIRED:")) {- final msg = event.substring(16);- processError(- AIError(- message: msg,- code: AIErrorCode.other,- ),- );- }-- if (event.startsWith("start:")) {- await onStart();- }-- if (event.startsWith("data:")) {- await processMessage(event.substring(5));- }-- if (event.startsWith("comment:")) {- await processAssistMessage(event.substring(8));- }-- if (event.startsWith("finish:")) {- await onEnd();- }-- if (event.startsWith("LOCAL_AI_NOT_READY:")) {- onLocalAIInitializing();- }-- if (event.startsWith("error:")) {- processError(- AIError(message: event.substring(6), code: AIErrorCode.other),- );- }+ await _handleEvent(event);},);}@@ -183,4 +131,67 @@ class AppFlowyCompletionStream extends CompletionStream {await _subscription.cancel();_port.close();}++ Future<void> _handleEvent(String event) async {+ // Check simple matches first+ if (event == AIStreamEventPrefix.aiResponseLimit) {+ processError(+ AIError(+ message: LocaleKeys.ai_textLimitReachedDescription.tr(),+ code: AIErrorCode.aiResponseLimitExceeded,+ ),+ );+ return;+ }++ if (event == AIStreamEventPrefix.aiImageResponseLimit) {+ processError(+ AIError(+ message: LocaleKeys.ai_imageLimitReachedDescription.tr(),+ code: AIErrorCode.aiImageResponseLimitExceeded,+ ),+ );+ return;+ }++ // Otherwise, parse out prefix:content+ final colonIndex = event.indexOf(':');+ final hasColon = colonIndex != -1;+ final prefix = hasColon ? event.substring(0, colonIndex) : event;+ final content = hasColon ? event.substring(colonIndex + 1) : '';++ switch (prefix) {+ case AIStreamEventPrefix.aiMaxRequired:+ processError(AIError(message: content, code: AIErrorCode.other));+ break;++ case AIStreamEventPrefix.start:+ await onStart();+ break;++ case AIStreamEventPrefix.data:+ await processMessage(content);+ break;++ case AIStreamEventPrefix.comment:+ await processAssistMessage(content);+ break;++ case AIStreamEventPrefix.finish:+ await onEnd();+ break;++ case AIStreamEventPrefix.localAINotReady:+ onLocalAIInitializing();+ break;++ case AIStreamEventPrefix.error:+ processError(AIError(message: content, code: AIErrorCode.other));+ break;++ default:+ Log.debug('Unknown AI event: $event');+ break;+ }+ }}diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_stream.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_stream.dartindex 00d48e9347..c22559f21b 100644--- a/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_stream.dart+++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_stream.dart@@ -2,19 +2,9 @@ import 'dart:async';import 'dart:ffi';import 'dart:isolate';+import 'package:appflowy/ai/service/ai_entities.dart';import 'package:appflowy/plugins/ai_chat/application/chat_message_service.dart';-/// Constants for event prefixes.-class AnswerEventPrefix {- static const data = 'data:';- static const error = 'error:';- static const metadata = 'metadata:';- static const aiResponseLimit = 'AI_RESPONSE_LIMIT';- static const aiImageResponseLimit = 'AI_IMAGE_RESPONSE_LIMIT';- static const aiMaxRequired = 'AI_MAX_REQUIRED:';- static const localAINotReady = 'LOCAL_AI_NOT_READY';-}-/// A stream that receives answer events from an isolate or external process./// It caches events that might occur before a listener is attached.class AnswerStream {@@ -68,31 +58,31 @@ class AnswerStream {/// Handles incoming events from the underlying stream.void _handleEvent(String event) {- if (event.startsWith(AnswerEventPrefix.data)) {+ if (event.startsWith(AIStreamEventPrefix.data)) {_hasStarted = true;- final newText = event.substring(AnswerEventPrefix.data.length);+ final newText = event.substring(AIStreamEventPrefix.data.length);_text += newText;_onData?.call(_text);- } else if (event.startsWith(AnswerEventPrefix.error)) {- _error = event.substring(AnswerEventPrefix.error.length);+ } else if (event.startsWith(AIStreamEventPrefix.error)) {+ _error = event.substring(AIStreamEventPrefix.error.length);_onError?.call(_error!);- } else if (event.startsWith(AnswerEventPrefix.metadata)) {- final s = event.substring(AnswerEventPrefix.metadata.length);+ } else if (event.startsWith(AIStreamEventPrefix.metadata)) {+ final s = event.substring(AIStreamEventPrefix.metadata.length);_onMetadata?.call(parseMetadata(s));- } else if (event == AnswerEventPrefix.aiResponseLimit) {+ } else if (event == AIStreamEventPrefix.aiResponseLimit) {_aiLimitReached = true;_onAIResponseLimit?.call();- } else if (event == AnswerEventPrefix.aiImageResponseLimit) {+ } else if (event == AIStreamEventPrefix.aiImageResponseLimit) {_aiImageLimitReached = true;_onAIImageResponseLimit?.call();- } else if (event.startsWith(AnswerEventPrefix.aiMaxRequired)) {- final msg = event.substring(AnswerEventPrefix.aiMaxRequired.length);+ } else if (event.startsWith(AIStreamEventPrefix.aiMaxRequired)) {+ final msg = event.substring(AIStreamEventPrefix.aiMaxRequired.length);if (_onAIMaxRequired != null) {_onAIMaxRequired!(msg);} else {_pendingAIMaxRequiredEvents.add(msg);}- } else if (event.startsWith(AnswerEventPrefix.localAINotReady)) {+ } else if (event.startsWith(AIStreamEventPrefix.localAINotReady)) {if (_onLocalAIInitializing != null) {_onLocalAIInitializing!();} else {diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dartindex 1a94342f11..56b4934e3d 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart@@ -577,11 +577,10 @@ class MainContentArea extends StatelessWidget {Opacity(opacity: 0.5,child: FlowyText(- LocaleKeys.settings_aiPage_keys_localAIInitializing.tr(),+ LocaleKeys.settings_aiPage_keys_localAINotReadyRetryLater+ .tr(),),),- const HSpace(8.0),- const CircularProgressIndicator.adaptive(),],),);diff --git a/frontend/rust-lib/flowy-ai/src/chat.rs b/frontend/rust-lib/flowy-ai/src/chat.rsindex ea553758df..e656aefa1e 100644--- a/frontend/rust-lib/flowy-ai/src/chat.rs+++ b/frontend/rust-lib/flowy-ai/src/chat.rs@@ -289,6 +289,10 @@ impl Chat {let _ = answer_sink.send(format!("LOCAL_AI_NOT_READY:{}", err.msg)).await;+ } else if err.is_local_ai_disabled() {+ let _ = answer_sink+ .send(format!("LOCAL_AI_DISABLED:{}", err.msg))+ .await;} else {let _ = answer_sink.send(StreamMessage::OnError(err.msg.clone()).to_string())diff --git a/frontend/rust-lib/flowy-ai/src/completion.rs b/frontend/rust-lib/flowy-ai/src/completion.rsindex 4a59cb0a8c..8e505a49b9 100644--- a/frontend/rust-lib/flowy-ai/src/completion.rs+++ b/frontend/rust-lib/flowy-ai/src/completion.rs@@ -198,6 +198,8 @@ async fn handle_error(sink: &mut IsolateSink, err: FlowyError) {let _ = sink.send(format!("AI_MAX_REQUIRED:{}", err.msg)).await;} else if err.is_local_ai_not_ready() {let _ = sink.send(format!("LOCAL_AI_NOT_READY:{}", err.msg)).await;+ } else if err.is_local_ai_disabled() {+ let _ = sink.send(format!("LOCAL_AI_DISABLED:{}", err.msg)).await;} else {let _ = sink.send(StreamMessage::OnError(err.msg.clone()).to_string())diff --git a/frontend/rust-lib/flowy-ai/src/middleware/chat_service_mw.rs b/frontend/rust-lib/flowy-ai/src/middleware/chat_service_mw.rsindex 41e7bcebd9..55f85ec5f9 100644--- a/frontend/rust-lib/flowy-ai/src/middleware/chat_service_mw.rs+++ b/frontend/rust-lib/flowy-ai/src/middleware/chat_service_mw.rs@@ -181,7 +181,11 @@ impl ChatCloudService for AICloudServiceMiddleware {},}} else {- Err(FlowyError::local_ai_not_ready())+ if self.local_ai.is_enabled() {+ Err(FlowyError::local_ai_not_ready())+ } else {+ Err(FlowyError::local_ai_disabled())+ }}} else {self@@ -313,7 +317,11 @@ impl ChatCloudService for AICloudServiceMiddleware {},}} else {- Err(FlowyError::local_ai_not_ready())+ if self.local_ai.is_enabled() {+ Err(FlowyError::local_ai_not_ready())+ } else {+ Err(FlowyError::local_ai_disabled())+ }}} else {selfdiff --git a/frontend/rust-lib/flowy-error/src/code.rs b/frontend/rust-lib/flowy-error/src/code.rsindex b16099013b..3288252ad2 100644--- a/frontend/rust-lib/flowy-error/src/code.rs+++ b/frontend/rust-lib/flowy-error/src/code.rs@@ -377,6 +377,9 @@ pub enum ErrorCode {#[error("MCP error")]MCPError = 129,++ #[error("Local AI disabled")]+ LocalAIDisabled = 130,}impl ErrorCode {diff --git a/frontend/rust-lib/flowy-error/src/errors.rs b/frontend/rust-lib/flowy-error/src/errors.rsindex 0a6721a31a..4cef7a8990 100644--- a/frontend/rust-lib/flowy-error/src/errors.rs+++ b/frontend/rust-lib/flowy-error/src/errors.rs@@ -99,6 +99,10 @@ impl FlowyError {self.code == ErrorCode::LocalAINotReady}+ pub fn is_local_ai_disabled(&self) -> bool {+ self.code == ErrorCode::LocalAIDisabled+ }+pub fn is_ai_max_required(&self) -> bool {self.code == ErrorCode::AIMaxRequired}@@ -156,6 +160,7 @@ impl FlowyError {static_flowy_error!(view_is_locked, ErrorCode::ViewIsLocked);static_flowy_error!(local_ai_not_ready, ErrorCode::LocalAINotReady);+ static_flowy_error!(local_ai_disabled, ErrorCode::LocalAIDisabled);}impl std::convert::From<ErrorCode> for FlowyError {