Documentation / Events

AI Tools

ai.create #

Returns the prompt message before sending the request to the server. This way you can modify the prompt and send the editor an already modified one. This event necessarily requires the prompt to be returned by setting it back to an event using event.set.


Redactor('#entry', {
    plugins: ['ai'],
    text: {
        url: '/path-to-server-side/',
        endpoint: 'https://api.openai.com/v1/chat/completions',
        model: 'gpt-3.5-turbo',
        stream: true
    },
    subscribe: {
        'ai.create': function(event) {
            let prompt = event.get('prompt');

            ...

            event.set('prompt', prompt);
        }
    }
});

ai.before.send #

Arguments
xhr ObjectXMLHttpRequest object

Occurs before sending the AI request to the server.

This event has event.stop method.


Redactor('#entry', {
    plugins: ['ai'],
    text: {
        url: '/path-to-server-side/',
        endpoint: 'https://api.openai.com/v1/chat/completions',
        model: 'gpt-3.5-turbo',
        stream: true
    },
    subscribe: {
        'ai.before.send': function(event) {
            let xhr = event.get('xhr');
            xhr.setRequestHeader('X-CSRF-Token', 'your-token-value');
        }
    }
});

ai.complete #

Occurs when a request with results is received from the server or when stream is done. Returns prompt and response from the server.


Redactor('#entry', {
    plugins: ['ai'],
    text: {
        url: '/path-to-server-side/',
        endpoint: 'https://api.openai.com/v1/chat/completions',
        model: 'gpt-3.5-turbo',
        stream: true
    },
    subscribe: {
        'ai.complete': function(event) {
            let prompt = event.get('prompt');
            let response = event.get('response');
        }
    }
});

ai.insert #

Occurs when a response from AI is inserted into the editor content. Returns the set of inserted DOM nodes.


Redactor('#entry', {
    plugins: ['ai'],
    text: {
        url: '/path-to-server-side/',
        endpoint: 'https://api.openai.com/v1/chat/completions',
        model: 'gpt-3.5-turbo',
        stream: true
    },
    subscribe: {
        'ai.complete': function(event) {
            let nodes = event.get('nodes');
            nodes.each(function($node) {
                console.log($node);
            });
        }
    }
});

ai.discard #

Occurs when the Discard button is pressed in an AI request.


Redactor('#entry', {
    plugins: ['ai'],
    text: {
        url: '/path-to-server-side/',
        endpoint: 'https://api.openai.com/v1/chat/completions',
        model: 'gpt-3.5-turbo',
        stream: true
    },
    subscribe: {
        'ai.discard': function() {
            ...
        }
    }
});

ai.stop #

Occurs when the Stop button is pressed in an AI request. Works if the request to AI was made as a stream. Returns prompt and response from the server.


Redactor('#entry', {
    plugins: ['ai'],
    text: {
        url: '/path-to-server-side/',
        endpoint: 'https://api.openai.com/v1/chat/completions',
        model: 'gpt-3.5-turbo',
        stream: true
    },
    subscribe: {
        'ai.stop': function(event) {
            let prompt = event.get('prompt');
            let response = event.get('response');
        }
    }
});

ai.error #

The error event occurs in two cases.

First, when an AI request or server returned an error. In this case, the event returns an object with data about the error.

Second, if you have specially generated a response in the server script as JSON, of this kind:


{
    "error": true,
    "message": "Something went wrong..."
}

This is useful, for example, if you are building AI query limits for users and want to show that the limit has been exhausted.

In all cases, the easiest way to get the response object is to use event.dump and see what data it contains.


Redactor('#entry', {
    plugins: ['ai'],
    text: {
        url: '/path-to-server-side/',
        endpoint: 'https://api.openai.com/v1/chat/completions',
        model: 'gpt-3.5-turbo',
        stream: true
    },
    subscribe: {
        'ai.error': function(event) {
            let response = event.dump();
            console.log(response);
        }
    }
});