Add SubmitPromptResponse model and update submitPrompt method to return structured response

This commit is contained in:
2025-03-20 15:43:15 +00:00
parent 697d2f812d
commit 41f85ae3c5
6 changed files with 46 additions and 464 deletions

View File

@@ -14,6 +14,7 @@ import 'models/checkpoint.dart';
import 'models/vae.dart';
import 'models/lora.dart';
import 'websocket_manager.dart';
import 'models/submit_prompt_response.dart';
/// A Dart SDK for interacting with the ComfyUI API
class ComfyUiApi {
@@ -186,7 +187,7 @@ class ComfyUiApi {
}
/// Submits a prompt (workflow) to generate an image
Future<Map<String, dynamic>> submitPrompt(Map<String, dynamic> prompt) async {
Future<SubmitPromptResponse> submitPrompt(Map<String, dynamic> prompt) async {
final response = await _httpClient.post(
Uri.parse('$host/api/prompt'),
headers: {'Content-Type': 'application/json'},
@@ -201,7 +202,7 @@ class ComfyUiApi {
_webSocketManager.triggerOnPromptStart(promptId);
}
return responseData;
return SubmitPromptResponse.fromJson(responseData);
}
/// Validates HTTP response and throws an exception if needed

View File

@@ -1,3 +1,5 @@
import 'package:http/http.dart' as http;
class HistoryResponse {
final Map<String, HistoryItem> items;
@@ -40,13 +42,13 @@ class Prompt {
final int id;
final String promptId;
final Map<String, Node> nodes;
final ExtraPngInfo extraPngInfo;
final ExtraPngInfo? extraPngInfo;
Prompt({
required this.id,
required this.promptId,
required this.nodes,
required this.extraPngInfo,
this.extraPngInfo,
});
factory Prompt.fromJson(List<dynamic> json) {
@@ -56,7 +58,9 @@ class Prompt {
nodes: (json[2] as Map<String, dynamic>).map(
(key, value) => MapEntry(key, Node.fromJson(value)),
),
extraPngInfo: ExtraPngInfo.fromJson(json[3]['extra_pnginfo']),
extraPngInfo: json[3]['extra_pnginfo'] != null
? ExtraPngInfo.fromJson(json[3]['extra_pnginfo'])
: null,
);
}
}
@@ -196,6 +200,15 @@ class Image {
type: json['type'] as String,
);
}
Future<List<int>> fetchImageBytes(String host) async {
final response =
await http.get(Uri.parse('$host/api/view?filename=$filename'));
if (response.statusCode != 200) {
throw Exception('Failed to fetch image: ${response.statusCode}');
}
return response.bodyBytes;
}
}
class Status {

View File

@@ -0,0 +1,27 @@
class SubmitPromptResponse {
final String promptId;
final int number;
final Map<String, dynamic> nodeErrors;
SubmitPromptResponse({
required this.promptId,
required this.number,
required this.nodeErrors,
});
factory SubmitPromptResponse.fromJson(Map<String, dynamic> json) {
return SubmitPromptResponse(
promptId: json['prompt_id'],
number: json['number'],
nodeErrors: json['node_errors'] ?? {},
);
}
Map<String, dynamic> toJson() {
return {
'prompt_id': promptId,
'number': number,
'node_errors': nodeErrors,
};
}
}