Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt.: auto scroll after submission #44

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/data/model/chat/gpt_next.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ abstract final class GPTNextConvertor {
'user' => ChatRole.user,
'assistant' => ChatRole.assist,
'system' => ChatRole.system,
'tool' => ChatRole.tool,
final role => throw ArgumentError('role: $role'),
};
final contentEnum = ChatContent.text(content);
Expand Down
7 changes: 6 additions & 1 deletion lib/data/model/chat/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ final class ChatHistory {
bool get isInitHelp =>
name == l10n.help &&
items.length == 1 &&
items.first.role == ChatRole.system &&
items.first.role.isSystem &&
items.first.content.length == 1 &&
items.first.content.first.raw.contains(Urls.repoIssue);
}
Expand Down Expand Up @@ -259,6 +259,11 @@ enum ChatRole {
tool,
;

bool get isUser => this == user;
bool get isAssist => this == assist;
bool get isSystem => this == system;
bool get isTool => this == tool;

OpenAIChatMessageRole get toOpenAI => switch (this) {
assist => OpenAIChatMessageRole.assistant,
system => OpenAIChatMessageRole.system,
Expand Down
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
"themeMode": "Theme mode",
"thirdParty": "Third Party",
"tool": "Tool",
"toolFinishTip": "Tools invocation completed.",
"toolAvailability": "Only on gpt-4o, gpt-4t & etc.",
"toolFinishTip": "🪄 Tools invocation completed.",
"tts": "TTS",
"untitled": "Untitled",
"update": "Update",
Expand Down
3 changes: 2 additions & 1 deletion lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
"themeMode": "主题模式",
"thirdParty": "第三方",
"tool": "工具",
"toolFinishTip": "工具调用完成",
"toolAvailability": "仅 gpt-4o、gpt-4t 等支持",
"toolFinishTip": "🪄 工具调用完成",
"tts": "文字转语音",
"untitled": "未命名",
"update": "更新",
Expand Down
15 changes: 6 additions & 9 deletions lib/view/page/home/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ class _ChatPageState extends State<_ChatPage>
final node = _chatItemRNMap.putIfAbsent(chatItem.id, () => RNode());
return InkWell(
borderRadius: BorderRadius.circular(13),
onTap: chatItem.role == ChatRole.tool
? () {
_MarkdownCopyPage.go(context, chatItem);
}
: null,
onTap: () {
if (chatItem.role.isTool) _MarkdownCopyPage.go(context, chatItem);
},
onLongPress: () {
final funcs = _buildChatItemFuncs(chatItems, chatItem);
context.showRoundDialog(
Expand Down Expand Up @@ -181,9 +179,9 @@ class _ChatPageState extends State<_ChatPage>

Widget _buildChatItemContent(ChatHistoryItem chatItem) {
if (_loadingToolReplies.contains(chatItem.id)) {
return UIs.centerSizedLoadingSmall;
return const LinearProgressIndicator();
}
if (chatItem.role == ChatRole.tool) {
if (chatItem.role.isTool) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expand Down Expand Up @@ -271,8 +269,7 @@ class _ChatPageState extends State<_ChatPage>
List<ChatHistoryItem> chatItems,
ChatHistoryItem chatItem,
) {
final replayEnabled =
chatItem.role == ChatRole.user && Stores.setting.replay.fetch();
final replayEnabled = chatItem.role.isUser && Stores.setting.replay.fetch();

Widget buildCircleBtn({
required VoidCallback onTap,
Expand Down
4 changes: 2 additions & 2 deletions lib/view/page/home/ctrl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ Set<String> findAllDuplicateIds(Map<String, ChatHistory> allHistories) {
if (history == null) continue;
for (final item in history.items) {
/// Only compare assist's reply which is variety
if (item.role != ChatRole.assist) continue;
if (!item.role.isAssist) continue;
final content = item.toMarkdown;
contentMap.putIfAbsent(content, () => []).add(id);
final time = timeMap[id];
Expand Down Expand Up @@ -416,7 +416,7 @@ void _onTapReplay(
String chatId,
ChatHistoryItem item,
) async {
if (item.role != ChatRole.user) return;
if (!item.role.isUser) return;
final sure = await context.showRoundDialog<bool>(
title: l10n.attention,
child: Text('${l10n.replay} ?'),
Expand Down
1 change: 0 additions & 1 deletion lib/view/page/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:dart_openai/dart_openai.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
//import 'package:flutter_tiktoken/flutter_tiktoken.dart';
import 'package:gpt_box/core/ext/chat_history.dart';
import 'package:gpt_box/core/ext/file.dart';
Expand Down
7 changes: 3 additions & 4 deletions lib/view/page/home/req.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ Future<void> _onCreateText(
);
_genChatTitle(context, chatId, config);
_inputCtrl.clear();
_autoScroll(chatId);

final useTools = Stores.setting.useTools.fetch() && forceUseTool;
if (useTools) {
final resp = await OpenAI.instance.chat.create(
model: config.model,
messages: [...historyCarried().reversed, questionForApi.toOpenAI],
tools: OpenAIFuncCalls.tools,
toolChoice: 'auto',
);

final toolCalls = resp.choices.firstOrNull?.message.toolCalls;
Expand Down Expand Up @@ -505,7 +505,7 @@ void _onReplay({

// remove exist reply
if (itemIdx + 1 < chatHistory.items.length &&
chatHistory.items[itemIdx + 1].role == ChatRole.assist) {
chatHistory.items[itemIdx + 1].role.isAssist) {
chatHistory.items.removeAt(itemIdx + 1);
}

Expand Down Expand Up @@ -533,8 +533,7 @@ void _onErr(Object e, StackTrace s, String chatId, String action) {
// If previous msg is assistant reply and it's empty, remove it
if (workingChat.items.isNotEmpty) {
final last = workingChat.items.last;
if (last.role == ChatRole.assist &&
last.content.every((e) => e.raw.isEmpty)) {
if (last.role.isAssist && last.content.every((e) => e.raw.isEmpty)) {
workingChat.items.removeLast();
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/view/page/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class _SettingPageState extends State<SettingPage> {

Widget _buildMore() {
final children = [
_buildUseTool(),
_buildScrollSwitchChat(),
_buildSaveErrChat(),
_buildCompressImg(),
Expand Down Expand Up @@ -777,4 +778,13 @@ class _SettingPageState extends State<SettingPage> {
trailing: StoreSwitch(prop: _store.scrollSwitchChat),
);
}

Widget _buildUseTool() {
return ListTile(
leading: const Icon(Icons.functions),
title: Text(l10n.tool),
subtitle: Text(l10n.toolAvailability, style: UIs.textGrey),
trailing: StoreSwitch(prop: _store.useTools),
);
}
}
Loading