aboutsummaryrefslogtreecommitdiff
path: root/frontend
diff options
context:
space:
mode:
authorGravatar hunteraraujo <hunter_araujo@msn.com> 2023-10-06 14:55:47 -0700
committerGravatar GitHub <noreply@github.com> 2023-10-06 14:55:47 -0700
commit0f2126e6f084396ed4966747aa634ac27e7d6564 (patch)
tree26cea39694d6cdcf4c8bba0168b2c26c7432a34d /frontend
parentUpdate incorrect docstring - "uri" to "relative_path" (diff)
downloadAuto-GPT-0f2126e6f084396ed4966747aa634ac27e7d6564.tar.gz
Auto-GPT-0f2126e6f084396ed4966747aa634ac27e7d6564.tar.bz2
Auto-GPT-0f2126e6f084396ed4966747aa634ac27e7d6564.zip
Implement Artifact Class for Enhanced Data Handling (#5585)
Diffstat (limited to 'frontend')
-rw-r--r--frontend/lib/models/artifact.dart62
-rw-r--r--frontend/lib/models/chat.dart8
-rw-r--r--frontend/lib/models/step.dart11
-rw-r--r--frontend/lib/views/chat/chat_view.dart12
4 files changed, 77 insertions, 16 deletions
diff --git a/frontend/lib/models/artifact.dart b/frontend/lib/models/artifact.dart
new file mode 100644
index 000000000..7e6f3d075
--- /dev/null
+++ b/frontend/lib/models/artifact.dart
@@ -0,0 +1,62 @@
+/// `Artifact` class represents an artifact either created by or submitted to the agent.
+///
+/// Each artifact object contains an ID, a flag indicating if it was created by the agent,
+/// a file name, and a relative path of the artifact in the agent's workspace.
+class Artifact {
+ // ID of the artifact.
+ final String artifactId;
+
+ // Whether the artifact has been created by the agent.
+ final bool agentCreated;
+
+ // Filename of the artifact.
+ final String fileName;
+
+ // Relative path of the artifact in the agent's workspace.
+ final String? relativePath;
+
+ /// Creates an `Artifact` instance.
+ ///
+ /// - `artifactId`: ID of the artifact. This is a required field.
+ /// - `agentCreated`: Indicates whether the artifact was created by the agent. This is a required field.
+ /// - `fileName`: The file name of the artifact. This is a required field.
+ /// - `relativePath`: The relative path of the artifact in the agent's workspace. This field can be null.
+ Artifact({
+ required this.artifactId,
+ required this.agentCreated,
+ required this.fileName,
+ this.relativePath,
+ });
+
+ /// Creates an `Artifact` instance from a map.
+ ///
+ /// This constructor is used for deserializing a JSON object into an `Artifact` instance.
+ /// It expects all the required fields to be present; otherwise, an error will be thrown.
+ ///
+ /// - `map`: The map from which the `Artifact` instance will be created.
+ factory Artifact.fromJson(Map<String, dynamic> map) {
+ if (map['artifact_id'] == null ||
+ map['agent_created'] == null ||
+ map['file_name'] == null) {
+ throw const FormatException(
+ 'Invalid JSON: Missing one of the required fields.');
+ }
+
+ return Artifact(
+ artifactId: map['artifact_id'],
+ agentCreated: map['agent_created'],
+ fileName: map['file_name'],
+ relativePath: map['relative_path'],
+ );
+ }
+
+ /// Converts the `Artifact` instance into a JSON object.
+ ///
+ /// This can be useful for encoding the `Artifact` object into a JSON string.
+ Map<String, dynamic> toJson() => {
+ 'artifact_id': artifactId,
+ 'agent_created': agentCreated,
+ 'file_name': fileName,
+ 'relative_path': relativePath,
+ };
+}
diff --git a/frontend/lib/models/chat.dart b/frontend/lib/models/chat.dart
index e9a281f41..6002f147f 100644
--- a/frontend/lib/models/chat.dart
+++ b/frontend/lib/models/chat.dart
@@ -1,3 +1,4 @@
+import 'package:auto_gpt_flutter_client/models/artifact.dart';
import 'package:auto_gpt_flutter_client/models/message_type.dart';
/// Represents a chat message related to a specific task.
@@ -8,7 +9,7 @@ class Chat {
final DateTime timestamp;
final MessageType messageType;
final Map<String, dynamic>? jsonResponse;
- final List<dynamic> artifacts;
+ final List<Artifact> artifacts;
Chat({
required this.id,
@@ -29,7 +30,10 @@ class Chat {
timestamp: DateTime.parse(map['timestamp']),
messageType: MessageType.values.firstWhere(
(e) => e.toString() == 'MessageType.${map['messageType']}'),
- artifacts: List<dynamic>.from(map['artifacts'] ?? []),
+ artifacts: (map['artifacts'] as List)
+ .map(
+ (artifact) => Artifact.fromJson(artifact as Map<String, dynamic>))
+ .toList(),
);
}
diff --git a/frontend/lib/models/step.dart b/frontend/lib/models/step.dart
index 951c5eea6..2cb596175 100644
--- a/frontend/lib/models/step.dart
+++ b/frontend/lib/models/step.dart
@@ -1,4 +1,6 @@
// TODO: Refactor this to match which values are required and optional
+import 'package:auto_gpt_flutter_client/models/artifact.dart';
+
class Step {
final String input;
final Map<String, dynamic> additionalInput;
@@ -8,8 +10,7 @@ class Step {
final String status;
final String output;
final Map<String, dynamic> additionalOutput;
- // TODO: Create an actual artifact object
- final List<dynamic> artifacts;
+ final List<Artifact> artifacts;
final bool isLast;
Step({
@@ -42,8 +43,10 @@ class Step {
additionalOutput: map['additional_output'] != null
? Map<String, dynamic>.from(map['additional_output'])
: {},
- artifacts:
- map['artifacts'] != null ? List<dynamic>.from(map['artifacts']) : [],
+ artifacts: (map['artifacts'] as List)
+ .map(
+ (artifact) => Artifact.fromJson(artifact as Map<String, dynamic>))
+ .toList(),
isLast: map['is_last'] ?? false,
);
}
diff --git a/frontend/lib/views/chat/chat_view.dart b/frontend/lib/views/chat/chat_view.dart
index 596d64f7f..68a2f73e0 100644
--- a/frontend/lib/views/chat/chat_view.dart
+++ b/frontend/lib/views/chat/chat_view.dart
@@ -90,18 +90,10 @@ class _ChatViewState extends State<ChatView> {
key: ValueKey(chat.id),
chat: chat,
onArtifactsButtonPressed: () {
- // TODO: Create an actual artifact object
// Loop through each artifact and download it using the artifact_id
for (var artifact in chat.artifacts) {
- if (artifact is Map) {
- final artifactMap = artifact.cast<String,
- dynamic>(); // Cast each item to Map<String, dynamic>
-
- final artifactId = artifactMap['artifact_id']
- .toString(); // Get the artifact_id
- widget.viewModel.downloadArtifact(
- chat.taskId, artifactId); // Download the artifact
- }
+ widget.viewModel
+ .downloadArtifact(chat.taskId, artifact.artifactId);
}
},
);