2 Tools
Robin edited this page 2026-11-22 06:37:56 +01:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Tools

The chatbot answers from the content of your site: it searches its passages and reads its pages. Some questions are not answered by text: "What is on this weekend?" needs the dates of your events, which live in a calendar plugin's own tables. A tool lets another plugin answer such questions: the chatbot's model calls it with parameters it chooses (two dates), your plugin returns the answer as text, with the addresses of its pages.

The site's administrator turns each tool on, per chatbot, in Chatbot › Tools: a tool declared by a plugin is off until then.

1. Declare the tool

add_filter( 'lrob_chatbot_tools', function ( array $tools ): array {
	$tools['calendar_events'] = array(
		'label'       => __( 'Calendar', 'my-calendar' ),              // shown to the administrator
		'status'      => __( 'Checking the calendar…', 'my-calendar' ), // shown to the visitor during the call
		'description' => 'Events of this website between two dates (inclusive): title, date and time, place, address of the event page. Use it for any question about dates, upcoming events or opening days.',
		'parameters'  => array(
			'type'       => 'object',
			'properties' => array(
				'from' => array( 'type' => 'string', 'format' => 'date', 'description' => 'First day, YYYY-MM-DD.' ),
				'to'   => array( 'type' => 'string', 'format' => 'date', 'description' => 'Last day, YYYY-MM-DD.' ),
				'lang' => array( 'type' => 'string', 'enum' => array( 'en', 'fr' ), 'description' => 'Language of the answer.' ),
			),
			'required'   => array( 'from', 'to' ),
		),
		'callback'    => 'my_calendar_tool',
	);
	return $tools;
} );
Field What it is
key (calendar_events) The tool's name, given to the model: a-z, 0-9, _, 3 to 41 characters, starting with a letter. Not search nor read_page. Keep it: a new name is a new tool.
label Its name in the settings, translated by your plugin.
status What the visitor reads while the tool runs, translated.
description For the model, in English: what the tool gives, and when to use it. Short and precise: the model decides from it.
parameters A JSON Schema object (the subset below). The model receives exactly this.
callback callable( array $args, array $context ): array.

Parameters: an object whose properties are of these types only:

  • string, with optional enum, maxLength (at most 200), format (date: YYYY-MM-DD, date-time: ISO 8601);
  • integer and number, with optional minimum and maximum;
  • boolean.

Each property may have a description for the model. required lists the properties the model must give.

2. The callback

function my_calendar_tool( array $args, array $context ): array {
	$events = my_calendar_between( $args['from'], $args['to'] ); // read only
	if ( array() === $events ) {
		return array( 'text' => 'No event between ' . $args['from'] . ' and ' . $args['to'] . '.' );
	}
	$lines = array();
	$pages = array();
	foreach ( $events as $event ) {
		$lines[] = sprintf( '- %s: %s, %s (%s)', $event['date'], $event['title'], $event['place'], $event['url'] );
		$pages[] = array( 'url' => $event['url'], 'title' => $event['title'] );
	}
	return array( 'text' => implode( "\n", $lines ), 'pages' => $pages );
}

The chatbot calls it with:

  • $args: the parameters given by the model, already checked against your schema (types, enum, bounds, required ones present, others dropped, strings cut at their maxLength or 200 characters). A call whose parameters do not fit is answered with an error to the model, and your callback is not called.
  • $context:
Key Value
lang Language of the visitor (fr, en…).
page Address of the page the visitor is on.
now Date and time of the site, ISO 8601 with its offset (2026-11-22T14:05:00+01:00).
timezone Time zone of the site (Europe/Paris).
user_id 0 for an anonymous visitor.

It returns:

Key Required What it is
text yes The answer for the model: plain text or Markdown, at most 20,000 characters (cut beyond).
pages no The pages it speaks of: url (http/https) and title. The answer may link to them, and they show among its sources.

Rules:

  • Read only. A tool gives information; it never changes anything (no booking, no sending, no writing). The chatbot cannot check it: the administrator turns a tool on knowing what it does.
  • Public information only. Anything returned can be quoted to any visitor.
  • Fast. A call is timed and written in the chatbot's log; beyond 5 seconds it is flagged.
  • An exception, or a return without text, is reported to the model as an error and written in the log.

3. What happens in a conversation

  • The tools turned on for the chatbot are given to the model with search and read_page. The date, the time and the time zone of the site are given with each message, so that "this weekend" can be turned into two dates.
  • Each call counts with the searches: Chatbot › Search before answering › Per answer › Searches and tools.
  • While the tool runs, the visitor sees its status (Checking the calendar…).
  • The result is given to the model as data, never as instructions.
  • The final answer is written without tools, the calls and their results given as text: it still appears word by word.

4. Example of a question

A visitor asks, on Friday 20 November 2026: "What's on this weekend?"

  1. The model calls calendar_events with {"from": "2026-11-21", "to": "2026-11-22", "lang": "en"}.

  2. The calendar plugin returns:

    - 2026-11-21 10:00: Open day, Main hall (https://example.com/events/open-day/)
    - 2026-11-22 15:00: Concert, Garden (https://example.com/events/concert/)
    
  3. The chatbot answers with both events and links to their pages.

Also indexed as content

The text of your events (their description) can still be indexed as content, by their post type or through a content source: the search finds an event by its subject, the tool by its dates.