Skip to content

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

chore: ai block with context (#7193)

Nathan.fooo342c36118445
diff --git a/frontend/appflowy_flutter/lib/ai/ai_client.dart b/frontend/appflowy_flutter/lib/ai/ai_client.dart
index 0c9ef8b954..a5c40c0569 100644
--- a/frontend/appflowy_flutter/lib/ai/ai_client.dart
+++ b/frontend/appflowy_flutter/lib/ai/ai_client.dart
@@ -18,6 +18,7 @@ abstract class AIRepository {
});
Future<void> streamCompletion({
+ String? objectId,
required String text,
required CompletionTypePB completionType,
required Future<void> Function() onStart,
diff --git a/frontend/appflowy_flutter/lib/ai/appflowy_ai_service.dart b/frontend/appflowy_flutter/lib/ai/appflowy_ai_service.dart
index f93a6f8b94..2066a489c8 100644
--- a/frontend/appflowy_flutter/lib/ai/appflowy_ai_service.dart
+++ b/frontend/appflowy_flutter/lib/ai/appflowy_ai_service.dart
@@ -40,6 +40,7 @@ class AppFlowyAIService implements AIRepository {
@override
Future<CompletionStream> streamCompletion({
+ String? objectId,
required String text,
required CompletionTypePB completionType,
required Future<void> Function() onStart,
@@ -53,10 +54,17 @@ class AppFlowyAIService implements AIRepository {
onEnd,
onError,
);
+ final List<String> ragIds = [];
+ if (objectId != null) {
+ ragIds.add(objectId);
+ }
+
final payload = CompleteTextPB(
text: text,
completionType: completionType,
streamPort: fixnum.Int64(stream.nativePort),
+ objectId: objectId ?? "",
+ ragIds: ragIds,
);
// ignore: unawaited_futures
diff --git a/frontend/appflowy_flutter/lib/ai/openai_client.dart b/frontend/appflowy_flutter/lib/ai/openai_client.dart
index 6f6bc9cc9b..f29024a914 100644
--- a/frontend/appflowy_flutter/lib/ai/openai_client.dart
+++ b/frontend/appflowy_flutter/lib/ai/openai_client.dart
@@ -160,6 +160,7 @@ class HttpOpenAIRepository implements AIRepository {
@override
Future<void> streamCompletion({
+ String? objectId,
required String text,
required CompletionTypePB completionType,
required Future<void> Function() onStart,
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ai_writer_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ai_writer_block_component.dart
index 24196aa439..4e0cb1b8ac 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ai_writer_block_component.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ai_writer_block_component.dart
@@ -224,7 +224,12 @@ class _AIWriterBlockComponentState extends State<AIWriterBlockComponent> {
BarrierDialog? barrierDialog;
final aiRepository = AppFlowyAIService();
+ final objectId =
+ editorState.document.root.context?.read<DocumentBloc>().documentId ??
+ "";
+
await aiRepository.streamCompletion(
+ objectId: objectId,
text: controller.text,
completionType: CompletionTypePB.ContinueWriting,
onStart: () async {
@@ -288,7 +293,11 @@ class _AIWriterBlockComponentState extends State<AIWriterBlockComponent> {
editorState: editorState,
);
final aiService = AppFlowyAIService();
+ final objectId =
+ editorState.document.root.context?.read<DocumentBloc>().documentId ??
+ "";
await aiService.streamCompletion(
+ objectId: objectId,
text: AIWriterBlockKeys.getRewritePrompt(previousOutput, prompt),
completionType: CompletionTypePB.ContinueWriting,
onStart: () async {
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_action_bloc.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_action_bloc.dart
index b464c72d1a..75875b9ab0 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_action_bloc.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_action_bloc.dart
@@ -25,6 +25,7 @@ class AskAIActionBloc extends Bloc<AskAIEvent, AskAIState> {
required this.node,
required this.editorState,
required this.action,
+ required this.objectId,
this.enableLogging = true,
}) : super(
AskAIState.initial(action),
@@ -73,6 +74,7 @@ class AskAIActionBloc extends Bloc<AskAIEvent, AskAIState> {
// used to wait for the aiRepository to be initialized
final aiRepositoryCompleter = Completer();
late final AIRepository aiRepository;
+ final String objectId;
bool isCanceled = false;
@@ -91,6 +93,7 @@ class AskAIActionBloc extends Bloc<AskAIEvent, AskAIState> {
final content = node.attributes[AskAIBlockKeys.content] as String;
await aiRepository.streamCompletion(
+ objectId: objectId,
text: content,
completionType: completionTypeFromInt(state.action),
onStart: () async {
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_block_component.dart
index 214a5affba..9fca4e7fa7 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_block_component.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_block_component.dart
@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/ai/ai_client.dart';
import 'package:appflowy/ai/error.dart';
+import 'package:appflowy/plugins/document/application/document_bloc.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/widgets/ai_limit_dialog.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_action.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/widgets/ask_ai_action_bloc.dart';
@@ -98,7 +99,12 @@ class _AskAIBlockComponentWidgetState extends State<AskAIBlockComponentWidget> {
popoverController.show();
});
+ final objectId =
+ editorState.document.root.context?.read<DocumentBloc>().documentId ??
+ "";
+
askAIBloc = AskAIActionBloc(
+ objectId: objectId,
node: widget.node,
editorState: editorState,
action: action,
diff --git a/frontend/appflowy_flutter/lib/shared/appflowy_network_image.dart b/frontend/appflowy_flutter/lib/shared/appflowy_network_image.dart
index 30144d12b9..8461380012 100644
--- a/frontend/appflowy_flutter/lib/shared/appflowy_network_image.dart
+++ b/frontend/appflowy_flutter/lib/shared/appflowy_network_image.dart
@@ -25,7 +25,7 @@ class FlowyNetworkImage extends StatefulWidget {
this.progressIndicatorBuilder,
this.errorWidgetBuilder,
required this.url,
- this.maxRetries = 3,
+ this.maxRetries = 5,
this.retryDuration = const Duration(seconds: 6),
this.retryErrorCodes = const {404},
this.onImageLoaded,
@@ -252,6 +252,6 @@ class _SensitiveContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
- return FlowyText(LocaleKeys.ai_sensitiveKeyword.tr());
+ return FlowyText(LocaleKeys.ai_contentPolicyViolation.tr());
}
}
diff --git a/frontend/appflowy_flutter/test/bloc_test/ask_ai_test/ask_ai_action_bloc_test.dart b/frontend/appflowy_flutter/test/bloc_test/ask_ai_test/ask_ai_action_bloc_test.dart
index 3165f541ea..d01f4b6c44 100644
--- a/frontend/appflowy_flutter/test/bloc_test/ask_ai_test/ask_ai_action_bloc_test.dart
+++ b/frontend/appflowy_flutter/test/bloc_test/ask_ai_test/ask_ai_action_bloc_test.dart
@@ -16,6 +16,7 @@ const _aiResponse = 'UPDATED:';
class _MockAIRepository extends Mock implements AIRepository {
@override
Future<void> streamCompletion({
+ String? objectId,
required String text,
required CompletionTypePB completionType,
required Future<void> Function() onStart,
@@ -35,6 +36,7 @@ class _MockAIRepository extends Mock implements AIRepository {
class _MockAIRepositoryLess extends Mock implements AIRepository {
@override
Future<void> streamCompletion({
+ String? objectId,
required String text,
required CompletionTypePB completionType,
required Future<void> Function() onStart,
@@ -52,6 +54,7 @@ class _MockAIRepositoryLess extends Mock implements AIRepository {
class _MockAIRepositoryMore extends Mock implements AIRepository {
@override
Future<void> streamCompletion({
+ String? objectId,
required String text,
required CompletionTypePB completionType,
required Future<void> Function() onStart,
@@ -71,6 +74,7 @@ class _MockAIRepositoryMore extends Mock implements AIRepository {
class _MockErrorRepository extends Mock implements AIRepository {
@override
Future<void> streamCompletion({
+ String? objectId,
required String text,
required CompletionTypePB completionType,
required Future<void> Function() onStart,
@@ -118,6 +122,7 @@ void main() {
content: [text1, text2, text3].join('\n'),
);
return AskAIActionBloc(
+ objectId: "",
node: node,
editorState: editorState,
action: AskAIAction.makeItLonger,
@@ -167,6 +172,7 @@ void main() {
content: [text1, text2, text3].join('\n'),
);
return AskAIActionBloc(
+ objectId: "",
node: node,
editorState: editorState,
action: AskAIAction.makeItLonger,
@@ -213,6 +219,7 @@ void main() {
content: [text1, text2, text3].join('\n\n'),
);
final bloc = AskAIActionBloc(
+ objectId: "",
node: node,
editorState: editorState,
action: AskAIAction.summarize,
@@ -260,6 +267,7 @@ void main() {
content: [text1, text2, text3].join('\n'),
);
final bloc = AskAIActionBloc(
+ objectId: "",
node: node,
editorState: editorState,
action: AskAIAction.summarize,
@@ -299,6 +307,7 @@ void main() {
content: [text1, text2, text3].join('\n'),
);
final bloc = AskAIActionBloc(
+ objectId: "",
node: node,
editorState: editorState,
action: AskAIAction.summarize,
diff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.json
index be01b7e06f..0648d3442c 100644
--- a/frontend/resources/translations/en.json
+++ b/frontend/resources/translations/en.json
@@ -3070,6 +3070,6 @@
}
},
"ai":{
- "sensitiveKeyword": "Image generation failed due to sensitive content. Please rephrase your input and try again"
+ "contentPolicyViolation": "Image generation failed due to sensitive content. Please rephrase your input and try again"
}
}
diff --git a/frontend/rust-lib/Cargo.lock b/frontend/rust-lib/Cargo.lock
index 6bd772670b..5e0e7d3826 100644
--- a/frontend/rust-lib/Cargo.lock
+++ b/frontend/rust-lib/Cargo.lock
@@ -163,7 +163,7 @@ checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
[[package]]
name = "app-error"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"bincode",
@@ -183,7 +183,7 @@ dependencies = [
[[package]]
name = "appflowy-ai-client"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"bytes",
@@ -786,7 +786,7 @@ dependencies = [
[[package]]
name = "client-api"
version = "0.2.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"again",
"anyhow",
@@ -843,7 +843,7 @@ dependencies = [
[[package]]
name = "client-api-entity"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"collab-entity",
"collab-rt-entity",
@@ -856,7 +856,7 @@ dependencies = [
[[package]]
name = "client-websocket"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"futures-channel",
"futures-util",
@@ -1128,7 +1128,7 @@ dependencies = [
[[package]]
name = "collab-rt-entity"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"bincode",
@@ -1153,7 +1153,7 @@ dependencies = [
[[package]]
name = "collab-rt-protocol"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"async-trait",
@@ -1548,7 +1548,7 @@ checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"
[[package]]
name = "database-entity"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"app-error",
@@ -2970,7 +2970,7 @@ dependencies = [
[[package]]
name = "gotrue"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"futures-util",
@@ -2987,7 +2987,7 @@ dependencies = [
[[package]]
name = "gotrue-entity"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"app-error",
@@ -3598,7 +3598,7 @@ dependencies = [
[[package]]
name = "infra"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"bytes",
@@ -6154,7 +6154,7 @@ dependencies = [
[[package]]
name = "shared-entity"
version = "0.1.0"
-source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=b47a635cfc9e42030706aebd47e40b95361cbdee#b47a635cfc9e42030706aebd47e40b95361cbdee"
+source = "git+https://github.com/AppFlowy-IO/AppFlowy-Cloud?rev=e745b1f35603600b8b41b0aef20bd65b2b697879#e745b1f35603600b8b41b0aef20bd65b2b697879"
dependencies = [
"anyhow",
"app-error",
diff --git a/frontend/rust-lib/Cargo.toml b/frontend/rust-lib/Cargo.toml
index a3feb2e0d3..cb9734d019 100644
--- a/frontend/rust-lib/Cargo.toml
+++ b/frontend/rust-lib/Cargo.toml
@@ -103,8 +103,8 @@ dashmap = "6.0.1"
# Run the script.add_workspace_members:
# scripts/tool/update_client_api_rev.sh new_rev_id
# ⚠️⚠️⚠️️
-client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "b47a635cfc9e42030706aebd47e40b95361cbdee" }
-client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "b47a635cfc9e42030706aebd47e40b95361cbdee" }
+client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "e745b1f35603600b8b41b0aef20bd65b2b697879" }
+client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "e745b1f35603600b8b41b0aef20bd65b2b697879" }
[profile.dev]
opt-level = 0
diff --git a/frontend/rust-lib/event-integration-test/src/chat_event.rs b/frontend/rust-lib/event-integration-test/src/chat_event.rs
index 5423927422..f1aafcd136 100644
--- a/frontend/rust-lib/event-integration-test/src/chat_event.rs
+++ b/frontend/rust-lib/event-integration-test/src/chat_event.rs
@@ -97,6 +97,8 @@ impl EventIntegrationTest {
text: text.to_string(),
completion_type,
stream_port: 0,
+ object_id: "".to_string(),
+ rag_ids: vec![],
};
EventBuilder::new(self.clone())
.event(AIEvent::CompleteText)
diff --git a/frontend/rust-lib/flowy-ai-pub/src/cloud.rs b/frontend/rust-lib/flowy-ai-pub/src/cloud.rs
index 67d31d0ded..bf787f3d20 100644
--- a/frontend/rust-lib/flowy-ai-pub/src/cloud.rs
+++ b/frontend/rust-lib/flowy-ai-pub/src/cloud.rs
@@ -1,8 +1,8 @@
use bytes::Bytes;
pub use client_api::entity::ai_dto::{
- AppFlowyOfflineAI, CompletionType, CreateChatContext, LLMModel, LocalAIConfig, ModelInfo,
- OutputContent, OutputLayout, RelatedQuestion, RepeatedRelatedQuestion, ResponseFormat,
- StringOrMessage,
+ AppFlowyOfflineAI, CompleteTextParams, CompletionMetadata, CompletionType, CreateChatContext,
+ LLMModel, LocalAIConfig, ModelInfo, OutputContent, OutputLayout, RelatedQuestion,
+ RepeatedRelatedQuestion, ResponseFormat, StringOrMessage,
};
pub use client_api::entity::billing_dto::SubscriptionPlan;
pub use client_api::entity::chat_dto::{
@@ -89,8 +89,7 @@ pub trait ChatCloudService: Send + Sync + 'static {
async fn stream_complete(
&self,
workspace_id: &str,
- text: &str,
- complete_type: CompletionType,
+ params: CompleteTextParams,
) -> Result<StreamComplete, FlowyError>;
async fn index_file(
diff --git a/frontend/rust-lib/flowy-ai/src/completion.rs b/frontend/rust-lib/flowy-ai/src/completion.rs
index ebb7f1f7a8..ccf8e425ad 100644
--- a/frontend/rust-lib/flowy-ai/src/completion.rs
+++ b/frontend/rust-lib/flowy-ai/src/completion.rs
@@ -3,7 +3,9 @@ use crate::entities::{CompleteTextPB, CompleteTextTaskPB, CompletionTypePB};
use allo_isolate::Isolate;
use dashmap::DashMap;
-use flowy_ai_pub::cloud::{ChatCloudService, CompletionType};
+use flowy_ai_pub::cloud::{
+ ChatCloudService, CompleteTextParams, CompletionMetadata, CompletionType,
+};
use flowy_error::{FlowyError, FlowyResult};
use futures::{SinkExt, StreamExt};
@@ -11,6 +13,7 @@ use lib_infra::isolate_stream::IsolateSink;
use std::sync::{Arc, Weak};
use tokio::select;
+use tracing::info;
pub struct AICompletion {
tasks: Arc<DashMap<String, tokio::sync::mpsc::Sender<()>>>,
@@ -95,8 +98,19 @@ impl CompletionTask {
};
let _ = sink.send("start:".to_string()).await;
+ let params = CompleteTextParams {
+ text: self.context.text,
+ completion_type: Some(complete_type),
+ custom_prompt: None,
+ metadata: Some(CompletionMetadata {
+ object_id: self.context.object_id,
+ rag_ids: Some(self.context.rag_ids),
+ }),
+ };
+
+ info!("start completion: {:?}", params);
match cloud_service
- .stream_complete(&self.workspace_id, &self.context.text, complete_type)
+ .stream_complete(&self.workspace_id, params)
.await
{
Ok(mut stream) => loop {
diff --git a/frontend/rust-lib/flowy-ai/src/entities.rs b/frontend/rust-lib/flowy-ai/src/entities.rs
index f0d3726b06..a5b9778f06 100644
--- a/frontend/rust-lib/flowy-ai/src/entities.rs
+++ b/frontend/rust-lib/flowy-ai/src/entities.rs
@@ -361,6 +361,12 @@ pub struct CompleteTextPB {
#[pb(index = 3)]
pub stream_port: i64,
+
+ #[pb(index = 4)]
+ pub object_id: String,
+
+ #[pb(index = 5)]
+ pub rag_ids: Vec<String>,
}
#[derive(Default, ProtoBuf, Clone, Debug)]
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.rs
index c8c3dda821..ae8ce9b8d0 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
@@ -10,7 +10,7 @@ use std::collections::HashMap;
use flowy_ai_pub::cloud::{
ChatCloudService, ChatMessage, ChatMessageMetadata, ChatMessageType, ChatSettings,
- CompletionType, LocalAIConfig, MessageCursor, RelatedQuestion, RepeatedChatMessage,
+ CompleteTextParams, LocalAIConfig, MessageCursor, RelatedQuestion, RepeatedChatMessage,
RepeatedRelatedQuestion, ResponseFormat, StreamAnswer, StreamComplete, SubscriptionPlan,
UpdateChatParams,
};
@@ -272,13 +272,12 @@ impl ChatCloudService for AICloudServiceMiddleware {
async fn stream_complete(
&self,
workspace_id: &str,
- text: &str,
- complete_type: CompletionType,
+ params: CompleteTextParams,
) -> Result<StreamComplete, FlowyError> {
if self.local_llm_controller.is_running() {
match self
.local_llm_controller
- .complete_text(text, complete_type as u8)
+ .complete_text(&params.text, params.completion_type.unwrap() as u8)
.await
{
Ok(stream) => Ok(
@@ -294,7 +293,7 @@ impl ChatCloudService for AICloudServiceMiddleware {
} else {
self
.cloud_service
- .stream_complete(workspace_id, text, complete_type)
+ .stream_complete(workspace_id, params)
.await
}
}
diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs
index c34b40023c..b01ea49f82 100644
--- a/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs
+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs
@@ -1,5 +1,5 @@
use client_api::collab_sync::{SinkConfig, SyncObject, SyncPlugin};
-use client_api::entity::ai_dto::{CompletionType, RepeatedRelatedQuestion};
+use client_api::entity::ai_dto::RepeatedRelatedQuestion;
use client_api::entity::search_dto::SearchDocumentResponseItem;
use client_api::entity::workspace_dto::PublishInfoView;
use client_api::entity::PublishInfo;
@@ -21,9 +21,9 @@ use collab_integrate::collab_builder::{
CollabCloudPluginProvider, CollabPluginProviderContext, CollabPluginProviderType,
};
use flowy_ai_pub::cloud::{
- ChatCloudService, ChatMessage, ChatMessageMetadata, ChatMessageType, ChatSettings, LocalAIConfig,
- MessageCursor, RepeatedChatMessage, ResponseFormat, StreamAnswer, StreamComplete,
- SubscriptionPlan, UpdateChatParams,
+ ChatCloudService, ChatMessage, ChatMessageMetadata, ChatMessageType, ChatSettings,
+ CompleteTextParams, LocalAIConfig, MessageCursor, RepeatedChatMessage, ResponseFormat,
+ StreamAnswer, StreamComplete, SubscriptionPlan, UpdateChatParams,
};
use flowy_database_pub::cloud::{
DatabaseAIService, DatabaseCloudService, DatabaseSnapshot, EncodeCollabByOid, SummaryRowContent,
@@ -771,15 +771,13 @@ impl ChatCloudService for ServerProvider {
async fn stream_complete(
&self,
workspace_id: &str,
- text: &str,
- complete_type: CompletionType,
+ params: CompleteTextParams,
) -> Result<StreamComplete, FlowyError> {
let workspace_id = workspace_id.to_string();
- let text = text.to_string();
let server = self.get_server()?;
server
.chat_service()
- .stream_complete(&workspace_id, &text, complete_type)
+ .stream_complete(&workspace_id, params)
.await
}
diff --git a/frontend/rust-lib/flowy-server/src/af_cloud/impls/chat.rs b/frontend/rust-lib/flowy-server/src/af_cloud/impls/chat.rs
index a7fe19980d..7512b9e48c 100644
--- a/frontend/rust-lib/flowy-server/src/af_cloud/impls/chat.rs
+++ b/frontend/rust-lib/flowy-server/src/af_cloud/impls/chat.rs
@@ -1,6 +1,6 @@
use crate::af_cloud::AFServer;
use client_api::entity::ai_dto::{
- ChatQuestionQuery, CompleteTextParams, CompletionType, RepeatedRelatedQuestion, ResponseFormat,
+ ChatQuestionQuery, CompleteTextParams, RepeatedRelatedQuestion, ResponseFormat,
};
use client_api::entity::chat_dto::{
CreateAnswerMessageParams, CreateChatMessageParams, CreateChatParams, MessageCursor,
@@ -188,15 +188,8 @@ where
async fn stream_complete(
&self,
workspace_id: &str,
- text: &str,
- completion_type: CompletionType,
+ params: CompleteTextParams,
) -> Result<StreamComplete, FlowyError> {
- // TODO(Nathan): Check if this is correct after updating to latest client-api
- let params = CompleteTextParams {
- text: text.to_string(),
- completion_type: Some(completion_type),
- custom_prompt: None,
- };
let stream = self
.inner
.try_get_client()?
diff --git a/frontend/rust-lib/flowy-server/src/default_impl.rs b/frontend/rust-lib/flowy-server/src/default_impl.rs
index 7fe409d781..792db6e23a 100644
--- a/frontend/rust-lib/flowy-server/src/default_impl.rs
+++ b/frontend/rust-lib/flowy-server/src/default_impl.rs
@@ -1,8 +1,8 @@
-use client_api::entity::ai_dto::{CompletionType, LocalAIConfig, RepeatedRelatedQuestion};
+use client_api::entity::ai_dto::{LocalAIConfig, RepeatedRelatedQuestion};
use flowy_ai_pub::cloud::{
- ChatCloudService, ChatMessage, ChatMessageMetadata, ChatMessageType, ChatSettings, MessageCursor,
- RepeatedChatMessage, ResponseFormat, StreamAnswer, StreamComplete, SubscriptionPlan,
- UpdateChatParams,
+ ChatCloudService, ChatMessage, ChatMessageMetadata, ChatMessageType, ChatSettings,
+ CompleteTextParams, MessageCursor, RepeatedChatMessage, ResponseFormat, StreamAnswer,
+ StreamComplete, SubscriptionPlan, UpdateChatParams,
};
use flowy_error::FlowyError;
use lib_infra::async_trait::async_trait;
@@ -96,8 +96,7 @@ impl ChatCloudService for DefaultChatCloudServiceImpl {
async fn stream_complete(
&self,
_workspace_id: &str,
- _text: &str,
- _complete_type: CompletionType,
+ _params: CompleteTextParams,
) -> Result<StreamComplete, FlowyError> {
Err(FlowyError::not_support().with_context("complete text is not supported in local server."))
}
diff --git a/frontend/rust-lib/flowy-user/src/services/sqlite_sql/workspace_sql.rs b/frontend/rust-lib/flowy-user/src/services/sqlite_sql/workspace_sql.rs
index f057f9eefb..8d5c1e8dc7 100644
--- a/frontend/rust-lib/flowy-user/src/services/sqlite_sql/workspace_sql.rs
+++ b/frontend/rust-lib/flowy-user/src/services/sqlite_sql/workspace_sql.rs
@@ -48,7 +48,7 @@ pub fn save_user_workspaces_op(
) -> Result<(), FlowyError> {
conn.immediate_transaction(|conn| {
delete_existing_workspaces(uid, conn)?;
- insert_new_workspaces_op(uid, user_workspaces, conn)?;
+ insert_or_update_workspaces_op(uid, user_workspaces, conn)?;
Ok(())
})
}
@@ -62,17 +62,30 @@ fn delete_existing_workspaces(uid: i64, conn: &mut SqliteConnection) -> Result<(
Ok(())
}
-pub fn insert_new_workspaces_op(
+pub fn insert_or_update_workspaces_op(
uid: i64,
user_workspaces: &[UserWorkspace],
conn: &mut SqliteConnection,
) -> Result<(), FlowyError> {
for user_workspace in user_workspaces {
let new_record = UserWorkspaceTable::try_from((uid, user_workspace))?;
+
diesel::insert_into(user_workspace_table::table)
- .values(new_record)
+ .values(new_record.clone())
+ .on_conflict(user_workspace_table::id)
+ .do_update()
+ .set((
+ user_workspace_table::name.eq(new_record.name),
+ user_workspace_table::uid.eq(new_record.uid),
+ user_workspace_table::created_at.eq(new_record.created_at),
+ user_workspace_table::database_storage_id.eq(new_record.database_storage_id),
+ user_workspace_table::icon.eq(new_record.icon),
+ user_workspace_table::member_count.eq(new_record.member_count),
+ user_workspace_table::role.eq(new_record.role),
+ ))
.execute(conn)?;
}
+
Ok(())
}
diff --git a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs
index 34a34bad55..31ad71ce74 100644
--- a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs
+++ b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs
@@ -33,7 +33,8 @@ use crate::services::sqlite_sql::member_sql::{
};
use crate::services::sqlite_sql::user_sql::UserTableChangeset;
use crate::services::sqlite_sql::workspace_sql::{
- get_all_user_workspace_op, get_user_workspace_op, insert_new_workspaces_op, UserWorkspaceTable,
+ get_all_user_workspace_op, get_user_workspace_op, insert_or_update_workspaces_op,
+ UserWorkspaceTable,
};
use crate::user_manager::{upsert_user_profile_change, UserManager};
use flowy_user_pub::session::Session;
@@ -214,7 +215,7 @@ impl UserManager {
// save the workspace to sqlite db
let uid = self.user_id()?;
let mut conn = self.db_connection(uid)?;
- insert_new_workspaces_op(uid, &[new_workspace.clone()], &mut conn)?;
+ insert_or_update_workspaces_op(uid, &[new_workspace.clone()], &mut conn)?;
Ok(new_workspace)
}