| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import '../../../../core/models/enums.dart'; |
| 4 | import '../tools/tool_event.dart'; |
| 5 | |
| 6 | class Message { |
| 7 | final String id; |
| 8 | final String conversationId; |
| 9 | final MessageRole role; |
| 10 | final String content; |
| 11 | final DateTime createdAt; |
| 12 | final MessageStatus status; |
| 13 | final String? modelId; |
| 14 | final int? tokenCount; |
| 15 | final int? inputTokenCount; |
| 16 | final double? tokensPerSecond; |
| 17 | final int? ttftMs; |
| 18 | final String? stopReason; |
| 19 | final String? errorMessage; |
| 20 | final List<String>? attachmentPaths; |
| 21 | final int? generationTimeMs; |
| 22 | final String? reasoningContent; |
| 23 | final List<ToolCallData>? toolCalls; |
| 24 | final String? toolCallId; |
| 25 | final bool isProcessing; |
| 26 | final String? toolSessionId; |
| 27 | final List<ToolEvent>? toolEvents; |
| 28 | final String? variantGroupId; |
| 29 | final int variantIndex; |
| 30 | final int threadOrder; |
| 31 | final bool isActiveVariant; |
| 32 | final String? parentMessageId; |
| 33 | final int? contentTokenCount; |
| 34 | |
| 35 | Message({ |
| 36 | required this.id, |
| 37 | required this.conversationId, |
| 38 | required this.role, |
| 39 | required this.content, |
| 40 | required this.createdAt, |
| 41 | this.status = MessageStatus.complete, |
| 42 | this.modelId, |
| 43 | this.tokenCount, |
| 44 | this.inputTokenCount, |
| 45 | this.tokensPerSecond, |
| 46 | this.ttftMs, |
| 47 | this.stopReason, |
| 48 | this.errorMessage, |
| 49 | this.attachmentPaths, |
| 50 | this.generationTimeMs, |
| 51 | this.reasoningContent, |
| 52 | this.toolCalls, |
| 53 | this.toolCallId, |
| 54 | this.isProcessing = false, |
| 55 | this.toolSessionId, |
| 56 | this.toolEvents, |
| 57 | this.variantGroupId, |
| 58 | this.variantIndex = 0, |
| 59 | this.threadOrder = 0, |
| 60 | this.isActiveVariant = true, |
| 61 | this.parentMessageId, |
| 62 | this.contentTokenCount, |
| 63 | }); |
| 64 | |
| 65 | Message copyWith({ |
| 66 | String? id, |
| 67 | String? conversationId, |
| 68 | MessageRole? role, |
| 69 | String? content, |
| 70 | DateTime? createdAt, |
| 71 | MessageStatus? status, |
| 72 | String? modelId, |
| 73 | int? tokenCount, |
| 74 | int? inputTokenCount, |
| 75 | double? tokensPerSecond, |
| 76 | int? ttftMs, |
| 77 | String? stopReason, |
| 78 | String? errorMessage, |
| 79 | List<String>? attachmentPaths, |
| 80 | int? generationTimeMs, |
| 81 | String? reasoningContent, |
| 82 | List<ToolCallData>? toolCalls, |
| 83 | String? toolCallId, |
| 84 | bool? isProcessing, |
| 85 | String? toolSessionId, |
| 86 | List<ToolEvent>? toolEvents, |
| 87 | String? variantGroupId, |
| 88 | int? variantIndex, |
| 89 | int? threadOrder, |
| 90 | bool? isActiveVariant, |
| 91 | String? parentMessageId, |
| 92 | int? contentTokenCount, |
| 93 | }) { |
| 94 | return Message( |
| 95 | id: id ?? this.id, |
| 96 | conversationId: conversationId ?? this.conversationId, |
| 97 | role: role ?? this.role, |
| 98 | content: content ?? this.content, |
| 99 | createdAt: createdAt ?? this.createdAt, |
| 100 | status: status ?? this.status, |
| 101 | modelId: modelId ?? this.modelId, |
| 102 | tokenCount: tokenCount ?? this.tokenCount, |
| 103 | inputTokenCount: inputTokenCount ?? this.inputTokenCount, |
| 104 | tokensPerSecond: tokensPerSecond ?? this.tokensPerSecond, |
| 105 | ttftMs: ttftMs ?? this.ttftMs, |
| 106 | stopReason: stopReason ?? this.stopReason, |
| 107 | errorMessage: errorMessage ?? this.errorMessage, |
| 108 | attachmentPaths: attachmentPaths ?? this.attachmentPaths, |
| 109 | generationTimeMs: generationTimeMs ?? this.generationTimeMs, |
| 110 | reasoningContent: reasoningContent ?? this.reasoningContent, |
| 111 | toolCalls: toolCalls ?? this.toolCalls, |
| 112 | toolCallId: toolCallId ?? this.toolCallId, |
| 113 | isProcessing: isProcessing ?? this.isProcessing, |
| 114 | toolSessionId: toolSessionId ?? this.toolSessionId, |
| 115 | toolEvents: toolEvents ?? this.toolEvents, |
| 116 | variantGroupId: variantGroupId ?? this.variantGroupId, |
| 117 | variantIndex: variantIndex ?? this.variantIndex, |
| 118 | threadOrder: threadOrder ?? this.threadOrder, |
| 119 | isActiveVariant: isActiveVariant ?? this.isActiveVariant, |
| 120 | parentMessageId: parentMessageId ?? this.parentMessageId, |
| 121 | contentTokenCount: contentTokenCount ?? this.contentTokenCount, |
| 122 | ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | class ToolCallData { |
| 127 | final String id; |
| 128 | final String toolName; |
| 129 | final Map<String, dynamic> arguments; |
| 130 | final String? result; |
| 131 | |
| 132 | ToolCallData({ |
| 133 | required this.id, |
| 134 | required this.toolName, |
| 135 | required this.arguments, |
| 136 | this.result, |
| 137 | }); |
| 138 | |
| 139 | Map<String, dynamic> toMap() { |
| 140 | return { |
| 141 | 'id': id, |
| 142 | 'toolName': toolName, |
| 143 | 'arguments': arguments, |
| 144 | 'result': result, |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | factory ToolCallData.fromMap(Map<String, dynamic> map) { |
| 149 | return ToolCallData( |
| 150 | id: (map['id'] as String?) ?? '', |
| 151 | toolName: (map['toolName'] as String?) ?? '', |
| 152 | arguments: Map<String, dynamic>.from((map['arguments'] as Map?) ?? {}), |
| 153 | result: map['result'] as String?, |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | String toJson() => json.encode(toMap()); |
| 158 | |
| 159 | factory ToolCallData.fromJson(String source) => |
| 160 | ToolCallData.fromMap(json.decode(source)); |
| 161 | } |