Skip to content

Commit

Permalink
Display connection details in gear menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
danopato committed Feb 3, 2025
1 parent 8206ddb commit 0ef163c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 114 deletions.
27 changes: 9 additions & 18 deletions gai-frontend/lib/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ class _ChatViewState extends State<ChatView> {
_modelManager.getModelsOrDefault(_userSelectedModelIds);

// Account
// This should be wrapped up in a provider. See WIP in vpn app.
EthereumAddress? _funder;
BigInt? _signerKey;
AccountDetailPoller? _accountDetail;
final _accountDetailNotifier = ValueNotifier<AccountDetail?>(null);

// Auth
// AuthTokenMethod _authTokenMethod = AuthTokenMethod.manual;
String? _authToken;
String? _inferenceUrl;
String? _scriptURLParam;
Expand Down Expand Up @@ -96,21 +94,12 @@ class _ChatViewState extends State<ChatView> {
}

void _initScripting() {
// final script = UserPreferencesScripts().userScript.get();
// log('User script on start: $script');

ChatScripting.init(
// If a script URL is provided, it will be loaded.
// url: 'lib/extensions/filter_example.js',
url: _scriptURLParam,
// If debugMode is true, the script will be re-loaded before each invocation
// debugMode: true,

// Allow a provided script url param to override the stored script
script: _scriptURLParam == null
? UserPreferencesScripts().userScript.get()
: null,

providerManager: _providerManager,
modelManager: _modelManager,
getUserSelectedModels: () => _userSelectedModels,
Expand Down Expand Up @@ -157,7 +146,6 @@ class _ChatViewState extends State<ChatView> {
);
}

// This should be wrapped up in a provider. See WIP in vpn app.
void _accountChanged() async {
log("chat: accountChanged: $_account");
_accountDetail?.cancel();
Expand Down Expand Up @@ -247,7 +235,6 @@ class _ChatViewState extends State<ChatView> {
_chatHistory.addMessage(message);
});
scrollMessagesDown();
// log('Chat history updated: ${_chatHistory.messages.length}, ${_chatHistory.messages}');
}

void _updateSelectedModels(List<String> modelIds) {
Expand All @@ -272,7 +259,6 @@ class _ChatViewState extends State<ChatView> {
initialInferenceUrl: _inferenceUrl,
accountDetailNotifier: _accountDetailNotifier,
onAccountChanged: (chain, funder, signerKey) {
// log('onAccountChanged: Account changed: $chain, $funder, $signerKey');
setState(() {
_selectedChain = chain;
_funder = funder;
Expand Down Expand Up @@ -354,7 +340,6 @@ class _ChatViewState extends State<ChatView> {

// Manage the prompt UI
_promptTextController.clear();
// FocusManager.instance.primaryFocus?.unfocus(); // ?

// If we have a script selected allow it to handle the prompt
if (ChatScripting.enabled) {
Expand Down Expand Up @@ -390,7 +375,7 @@ class _ChatViewState extends State<ChatView> {
if (chatResponse != null) {
_handleChatResponseDefaultBehavior(chatResponse);
} else {
// The provider connection should have logged the issue. Do nothing.
// The provider connection should have logged the issue. Do nothing.
}
} catch (e) {
_addMessage(
Expand Down Expand Up @@ -605,12 +590,16 @@ class _ChatViewState extends State<ChatView> {
).left(8),

// Settings button
_buildSettingsButton(buttonHeight).left(8),
_buildSettingsButton(
buttonHeight,
authToken: _authToken ?? _providerManager.providerConnection?.inferenceClient?.authToken,
inferenceUrl: _inferenceUrl ?? _providerManager.providerConnection?.inferenceClient?.baseUrl,
).left(8),
],
);
}

SizedBox _buildSettingsButton(double buttonHeight) {
SizedBox _buildSettingsButton(double buttonHeight, {String? authToken, String? inferenceUrl}) {
final settingsIconSize = buttonHeight * 1.5;
return SizedBox(
width: settingsIconSize,
Expand Down Expand Up @@ -644,6 +633,8 @@ class _ChatViewState extends State<ChatView> {
editUserScript: () {
UserScriptDialog.show(context);
},
authToken: authToken,
inferenceUrl: inferenceUrl,
),
),
);
Expand Down
165 changes: 69 additions & 96 deletions gai-frontend/lib/chat/chat_settings_button.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/services.dart';
import 'package:orchid/chat/identicon_options_menu_item.dart';
import 'package:orchid/chat/scripting/code_viewer/scripts_menu_item.dart';
import 'package:orchid/orchid/orchid.dart';
Expand All @@ -17,6 +18,8 @@ class ChatSettingsButton extends StatefulWidget {
final VoidCallback onPartyModeChanged;
final VoidCallback onClearChat;
final VoidCallback editUserScript;
final String? authToken;
final String? inferenceUrl;

const ChatSettingsButton({
Key? key,
Expand All @@ -28,6 +31,8 @@ class ChatSettingsButton extends StatefulWidget {
required this.onPartyModeChanged,
required this.onClearChat,
required this.editUserScript,
this.authToken,
this.inferenceUrl,
}) : super(key: key);

@override
Expand All @@ -40,12 +45,68 @@ class _ChatSettingsButtonState extends State<ChatSettingsButton> {
final _textStyle = OrchidText.medium_16_025.copyWith(height: 2.0);
bool _buttonSelected = false;

Future<void> _copyToClipboard(String text, String label) async {
await Clipboard.setData(ClipboardData(text: text));
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('$label copied to clipboard')),
);
}
}

List<PopupMenuEntry<dynamic>> _buildInferenceInfo() {
if (widget.authToken == null || widget.inferenceUrl == null) {
return [];
}

return [
const PopupMenuDivider(height: 1.0),
PopupMenuItem<String>(
height: _height,
child: Theme(
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
child: ExpansionTile(
title: Text('Inference Connection', style: _textStyle),
children: [
ListTile(
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Auth Token:', style: TextStyle(color: Colors.white)),
Text(
widget.authToken!,
style: const TextStyle(fontSize: 12, color: Colors.white70),
overflow: TextOverflow.ellipsis,
),
TextButton(
onPressed: () => _copyToClipboard(widget.authToken!, 'Auth token'),
child: const Text('Copy Token', style: TextStyle(color: Colors.white)),
),
const SizedBox(height: 8),
const Text('Inference URL:', style: TextStyle(color: Colors.white)),
Text(
widget.inferenceUrl!,
style: const TextStyle(fontSize: 12, color: Colors.white70),
overflow: TextOverflow.ellipsis,
),
TextButton(
onPressed: () => _copyToClipboard(widget.inferenceUrl!, 'Inference URL'),
child: const Text('Copy URL', style: TextStyle(color: Colors.white)),
),
],
),
),
],
),
),
),
];
}

@override
Widget build(BuildContext context) {
final buildCommit =
const String.fromEnvironment('build_commit', defaultValue: '...');
final githubUrl =
'https://github.com/OrchidTechnologies/orchid/tree/$buildCommit/web-ethereum/dapp2';
final buildCommit = const String.fromEnvironment('build_commit', defaultValue: '...');
final githubUrl = 'https://github.com/OrchidTechnologies/orchid/tree/$buildCommit/web-ethereum/dapp2';

return OrchidPopupMenuButton<dynamic>(
width: 30,
Expand Down Expand Up @@ -124,30 +185,6 @@ class _ChatSettingsButtonState extends State<ChatSettingsButton> {
),
div,

/*
// party mode
PopupMenuItem<String>(
onTap: widget.onPartyModeChanged,
height: _height,
child: SizedBox(
width: _width,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Party Mode", style: _textStyle),
Icon(
widget.partyMode
? Icons.check_box_outlined
: Icons.check_box_outline_blank,
color: Colors.white,
),
],
),
),
),
div,
*/

// scripts
SubmenuPopopMenuItemBuilder<String>(
builder: (bool expanded) => ScriptsMenuItem(
Expand All @@ -156,7 +193,6 @@ class _ChatSettingsButtonState extends State<ChatSettingsButton> {
expanded: expanded,
textStyle: _textStyle,
editScript: () {
log('edit script');
Navigator.pop(context);
widget.editUserScript();
},
Expand All @@ -171,6 +207,10 @@ class _ChatSettingsButtonState extends State<ChatSettingsButton> {
textStyle: _textStyle,
),
),

// Inference connection info
..._buildInferenceInfo(),

div,

// build version
Expand Down Expand Up @@ -198,71 +238,4 @@ class _ChatSettingsButtonState extends State<ChatSettingsButton> {
),
);
}

/*
Future _openLicensePage(BuildContext context) {
// TODO:
return Future.delayed(millis(100), () async {});
}
Widget _buildLanguagePref(bool expanded) {
return UserPreferencesUI().languageOverride.builder((languageOverride) {
return ExpandingPopupMenuItem(
expanded: expanded,
title: s.language,
currentSelectionText: OrchidLanguage.languages[languageOverride] ?? '',
expandedContent: _languageOptions(languageOverride),
expandedHeight: 690,
textStyle: _textStyle,
);
});
}
*/

Widget _languageOptions(String? selected) {
var items = OrchidLanguage.languages.keys
.map(
(key) => _listMenuItem(
selected: key == selected,
title: OrchidLanguage.languages[key]!,
onTap: () async {
await UserPreferencesUI().languageOverride.set(key);
},
),
)
.toList()
.cast<PopupMenuEntry>()
.separatedWith(
const PopupMenuDivider(height: 1.0),
);

items.insert(
0,
_listMenuItem(
selected: selected == null,
title: s.systemDefault,
onTap: () async {
await UserPreferencesUI().languageOverride.set(null);
},
));

return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: items,
);
}

PopupMenuItem _listMenuItem({
required bool selected,
required String title,
required VoidCallback onTap,
}) {
return OrchidPopupMenuItemUtils.listMenuItem(
context: context,
selected: selected,
title: title,
onTap: onTap,
textStyle: _textStyle,
);
}
}
3 changes: 3 additions & 0 deletions gai-frontend/lib/chat/inference_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class InferenceClient {
final String baseUrl;
String? _authToken;

// Add getter for auth token
String? get authToken => _authToken;

InferenceClient({required String baseUrl})
: baseUrl = _normalizeBaseUrl(baseUrl);

Expand Down

0 comments on commit 0ef163c

Please sign in to comment.