|string $atts */ public function render(array|string $atts = '', ?string $content = null, string $tag = self::TAG): string { $this->deprecate(); $a = shortcode_atts(['name' => 'get', 'url' => '', 'length' => '', 'model' => '', 'cache' => Chat::DEFAULT_CACHE], is_array($atts) ? $atts : [], self::TAG); $name = strtolower(trim((string) $a['name'])); $url = trim((string) $a['url']); $length = trim((string) $a['length']); $model = $this->chat->authorModel(trim((string) $a['model'])); if (!in_array($name, self::AGENTS, true)) { /* translators: %s: the name attribute as written */ return $this->chat->notice(sprintf(__('[alpacabot_agent] has no agent named "%s"; it knows get and summarize.', 'alpaca-bot'), $name)); } if ($url === '') { return $this->chat->notice(__('[alpacabot_agent] needs a url attribute.', 'alpaca-bot')); } $identity = ['name' => $name, 'url' => $url, 'length' => $length, 'model' => $model]; return $this->chat->answer(self::TAG, $identity, Chat::cacheSeconds((string) $a['cache']), $name === 'get' ? 'text' : 'markdown', function (int $userId) use ($name, $url, $length, $model): string { $page = $this->fetch($userId, $url); if ($name === 'get') { return $page; } // English on purpose, as the toolkits' own prompts are: it is read by the model. $prompt = 'Summarize ' . $url . ($length !== '' ? ' in ' . $length : '') . ":\n\n" . $page; $options = [ 'ephemeral' => true, 'context' => [], 'system' => 'The user sends the text of a web page and asks for a summary. Return only the summary: no preamble, no commentary, and nothing that is not in the text.', ]; if ($model !== '') { $options['model'] = $model; } return trim($this->pipeline->complete($userId, $prompt, $options)->reply->content); }); } /** * The page's text through the web_fetch tool the registry enables for this viewer. Its * refusal (an address that is not public, a page that is not text, an HTTP error) and the * tool not being on offer to this viewer — switched off, or held back by their * `tool.web_fetch` row — are thrown as the caller's mistake, the arm of * Errors::fromPipeline() that shows the message: the URL is the author's and the * tool's words are written for them, and both screens are the administrator's to name. The * two are not told apart here, and the message says so rather than naming one: enabled() * hands back an absence, not a reason. * * @throws \InvalidArgumentException with the tool's own message, or the two screens' names * @throws \RuntimeException when the enabled toolkit has no web_fetch tool, which is a plugin bug */ private function fetch(int $userId, string $url): string { $toolkit = $this->toolkits->enabled($userId)['web_fetch'] ?? null; if ($toolkit === null) { throw new \InvalidArgumentException(__('The web_fetch tool is not available here: it is switched off in Settings › Tools, or Settings › Access keeps it from your role, so [alpacabot_agent] fetches nothing.', 'alpaca-bot')); } $tool = Registry::tool($toolkit, 'web_fetch'); if ($tool === null) { throw new \RuntimeException(__('The web_fetch tool is not available.', 'alpaca-bot')); } $result = $tool->execute(['url' => $url]); if ($result->status !== ToolResultStatus::Success) { throw new \InvalidArgumentException($result->content); } return $result->content; } private function deprecate(): void { if ($this->warned) { return; } $this->warned = true; // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Every part of this notice is a literal: a translated string and two hard-coded shortcode examples, with no caller data. esc_html() would render the quotes in prompt="…" as " in the developer notice. _doing_it_wrong('alpacabot_agent', sprintf( /* translators: 1: [alpacabot_agent], 2: the replacement shortcode */ __('The %1$s shortcode is deprecated since 0.5.0 and goes away in a later 0.x release. It still fetches its URL and, for summarize, asks the model. Put the text to work on in the prompt of %2$s instead, or open the URL in the chat screen, where the web_fetch and summarize tools read it for you.', 'alpaca-bot'), '[alpacabot_agent]', '[alpacabot prompt="Summarize the following: …"]', ), '0.5.0'); // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped } }