mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
Refactor DashBot
This commit is contained in:
47
lib/dashbot/models/chat_action.dart
Normal file
47
lib/dashbot/models/chat_action.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import '../constants.dart';
|
||||
|
||||
class ChatAction {
|
||||
final String action;
|
||||
final String target;
|
||||
final String field;
|
||||
final String? path;
|
||||
final dynamic value;
|
||||
final ChatActionType actionType; // enum representation
|
||||
final ChatActionTarget targetType; // enum representation
|
||||
|
||||
const ChatAction({
|
||||
required this.action,
|
||||
required this.target,
|
||||
this.field = '', // Default to empty string
|
||||
this.path,
|
||||
this.value,
|
||||
required this.actionType,
|
||||
required this.targetType,
|
||||
});
|
||||
|
||||
factory ChatAction.fromJson(Map<String, dynamic> json) {
|
||||
final actionStr = json['action'] as String? ?? 'other';
|
||||
final targetStr = json['target'] as String? ?? 'httpRequestModel';
|
||||
return ChatAction(
|
||||
action: actionStr,
|
||||
target: targetStr,
|
||||
field: json['field'] as String? ?? '',
|
||||
path: json['path'] as String?,
|
||||
value: json['value'],
|
||||
actionType: chatActionTypeFromString(actionStr),
|
||||
targetType: chatActionTargetFromString(targetStr),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'action': action,
|
||||
'target': target,
|
||||
'field': field,
|
||||
'path': path,
|
||||
'value': value,
|
||||
'action_type': actionType.text,
|
||||
'target_type': targetType.name,
|
||||
};
|
||||
}
|
||||
}
|
||||
19
lib/dashbot/models/chat_attachment.dart
Normal file
19
lib/dashbot/models/chat_attachment.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class ChatAttachment {
|
||||
final String id;
|
||||
final String name;
|
||||
final String mimeType;
|
||||
final int sizeBytes;
|
||||
final Uint8List data;
|
||||
final DateTime createdAt;
|
||||
|
||||
ChatAttachment({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.mimeType,
|
||||
required this.sizeBytes,
|
||||
required this.data,
|
||||
required this.createdAt,
|
||||
});
|
||||
}
|
||||
67
lib/dashbot/models/chat_message.dart
Normal file
67
lib/dashbot/models/chat_message.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../constants.dart';
|
||||
import 'chat_action.dart';
|
||||
|
||||
class ChatMessage {
|
||||
final String id;
|
||||
final String content;
|
||||
final MessageRole role;
|
||||
final DateTime timestamp;
|
||||
final ChatMessageType? messageType;
|
||||
// Multiple actions support. If provided, UI should render these.
|
||||
final List<ChatAction>? actions;
|
||||
|
||||
const ChatMessage({
|
||||
required this.id,
|
||||
required this.content,
|
||||
required this.role,
|
||||
required this.timestamp,
|
||||
this.messageType,
|
||||
this.actions,
|
||||
});
|
||||
|
||||
ChatMessage copyWith({
|
||||
String? id,
|
||||
String? content,
|
||||
MessageRole? role,
|
||||
DateTime? timestamp,
|
||||
ChatMessageType? messageType,
|
||||
List<ChatAction>? actions,
|
||||
}) {
|
||||
return ChatMessage(
|
||||
id: id ?? this.id,
|
||||
content: content ?? this.content,
|
||||
role: role ?? this.role,
|
||||
timestamp: timestamp ?? this.timestamp,
|
||||
messageType: messageType ?? this.messageType,
|
||||
actions: actions ?? this.actions,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ChatMessage(id: $id, content: $content, role: $role, timestamp: $timestamp, messageType: $messageType, actions: $actions)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(covariant ChatMessage other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other.id == id &&
|
||||
other.content == content &&
|
||||
other.role == role &&
|
||||
other.timestamp == timestamp &&
|
||||
other.messageType == messageType &&
|
||||
listEquals(other.actions, actions);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return id.hashCode ^
|
||||
content.hashCode ^
|
||||
role.hashCode ^
|
||||
timestamp.hashCode ^
|
||||
messageType.hashCode ^
|
||||
actions.hashCode;
|
||||
}
|
||||
}
|
||||
21
lib/dashbot/models/chat_response.dart
Normal file
21
lib/dashbot/models/chat_response.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import '../constants.dart';
|
||||
|
||||
class ChatResponse {
|
||||
final String content;
|
||||
final ChatMessageType? messageType;
|
||||
|
||||
const ChatResponse({
|
||||
required this.content,
|
||||
this.messageType,
|
||||
});
|
||||
|
||||
ChatResponse copyWith({
|
||||
String? content,
|
||||
ChatMessageType? messageType,
|
||||
}) {
|
||||
return ChatResponse(
|
||||
content: content ?? this.content,
|
||||
messageType: messageType ?? this.messageType,
|
||||
);
|
||||
}
|
||||
}
|
||||
35
lib/dashbot/models/chat_state.dart
Normal file
35
lib/dashbot/models/chat_state.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import '../error/chat_failure.dart';
|
||||
import 'chat_message.dart';
|
||||
|
||||
class ChatState {
|
||||
final Map<String, List<ChatMessage>> chatSessions; // requestId -> messages
|
||||
final bool isGenerating;
|
||||
final String currentStreamingResponse;
|
||||
final String? currentRequestId;
|
||||
final ChatFailure? lastError;
|
||||
|
||||
const ChatState({
|
||||
this.chatSessions = const {},
|
||||
this.isGenerating = false,
|
||||
this.currentStreamingResponse = '',
|
||||
this.currentRequestId,
|
||||
this.lastError,
|
||||
});
|
||||
|
||||
ChatState copyWith({
|
||||
Map<String, List<ChatMessage>>? chatSessions,
|
||||
bool? isGenerating,
|
||||
String? currentStreamingResponse,
|
||||
String? currentRequestId,
|
||||
ChatFailure? lastError,
|
||||
}) {
|
||||
return ChatState(
|
||||
chatSessions: chatSessions ?? this.chatSessions,
|
||||
isGenerating: isGenerating ?? this.isGenerating,
|
||||
currentStreamingResponse:
|
||||
currentStreamingResponse ?? this.currentStreamingResponse,
|
||||
currentRequestId: currentRequestId ?? this.currentRequestId,
|
||||
lastError: lastError ?? this.lastError,
|
||||
);
|
||||
}
|
||||
}
|
||||
39
lib/dashbot/models/dashbot_window_model.dart
Normal file
39
lib/dashbot/models/dashbot_window_model.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
class DashbotWindowModel {
|
||||
final double width;
|
||||
final double height;
|
||||
final double right;
|
||||
final double bottom;
|
||||
final bool isActive;
|
||||
final bool isPopped;
|
||||
final bool isHidden;
|
||||
|
||||
const DashbotWindowModel({
|
||||
this.width = 400,
|
||||
this.height = 515,
|
||||
this.right = 50,
|
||||
this.bottom = 100,
|
||||
this.isActive = false,
|
||||
this.isPopped = true,
|
||||
this.isHidden = false,
|
||||
});
|
||||
|
||||
DashbotWindowModel copyWith({
|
||||
double? width,
|
||||
double? height,
|
||||
double? right,
|
||||
double? bottom,
|
||||
bool? isActive,
|
||||
bool? isPopped,
|
||||
bool? isHidden,
|
||||
}) {
|
||||
return DashbotWindowModel(
|
||||
width: width ?? this.width,
|
||||
height: height ?? this.height,
|
||||
right: right ?? this.right,
|
||||
bottom: bottom ?? this.bottom,
|
||||
isActive: isActive ?? this.isActive,
|
||||
isPopped: isPopped ?? this.isPopped,
|
||||
isHidden: isHidden ?? this.isHidden,
|
||||
);
|
||||
}
|
||||
}
|
||||
6
lib/dashbot/models/models.dart
Normal file
6
lib/dashbot/models/models.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
export 'chat_action.dart';
|
||||
export 'chat_attachment.dart';
|
||||
export 'chat_message.dart';
|
||||
export 'chat_response.dart';
|
||||
export 'chat_state.dart';
|
||||
export 'dashbot_window_model.dart';
|
||||
Reference in New Issue
Block a user