import 'package:http/http.dart' as http; class HistoryResponse { final Map items; HistoryResponse({required this.items}); factory HistoryResponse.fromJson(Map json) { return HistoryResponse( items: json.map((key, value) => MapEntry(key, HistoryItem.fromJson(value))), ); } } class HistoryItem { final Prompt prompt; final Outputs outputs; final Status status; final Map meta; HistoryItem({ required this.prompt, required this.outputs, required this.status, required this.meta, }); factory HistoryItem.fromJson(Map json) { return HistoryItem( prompt: Prompt.fromJson(json['prompt']), outputs: Outputs.fromJson(json['outputs']), status: Status.fromJson(json['status']), meta: (json['meta'] as Map).map( (key, value) => MapEntry(key, Meta.fromJson(value)), ), ); } } class Prompt { final int id; final String promptId; final Map nodes; final ExtraPngInfo? extraPngInfo; Prompt({ required this.id, required this.promptId, required this.nodes, this.extraPngInfo, }); factory Prompt.fromJson(List json) { return Prompt( id: json[0] as int, promptId: json[1] as String, nodes: (json[2] as Map).map( (key, value) => MapEntry(key, Node.fromJson(value)), ), extraPngInfo: json[3]['extra_pnginfo'] != null ? ExtraPngInfo.fromJson(json[3]['extra_pnginfo']) : null, ); } } class Node { final Map inputs; final String classType; final Meta meta; Node({ required this.inputs, required this.classType, required this.meta, }); factory Node.fromJson(Map json) { return Node( inputs: json['inputs'] as Map, classType: json['class_type'] as String, meta: Meta.fromJson(json['_meta']), ); } } class ExtraPngInfo { final Workflow workflow; ExtraPngInfo({required this.workflow}); factory ExtraPngInfo.fromJson(Map json) { return ExtraPngInfo( workflow: Workflow.fromJson(json['workflow']), ); } } class Workflow { final List nodes; final List links; Workflow({ required this.nodes, required this.links, }); factory Workflow.fromJson(Map json) { return Workflow( nodes: (json['nodes'] as List) .map((e) => NodeInfo.fromJson(e)) .toList(), links: (json['links'] as List) .map((e) => Link.fromJson(e)) .toList(), ); } } class NodeInfo { final int id; final String type; NodeInfo({ required this.id, required this.type, }); factory NodeInfo.fromJson(Map json) { return NodeInfo( id: json['id'] as int, type: json['type'] as String, ); } } class Link { final int id; final int sourceNodeId; final int targetNodeId; Link({ required this.id, required this.sourceNodeId, required this.targetNodeId, }); factory Link.fromJson(List json) { return Link( id: json[0] as int, sourceNodeId: json[1] as int, targetNodeId: json[2] as int, ); } } class Outputs { final Map nodes; Outputs({required this.nodes}); factory Outputs.fromJson(Map json) { return Outputs( nodes: json.map((key, value) => MapEntry(key, OutputNode.fromJson(value))), ); } } class OutputNode { final List images; OutputNode({required this.images}); factory OutputNode.fromJson(Map json) { return OutputNode( images: (json['images'] as List) .map((e) => Image.fromJson(e)) .toList(), ); } } class Image { final String filename; final String subfolder; final String type; Image({ required this.filename, required this.subfolder, required this.type, }); factory Image.fromJson(Map json) { return Image( filename: json['filename'] as String, subfolder: json['subfolder'] as String, type: json['type'] as String, ); } Future> 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 { final String statusStr; final bool completed; Status({ required this.statusStr, required this.completed, }); factory Status.fromJson(Map json) { return Status( statusStr: json['status_str'] as String, completed: json['completed'] as bool, ); } } class Meta { final String? nodeId; Meta({this.nodeId}); factory Meta.fromJson(Map json) { return Meta( nodeId: json['node_id'] as String?, ); } }