Skip to content

Commit

Permalink
chore: connect to API
Browse files Browse the repository at this point in the history
  • Loading branch information
richardshiue committed Jan 3, 2025
1 parent 6671422 commit dc0e491
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,13 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
},
sendMessage: (
String message,
PredefinedFormat? format,
Map<String, dynamic>? metadata,
) {
numSendMessage += 1;

_clearRelatedQuestions();
_startStreamingMessage(message, metadata);
_startStreamingMessage(message, format, metadata);
lastSentMessage = null;

emit(
Expand Down Expand Up @@ -233,7 +234,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
},
regenerateAnswer: (id, format) {
_clearRelatedQuestions();
_regenerateAnswer(id);
_regenerateAnswer(id, format);
lastSentMessage = null;

emit(
Expand Down Expand Up @@ -398,6 +399,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {

Future<void> _startStreamingMessage(
String message,
PredefinedFormat? format,
Map<String, dynamic>? metadata,
) async {
await answerStream?.dispose();
Expand All @@ -420,6 +422,9 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
answerStreamPort: Int64(answerStream!.nativePort),
metadata: await metadataPBFromMetadata(metadata),
);
if (format != null) {
payload.format = format.toPB();
}

// stream the question to the server
await AIEventStreamMessage(payload).send().fold(
Expand Down Expand Up @@ -460,7 +465,10 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
);
}

void _regenerateAnswer(String answerMessageIdString) async {
void _regenerateAnswer(
String answerMessageIdString,
PredefinedFormat? format,
) async {
final id = temporaryMessageIDMap.entries
.firstWhereOrNull((e) => e.value == answerMessageIdString)
?.key ??
Expand All @@ -479,6 +487,9 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
answerMessageId: answerMessageId,
answerStreamPort: Int64(answerStream!.nativePort),
);
if (format != null) {
payload.format = format.toPB();
}

await AIEventRegenerateResponse(payload).send().fold(
(success) {
Expand Down Expand Up @@ -586,6 +597,7 @@ class ChatEvent with _$ChatEvent {
// send message
const factory ChatEvent.sendMessage({
required String message,
PredefinedFormat? format,
Map<String, dynamic>? metadata,
}) = _SendMessage;
const factory ChatEvent.finishSending() = _FinishSendMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/entities.pbenum.dart';
import 'package:appflowy_backend/protobuf/flowy-ai/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart';
import 'package:easy_localization/easy_localization.dart';
Expand Down Expand Up @@ -154,6 +155,23 @@ class PredefinedFormat extends Equatable {
final ImageFormat imageFormat;
final TextFormat? textFormat;

PredefinedFormatPB toPB() {
return PredefinedFormatPB(
imageFormat: switch (imageFormat) {
ImageFormat.text => ResponseImageFormatPB.TextOnly,
ImageFormat.image => ResponseImageFormatPB.ImageOnly,
ImageFormat.textAndImage => ResponseImageFormatPB.TextAndImage,
},
textFormat: switch (textFormat) {
TextFormat.auto => ResponseTextFormatPB.Paragraph,
TextFormat.bulletList => ResponseTextFormatPB.BulletedList,
TextFormat.numberedList => ResponseTextFormatPB.NumberedList,
TextFormat.table => ResponseTextFormatPB.Table,
null => null
},
);
}

@override
List<Object?> get props => [imageFormat, textFormat];
}
Expand Down
6 changes: 4 additions & 2 deletions frontend/appflowy_flutter/lib/plugins/ai_chat/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,11 @@ class _ChatContentPage extends StatelessWidget {
onStopStreaming: () {
chatBloc.add(const ChatEvent.stopStream());
},
onSubmitted: (text, metadata) {
onSubmitted: (text, format, metadata) {
chatBloc.add(
ChatEvent.sendMessage(
message: text,
format: format,
metadata: metadata,
),
);
Expand All @@ -313,10 +314,11 @@ class _ChatContentPage extends StatelessWidget {
onStopStreaming: () {
chatBloc.add(const ChatEvent.stopStream());
},
onSubmitted: (text, metadata) {
onSubmitted: (text, format, metadata) {
chatBloc.add(
ChatEvent.sendMessage(
message: text,
format: format,
metadata: metadata,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class DesktopAIPromptInput extends StatefulWidget {
final String chatId;
final bool isStreaming;
final void Function() onStopStreaming;
final void Function(String, Map<String, dynamic>) onSubmitted;
final void Function(String, PredefinedFormat?, Map<String, dynamic>)
onSubmitted;
final void Function(List<String>) onUpdateSelectedSources;

@override
Expand Down Expand Up @@ -252,14 +253,13 @@ class _DesktopAIPromptInputState extends State<DesktopAIPromptInput> {
}

// get the attached files and mentioned pages
final metadata = {
...context.read<AIPromptInputBloc>().consumeMetadata(),
if (showPredefinedFormatSection) ...{
"format": predefinedFormat,
},
};
final metadata = context.read<AIPromptInputBloc>().consumeMetadata();

widget.onSubmitted(trimmedText, metadata);
if (showPredefinedFormatSection) {
widget.onSubmitted(trimmedText, predefinedFormat, metadata);
} else {
widget.onSubmitted(trimmedText, null, metadata);
}
}

void handleTextControllerChanged() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class MobileAIPromptInput extends StatefulWidget {
final String chatId;
final bool isStreaming;
final void Function() onStopStreaming;
final void Function(String, Map<String, dynamic>) onSubmitted;
final void Function(String, PredefinedFormat?, Map<String, dynamic>)
onSubmitted;
final void Function(List<String>) onUpdateSelectedSources;

@override
Expand Down Expand Up @@ -183,14 +184,13 @@ class _MobileAIPromptInputState extends State<MobileAIPromptInput> {
}

// get the attached files and mentioned pages
final metadata = {
...context.read<AIPromptInputBloc>().consumeMetadata(),
if (showPredefinedFormatSection) ...{
"format": predefinedFormat,
},
};
final metadata = context.read<AIPromptInputBloc>().consumeMetadata();

widget.onSubmitted(trimmedText, metadata);
if (showPredefinedFormatSection) {
widget.onSubmitted(trimmedText, predefinedFormat, metadata);
} else {
widget.onSubmitted(trimmedText, null, metadata);
}
}

void handleTextControllerChange() {
Expand Down
24 changes: 12 additions & 12 deletions frontend/rust-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/rust-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "9ace96286e39e9868f47088c4065b77cf31d5d6f" }
client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "9ace96286e39e9868f47088c4065b77cf31d5d6f" }
client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "bb590c0d843c350a111a4bd079864ef1bd2583e1" }
client-api-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "bb590c0d843c350a111a4bd079864ef1bd2583e1" }

[profile.dev]
opt-level = 0
Expand Down
4 changes: 3 additions & 1 deletion frontend/rust-lib/flowy-ai-pub/src/cloud.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bytes::Bytes;
pub use client_api::entity::ai_dto::{
AppFlowyOfflineAI, CompletionType, CreateChatContext, LLMModel, LocalAIConfig, ModelInfo,
RelatedQuestion, RepeatedRelatedQuestion, StringOrMessage,
OutputContent, OutputLayout, RelatedQuestion, RepeatedRelatedQuestion, ResponseFormat,
StringOrMessage,
};
pub use client_api::entity::billing_dto::SubscriptionPlan;
pub use client_api::entity::chat_dto::{
Expand Down Expand Up @@ -53,6 +54,7 @@ pub trait ChatCloudService: Send + Sync + 'static {
workspace_id: &str,
chat_id: &str,
message_id: i64,
format: ResponseFormat,
) -> Result<StreamAnswer, FlowyError>;

async fn get_answer(
Expand Down
8 changes: 6 additions & 2 deletions frontend/rust-lib/flowy-ai/src/ai_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::chat::Chat;
use crate::entities::{
ChatInfoPB, ChatMessageListPB, ChatMessagePB, ChatSettingsPB, FilePB, RepeatedRelatedQuestionPB,
ChatInfoPB, ChatMessageListPB, ChatMessagePB, ChatSettingsPB, FilePB, PredefinedFormatPB,
RepeatedRelatedQuestionPB,
};
use crate::local_ai::local_llm_chat::LocalAIController;
use crate::middleware::chat_service_mw::AICloudServiceMiddleware;
Expand Down Expand Up @@ -219,6 +220,7 @@ impl AIManager {
message_type: ChatMessageType,
answer_stream_port: i64,
question_stream_port: i64,
format: Option<PredefinedFormatPB>,
metadata: Vec<ChatMessageMetadata>,
) -> Result<ChatMessagePB, FlowyError> {
let chat = self.get_or_create_chat_instance(chat_id).await?;
Expand All @@ -228,6 +230,7 @@ impl AIManager {
message_type,
answer_stream_port,
question_stream_port,
format,
metadata,
)
.await?;
Expand All @@ -243,13 +246,14 @@ impl AIManager {
chat_id: &str,
answer_message_id: i64,
answer_stream_port: i64,
format: Option<PredefinedFormatPB>,
) -> FlowyResult<()> {
let chat = self.get_or_create_chat_instance(chat_id).await?;
let question_message_id = chat
.get_question_id_from_answer_id(answer_message_id)
.await?;
chat
.stream_regenerate_response(question_message_id, answer_stream_port)
.stream_regenerate_response(question_message_id, answer_stream_port, format)
.await?;
Ok(())
}
Expand Down
Loading

0 comments on commit dc0e491

Please sign in to comment.