Files
hub/frontend/lib/agent_screen.dart
T

137 lines
4.0 KiB
Dart

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;
// There should be an additional property to indicate whether this field is deactivated/removed/reset
// meaning it will be set as 'null' (default) rather than any string, otherwise only an empty string can be set
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();
String description = "";
List<ConfigField> configFields = [];
@override
void initState() {
super.initState();
_loadAgentInfo();
_loadConfigFields();
}
void _loadAgentInfo() async {
final agentsResponse = await Backend.sendAction('get-agents', {});
var agentEntry = null;
for (var entry in agentsResponse['agents']) {
if (entry['id'] == widget.agentId) {
agentEntry = entry;
}
}
if (agentEntry == null) {
print("Agent ${widget.agentId} not found in agents list");
return;
}
final kindResponse = await Backend.sendAction('get-agent-kind-info', {'kind': agentEntry['kind']});
setState(() {
description = kindResponse['info']['description'];
});
}
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: [
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Align(
alignment: Alignment.centerLeft,
child: Text(description),
)
),
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'),
),
],
),
),
),
);
}
}