ai chat version 1

This commit is contained in:
OkaykOrhmn 2024-08-27 10:23:29 +03:30
parent 551a85e851
commit 5ec13a7a52
12 changed files with 636 additions and 141 deletions

View File

@ -2,6 +2,9 @@ class BotsModel {
int? id; int? id;
String? name; String? name;
String? image; String? image;
String? description;
List<String>? attachmentType;
int? attachment;
BotsModel({this.id, this.name, this.image}); BotsModel({this.id, this.name, this.image});
@ -9,6 +12,14 @@ class BotsModel {
id = json['id']; id = json['id'];
name = json['name']; name = json['name'];
image = json['image']; image = json['image'];
description = json['description'];
if (json['attachmentType'] != null) {
attachmentType = <String>[];
json['attachmentType'].forEach((v) {
attachmentType!.add(v);
});
}
attachment = json['attachment'];
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
@ -16,6 +27,11 @@ class BotsModel {
data['id'] = id; data['id'] = id;
data['name'] = name; data['name'] = name;
data['image'] = image; data['image'] = image;
data['description'] = description;
if (attachmentType != null) {
data['attachmentType'] = attachmentType!.map((v) => v).toList();
}
data['attachment'] = attachment;
return data; return data;
} }
} }

View File

@ -5,6 +5,7 @@ class ChatsModel {
int? userId; int? userId;
int? botId; int? botId;
String? title; String? title;
String? placeholder;
String? createdAt; String? createdAt;
String? updatedAt; String? updatedAt;
BotsModel? bot; BotsModel? bot;
@ -15,6 +16,7 @@ class ChatsModel {
this.userId, this.userId,
this.botId, this.botId,
this.title, this.title,
this.placeholder,
this.createdAt, this.createdAt,
this.updatedAt, this.updatedAt,
this.bot, this.bot,
@ -25,6 +27,7 @@ class ChatsModel {
userId = json['userId']; userId = json['userId'];
botId = json['botId']; botId = json['botId'];
title = json['title']; title = json['title'];
placeholder = json['placeholder'];
createdAt = json['createdAt']; createdAt = json['createdAt'];
updatedAt = json['updatedAt']; updatedAt = json['updatedAt'];
bot = json['bot'] != null ? BotsModel.fromJson(json['bot']) : null; bot = json['bot'] != null ? BotsModel.fromJson(json['bot']) : null;
@ -42,6 +45,7 @@ class ChatsModel {
data['userId'] = userId; data['userId'] = userId;
data['botId'] = botId; data['botId'] = botId;
data['title'] = title; data['title'] = title;
data['placeholder'] = placeholder;
data['createdAt'] = createdAt; data['createdAt'] = createdAt;
data['updatedAt'] = updatedAt; data['updatedAt'] = updatedAt;
if (bot != null) { if (bot != null) {
@ -58,6 +62,8 @@ class Prompts {
int? id; int? id;
int? chatId; int? chatId;
String? text; String? text;
String? file;
String? fileName;
String? role; String? role;
String? createdAt; String? createdAt;
bool? finished; bool? finished;
@ -66,6 +72,8 @@ class Prompts {
{this.id, {this.id,
this.chatId, this.chatId,
this.text, this.text,
this.file,
this.fileName,
this.role, this.role,
this.createdAt, this.createdAt,
this.finished}); this.finished});
@ -74,6 +82,8 @@ class Prompts {
id = json['id']; id = json['id'];
chatId = json['chatId']; chatId = json['chatId'];
text = json['text']; text = json['text'];
file = json['file'];
fileName = json['fileName'];
role = json['role']; role = json['role'];
createdAt = json['createdAt']; createdAt = json['createdAt'];
} }
@ -83,6 +93,8 @@ class Prompts {
data['id'] = id; data['id'] = id;
data['chatId'] = chatId; data['chatId'] = chatId;
data['text'] = text; data['text'] = text;
data['file'] = file;
data['fileName'] = fileName;
data['role'] = role; data['role'] = role;
data['createdAt'] = createdAt; data['createdAt'] = createdAt;
return data; return data;
@ -92,6 +104,8 @@ class Prompts {
int? id, int? id,
int? chatId, int? chatId,
String? text, String? text,
String? file,
String? fileName,
String? role, String? role,
String? createdAt, String? createdAt,
bool? finished, bool? finished,
@ -100,6 +114,8 @@ class Prompts {
id: id ?? this.id, id: id ?? this.id,
chatId: chatId ?? this.chatId, chatId: chatId ?? this.chatId,
text: text ?? this.text, text: text ?? this.text,
file: file ?? this.file,
fileName: fileName ?? this.fileName,
role: role ?? this.role, role: role ?? this.role,
createdAt: createdAt ?? this.createdAt, createdAt: createdAt ?? this.createdAt,
finished: finished ?? this.finished, finished: finished ?? this.finished,

View File

@ -0,0 +1,8 @@
import 'package:didvan/models/ai/chats_model.dart';
class MessageModel {
final String dateTime;
final List<Prompts> prompts;
MessageModel({required this.dateTime, required this.prompts});
}

View File

@ -1,37 +1,55 @@
// ignore_for_file: depend_on_referenced_packages
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:io';
import 'package:didvan/services/storage/storage.dart'; import 'package:didvan/services/storage/storage.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:http/http.dart'; import 'package:http/http.dart';
import 'package:path/path.dart' as p;
import 'package:http_parser/http_parser.dart' as parser;
class AiApiService { class AiApiService {
static const String baseUrl = 'https://api.didvan.app/ai'; static const String baseUrl = 'https://api.didvan.app/ai';
static final _client = http.Client(); static final _client = http.Client();
static Future<Request> initial( static Future<http.MultipartRequest> initial(
{required final String url, {required final String url,
required final String message, required final String message,
final int? chatId}) async { final int? chatId,
final File? file}) async {
final headers = { final headers = {
"Content-Type": "application/json", "Authorization": "Bearer ${await StorageService.getValue(key: 'token')}",
"Authorization": "Bearer ${await StorageService.getValue(key: 'token')}" 'Content-Type': 'multipart/form-data'
}; };
var request = http.Request('POST', Uri.parse(baseUrl + url)) var request = http.MultipartRequest('POST', Uri.parse(baseUrl + url))
..headers.addAll(headers); ..headers.addAll(headers);
var body = {'prompt': message}; request.fields['prompt'] = message;
if (chatId != null) { if (chatId != null) {
body.addAll({'chatId': chatId.toString()}); request.fields['chatId'] = chatId.toString();
}
if (file != null) {
final length = await file.length();
String basename = p.basename(file.path);
String mediaExtension = p.extension(file.path).replaceAll('.', '');
String mediaFormat = 'image';
if (mediaExtension.contains('pdf')) {
mediaFormat = 'application';
}
request.files.add(
http.MultipartFile('file', file.readAsBytes().asStream(), length,
filename: basename,
contentType: parser.MediaType(mediaFormat, mediaExtension)),
);
} }
request.body = jsonEncode(body);
return request; return request;
} }
static Future<ByteStream> getResponse(Request request) async { static Future<ByteStream> getResponse(http.MultipartRequest request) async {
final res = _client.send(request).timeout( final res = _client.send(request).timeout(
const Duration(seconds: 15), const Duration(seconds: 30),
); );
final http.StreamedResponse response = await res; final http.StreamedResponse response = await res;
return response.stream; return response.stream;

View File

@ -7,6 +7,7 @@ import 'package:didvan/services/storage/storage.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:just_audio/just_audio.dart'; import 'package:just_audio/just_audio.dart';
import 'package:file_picker/file_picker.dart';
class MediaService { class MediaService {
static final audioPlayer = AudioPlayer(); static final audioPlayer = AudioPlayer();
@ -99,4 +100,13 @@ class MediaService {
final XFile? pickedFile = await imagePicker.pickImage(source: source); final XFile? pickedFile = await imagePicker.pickImage(source: source);
return pickedFile; return pickedFile;
} }
static Future<FilePickerResult?> pickPdfFile() async {
final FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['pdf'],
allowMultiple: false,
);
return result;
}
} }

17
lib/views/ai/ai.dart Normal file
View File

@ -0,0 +1,17 @@
// ignore_for_file: library_private_types_in_public_api
import 'package:flutter/material.dart';
class Ai extends StatefulWidget {
const Ai({Key? key}) : super(key: key);
@override
_AiState createState() => _AiState();
}
class _AiState extends State<Ai> {
@override
Widget build(BuildContext context) {
return Container();
}
}

View File

@ -1,4 +1,6 @@
// ignore_for_file: library_private_types_in_public_api, deprecated_member_use // ignore_for_file: library_private_types_in_public_api, deprecated_member_use, depend_on_referenced_packages
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:didvan/config/design_config.dart'; import 'package:didvan/config/design_config.dart';
@ -8,20 +10,30 @@ import 'package:didvan/constants/assets.dart';
import 'package:didvan/main.dart'; import 'package:didvan/main.dart';
import 'package:didvan/models/ai/ai_chat_args.dart'; import 'package:didvan/models/ai/ai_chat_args.dart';
import 'package:didvan/models/ai/chats_model.dart'; import 'package:didvan/models/ai/chats_model.dart';
import 'package:didvan/models/ai/messages_model.dart';
import 'package:didvan/models/enums.dart'; import 'package:didvan/models/enums.dart';
import 'package:didvan/models/view/alert_data.dart'; import 'package:didvan/models/view/alert_data.dart';
import 'package:didvan/services/media/media.dart';
import 'package:didvan/utils/action_sheet.dart'; import 'package:didvan/utils/action_sheet.dart';
import 'package:didvan/utils/date_time.dart'; import 'package:didvan/utils/date_time.dart';
import 'package:didvan/views/ai/ai_chat_state.dart'; import 'package:didvan/views/ai/ai_chat_state.dart';
import 'package:didvan/views/ai/history_ai_chat_state.dart'; import 'package:didvan/views/ai/history_ai_chat_state.dart';
import 'package:didvan/views/widgets/animated_visibility.dart';
import 'package:didvan/views/widgets/didvan/icon_button.dart'; import 'package:didvan/views/widgets/didvan/icon_button.dart';
import 'package:didvan/views/widgets/didvan/text.dart'; import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/marquee_text.dart';
import 'package:didvan/views/widgets/state_handlers/empty_state.dart'; import 'package:didvan/views/widgets/state_handlers/empty_state.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:persian_number_utility/persian_number_utility.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:path/path.dart' as p;
class AiChatPage extends StatefulWidget { class AiChatPage extends StatefulWidget {
final AiChatArgs args; final AiChatArgs args;
@ -81,10 +93,8 @@ class _AiChatPageState extends State<AiChatPage> {
], ],
), ),
body: Consumer<AiChatState>( body: Consumer<AiChatState>(
builder: (BuildContext context, AiChatState state, Widget? child) { builder: (BuildContext context, AiChatState state, Widget? child) =>
return ValueListenableBuilder<bool>( state.loading
valueListenable: state.loading,
builder: (context, value, child) => value
? Center( ? Center(
child: Image.asset( child: Image.asset(
Assets.loadingAnimation, Assets.loadingAnimation,
@ -101,23 +111,37 @@ class _AiChatPageState extends State<AiChatPage> {
) )
: SingleChildScrollView( : SingleChildScrollView(
reverse: true, reverse: true,
controller: state.scrollController,
child: ListView.builder( child: ListView.builder(
itemCount: state.messages.length, itemCount: state.messages.length,
controller: state.scrollController,
shrinkWrap: true, shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
padding: const EdgeInsets.only(top: 12, bottom: 90), padding: const EdgeInsets.only(bottom: 90),
itemBuilder: (context, mIndex) {
final prompts = state.messages[mIndex].prompts;
final time = state.messages[mIndex].dateTime;
return Column(
children: [
timeLabel(context, time),
ListView.builder(
itemCount: prompts.length,
shrinkWrap: true,
physics:
const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) { itemBuilder: (context, index) {
final message = state.messages[index]; final message = prompts[index];
return messageBubble( return messageBubble(message, context,
message, context, state, index); state, index, mIndex);
}, },
), ),
), ],
); );
}, }),
), ),
bottomSheet: Container( ),
bottomSheet: Consumer<AiChatState>(
builder: (BuildContext context, AiChatState state, Widget? child) {
return Container(
width: MediaQuery.sizeOf(context).width, width: MediaQuery.sizeOf(context).width,
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8), padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
decoration: BoxDecoration( decoration: BoxDecoration(
@ -128,10 +152,12 @@ class _AiChatPageState extends State<AiChatPage> {
), ),
color: Theme.of(context).colorScheme.surface, color: Theme.of(context).colorScheme.surface,
), ),
child: ValueListenableBuilder( child: Column(
valueListenable: context.read<AiChatState>().onResponsing, mainAxisSize: MainAxisSize.min,
builder: (context, value, child) => Row( children: [
crossAxisAlignment: CrossAxisAlignment.start, if (widget.args.bot.attachment! == 2) fileContainer(context),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [ children: [
const SizedBox( const SizedBox(
width: 12, width: 12,
@ -140,34 +166,76 @@ class _AiChatPageState extends State<AiChatPage> {
width: 46, width: 46,
height: 46, height: 46,
child: Center( child: Center(
child: value child: state.onResponsing
? Center( ? Center(
child: SpinKitThreeBounce( child: SpinKitThreeBounce(
size: 18, size: 18,
color: Theme.of(context).colorScheme.primary, color: Theme.of(context)
.colorScheme
.focusedBorder,
), ),
) )
: DidvanIconButton( : DidvanIconButton(
icon: DidvanIcons.send_solid, icon: DidvanIcons.send_solid,
size: 32,
color: Theme.of(context)
.colorScheme
.focusedBorder,
onPressed: () async { onPressed: () async {
if (message.text.isEmpty) { if (state.file == null &&
message.text.isEmpty) {
return; return;
} }
final state = context.read<AiChatState>(); if (state.messages.isNotEmpty &&
state.messages.add(Prompts( DateTime.parse(
state.messages.last.dateTime)
.toPersianDateStr()
.contains(DateTime.parse(
DateTime.now()
.subtract(
const Duration(
minutes: 210))
.toIso8601String())
.toPersianDateStr())) {
state.messages.last.prompts.add(Prompts(
text: message.text, text: message.text,
file: p.basename(state.file!.path),
fileName: p.basename(state.file!.path),
finished: true, finished: true,
role: 'user', role: 'user',
createdAt: DateTime.now() createdAt: DateTime.now()
.subtract(const Duration(minutes: 210)) .subtract(
const Duration(minutes: 210))
.toIso8601String(), .toIso8601String(),
)); ));
message.clear(); } else {
state.messages.add(MessageModel(
dateTime: DateTime.now()
.subtract(
const Duration(minutes: 210))
.toIso8601String(),
prompts: [
Prompts(
text: message.text,
finished: true,
file:
p.basename(state.file!.path),
fileName:
p.basename(state.file!.path),
role: 'user',
createdAt: DateTime.now()
.subtract(const Duration(
minutes: 210))
.toIso8601String(),
)
]));
}
await state.postMessage(widget.args.bot); await state.postMessage(widget.args.bot);
message.clear();
state.file = null;
}, },
size: 32,
color: Theme.of(context).colorScheme.focusedBorder,
), ),
), ),
), ),
@ -175,16 +243,16 @@ class _AiChatPageState extends State<AiChatPage> {
width: 12, width: 12,
), ),
Expanded( Expanded(
flex: 15,
child: Form( child: Form(
child: TextFormField( child: widget.args.bot.attachment! != 1
? TextFormField(
textInputAction: TextInputAction.newline, textInputAction: TextInputAction.newline,
style: Theme.of(context).textTheme.bodyMedium, style: Theme.of(context).textTheme.bodyMedium,
maxLines: 6, maxLines: 6,
minLines: 1, minLines: 1,
// keyboardType: TextInputType.text, // keyboardType: TextInputType.text,
controller: message, controller: message,
enabled: !value, enabled: !state.onResponsing,
decoration: InputDecoration( decoration: InputDecoration(
border: InputBorder.none, border: InputBorder.none,
hintText: 'بنویسید...', hintText: 'بنویسید...',
@ -192,23 +260,201 @@ class _AiChatPageState extends State<AiChatPage> {
.textTheme .textTheme
.bodySmall! .bodySmall!
.copyWith( .copyWith(
color: color: Theme.of(context)
Theme.of(context).colorScheme.disabledText), .colorScheme
.disabledText),
), ),
onChanged: (value) {}, onChanged: (value) {},
)
: fileContainer(context),
),
),
const SizedBox(
width: 12,
),
if (widget.args.bot.attachment! != 0)
SizedBox(
width: 46,
height: 46,
child: Center(
child: PopupMenuButton(
onSelected: (value) async {
switch (value) {
case 'Pdf':
FilePickerResult? result =
await MediaService.pickPdfFile();
if (result != null) {
final File file =
File(result.files.single.path!);
state.file = file;
// Do something with the selected PDF file
}
// else {
//// User cancelled the file selection
// }
break;
case 'Image':
final pickedFile =
await MediaService.pickImage(
source: ImageSource.gallery);
File? file;
if (pickedFile != null && !kIsWeb) {
file = await ImageCropper().cropImage(
sourcePath: pickedFile.path,
androidUiSettings:
const AndroidUiSettings(
toolbarTitle: 'برش تصویر'),
iosUiSettings: const IOSUiSettings(
title: 'برش تصویر',
doneButtonTitle: 'تایید',
cancelButtonTitle: 'بازگشت',
),
compressQuality: 30,
);
if (file == null) return;
}
if (pickedFile == null) return;
state.file =
kIsWeb ? File(pickedFile.path) : file;
break;
default:
}
state.update();
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry>[
popUpBtns(value: 'Pdf'),
popUpBtns(value: 'Image'),
],
offset: const Offset(0, -140),
position: PopupMenuPosition.over,
child: Icon(
Icons.attach_file_rounded,
color:
Theme.of(context).colorScheme.focusedBorder,
),
), ),
), ),
), ),
], ],
), ),
],
));
}),
), ),
);
}
AnimatedVisibility fileContainer(BuildContext context) {
final state = context.read<AiChatState>();
String basename = '';
if (state.file != null) {
basename = p.basename(state.file!.path);
}
return AnimatedVisibility(
isVisible: state.file != null,
duration: DesignConfig.lowAnimationDuration,
child: Container(
decoration: BoxDecoration(
borderRadius: DesignConfig.mediumBorderRadius,
color: Theme.of(context).colorScheme.border,
),
padding: const EdgeInsets.fromLTRB(12, 8, 12, 8),
margin: widget.args.bot.attachment! != 1
? const EdgeInsets.symmetric(horizontal: 12)
: EdgeInsets.zero,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
const Icon(Icons.file_copy),
const SizedBox(
width: 12,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 160,
height: 24,
child: MarqueeText(
text: basename,
style: const TextStyle(fontSize: 14),
stop: const Duration(seconds: 3),
),
),
if (state.file != null)
FutureBuilder(
future: state.file!.length(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
}
return DidvanText(
'File Size ${(snapshot.data! / 1000).round()} KB',
fontSize: 12,
);
})
],
)
],
),
InkWell(
onTap: () {
state.file = null;
state.update();
},
child: const Icon(DidvanIcons.close_circle_solid))
],
), ),
), ),
); );
} }
Padding messageBubble( PopupMenuItem<dynamic> popUpBtns({required final String value}) {
Prompts message, BuildContext context, AiChatState state, int index) { return PopupMenuItem(
value: value,
height: 46,
child: Row(
children: [
const Icon(Icons.picture_as_pdf_rounded),
const SizedBox(
width: 12,
),
DidvanText(
value,
),
],
),
);
}
Center timeLabel(BuildContext context, String time) {
return Center(
child: Container(
margin: const EdgeInsets.symmetric(vertical: 12),
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.splash,
borderRadius: DesignConfig.lowBorderRadius,
),
child: DidvanText(
DateTime.parse(time).toPersianDateStr(),
style: Theme.of(context).textTheme.labelSmall,
color: DesignConfig.isDark
? Theme.of(context).colorScheme.white
: Theme.of(context).colorScheme.black,
),
),
);
}
Padding messageBubble(Prompts message, BuildContext context,
AiChatState state, int index, int mIndex) {
MarkdownStyleSheet defaultMarkdownStyleSheet = MarkdownStyleSheet( MarkdownStyleSheet defaultMarkdownStyleSheet = MarkdownStyleSheet(
code: TextStyle( code: TextStyle(
backgroundColor: Theme.of(context).colorScheme.black, backgroundColor: Theme.of(context).colorScheme.black,
@ -255,8 +501,7 @@ class _AiChatPageState extends State<AiChatPage> {
child: Container( child: Container(
constraints: BoxConstraints( constraints: BoxConstraints(
maxWidth: MediaQuery.sizeOf(context).width / 1.5), maxWidth: MediaQuery.sizeOf(context).width / 1.5),
child: state.messages[index].finished != null && child: message.finished != null && !message.finished!
!state.messages[index].finished!
? StreamBuilder<String>( ? StreamBuilder<String>(
stream: state.messageOnstream, stream: state.messageOnstream,
builder: (context, snapshot) { builder: (context, snapshot) {
@ -273,8 +518,53 @@ class _AiChatPageState extends State<AiChatPage> {
) )
: Column( : Column(
children: [ children: [
if (message.role.toString().contains('user') &&
message.file != null)
Container(
decoration: BoxDecoration(
borderRadius: DesignConfig.mediumBorderRadius,
color: Theme.of(context).colorScheme.border,
),
padding:
const EdgeInsets.fromLTRB(12, 8, 12, 8),
margin: widget.args.bot.attachment! != 1
? const EdgeInsets.symmetric(horizontal: 12)
: EdgeInsets.zero,
child: Row(
children: [
const Icon(Icons.file_copy),
const SizedBox(
width: 12,
),
Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
SizedBox(
width: 200,
child: DidvanText(
(message.fileName.toString())),
),
if (state.file != null)
FutureBuilder(
future: state.file!.length(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
}
return DidvanText(
'File Size ${(snapshot.data! / 1000).round()} KB',
fontSize: 12,
);
})
],
)
],
),
),
if (message.text != null)
Markdown( Markdown(
data: state.messages[index].text.toString(), data: message.text.toString(),
selectable: true, selectable: true,
shrinkWrap: true, shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
@ -289,7 +579,8 @@ class _AiChatPageState extends State<AiChatPage> {
child: InkWell( child: InkWell(
onTap: () async { onTap: () async {
await Clipboard.setData(ClipboardData( await Clipboard.setData(ClipboardData(
text: state.messages[index].text text: state.messages[mIndex]
.prompts[index].text
.toString())); .toString()));
ActionSheetUtils.showAlert(AlertData( ActionSheetUtils.showAlert(AlertData(
message: "متن با موفقیت کپی شد", message: "متن با موفقیت کپی شد",

View File

@ -1,7 +1,9 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io';
import 'package:didvan/models/ai/bots_model.dart'; import 'package:didvan/models/ai/bots_model.dart';
import 'package:didvan/models/ai/chats_model.dart'; import 'package:didvan/models/ai/chats_model.dart';
import 'package:didvan/models/ai/messages_model.dart';
import 'package:didvan/models/enums.dart'; import 'package:didvan/models/enums.dart';
import 'package:didvan/models/view/alert_data.dart'; import 'package:didvan/models/view/alert_data.dart';
import 'package:didvan/providers/core.dart'; import 'package:didvan/providers/core.dart';
@ -11,14 +13,16 @@ import 'package:didvan/services/network/request_helper.dart';
import 'package:didvan/utils/action_sheet.dart'; import 'package:didvan/utils/action_sheet.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:persian_number_utility/persian_number_utility.dart';
class AiChatState extends CoreProvier { class AiChatState extends CoreProvier {
Stream<String> messageOnstream = const Stream.empty(); Stream<String> messageOnstream = const Stream.empty();
List<Prompts> messages = []; List<MessageModel> messages = [];
ValueNotifier<bool> onResponsing = ValueNotifier(false); bool onResponsing = false;
ValueNotifier<bool> loading = ValueNotifier(false); bool loading = false;
final ScrollController scrollController = ScrollController(); final ScrollController scrollController = ScrollController();
int? chatId; int? chatId;
File? file;
Future<void> _scrolledEnd() async { Future<void> _scrolledEnd() async {
WidgetsBinding.instance.addPostFrameCallback((_) async { WidgetsBinding.instance.addPostFrameCallback((_) async {
@ -31,9 +35,9 @@ class AiChatState extends CoreProvier {
} }
Future<void> _onError(e) async { Future<void> _onError(e) async {
onResponsing.value = false; onResponsing = false;
messages.removeLast(); messages.last.prompts.removeLast();
messages.removeLast(); messages.last.prompts.removeLast();
messageOnstream = const Stream.empty(); messageOnstream = const Stream.empty();
await ActionSheetUtils.showAlert(AlertData( await ActionSheetUtils.showAlert(AlertData(
@ -55,22 +59,33 @@ class AiChatState extends CoreProvier {
} }
Future<void> getAllMessages(int chatId) async { Future<void> getAllMessages(int chatId) async {
loading.value = true; loading = true;
onResponsing.value = true; onResponsing = true;
final service = RequestService( final service = RequestService(
RequestHelper.aiAChat(chatId), RequestHelper.aiAChat(chatId),
); );
await service.httpGet(); await service.httpGet();
if (service.isSuccess) { if (service.isSuccess) {
messages.clear(); messages.clear();
final allMessages = service.result['prompts']; final ChatsModel allMessages =
for (var i = 0; i < allMessages.length; i++) { ChatsModel.fromJson(service.result['chat']);
messages.add(Prompts.fromJson(allMessages[i])); for (var i = 0; i < allMessages.prompts!.length; i++) {
final prompt = allMessages.prompts![i];
if (messages.isNotEmpty &&
DateTime.parse(messages.last.prompts.last.createdAt.toString())
.toPersianDateStr()
.contains(DateTime.parse(prompt.createdAt.toString())
.toPersianDateStr())) {
messages.last.prompts.add(prompt);
} else {
messages.add(MessageModel(
dateTime: prompt.createdAt.toString(), prompts: [prompt]));
}
// chats.add("chats: $chats"); // chats.add("chats: $chats");
} }
appState = AppState.idle; appState = AppState.idle;
loading.value = false; loading = false;
onResponsing.value = false; onResponsing = false;
// Add this code to scroll to maxScrollExtent after the ListView is built // Add this code to scroll to maxScrollExtent after the ListView is built
update(); update();
@ -78,30 +93,32 @@ class AiChatState extends CoreProvier {
return; return;
} }
appState = AppState.failed; appState = AppState.failed;
loading.value = false; loading = false;
onResponsing.value = false; onResponsing = false;
update(); update();
} }
Future<void> postMessage(BotsModel bot) async { Future<void> postMessage(BotsModel bot) async {
onResponsing.value = true; onResponsing = true;
update(); update();
await _scrolledEnd(); String message = messages.last.prompts.last.text!;
String message = messages.last.text!;
messages.add(Prompts( messages.last.prompts.add(Prompts(
finished: false, finished: false,
text: '...', text: '...',
role: 'bot', role: 'bot',
createdAt: DateTime.now() createdAt: DateTime.now()
.subtract(const Duration(minutes: 210)) .subtract(const Duration(minutes: 210))
.toIso8601String())); .toIso8601String()));
update();
await _scrolledEnd();
final req = await AiApiService.initial( final req = await AiApiService.initial(
url: '/${bot.id}/${bot.name}'.toLowerCase(), url: '/${bot.id}/${bot.name}'.toLowerCase(),
message: message, message: message,
chatId: chatId); chatId: chatId,
file: file);
final res = await AiApiService.getResponse(req).catchError((e) { final res = await AiApiService.getResponse(req).catchError((e) {
_onError(e); _onError(e);
throw e; throw e;
@ -113,7 +130,6 @@ class AiChatState extends CoreProvier {
messageOnstream = Stream.value(responseMessgae); messageOnstream = Stream.value(responseMessgae);
update(); update();
_scrolledEnd();
}); });
r.onDone(() async { r.onDone(() async {
@ -124,8 +140,8 @@ class AiChatState extends CoreProvier {
return; return;
} }
} }
onResponsing.value = false; onResponsing = false;
messages.last = messages.last.copyWith( messages.last.prompts.last = messages.last.prompts.last.copyWith(
finished: true, finished: true,
text: responseMessgae, text: responseMessgae,
); );

View File

@ -0,0 +1,49 @@
import 'package:didvan/views/widgets/overflow_proof_text.dart';
import 'package:flutter/widgets.dart';
import 'package:marquee/marquee.dart';
class MarqueeText extends StatelessWidget {
final String text;
final TextStyle style;
final String? marqueeText;
final TextStyle? marqueeStyle;
final Duration stop;
final TextDirection textDirection;
const MarqueeText(
{super.key,
required this.text,
required this.style,
this.marqueeText,
this.marqueeStyle,
this.stop = Duration.zero,
this.textDirection = TextDirection.ltr});
@override
Widget build(BuildContext context) {
return OverflowProofText(
text: Text(
text,
style: style,
maxLines: 1,
),
fallback: SizedBox(
height: (marqueeStyle == null
? style.fontSize!
: marqueeStyle!.fontSize!) *
DefaultTextStyle.of(context).style.height!,
child: Center(
child: Marquee(
text: marqueeText ?? text,
scrollAxis: Axis.horizontal,
textDirection: textDirection,
crossAxisAlignment: CrossAxisAlignment.center,
accelerationCurve: Curves.easeOut,
velocity: 30,
blankSpace: 34.0,
accelerationDuration: stop,
style: marqueeStyle ?? style,
),
),
));
}
}

View File

@ -0,0 +1,28 @@
import 'package:flutter/widgets.dart';
class OverflowProofText extends StatelessWidget {
const OverflowProofText(
{super.key, required this.text, required this.fallback});
final Text text;
final Widget fallback;
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child:
LayoutBuilder(builder: (BuildContext context, BoxConstraints size) {
final TextPainter painter = TextPainter(
maxLines: 1,
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
text: TextSpan(text: text.data),
);
painter.layout(maxWidth: size.maxWidth);
return painter.didExceedMaxLines ? fallback : text;
}));
}
}

View File

@ -225,6 +225,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.1.1+1" version: "1.1.1+1"
fading_edge_scrollview:
dependency: transitive
description:
name: fading_edge_scrollview
sha256: c25c2231652ce774cc31824d0112f11f653881f43d7f5302c05af11942052031
url: "https://pub.dev"
source: hosted
version: "3.0.0"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
@ -249,6 +257,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "7.0.0" version: "7.0.0"
file_picker:
dependency: "direct main"
description:
name: file_picker
sha256: "2ca051989f69d1b2ca012b2cf3ccf78c70d40144f0861ff2c063493f7c8c3d45"
url: "https://pub.dev"
source: hosted
version: "8.0.5"
file_selector_linux: file_selector_linux:
dependency: transitive dependency: transitive
description: description:
@ -677,6 +693,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "7.2.2" version: "7.2.2"
marquee:
dependency: "direct main"
description:
name: marquee
sha256: "4b5243d2804373bdc25fc93d42c3b402d6ec1f4ee8d0bb72276edd04ae7addb8"
url: "https://pub.dev"
source: hosted
version: "2.2.3"
matcher: matcher:
dependency: transitive dependency: transitive
description: description:

View File

@ -88,6 +88,8 @@ dependencies:
chewie: ^1.8.3 chewie: ^1.8.3
typewritertext: ^3.0.8 typewritertext: ^3.0.8
flutter_markdown: ^0.7.3+1 flutter_markdown: ^0.7.3+1
file_picker: ^8.0.5
marquee: ^2.2.3
# onesignal_flutter: ^3.5.0 # onesignal_flutter: ^3.5.0
dev_dependencies: dev_dependencies: