深度思考可折叠组件

ThinkCollapsible.vue

<template>
  <div v-if="thinkContent" class="think-collapsible">
    <div 
      class="think-header" 
      @click="toggleThinkCollapse"
      :class="{ 'collapsed': isCollapsed }"
    >
      <div class="think-header-left">
        <div class="think-icon" :class="{ 'thinking': isThinking }">
          <el-icon v-if="isThinking"><Loading /></el-icon>
          <el-icon v-else><Check /></el-icon>
        </div>
        <span class="think-text">{{ isThinking ? '思考中' : `已深度思考 (用时${duration}秒)` }}</span>
      </div>
      <div class="think-arrow">
        <el-icon><ArrowUp /></el-icon>
      </div>
    </div>
    <div 
      v-show="!isCollapsed" 
      class="think-content"
    >
      <p class="think-content-text">{{ thinkContent }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { Check, ArrowUp, Loading } from '@element-plus/icons-vue'

interface Props {
  thinkContent: string
  isThinking?: boolean
  duration?: number
}

const props = withDefaults(defineProps<Props>(), {
  isThinking: false,
  duration: 0
})

const isCollapsed = ref(false)

const toggleThinkCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

// 当思考内容变化时,自动展开
watch(() => props.thinkContent, (newContent) => {
  if (newContent && !isCollapsed.value) {
    isCollapsed.value = false
  }
})
</script>

<style lang="scss" scoped>
.think-collapsible {
  margin-bottom: 16px;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.think-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%);
  cursor: pointer;
  transition: all 0.3s ease;
  user-select: none;

  &:hover {
    background: linear-gradient(135deg, #bbdefb 0%, #e1bee7 100%);
  }

  &.collapsed .think-arrow {
    transform: rotate(180deg);
  }
}

.think-header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.think-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  background: #4caf50;
  border-radius: 50%;
  color: white;
  font-size: 12px;
  transition: all 0.3s ease;

  &.thinking {
    background: #ff9800;
    animation: pulse 1.5s infinite;
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.8;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.think-text {
  font-size: 14px;
  font-weight: 500;
  color: #2c3e50;
}

.think-arrow {
  display: flex;
  align-items: center;
  color: #6c757d;
  transition: transform 0.3s ease;
}

.think-content {
  background: #f8f9fa;
  border-top: 1px solid #e9ecef;
}

.think-content-text {
  padding: 16px;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-all;
  font-size: 14px;
  line-height: 1.6;
  color: #6e798c;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151