addTool method

void addTool(
  1. ToolUseBlock block,
  2. AssistantMessage assistantMessage
)

Add a tool to the execution queue. Starts executing immediately if conditions allow.

Implementation

void addTool(ToolUseBlock block, AssistantMessage assistantMessage) {
  final toolDefinition = findToolByName(_toolDefinitions, block.name);

  if (toolDefinition == null) {
    _tools.add(
      _TrackedTool(
        id: block.id,
        block: block,
        assistantMessage: assistantMessage,
        status: ToolStatus.completed,
        isConcurrencySafe: true,
        results: [
          ToolMessage(
            type: 'user',
            toolResults: [
              ToolResultBlock(
                toolUseId: block.id,
                content:
                    '<tool_use_error>Error: No such tool available: ${block.name}</tool_use_error>',
                isError: true,
              ),
            ],
            toolUseResult: 'Error: No such tool available: ${block.name}',
            sourceToolAssistantUUID: assistantMessage.uuid,
          ),
        ],
      ),
    );
    return;
  }

  bool isConcurrencySafe;
  try {
    isConcurrencySafe = toolDefinition.isConcurrencySafe(block.input);
  } catch (_) {
    isConcurrencySafe = false;
  }

  _tools.add(
    _TrackedTool(
      id: block.id,
      block: block,
      assistantMessage: assistantMessage,
      status: ToolStatus.queued,
      isConcurrencySafe: isConcurrencySafe,
    ),
  );

  _processQueue();
}