Implement Artifact Class for Enhanced Data Handling (#5585)

pull/5586/head
hunteraraujo 2023-10-06 14:55:47 -07:00 committed by GitHub
parent a615eda205
commit 0f2126e6f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 16 deletions

View File

@ -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,
};
}

View File

@ -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(),
);
}

View File

@ -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,
);
}

View File

@ -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);
}
},
);