m->role === 'assistant'; $name = $assistant ? ($this->m->model !== '' ? $this->m->model : __('Assistant', 'alpaca-bot')) : $this->userName; $content = $this->streaming ? '' : ($assistant ? $this->md->toHtml($this->m->content) : nl2br($this->e($this->m->content), false)); $actions = $this->tag('button', ['type' => 'button', 'class' => 'ab-msg__action', 'data-action' => 'copy', 'aria-label' => __('Copy message', 'alpaca-bot')], Icon::svg('copy')); if (!$assistant) { $actions .= $this->tag('button', ['type' => 'button', 'class' => 'ab-msg__action', 'data-action' => 'edit', 'aria-label' => __('Edit and resend', 'alpaca-bot')], Icon::svg('square-pen')); } $receipt = $assistant && $this->m->usage !== null ? (new Receipt([ 'model' => $this->m->model, 'total_tokens' => ($this->m->usage['prompt_tokens'] ?? 0) + ($this->m->usage['completion_tokens'] ?? 0), 'duration_ms' => (int) ($this->m->meta['duration_ms'] ?? 0), 'tool_calls' => is_array($this->m->meta['tool_calls'] ?? null) ? count($this->m->meta['tool_calls']) : 0, ]))->render() : ''; $inner = $this->tag('img', ['class' => 'ab-msg__avatar', 'src' => $this->u($assistant ? $this->assistantAvatar : $this->userAvatar), 'alt' => '']) . $this->tag('div', ['class' => 'ab-msg__body'], $this->tag('header', ['class' => 'ab-msg__meta'], $this->tag('span', ['class' => 'ab-msg__name'], $this->e($name)) . $this->tag('span', ['class' => 'ab-msg__actions'], $actions)) . ($assistant ? '' : $this->images()) . $this->tag('div', ['class' => 'ab-msg__content', 'aria-live' => $this->streaming ? 'polite' : null], $content) . $receipt); return $this->tag('article', ['class' => 'ab-msg ab-msg--' . ($assistant ? 'assistant' : 'user'), 'data-role' => $this->m->role, 'data-streaming' => $this->streaming ? '1' : null], $inner); } private function images(): string { $imgs = ''; foreach ($this->m->images as $src) { if (ImageData::isValid($src)) { $imgs .= $this->tag('img', ['class' => 'ab-msg__image', 'src' => $src, 'alt' => __('Attached image', 'alpaca-bot')]); } } // The marker is read as stored (an int, or whatever a filter left there): only a count above zero says anything. $evicted = $this->m->meta['images_evicted'] ?? 0; $evicted = is_int($evicted) ? $evicted : 0; if ($evicted > 0) { /* translators: %s: how many of the turn's attached images the store let go to fit the transcript into the database */ $imgs .= $this->tag('span', ['class' => 'ab-msg__evicted'], $this->e(sprintf(_n('%s attached image is no longer stored.', '%s attached images are no longer stored.', $evicted, 'alpaca-bot'), number_format_i18n($evicted)))); } return $imgs === '' ? '' : $this->tag('div', ['class' => 'ab-msg__images'], $imgs); } }