mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 06:24:20 +01:00
Implement Artifact Class for Enhanced Data Handling (#5585)
This commit is contained in:
62
frontend/lib/models/artifact.dart
Normal file
62
frontend/lib/models/artifact.dart
Normal 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:auto_gpt_flutter_client/models/artifact.dart';
|
||||||
import 'package:auto_gpt_flutter_client/models/message_type.dart';
|
import 'package:auto_gpt_flutter_client/models/message_type.dart';
|
||||||
|
|
||||||
/// Represents a chat message related to a specific task.
|
/// Represents a chat message related to a specific task.
|
||||||
@@ -8,7 +9,7 @@ class Chat {
|
|||||||
final DateTime timestamp;
|
final DateTime timestamp;
|
||||||
final MessageType messageType;
|
final MessageType messageType;
|
||||||
final Map<String, dynamic>? jsonResponse;
|
final Map<String, dynamic>? jsonResponse;
|
||||||
final List<dynamic> artifacts;
|
final List<Artifact> artifacts;
|
||||||
|
|
||||||
Chat({
|
Chat({
|
||||||
required this.id,
|
required this.id,
|
||||||
@@ -29,7 +30,10 @@ class Chat {
|
|||||||
timestamp: DateTime.parse(map['timestamp']),
|
timestamp: DateTime.parse(map['timestamp']),
|
||||||
messageType: MessageType.values.firstWhere(
|
messageType: MessageType.values.firstWhere(
|
||||||
(e) => e.toString() == 'MessageType.${map['messageType']}'),
|
(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(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
// TODO: Refactor this to match which values are required and optional
|
// TODO: Refactor this to match which values are required and optional
|
||||||
|
import 'package:auto_gpt_flutter_client/models/artifact.dart';
|
||||||
|
|
||||||
class Step {
|
class Step {
|
||||||
final String input;
|
final String input;
|
||||||
final Map<String, dynamic> additionalInput;
|
final Map<String, dynamic> additionalInput;
|
||||||
@@ -8,8 +10,7 @@ class Step {
|
|||||||
final String status;
|
final String status;
|
||||||
final String output;
|
final String output;
|
||||||
final Map<String, dynamic> additionalOutput;
|
final Map<String, dynamic> additionalOutput;
|
||||||
// TODO: Create an actual artifact object
|
final List<Artifact> artifacts;
|
||||||
final List<dynamic> artifacts;
|
|
||||||
final bool isLast;
|
final bool isLast;
|
||||||
|
|
||||||
Step({
|
Step({
|
||||||
@@ -42,8 +43,10 @@ class Step {
|
|||||||
additionalOutput: map['additional_output'] != null
|
additionalOutput: map['additional_output'] != null
|
||||||
? Map<String, dynamic>.from(map['additional_output'])
|
? Map<String, dynamic>.from(map['additional_output'])
|
||||||
: {},
|
: {},
|
||||||
artifacts:
|
artifacts: (map['artifacts'] as List)
|
||||||
map['artifacts'] != null ? List<dynamic>.from(map['artifacts']) : [],
|
.map(
|
||||||
|
(artifact) => Artifact.fromJson(artifact as Map<String, dynamic>))
|
||||||
|
.toList(),
|
||||||
isLast: map['is_last'] ?? false,
|
isLast: map['is_last'] ?? false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,18 +90,10 @@ class _ChatViewState extends State<ChatView> {
|
|||||||
key: ValueKey(chat.id),
|
key: ValueKey(chat.id),
|
||||||
chat: chat,
|
chat: chat,
|
||||||
onArtifactsButtonPressed: () {
|
onArtifactsButtonPressed: () {
|
||||||
// TODO: Create an actual artifact object
|
|
||||||
// Loop through each artifact and download it using the artifact_id
|
// Loop through each artifact and download it using the artifact_id
|
||||||
for (var artifact in chat.artifacts) {
|
for (var artifact in chat.artifacts) {
|
||||||
if (artifact is Map) {
|
widget.viewModel
|
||||||
final artifactMap = artifact.cast<String,
|
.downloadArtifact(chat.taskId, artifact.artifactId);
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user