addQueryChunk method

Future<void> addQueryChunk(
  1. Message message, [
  2. bool noTool = false
])

Implementation

Future<void> addQueryChunk(Message message, [bool noTool = false]) async {
  var messageToSend = message;
  // Only add tools prompt for the first user text message (not a tool response)
  // and only if the model supports function calls
  if (message.isUser && message.type == MessageType.text && !_toolsInstructionSent && tools.isNotEmpty && !noTool && supportsFunctionCalls) {
    _toolsInstructionSent = true;
    final toolsPrompt = _createToolsPrompt();
    final newText = '$toolsPrompt\n${message.text}';
    messageToSend = message.copyWith(text: newText);
  } else if (!supportsFunctionCalls && tools.isNotEmpty && !noTool) {
    // Log warning if model doesn't support function calls but tools are provided
    debugPrint('WARNING: Model does not support function calls, but tools were provided. Tools will be ignored.');
  }

  // --- DETAILED LOGGING ---
  final historyForLogging = _modelHistory.map((m) => m.text).join('\n');
  debugPrint('--- Sending to Native ---');
  debugPrint('History:\n$historyForLogging');
  debugPrint('Current Message:\n${messageToSend.text}');
  debugPrint('-------------------------');
  // --- END LOGGING ---

  await session.addQueryChunk(messageToSend);

  // THE FIX: Add the message that was *actually* sent to the model to the history.
  _fullHistory.add(messageToSend);
  _modelHistory.add(messageToSend);
}