concat method
Merges this message with another by concatenating the content.
Implementation
@override
AIChatMessage concat(final ChatMessage other) {
if (other is! AIChatMessage) {
return this;
}
final toolCalls = <AIChatMessageToolCall>[];
if (this.toolCalls.isNotEmpty || other.toolCalls.isNotEmpty) {
final thisToolCallsById = {
for (final toolCall in this.toolCalls) toolCall.id: toolCall,
};
final otherToolCallsById = {
for (final toolCall in other.toolCalls)
(toolCall.id.isNotEmpty
? toolCall.id
: (this.toolCalls.lastOrNull?.id ?? '')): toolCall,
};
final toolCallsIds = {
...thisToolCallsById.keys,
...otherToolCallsById.keys,
};
for (final id in toolCallsIds) {
final thisToolCall = thisToolCallsById[id];
final otherToolCall = otherToolCallsById[id];
toolCalls.add(
AIChatMessageToolCall(
id: id,
name: (thisToolCall?.name ?? '') + (otherToolCall?.name ?? ''),
argumentsRaw: (thisToolCall?.argumentsRaw ?? '') +
(otherToolCall?.argumentsRaw ?? ''),
arguments: {
...?thisToolCall?.arguments,
...?otherToolCall?.arguments,
},
),
);
}
}
return AIChatMessage(
content: content + other.content,
toolCalls: toolCalls,
);
}