Skip to content

Commit

Permalink
gai: UI supports scripting extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
patniemeyer committed Dec 17, 2024
1 parent d891c89 commit 3f03311
Show file tree
Hide file tree
Showing 15 changed files with 1,192 additions and 255 deletions.
148 changes: 148 additions & 0 deletions gai-frontend/lib/chat/api/user_preferences_chat.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import 'package:orchid/api/preferences/observable_preference.dart';
import 'package:orchid/api/preferences/user_preferences.dart';

class UserScript {
// A user-entered name for the script entry.
final String name;

// The script body, or, if script begins with 'https' a URL from which to fetch the script.
final String script;

// Whether the script is selected for use.
final bool selected;

UserScript(this.name, this.script, this.selected);

UserScript.fromJson(Map<String, dynamic> json)
: name = json['name'],
script = json['script'],
selected = json['selected'];

Map<String, dynamic> toJson() => {
'name': name,
'script': script,
'selected': selected,
};

@override
String toString() {
return 'UserScript{name: $name, script: $script, selected: $selected}';
}
}

class UserPreferencesScripts {
static final UserPreferencesScripts _singleton = UserPreferencesScripts._internal();

factory UserPreferencesScripts() {
return _singleton;
}

UserPreferencesScripts._internal();

// Temporary implementation for single script management.
final userScript = ObservableStringPreference(_UserPreferenceKeys.Script);
final userScriptEnabled = ObservableBoolPreference(_UserPreferenceKeys.ScriptEnabled);

/*
ObservablePreference<List<UserScript>> keys = ObservablePreference(
key: _UserPreferenceKeys.Scripts,
getValue: (key) {
return _getScripts();
},
putValue: (key, keys) {
return _setKeys(keys);
});
static List<UserScript> _getScripts() {
String? value =
UserPreferences().getStringForKey(_UserPreferenceKeyKeys.Keys);
if (value == null) {
return [];
}
try {
var jsonList = jsonDecode(value) as List<dynamic>;
return jsonList
.map((el) {
try {
return StoredEthereumKey.fromJson(el);
} catch (err) {
log("Error decoding key: $err");
return null;
}
})
.whereType<StoredEthereumKey>()
.toList();
} catch (err) {
log("Error retrieving keys!: $value, $err");
return [];
}
}
static Future<bool> _setKeys(List<StoredEthereumKey>? keys) async {
print("setKeys: storing keys: ${jsonEncode(keys)}");
if (keys == null) {
return UserPreferences()
.sharedPreferences()
.remove(_UserPreferenceKeyKeys.Keys.toString());
}
try {
var value = jsonEncode(keys);
return await UserPreferences()
.putStringForKey(_UserPreferenceKeyKeys.Keys, value);
} catch (err) {
log("Error storing keys!: $err");
return false;
}
}
/// Remove a key from the user's keystore.
Future<bool> removeKey(StoredEthereumKeyRef keyRef) async {
var keysList = ((keys.get()) ?? []);
try {
keysList.removeWhere((key) => key.uid == keyRef.keyUid);
} catch (err) {
log("account: error removing key: $keyRef");
return false;
}
await keys.set(keysList);
return true;
}
/// Add a key to the user's keystore if it does not already exist.
Future<void> addKey(StoredEthereumKey key) async {
return addKeyIfNeeded(key);
}
/// Add a key to the user's keystore if it does not already exist.
Future<void> addKeyIfNeeded(StoredEthereumKey key) async {
log("XXX: addKeyIfNeeded: add key if needed: $key");
var curKeys = keys.get() ?? [];
if (!curKeys.contains(key)) {
log("XXX: addKeyIfNeeded: adding key");
await keys.set(curKeys + [key]);
} else {
log("XXX: addKeyIfNeeded: duplicate key");
}
}
/// Add a list of keys to the user's keystore.
Future<void> addKeys(List<StoredEthereumKey> newKeys) async {
var allKeys = ((keys.get()) ?? []) + newKeys;
await keys.set(allKeys);
}
*/

///
/// End: Keys
///

}

enum _UserPreferenceKeys implements UserPreferenceKey {
Script,
ScriptEnabled,
// Scripts,
}
74 changes: 36 additions & 38 deletions gai-frontend/lib/chat/auth_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,47 +109,45 @@ class _AuthDialogState extends State<AuthDialog> {
return SizedBox(
key: ValueKey(widget.accountDetailNotifier?.hashCode ?? 'key'),
width: 500,
child: IntrinsicHeight(
child: ValueListenableBuilder(
valueListenable: widget.accountDetailNotifier,
builder: (BuildContext context, AccountDetail? accountDetail,
Widget? child) {
return OrchidTitledPanel(
highlight: false,
opaque: true,
titleText: "Connect to Provider",
onDismiss: () {
Navigator.pop(context);
},
body: DefaultTabController(
length: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const TabBar(
tabs: [
Tab(text: 'Orchid Account'),
Tab(text: 'Auth Token'),
child: ValueListenableBuilder(
valueListenable: widget.accountDetailNotifier,
builder: (BuildContext context, AccountDetail? accountDetail,
Widget? child) {
return OrchidTitledPanel(
highlight: false,
opaque: true,
titleText: "Connect to Provider",
onDismiss: () {
Navigator.pop(context);
},
body: DefaultTabController(
length: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const TabBar(
tabs: [
Tab(text: 'Orchid Account'),
Tab(text: 'Auth Token'),
],
labelColor: Colors.white,
unselectedLabelColor: Colors.white60,
indicatorColor: Colors.white,
),
SizedBox(
height: 560,
child: TabBarView(
children: [
_buildOrchidAccountTab(accountDetail),
_buildAuthTokenTab(),
],
labelColor: Colors.white,
unselectedLabelColor: Colors.white60,
indicatorColor: Colors.white,
),
SizedBox(
height: 500,
child: TabBarView(
children: [
_buildOrchidAccountTab(accountDetail),
_buildAuthTokenTab(),
],
),
),
],
),
),
],
),
);
}),
),
),
);
}),
);
}

Expand Down
Loading

0 comments on commit 3f03311

Please sign in to comment.