` that forms.css displays as a table * (forms.css:1034-1046; misc.php:1041 prints it), so `.form-table td` never reaches its * cells, and the palette rule's `td` selector matches no markup core prints. What the * overrides rule restores is widefat's padding, `display: table-cell` and `width: auto`; forms.css's 14px * font and 600-weight `th` are left as they are. The `vertical-align: middle` restores * nothing: the `th` is top-aligned under widefat (common.css:504) and under `.form-table th` * (forms.css:931) alike, which put each model name some 11px above the centre of its row's * inputs, while the `td` was already centred by `.form-table td` (forms.css:916-920); so the * rule changes the `th` and only restates the `td`. And the wrapper scrolls sideways where * the table is wider than the row. * * It can, because it is sized by the row rather than by the table (`contain: inline-size`). A * table cell's minimum is its content's, so without that the nested table's minimum was the * row's, and the page's. widefat's own `word-wrap: break-word` (common.css) does not lower a * minimum, so a model id, which breaks only where line breaking allows (after most hyphens), * held its column at its longest unbreakable run, and the system prompt held 25em: at 1280px * with the menu expanded the page was 1343px wide (Kanboard #4348). Under 782px, core's * breakpoint, the row's cells are blocks, but the block `td` still sits in an anonymous * table cell, so the same holds there. * * The table's own minimum still decides when the wrapper scrolls, so the cells give width * back first. A model id breaks where it has to (`overflow-wrap: anywhere`, which unlike * `break-word` lowers the column's minimum) but its column keeps 9em; the system prompt * fills a column that asks for 35% of the table and shrinks to 12em, a floor that also keeps * core's narrow-screen `width: 100%` from collapsing the field in an auto-width cell. The * same `width: 100%` shrank the Tools select to 40px and clipped "Model default" (Kanboard * #4363), so the select is `width: auto`: sized by its options, it holds its column at its * label's width. With the menu expanded the columns fit at 1280px and 1440px; at 960px, * where core folds the menu, and under 782px they scroll inside the wrapper. The label * column keeps its 200px, which the table used to take from it. * * The MCP servers table (SettingsPage::renderMcpServers()) is the same kind of table, a * `widefat` nested in a Settings API row, and its wrapper, `div.ab-mcp-servers`, gets the * first two of these rules: the wrapper sized by the row and scrolling, and the cells made * cells again with widefat's padding. One more rule is its own: a tool's input schema in the * approval list (View\Settings\McpTools) wraps inside its cell rather than widening the table * to its longest line, and scrolls past 20em. The rest are about the overrides table's own * columns and are scoped to it. * * Inline on core's `forms` handle rather than in a stylesheet of the plugin's: the settings * page loads no plugin stylesheet, the chat shell's is another screen's stylesheet, and a * rule attached to forms.css prints after the rules it answers by construction, and prints * in a checkout that has not run `pnpm build`. */ private const OVERRIDES_CSS = <<<'CSS' .form-table .ab-overrides { overflow-x: auto; contain: inline-size; } .form-table .ab-overrides th, .form-table .ab-overrides td { display: table-cell; width: auto; padding: 8px 10px; vertical-align: middle; } .form-table .ab-overrides tbody th { overflow-wrap: anywhere; min-width: 9em; } .form-table .ab-overrides th.ab-overrides__system { width: 35%; } .form-table .ab-overrides .regular-text { width: 100%; min-width: 12em; } .form-table .ab-overrides select { width: auto; } .form-table .ab-mcp-servers { overflow-x: auto; contain: inline-size; } .form-table .ab-mcp-servers th, .form-table .ab-mcp-servers td { display: table-cell; width: auto; padding: 8px 10px; vertical-align: middle; } .form-table .ab-mcp-schema pre { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 20em; overflow: auto; } CSS; public function enqueue(string $hook): void { if ($hook === SettingsPage::screen()) { wp_add_inline_style('forms', self::OVERRIDES_CSS); // The Tools tab's Discover button is an hx-get at the MCP approval fragment // (SettingsPage::renderMcpServers()); htmx is already shipped and pinned, under the // handle the chat screen uses, whose tag id mount() hands the drawer's loader, so the // loader on this screen finds it rather than adding a second copy (resources/ts/mount.ts). self::enqueueHtmx(); return; } if ($hook !== self::HOOK) { return; } wp_enqueue_media(); $this->enqueueChat(); } /** * The same bundle for a `[alpacabot]` shell on a front-end page, called from the shortcode * handler (Shortcodes\Chat) as it renders, so only a page that rendered a shell loads it and * every other front-end page loads nothing. That is later than wp_enqueue_scripts: the * scripts are footer scripts (`in_footer`) and print with wp_footer, and a stylesheet * enqueued after wp_head prints there too, through print_late_styles(), so the shell is * unstyled for the moment between its markup and the footer. The alternative, scanning the * queried post for the shortcode on wp_enqueue_scripts, loads the bundle for a page whose * shortcode then renders nothing (a visitor, a `prompt` form) and for one whose content the * theme never prints; the flash is the cheaper cost. * * What of the admin enqueue is kept: the same handles, so a page that somehow carries both * loads each file once; the localised settings whole, since the bundle signs every request * with the nonce and reads the REST root and the image cap on the front end as in wp-admin, * and rest_url() is right under either permalink form; core's heartbeat, which the bundle * depends on for the nonce refresh and which runs on the front end for a logged-in user * (the `heartbeat_received` answer is hooked at register(), not from any screen). What is * not: wp_enqueue_media() unconditionally. It is the whole media library (Backbone, the * views, plupload, the modal templates in wp_footer), and the picker it opens queries the * library as the viewer, which core refuses to a user without `upload_files`: an editor's * Contributor would load all of it for an empty modal. So it is loaded for a viewer who can * upload, and the composer's image button is inert for one who cannot (`pickImage()` in * `resources/ts/boot.ts` returns when `wp.media` is absent). */ public function enqueueFront(): void { if (current_user_can('upload_files')) { wp_enqueue_media(); } $this->enqueueChat(); } /** * The few rules for what a shortcode prints on a page that is not the chat screen: the * `.alpaca-bot-answer` block and the `.alpaca-bot-notice` line. Its own small file (900 * bytes) rather than the shell's stylesheet, since a visitor's login notice must not cost * the whole of that file, and theme-neutral (the theme's type and colour, a quiet box for * the notice) so a theme's own rules over the two classes win without a fight. The * shortcode runs after `wp_head`, so it prints from the footer through print_late_styles(): * the markup is unstyled for the moment between its position and the footer, which for a * paragraph is a reflow, not a flash of the shell. */ public function enqueueShortcode(): void { wp_enqueue_style('alpaca-bot-shortcode', plugins_url('assets/css/alpaca-bot-shortcode.css', ALPACA_BOT_FILE), [], self::version('assets/css/alpaca-bot-shortcode.css')); } /** htmx, the bundle, the stylesheet and the bundle's settings: the class docblock. */ private function enqueueChat(): void { self::enqueueHtmx(); wp_enqueue_script('alpaca-bot-chat', plugins_url('assets/js/chat.js', ALPACA_BOT_FILE), [self::HTMX_HANDLE, 'heartbeat', 'wp-hooks'], self::version('assets/js/chat.js'), true); wp_enqueue_style('alpaca-bot', plugins_url('assets/css/alpaca-bot.css', ALPACA_BOT_FILE), [], self::version('assets/css/alpaca-bot.css')); wp_localize_script('alpaca-bot-chat', 'alpacaBot', $this->settings()); } /** htmx, in the footer, at the version package.json pins. */ private static function enqueueHtmx(): void { wp_enqueue_script(self::HTMX_HANDLE, plugins_url('assets/js/htmx.min.js', ALPACA_BOT_FILE), [], self::HTMX_VERSION, true); } /** * The chat bundle's settings object, `alpacaBot`: the REST root (rest_url(), so it is right * under either permalink form), the nonce it signs with, the largest image the site takes, and * the strings it shows. Public because this class is not the only one that hands it over: * Admin\Drawer localises the same object on its loader, so a bundle added to the page later * reads exactly what it reads on the chat screen. * * @return array */ public function settings(): array { return [ 'rest' => rest_url('alpaca-bot/v1'), 'nonce' => wp_create_nonce('wp_rest'), 'maxImageBytes' => self::maxImageBytes(), 'i18n' => [ 'copy' => __('Copy', 'alpaca-bot'), 'copied' => __('Copied', 'alpaca-bot'), 'copyCode' => __('Copy code', 'alpaca-bot'), 'copyFailed' => __('Copying failed. Select the text and copy it yourself.', 'alpaca-bot'), 'failed' => __('The request failed. Try again.', 'alpaca-bot'), 'sessionExpired' => __('Your session has expired. Reload the page to keep chatting.', 'alpaca-bot'), 'imageTitle' => __('Attach an image', 'alpaca-bot'), 'imageButton' => __('Use this image', 'alpaca-bot'), // image.ts fills {size} with the image's size and {max} with maxImageBytes, both formatted. /* translators: {size} and {max} are filled in by the browser with figures such as "4 MB". */ 'imageTooLarge' => __('That image is {size}; this site takes an image up to {max}. Pick a smaller one.', 'alpaca-bot'), // The same two figures for the turn's images together: image.ts fills {size} with their total. The // allowance is a per-message total on the server too (Pipeline::images()), in the same words. /* translators: {size} and {max} are filled in by the browser with figures such as "7 MB". */ 'imagesTooLarge' => __('Those images total {size}; this site takes up to {max} per message. Attach fewer or smaller images.', 'alpaca-bot'), // A turn refused before it ran, whose draft could not go back into the box because the user switched // conversation or typed or picked something since (resources/ts/boot.ts giveBack()). keepUnsent() shows // these in a slot beside the status line that no notice replaces, until the user dismisses it; a reload // loses it, and an attached image is always lost there, which notSentImage says. The browser fills // {text} with the message as typed, and adds it after a translation that has no {text}. /* translators: {text} is filled in by the browser with the message the user wrote. */ 'notSent' => __('Your message was not sent. Its text: {text}', 'alpaca-bot'), 'notSentImage' => __('The image attached to it was not sent. Attach it again to retry.', 'alpaca-bot'), 'dismiss' => __('Dismiss', 'alpaca-bot'), 'thinking' => __('Thinking…', 'alpaca-bot'), 'callingTool' => __('Calling a tool…', 'alpaca-bot'), ], 'offline' => __('You are offline. Messages will send once the connection is back.', 'alpaca-bot'), ]; } /** * What a loader needs to put the chat into a page that did not enqueue it (`alpacaBotMount`, * resources/ts/mount.ts): the fragment route, the route the drawer's state goes to, the three * files, each with the version query its enqueue would have given it, so a rebuild busts the * browser cache the same way, the id core prints on htmx's tag, by which the loader knows a * page that enqueued htmx itself (the settings screen does), the chat's name, for a host that * titles the panel it puts the chat in, and the line shown when the fragment does not arrive. * * @return array */ public function mount(): array { return [ 'panel' => rest_url('alpaca-bot/v1/view/panel'), 'prefs' => rest_url('alpaca-bot/v1/view/drawer'), 'htmx' => self::versioned('assets/js/htmx.min.js', self::HTMX_VERSION), 'htmxId' => self::HTMX_HANDLE . '-js', 'chat' => self::versioned('assets/js/chat.js', self::version('assets/js/chat.js')), 'css' => self::versioned('assets/css/alpaca-bot.css', self::version('assets/css/alpaca-bot.css')), 'title' => __('Alpaca Bot', 'alpaca-bot'), 'failed' => __('The chat could not be loaded. Reload the page and try again.', 'alpaca-bot'), ]; } /** A plugin file's URL with `?ver=`, as core's script loader writes an enqueued file's. */ private static function versioned(string $relative, string $version): string { return plugins_url($relative, ALPACA_BOT_FILE) . '?ver=' . rawurlencode($version); } /** * `heartbeat_received`: the chat bundle sends `alpaca_bot_nonce: 1` on heartbeat-send, and * so do the drawer's loader and the editor sidebar before it is added (resources/ts/nonce.ts), * and the answer carries a fresh `wp_rest` nonce under the same key. A heartbeat that did not * ask (another screen's) is left as it was. * * @param array $response * @param array $data what the browser sent * @return array */ public function heartbeat(array $response, array $data): array { if (!empty($data['alpaca_bot_nonce'])) { $response['alpaca_bot_nonce'] = wp_create_nonce('wp_rest'); } return $response; } /** * The budget for the part of a POST /chat body that is not the image: the ids, the model, * the data URL's own prefix, the JSON around them all, and the message text. The fixed * fields come to well under a kibibyte, so for them 64 KiB is generous. The message text * has no cap (the composer sets no maxlength and the /chat schema no maxLength, on purpose: * pasting a long document into a prompt is a real thing to do), so for it the allowance is * a ceiling, and one it meets only when a maximum-size image is attached: the cap consumes * the whole remainder, every raw image byte spends 4/3 of one, and there is no slack * elsewhere to absorb the overrun. A message past ~64 KB (some ten thousand words of ASCII) * beside an image at the cap lands in exactly the bare failure the guard exists to prevent. * That corner is the trade taken: the budget is 0.8% of a stock 8M, and a bound on the * field would cost a real capability to make it go away. */ public const IMAGE_BODY_ALLOWANCE = 64 * 1024; /** * The largest image the chat bundle sends, in bytes. The image is not an upload: it travels * base64-encoded inside the JSON body of POST /chat, so the only PHP limit it meets is * post_max_size. upload_max_filesize and wp_max_upload_size() govern multipart uploads and * have no say here. A body past post_max_size is dropped before WordPress sees it and the * only answer is a bare failure, which is what image.ts's guard exists to prevent: the guard * is only as good as its figure, and this is the site's own rather than a guess at it. * * The figure is the raw image size, which is what image.ts compares and the message prints, * so it is post_max_size less the allowance for the rest of the body, scaled by 3/4 for the * encoding: base64 spends four characters on every three bytes, and those characters need * no JSON escaping, so the body is the allowance plus 4/3 of the image. AssetsTest holds * that an image at the cap fits under post_max_size and one quantum more does not. * * 0 is "no limit": post_max_size=0 is PHP's own spelling of it, and image.ts reads 0 as "no * figure" and keeps its constant (imageLimit() says why the two are let coincide). A * post_max_size the allowance alone exhausts is a site nothing posts to, and answers 0 too * rather than a negative. The ini value is a parameter so a test can pass one; a caller * passes nothing. The division comes before the multiplication so the arithmetic stays in * int on a 32-bit build: wp_convert_hr_to_bytes() clamps at PHP_INT_MAX, a product past it * is a float, and a float into intdiv() is a TypeError under strict_types. The order costs * at most 2 bytes of cap. * * A web-server body limit (nginx's client_max_body_size) is invisible to PHP and is not * counted; that failure mode stays as it was. * * @internal Public only so the tests can call it; not part of the plugin's API. */ public static function maxImageBytes(?string $postMaxSize = null): int { $postMax = wp_convert_hr_to_bytes($postMaxSize ?? (string) ini_get('post_max_size')); if ($postMax <= 0) { return 0; } return max(0, intdiv($postMax - self::IMAGE_BODY_ALLOWANCE, 4) * 3); } /** * The version string for a build output; `$debug` is WP_DEBUG unless a caller (a test) says otherwise. * * @internal Public only so the tests can call it; not part of the plugin's API. */ public static function version(string $relative, ?bool $debug = null): string { if (!($debug ?? (defined('WP_DEBUG') && WP_DEBUG))) { return Plugin::VERSION; } $path = dirname(ALPACA_BOT_FILE) . '/' . $relative; $mtime = is_readable($path) ? filemtime($path) : false; return $mtime === false ? Plugin::VERSION : (string) $mtime; } }