Full agent admin via API and UI

This commit is contained in:
Shivering Ocelot committed 2025-05-25 18:59:23 -05:00
1 parent 5351144edf
commit 08f0668997
7 files changed
+389 -32

No files matched your search

+129 -8
View File
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'backend.dart';
import 'chat_screen.dart';
import 'create_chat_screen.dart';
import 'agent_screen.dart';
class AgentEntry {
final int id;
@@ -25,18 +25,139 @@ class AgentListScreen extends StatelessWidget {
return chats;
}
static Future<List<String>> getAgentKinds() async {
final response = await Backend.sendAction("get-agent-kinds", {});
return response['kinds'];
}
// Function to create a new agent and navigate
Future<void> createAgent(BuildContext context, String name, String kind) async {
try {
// Pass the 'kind' to the backend as well
final response = await Backend.sendAction("create-agent", {"name": name, "kind": kind});
final int newAgentId = response['agentId']; // Assuming your backend returns the new agent's ID
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Agent $name created.')),
);
// Navigate to the new agent's chat screen
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AgentScreen(agentId: newAgentId),
),
);
}
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to create agent: $e')),
);
}
}
}
// Function to show the create agent dialog
void _showCreateAgentDialog(BuildContext context) {
TextEditingController usernameController = TextEditingController();
String? selectedKind; // To hold the selected kind value
showDialog(
context: context,
builder: (BuildContext dialogContext) {
return AlertDialog(
title: const Text('Create New Agent'),
content: SingleChildScrollView( // Use SingleChildScrollView to prevent overflow
child: ListBody(
children: <Widget>[
TextField(
controller: usernameController,
decoration: const InputDecoration(hintText: "Enter agent name"),
),
const SizedBox(height: 16.0), // Spacing between fields
FutureBuilder<List<String>>(
future: getAgentKinds(), // Call the asynchronous function to get kinds
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error loading kinds: ${snapshot.error}'));
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const Center(child: Text('No agent kinds available.'));
} else {
final kinds = snapshot.data!;
// Ensure a default selection if none is made, or allow null
if (selectedKind == null && kinds.isNotEmpty) {
selectedKind = kinds.first; // Set the first item as default
}
return StatefulBuilder( // Use StatefulBuilder to update dropdown state
builder: (context, setState) {
return DropdownButtonFormField<String>(
value: selectedKind,
hint: const Text('Select Kind'),
decoration: const InputDecoration(
labelText: 'Kind',
border: OutlineInputBorder(),
),
items: kinds.map((String kind) {
return DropdownMenuItem<String>(
value: kind,
child: Text(kind),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
selectedKind = newValue;
});
},
);
},
);
}
},
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.of(dialogContext).pop(); // Dismiss the dialog
},
),
TextButton(
child: const Text('Create'),
onPressed: () {
if (usernameController.text.isNotEmpty && selectedKind != null) {
Navigator.of(dialogContext).pop(); // Dismiss the dialog first
createAgent(context, usernameController.text, selectedKind!); // Pass selectedKind
} else {
if (dialogContext.mounted) {
ScaffoldMessenger.of(dialogContext).showSnackBar(
const SnackBar(content: Text('Agent name and kind cannot be empty')),
);
}
}
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Agents')),
body: Column( // Use a Column to stack widgets vertically
body: Column(
children: [
ListTile(
leading: const Icon(Icons.add),
title: const Text('Create'),
onTap: () {
print("TODO: Create new agents");
},
onTap: () => _showCreateAgentDialog(context), // Open the dialog
),
const Divider(height: 1),
Expanded(
@@ -59,12 +180,12 @@ class AgentListScreen extends StatelessWidget {
title: Text(agent.name),
onTap: () {
print("Navigate to agent ${agent.id}");
/*Navigator.push(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChatScreen(agentId: agent.id),
builder: (context) => AgentScreen(agentId: agent.id),
),
);*/
);
},
);
},
+105
View File
@@ -0,0 +1,105 @@
import 'package:flutter/material.dart';
import 'app_menu.dart';
import 'backend.dart';
import 'chat_screen.dart';
class ConfigField {
final String name;
final String type;
final TextEditingController controller;
ConfigField({required this.name, required this.type, required this.controller});
}
class AgentScreen extends StatefulWidget {
final int agentId;
AgentScreen({required this.agentId});
@override
_AgentScreenState createState() => _AgentScreenState();
}
class _AgentScreenState extends State<AgentScreen> {
String? _title;
final TextEditingController nameController = TextEditingController();
List<ConfigField> configFields = [];
@override
void initState() {
super.initState();
_loadConfigFields();
}
void _loadConfigFields() async {
final response = await Backend.sendAction('get-agent-config-fields', {'agentId': widget.agentId});
setState(() {
configFields = response['fields'].map((jsonField) {
return ConfigField(
name: jsonField['name'],
type: jsonField['type'],
controller: TextEditingController(text: jsonField['value']),
);
}).toList();
});
}
void _saveChanges() async {
// Backend.invalidateCache(agent: true);
final dataObject = {};
for (var configField in configFields) {
dataObject[configField.name] = configField.controller.text;
}
await Backend.sendAction("update-agent", {
'agentId': widget.agentId,
'data': dataObject,
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Agent Updated.')),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_title?? 'Agent ${widget.agentId}'),
actions: [
IconButton(icon: Icon(Icons.settings), onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (_) => AppMenu()));
}),
],
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
ListView.builder(
shrinkWrap: true,
itemCount: configFields.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: TextField(
keyboardType: TextInputType.multiline,
minLines: 1,
maxLines: 8,
controller: configFields[index].controller,
decoration: InputDecoration(labelText: configFields[index].name),
),
);
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _saveChanges,
child: Text('Save'),
),
],
),
),
),
);
}
}