237 lines
9.0 KiB
Dart
237 lines
9.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;
|
|
String? _agentname;
|
|
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(() {
|
|
_title = agentEntry['name'];
|
|
_agentname = agentEntry['agentname'];
|
|
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),
|
|
)
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 8.0),
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Row(
|
|
children: [
|
|
Text('agent id: ${widget.agentId}, identifier: ${_agentname}'),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
TextEditingController _textFieldController = TextEditingController();
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: Text('Enter new identifier'),
|
|
content: TextField(
|
|
controller: _textFieldController,
|
|
decoration: InputDecoration(hintText: "agent identifier"),
|
|
),
|
|
actions: <Widget>[
|
|
ElevatedButton(
|
|
child: Text('CANCEL'),
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
),
|
|
ElevatedButton(
|
|
child: Text('OK'),
|
|
onPressed: () {
|
|
Backend.sendAction("update-agent", {
|
|
'agentId': widget.agentId,
|
|
'agentname': _textFieldController.text,
|
|
}).then((response) {
|
|
final message = response['error'] != null ?
|
|
'Error updating agent identifier.' :
|
|
'Agent identifier updated.';
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(message)),
|
|
);
|
|
});
|
|
Navigator.pop(context);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
child: Text('Update identifier'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
TextEditingController _textFieldController = TextEditingController();
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: Text('Enter new agent name'),
|
|
content: TextField(
|
|
controller: _textFieldController,
|
|
decoration: InputDecoration(hintText: "name"),
|
|
),
|
|
actions: <Widget>[
|
|
ElevatedButton(
|
|
child: Text('CANCEL'),
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
),
|
|
ElevatedButton(
|
|
child: Text('OK'),
|
|
onPressed: () {
|
|
Backend.sendAction("update-agent", {
|
|
'agentId': widget.agentId,
|
|
'name': _textFieldController.text,
|
|
}).then((response) {
|
|
final message = response['error'] != null ?
|
|
'Error updating agent name.' :
|
|
'Agent name updated.';
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(message)),
|
|
);
|
|
});
|
|
Navigator.pop(context);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
child: Text('Update name'),
|
|
),
|
|
]
|
|
)
|
|
)
|
|
),
|
|
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'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|