`, backticks, `~` and spaces are also trimmed from the front, and * spaces from the end, so the one line cannot open a Markdown heading, a quote or a code fence * wherever a description is printed as a line of Markdown. SystemPrompt::withTools() prints it * that way; the agent loop does not call it, so for the path the model reads this is defence in * depth. Text that is not valid UTF-8 becomes ''. None of that makes the text trustworthy, only * bounded, which is why the Tools tab shows an administrator the same text, through this same * function, when choosing the tool. It covers the description only, not the schema's own text * (above). * * @since 0.6.0 */ final class SchemaTool implements ToolInterface { /** The most of someone else's description the model is given, in characters. */ public const DESCRIPTION_CHARS = 300; /** * The most of a tool's result the model is given, in characters: web_fetch's page cap, so a * page and an ability's answer are held to the same cap. */ public const RESULT_CHARS = WebFetchToolkit::MAX_CHARS; /** What a result cut at RESULT_CHARS ends with, so the model knows there was more. English on purpose, as a tool's guidelines are. */ public const CUT_MARKER = '… [cut at ' . self::RESULT_CHARS . ' characters]'; /** * @param array $schema the tool's input as JSON Schema * @param \Closure(array): ToolResult $run what the tool does with the model's arguments */ public function __construct(private string $name, private string $description, private array $schema, private \Closure $run) {} public function name(): string { return $this->name; } public function description(): string { return $this->description; } /** @return array{} the class docblock says why this is empty */ public function parameters(): array { return []; } public function execute(array $input): ToolResult { try { $result = ($this->run)($input); } catch (\Throwable) { return self::failed($this->name); } $content = self::scrub($result->content); if (mb_strlen($content, 'UTF-8') > self::RESULT_CHARS) { $content = mb_substr($content, 0, self::RESULT_CHARS, 'UTF-8') . self::CUT_MARKER; } return $content === $result->content ? $result : $result->withContent($content); } /** `$text` with every byte sequence that is not UTF-8 replaced by U+FFFD (the class docblock says why). */ private static function scrub(string $text): string { if (mb_check_encoding($text, 'UTF-8')) { return $text; } // mb_scrub() writes the substitute character the site's mbstring is set to, `?` unless // configured, and that setting is global, so it is set for this call and put back. $substitute = mb_substitute_character(); mb_substitute_character(0xFFFD); try { return mb_scrub($text, 'UTF-8'); } finally { mb_substitute_character($substitute); } } /** The fixed error a tool named `$name` answers in place of a throw's message (the class docblock says why). */ public static function failed(string $name): ToolResult { /* translators: %s: the tool's name as the model knows it */ return ToolResult::error(sprintf(__('The %s tool failed before it could answer.', 'alpaca-bot'), $name)); } public function toFunctionSchema(): array { $schema = $this->schema; if (($schema['type'] ?? 'object') === 'object') { $schema['type'] = 'object'; $schema['properties'] ??= []; } return ['type' => 'function', 'function' => ['name' => $this->name, 'description' => $this->description, 'parameters' => JsonSchemaRepair::repair($schema)]]; } public static function describe(string $untrusted): string { $text = (string) preg_replace('/\p{Cf}+/u', '', wp_strip_all_tags($untrusted)); $text = ltrim((string) preg_replace('/[\s\p{Cc}\p{Z}]+/u', ' ', $text), '#>`~ '); $text = rtrim($text, ' '); return mb_strlen($text) > self::DESCRIPTION_CHARS ? rtrim(mb_substr($text, 0, self::DESCRIPTION_CHARS), ' ') . '…' : $text; } }