StreamStatusBar.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <div class="ai-status">
  3. <span class="ai-status__dot"></span>
  4. <span class="ai-status__phase">{{ phaseText }}</span>
  5. <span class="ai-status__elapsed">{{ elapsed }}s</span>
  6. <template v-if="intent?.intent">
  7. <span class="ai-status__sep"></span>
  8. <a-tag class="ai-status__tag" :bordered="false">{{ shortIntent }}</a-tag>
  9. <a-tooltip v-if="intentDetail" placement="topLeft" :title="intentDetail">
  10. <span class="ai-status__hint">意图详情</span>
  11. </a-tooltip>
  12. </template>
  13. </div>
  14. </template>
  15. <script lang="ts" setup>
  16. /**
  17. * 流式状态条
  18. *
  19. * 让用户随时知道 AI 当前处于哪个阶段(识别意图 / 调用工具 / 生成回答),
  20. * 并展示意图识别结果与耗时 —— 避免长任务期间界面"看起来卡住了"。
  21. */
  22. import { computed, onBeforeUnmount, ref, watch } from 'vue';
  23. import { Tag, Tooltip } from 'ant-design-vue';
  24. import type { SseIntentData, ToolStep } from '../api/types';
  25. import { toolLabel } from '../utils/constants';
  26. const ATag = Tag;
  27. const ATooltip = Tooltip;
  28. const props = defineProps<{
  29. streaming: boolean;
  30. intent?: SseIntentData;
  31. steps?: ToolStep[];
  32. /** 是否已开始输出正文 */
  33. hasContent?: boolean;
  34. /** 模型是否正在输出思考过程 */
  35. thinkingActive?: boolean;
  36. }>();
  37. const elapsed = ref(0);
  38. let timer: ReturnType<typeof setInterval> | null = null;
  39. function startTimer() {
  40. elapsed.value = 0;
  41. stopTimer();
  42. timer = setInterval(() => (elapsed.value += 1), 1000);
  43. }
  44. function stopTimer() {
  45. if (timer) {
  46. clearInterval(timer);
  47. timer = null;
  48. }
  49. }
  50. watch(
  51. () => props.streaming,
  52. (val) => (val ? startTimer() : stopTimer()),
  53. { immediate: true },
  54. );
  55. onBeforeUnmount(stopTimer);
  56. const runningTool = computed(() => props.steps?.find((s) => s.status === 'running'));
  57. const phaseText = computed(() => {
  58. if (runningTool.value) return `正在执行:${toolLabel(runningTool.value.toolName)}`;
  59. if (props.hasContent) return '正在生成回答';
  60. if (props.thinkingActive) return '正在深度思考';
  61. if (props.steps?.length) return '正在整理结果';
  62. if (props.intent) return '正在规划分析步骤';
  63. return '正在理解问题';
  64. });
  65. const shortIntent = computed(() => {
  66. const raw = props.intent?.intent || '';
  67. return raw.replace(/^《|》$/g, '').slice(0, 12) || '意图';
  68. });
  69. const intentDetail = computed(() => {
  70. const parts: string[] = [];
  71. if (props.intent?.enhanced) parts.push(`增强问题:${props.intent.enhanced}`);
  72. if (props.intent?.entities?.length) parts.push(`关键实体:${props.intent.entities.join('、')}`);
  73. return parts.join('\n');
  74. });
  75. </script>
  76. <style lang="less" scoped>
  77. .ai-status {
  78. display: flex;
  79. align-items: center;
  80. gap: var(--ai-sp-2);
  81. margin-bottom: var(--ai-sp-2);
  82. font-size: 12px;
  83. color: var(--ai-text-2);
  84. &__dot {
  85. flex: 0 0 auto;
  86. width: 7px;
  87. height: 7px;
  88. border-radius: 50%;
  89. background: @primary-color;
  90. animation: ai-status-pulse 1.2s ease-in-out infinite;
  91. }
  92. &__phase {
  93. color: var(--ai-text);
  94. }
  95. &__elapsed {
  96. font-variant-numeric: tabular-nums;
  97. opacity: 0.75;
  98. }
  99. &__sep {
  100. width: 1px;
  101. height: 12px;
  102. background: var(--ai-border);
  103. }
  104. &__tag {
  105. margin: 0;
  106. font-size: 11px;
  107. line-height: 18px;
  108. color: @primary-color;
  109. background: var(--ai-primary-soft);
  110. }
  111. &__hint {
  112. font-size: 11px;
  113. color: var(--ai-text-2);
  114. border-bottom: 1px dashed var(--ai-border-strong);
  115. cursor: help;
  116. }
  117. }
  118. @keyframes ai-status-pulse {
  119. 0%,
  120. 100% {
  121. opacity: 1;
  122. transform: scale(1);
  123. }
  124. 50% {
  125. opacity: 0.35;
  126. transform: scale(0.82);
  127. }
  128. }
  129. </style>