How to Add Your Own Custom Function Tool

Register your own custom function tool so the Chat Agent can call it via LLM function calling.

Step 1: Register Your Function Tool

Use the aizl_function_map filter to map your function name:

add_filter('aizl_function_map', function($map) {
    $map['check_system_status'] = ['internal_function_check_system_status'];
    return $map;
});

Step 2: Define the Handler Class

Create a class that extends \AIZLabs\ChatAgent\FunctionHandler:

class UserCustomFunctionHandler extends \AIZLabs\ChatAgent\FunctionHandler {
    public static function internal_function_check_system_status($args) {
        $system_id = $args['system_id'] ?? '';
        return json_encode(['status' => 'in maintenance', 'system_id' => $system_id]);
    }
}

Return a string (JSON preferred). If no return value needed, return "success" or similar message.

Step 3: Register the Handler

$result = \AIZLabs\ChatAgent\FunctionHandler::register_handler(UserCustomFunctionHandler::class);
if (strpos($result, 'Error:') === 0) {
    error_log("Handler registration failed: $result");
}

Step 4: Add to Theme functions.php

Add this complete code to your theme's functions.php

// Check if class exists before extending
if (class_exists('\AIZLabs\ChatAgent\FunctionHandler')) {
    class UserCustomFunctionHandler extends \AIZLabs\ChatAgent\FunctionHandler {
        public static function internal_function_check_system_status($args) {
            $system_id = $args['system_id'] ?? '';
            return json_encode([
                'status' => 'in maintenance',
                'system_id' => $system_id
            ]);
        }
    }

    add_action('aizl_custom_function_handler', function() {
        add_filter('aizl_function_map', function($map) {
            $map['check_system_status'] = ['internal_function_check_system_status'];
            return $map;
        });

        $result = \AIZLabs\ChatAgent\FunctionHandler::register_handler(UserCustomFunctionHandler::class);
        if (strpos($result, 'Error:') === 0) {
            error_log("Handler registration failed: $result");
        }
    });
}
⚠️ Important: The class_exists() check prevents fatal errors if the plugin is deactivated. Always include it!

Database Setup

Insert function metadata into the database:

$definition = json_encode([
    "type" => "function",
    "name" => "check_system_status",
    "description" => "Check System Status",
    "parameters" => [
        "type" => "object",
        "properties" => [
            "system_id" => ["type" => "string", "description" => "System ID"]
        ],
        "required" => ["system_id"],
        "additionalProperties" => false
    ]
]);

global $wpdb;
$wpdb->insert($wpdb->prefix . 'aizl_function', [
    'name' => 'check_system_status',
    'description' => 'Check System Status',
    'definition' => $definition,
    'created_time' => current_time('mysql', 1)
]);

Troubleshooting

Important Notes