AiTemplate.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Services\Markup\Format;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Ir\Document;
  6. use App\Services\Markup\Render\HtmlRenderer;
  7. use App\Services\Markup\Render\RenderContext;
  8. /**
  9. * AI 生成内容提示:`{{ai}}`、`{{ai|标题}}`、`{{ai|标题|说明}}`、`{{ai|title=…|message=…|style=tag}}`。
  10. *
  11. * - title:标题,缺省(或为空)是「AI 生成内容」(labels.ai_generated,随内容语言)
  12. * - message:说明文字,markdown(可嵌模板)
  13. * - style:`block`(默认)黄底引用块,左上角警告图标 + 标题,下面是说明;
  14. * `tag` 行内只显示图标与「AI」,鼠标移入才显示标题与说明
  15. *
  16. * tree:`{name: 'ai', props: {title, style}, children: message}`。
  17. *
  18. * 取代旧的 AI 对话组件 `{{ai|model}}`(只有 v2 TemplateRender / dashboard-v4 在用,那边不动)。
  19. */
  20. final class AiTemplate extends Template
  21. {
  22. protected string $component = 'ai';
  23. private const STYLES = ['block', 'tag'];
  24. private const ICON = '<svg class="markup-ai-icon" viewBox="0 0 16 16" width="1em" height="1em" aria-hidden="true">'
  25. .'<path fill="currentColor" d="M8.87 1.5a1 1 0 0 0-1.74 0L.63 13a1 1 0 0 0 .87 1.5h13a1 1 0 0 0 .87-1.5L8.87 1.5ZM8 5a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 8 5Zm0 7.25a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"/>'
  26. .'</svg>';
  27. public function params(): array
  28. {
  29. return [
  30. 'title' => ParamSpec::at(1),
  31. 'message' => ParamSpec::markdown(2),
  32. 'style' => ParamSpec::at(3, 'block'),
  33. ];
  34. }
  35. public function block(Call $call): bool
  36. {
  37. return $this->style($call) === 'block';
  38. }
  39. private function style(Call $call): string
  40. {
  41. $style = trim((string) $call->param('style'));
  42. return in_array($style, self::STYLES, true) ? $style : 'block';
  43. }
  44. private function title(Call $call, RenderContext $ctx): string
  45. {
  46. $title = trim((string) $call->param('title'));
  47. return $title !== '' ? $title : (string) __('labels.ai_generated', [], $ctx->lang($call->scope));
  48. }
  49. /**
  50. * @return array<string, mixed>
  51. */
  52. public function props(Call $call, RenderContext $ctx): array
  53. {
  54. return ['title' => $this->title($call, $ctx), 'style' => $this->style($call)];
  55. }
  56. public function children(Call $call, RenderContext $ctx): ?Document
  57. {
  58. return $call->slot('message');
  59. }
  60. private function message(Call $call, RenderContext $ctx): string
  61. {
  62. $slot = $call->slot('message');
  63. return $slot === null ? '' : trim($ctx->renderString($slot));
  64. }
  65. public function text(Call $call, RenderContext $ctx): string
  66. {
  67. $message = $this->message($call, $ctx);
  68. $title = $this->title($call, $ctx);
  69. return $message === '' ? "[{$title}]" : "[{$title}] {$message}";
  70. }
  71. /**
  72. * 是界面提示而不是正文:prompt 里不出现。
  73. */
  74. public function plain(Call $call, RenderContext $ctx, Format $format): string
  75. {
  76. return $format === Format::Prompt ? '' : $this->text($call, $ctx);
  77. }
  78. /**
  79. * 只用 span(block 样式靠 CSS 显示成块),写在句子中间、脚注里都不会切断外层结构。
  80. */
  81. public function html(Call $call, RenderContext $ctx): string
  82. {
  83. $slot = $call->slot('message');
  84. $message = $slot !== null && $ctx->renderer instanceof HtmlRenderer
  85. ? $ctx->renderer->inlineSafe($slot, $ctx)
  86. : e($this->message($call, $ctx));
  87. $title = e($this->title($call, $ctx));
  88. $messageHtml = $message === '' ? '' : '<span class="markup-ai-message">'.$message.'</span>';
  89. if ($this->style($call) === 'tag') {
  90. return '<span class="markup-ai markup-ai-tag" tabindex="0">'.self::ICON.'AI'
  91. .'<span class="markup-ai-popup" role="tooltip"><span class="markup-ai-title">'.$title.'</span>'.$messageHtml.'</span>'
  92. .'</span>';
  93. }
  94. return '<span class="markup-ai markup-ai-block" role="note">'
  95. .'<span class="markup-ai-title">'.self::ICON.$title.'</span>'.$messageHtml
  96. .'</span>';
  97. }
  98. public function markdown(Call $call, RenderContext $ctx): string
  99. {
  100. $title = $this->title($call, $ctx);
  101. $message = $this->message($call, $ctx);
  102. if ($this->style($call) === 'tag') {
  103. return '⚠ AI';
  104. }
  105. return "\n\n> ⚠ **{$title}**".($message === '' ? '' : "\n>\n> ".str_replace("\n", "\n> ", $message))."\n\n";
  106. }
  107. }