feat: add thinking mode support for agents
This commit is contained in:
@@ -312,6 +312,7 @@ export default {
|
||||
availablePlaceholders: 'Available Placeholders',
|
||||
placeholderHint: 'Type {{ to trigger autocomplete',
|
||||
temperature: 'Temperature',
|
||||
thinking: 'Thinking Mode',
|
||||
welcomeMessage: 'Welcome Message',
|
||||
welcomeMessagePlaceholder: 'Message displayed when this agent is selected',
|
||||
suggestedPrompts: 'Suggested Prompts',
|
||||
|
||||
@@ -842,6 +842,7 @@ export default {
|
||||
availablePlaceholders: "可用占位符",
|
||||
placeholderHint: "输入 {'{{'} 触发自动补全",
|
||||
temperature: "温度",
|
||||
thinking: "思考模式",
|
||||
welcomeMessage: "欢迎消息",
|
||||
welcomeMessagePlaceholder: "选择该智能体时显示的欢迎消息",
|
||||
suggestedPrompts: "推荐问题",
|
||||
|
||||
@@ -307,6 +307,17 @@
|
||||
<t-input-number v-model="formData.config.max_completion_tokens" :min="100" :max="100000" :step="100" theme="column" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 思考模式 -->
|
||||
<div class="setting-row">
|
||||
<div class="setting-info">
|
||||
<label>{{ $t('agent.editor.thinking') || '思考模式' }}</label>
|
||||
<p class="desc">启用模型的扩展思考能力(需要模型支持)</p>
|
||||
</div>
|
||||
<div class="setting-control">
|
||||
<t-switch v-model="thinkingEnabled" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1173,6 +1184,7 @@ const defaultFormData = {
|
||||
rerank_model_id: '',
|
||||
temperature: 0.7,
|
||||
max_completion_tokens: 2048,
|
||||
thinking: false, // 默认禁用思考模式
|
||||
// Agent模式设置
|
||||
max_iterations: 10,
|
||||
allowed_tools: [] as string[],
|
||||
@@ -1223,6 +1235,12 @@ const agentMode = computed({
|
||||
|
||||
const isAgentMode = computed(() => agentMode.value === 'smart-reasoning');
|
||||
|
||||
// 思考模式计算属性(直接绑定 boolean)
|
||||
const thinkingEnabled = computed({
|
||||
get: () => formData.value.config.thinking === true,
|
||||
set: (val: boolean) => { formData.value.config.thinking = val; }
|
||||
});
|
||||
|
||||
// 是否为内置智能体
|
||||
const isBuiltinAgent = computed(() => {
|
||||
return formData.value.is_builtin === true;
|
||||
|
||||
@@ -24,16 +24,9 @@
|
||||
</div>
|
||||
<!-- 非 Agent 模式下才显示传统的 markdown 渲染 -->
|
||||
<div ref="parentMd" v-if="!session.hideContent && !session.isAgentMode">
|
||||
<!-- 消息正在总结中则渲染加载动画 -->
|
||||
<div v-if="session.thinking" class="thinking-loading">
|
||||
<div class="loading-typing">
|
||||
<span></span>
|
||||
<span></span>
|
||||
<span></span>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 直接渲染完整内容,避免切分导致的问题,样式与 thinking 一致 -->
|
||||
<div class="content-wrapper">
|
||||
<!-- 只有当有实际内容时才显示包围框 -->
|
||||
<div class="content-wrapper" v-if="hasActualContent">
|
||||
<div class="ai-markdown-template markdown-content">
|
||||
<div v-for="(token, index) in markdownTokens" :key="index" v-html="renderToken(token)"></div>
|
||||
</div>
|
||||
@@ -136,6 +129,12 @@ const markdownTokens = computed(() => {
|
||||
return marked.lexer(safeMarkdown);
|
||||
});
|
||||
|
||||
// 计算属性:判断是否有实际内容(非空且不只是空白)
|
||||
const hasActualContent = computed(() => {
|
||||
const text = props.content || props.session?.content || '';
|
||||
return text && text.trim().length > 0;
|
||||
});
|
||||
|
||||
// 渲染单个 token 为 HTML
|
||||
const renderToken = (token) => {
|
||||
try {
|
||||
@@ -428,8 +427,7 @@ onBeforeUnmount(() => {
|
||||
}
|
||||
|
||||
.thinking-loading {
|
||||
margin-left: 16px;
|
||||
margin-bottom: 8px;
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.loading-typing {
|
||||
|
||||
@@ -1,38 +1,32 @@
|
||||
<template>
|
||||
<div class='deep-think'>
|
||||
<t-collapse :default-value="deepSession.thinking ? [0] : []" expand-icon-placement="right" :expand-icon="true"
|
||||
:borderless="true" @change="handlePanelChange">
|
||||
<t-collapse-panel>
|
||||
<template #expandIcon>
|
||||
<div v-if="!deepSession.thinking" class="chevron">
|
||||
<t-icon :name="!isFold ? 'chevron-up' : 'chevron-down'" />
|
||||
</div>
|
||||
</template>
|
||||
<template #header>
|
||||
<div class="deep-title">
|
||||
<div v-if="deepSession.thinking" class="thinking">
|
||||
<img class="img_gif" src="@/assets/img/think.gif" :alt="$t('chat.thinkingAlt')">{{ $t('chat.thinking') }}
|
||||
</div>
|
||||
<div v-else class="done">
|
||||
<img class="icon deep_icon" src="@/assets/img/Frame3718.svg" :alt="$t('chat.deepThoughtAlt')"></img>{{ $t('chat.deepThoughtCompleted') }}
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<div class="content">
|
||||
<span v-html="safeProcessThinkContent(deepSession.thinkContent)"></span>
|
||||
</div>
|
||||
</t-collapse-panel>
|
||||
|
||||
</t-collapse>
|
||||
|
||||
<div class="think-header" @click="toggleFold">
|
||||
<div class="think-title">
|
||||
<span v-if="deepSession.thinking" class="thinking-status">
|
||||
<img class="thinking-gif" src="@/assets/img/think.gif" :alt="$t('chat.thinkingAlt')">
|
||||
<span class="thinking-text">{{ $t('chat.thinking') }}</span>
|
||||
</span>
|
||||
<span v-else class="done-status">
|
||||
<img class="done-icon" src="@/assets/img/Frame3718.svg" :alt="$t('chat.deepThoughtAlt')">
|
||||
<span class="done-text">{{ $t('chat.deepThoughtCompleted') }}</span>
|
||||
</span>
|
||||
</div>
|
||||
<div class="toggle-icon-wrapper">
|
||||
<t-icon :name="isFold ? 'chevron-down' : 'chevron-up'" class="toggle-icon" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="think-content" v-show="!isFold || deepSession.thinking">
|
||||
<div ref="contentInnerRef" class="content-inner" v-html="safeProcessThinkContent(deepSession.thinkContent)"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<script setup>
|
||||
import { onMounted, watch, computed, ref, reactive, defineProps } from 'vue';
|
||||
import { watch, ref, defineProps, onMounted, nextTick } from 'vue';
|
||||
import { sanitizeHTML } from '@/utils/security';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
const isFold = ref(true)
|
||||
const isFold = ref(false)
|
||||
const contentInnerRef = ref(null)
|
||||
const { t } = useI18n()
|
||||
const props = defineProps({
|
||||
// 必填项
|
||||
@@ -42,18 +36,45 @@ const props = defineProps({
|
||||
}
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.deepSession,
|
||||
(newVal) => { },
|
||||
{ deep: true }
|
||||
);
|
||||
// 初始化时检查:如果 thinking 已完成(从历史记录加载),默认折叠
|
||||
onMounted(() => {
|
||||
})
|
||||
const showHide = () => {
|
||||
isFold.value = !isFold.value;
|
||||
}
|
||||
const handlePanelChange = (val) => {
|
||||
isFold.value = !val.length ? true : false;
|
||||
if (props.deepSession?.thinking === false) {
|
||||
isFold.value = true;
|
||||
}
|
||||
});
|
||||
|
||||
// 监听 thinking 状态变化,自动折叠
|
||||
watch(
|
||||
() => props.deepSession?.thinking,
|
||||
(newVal, oldVal) => {
|
||||
// 当 thinking 从 true 变为 false 时,自动折叠 thinking 内容
|
||||
// 只在流式输出场景下触发(oldVal 为 true)
|
||||
if (oldVal === true && newVal === false) {
|
||||
isFold.value = true;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 监听内容变化,自动滚动到底部
|
||||
watch(
|
||||
() => props.deepSession?.thinkContent,
|
||||
() => {
|
||||
// 只在 thinking 进行中时滚动
|
||||
if (props.deepSession?.thinking) {
|
||||
nextTick(() => {
|
||||
if (contentInnerRef.value) {
|
||||
contentInnerRef.value.scrollTop = contentInnerRef.value.scrollHeight;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const toggleFold = () => {
|
||||
// 只有 thinking 完成后才能折叠/展开
|
||||
if (!props.deepSession?.thinking) {
|
||||
isFold.value = !isFold.value;
|
||||
}
|
||||
}
|
||||
|
||||
// 安全地处理思考内容,防止XSS攻击
|
||||
@@ -71,83 +92,103 @@ const safeProcessThinkContent = (content) => {
|
||||
</script>
|
||||
<style lang="less" scoped>
|
||||
.deep-think {
|
||||
background: #30323605;
|
||||
border-radius: 2px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
font-size: 12px;
|
||||
width: 100%;
|
||||
border-radius: 8px;
|
||||
background-color: #ffffff;
|
||||
box-shadow: 0 2px 4px rgba(7, 192, 95, 0.08);
|
||||
overflow: hidden;
|
||||
box-sizing: border-box;
|
||||
transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
margin: -8px 0px 10px 0px;
|
||||
|
||||
:deep(.t-collapse.t--border-less .t-collapse-panel__body) {
|
||||
background: #30323605 !important;
|
||||
}
|
||||
|
||||
|
||||
:deep(.t-collapse-panel__wrapper .t-collapse-panel__header) {
|
||||
background: #30323605 !important;
|
||||
padding: 4px;
|
||||
|
||||
&:hover {
|
||||
background-color: #30323605 !important;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.t-collapse-panel__wrapper .t-collapse-panel__content) {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.chevron {
|
||||
color: #00000099;
|
||||
font-size: 14px;
|
||||
padding: 0 2px 1px 2px;
|
||||
}
|
||||
|
||||
.deep-title {
|
||||
.think-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 6px 14px;
|
||||
color: #333333;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
// padding: 4px;
|
||||
user-select: none;
|
||||
|
||||
&:hover {
|
||||
background: #30323605;
|
||||
background-color: rgba(7, 192, 95, 0.04);
|
||||
}
|
||||
|
||||
|
||||
|
||||
.thinking {
|
||||
font-size: 14px;
|
||||
color: #000000e6;
|
||||
.think-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.img_gif {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
margin-right: 4px;
|
||||
}
|
||||
|
||||
.done {
|
||||
font-size: 14px;
|
||||
color: #00000099;
|
||||
.thinking-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
.deep_icon {
|
||||
|
||||
.thinking-gif {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
color: #07c05f;
|
||||
margin-right: 4px;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.thinking-text {
|
||||
font-size: 12px;
|
||||
color: #333333;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
|
||||
.done-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
.done-icon {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.done-text {
|
||||
font-size: 12px;
|
||||
color: #333333;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
|
||||
.toggle-icon-wrapper {
|
||||
font-size: 14px;
|
||||
padding: 0 2px 1px 2px;
|
||||
color: #07c05f;
|
||||
|
||||
.toggle-icon {
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.content {
|
||||
line-height: 23px;
|
||||
font-size: 14px;
|
||||
font-family: "PingFang SC";
|
||||
color: #00000099;
|
||||
margin: 6px 8px 4px;
|
||||
word-break: break-all;
|
||||
text-align: justify;
|
||||
.think-content {
|
||||
border-top: 1px solid #f0f0f0;
|
||||
|
||||
.content-inner {
|
||||
padding: 8px 14px;
|
||||
font-size: 12px;
|
||||
line-height: 1.6;
|
||||
color: #666666;
|
||||
max-height: 200px;
|
||||
overflow-y: auto;
|
||||
word-break: break-word;
|
||||
|
||||
&::-webkit-scrollbar {
|
||||
width: 4px;
|
||||
}
|
||||
|
||||
&::-webkit-scrollbar-thumb {
|
||||
background: rgba(0, 0, 0, 0.1);
|
||||
border-radius: 2px;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
@@ -243,12 +243,21 @@ const handleMsgList = async (data, isScrollType = false, newScrollHeight) => {
|
||||
item.thinkContent = "";
|
||||
item.content = item.content;
|
||||
item.showThink = false;
|
||||
item.thinking = false;
|
||||
} else if (item.content.includes('<\/think>')) {
|
||||
// 历史消息中包含完整的 <think>...</think> 标签,说明 thinking 已完成
|
||||
const arr = item.content.trim().split('<\/think>');
|
||||
item.showThink = true;
|
||||
item.thinking = false; // 关键:标记 thinking 已完成,使 deepThink 默认折叠
|
||||
item.thinkContent = arr[0].trim().replace('<think>', '');
|
||||
let index = item.content.trim().lastIndexOf('<\/think>')
|
||||
item.content = item.content.substring(index + 8);
|
||||
} else if (item.content.includes('<think>')) {
|
||||
// 内容包含 <think> 但没有 </think>,说明 thinking 还在进行中(不太可能出现在历史消息中)
|
||||
item.showThink = true;
|
||||
item.thinking = true;
|
||||
item.thinkContent = item.content.replace('<think>', '').trim();
|
||||
item.content = '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,16 +417,55 @@ onChunk((data) => {
|
||||
}
|
||||
|
||||
// 判断是否是 Agent 模式的响应
|
||||
const isAgentResponse = data.response_type === 'thinking' ||
|
||||
data.response_type === 'tool_call' ||
|
||||
data.response_type === 'tool_result' ||
|
||||
data.response_type === 'references' ||
|
||||
data.response_type === 'answer' ||
|
||||
data.response_type === 'reflection' ||
|
||||
data.response_type === 'stop';
|
||||
// 注意:'answer', 'complete', 'references' 类型可能在两种模式下都存在
|
||||
// 只有 'thinking', 'tool_call', 'tool_result', 'reflection' 是 Agent 专有的
|
||||
const isAgentOnlyResponse = data.response_type === 'thinking' ||
|
||||
data.response_type === 'tool_call' ||
|
||||
data.response_type === 'tool_result' ||
|
||||
data.response_type === 'reflection';
|
||||
|
||||
// 检查当前消息是否已经是 Agent 模式
|
||||
const lastMessage = messagesList[messagesList.length - 1];
|
||||
const isCurrentlyAgentMode = lastMessage?.isAgentMode === true;
|
||||
|
||||
// 如果是 Agent 专有的响应类型,或者当前消息已经是 Agent 模式,则走 Agent 处理
|
||||
const shouldHandleAsAgent = isAgentOnlyResponse || isCurrentlyAgentMode;
|
||||
|
||||
// 处理 references 事件 - 在两种模式下都需要处理,但不改变模式
|
||||
if (data.response_type === 'references') {
|
||||
// 如果当前是 Agent 模式,走 Agent 处理
|
||||
if (isCurrentlyAgentMode) {
|
||||
handleAgentChunk(data);
|
||||
return;
|
||||
}
|
||||
// 非 Agent 模式:将 references 保存到消息中供 botmsg 使用
|
||||
let existingMessage = messagesList.findLast((item) => item.request_id === data.id || item.id === data.id);
|
||||
|
||||
// 如果消息还不存在,先创建一个空的 assistant 消息
|
||||
if (!existingMessage) {
|
||||
existingMessage = {
|
||||
id: data.id,
|
||||
request_id: data.id,
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
showThink: false,
|
||||
thinkContent: '',
|
||||
thinking: false,
|
||||
is_completed: false,
|
||||
knowledge_references: []
|
||||
};
|
||||
messagesList.push(existingMessage);
|
||||
loading.value = false; // 消息已创建,关闭 loading
|
||||
scrollToBottom();
|
||||
}
|
||||
|
||||
existingMessage.knowledge_references = data.knowledge_references || data.data?.references || [];
|
||||
console.log('[References] Saved to message, count:', existingMessage.knowledge_references.length);
|
||||
return;
|
||||
}
|
||||
|
||||
// Agent 模式处理(包括 stop 事件)
|
||||
if (isAgentResponse || messagesList[messagesList.length - 1]?.isAgentMode) {
|
||||
if (shouldHandleAsAgent) {
|
||||
// 在 handleAgentChunk 中处理 loading 状态
|
||||
handleAgentChunk(data);
|
||||
|
||||
@@ -432,7 +480,23 @@ onChunk((data) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 原有的知识库 QA 处理逻辑
|
||||
// 原有的知识库 QA 处理逻辑(非 Agent 模式)
|
||||
// answer 内容中可能包含 <think>...</think> 标签
|
||||
|
||||
// 检查消息是否已经完成,如果已完成则忽略后续的完成事件(防止空内容覆盖)
|
||||
const existingMessage = messagesList.findLast((item) => {
|
||||
if (item.request_id === data.id) {
|
||||
return true
|
||||
}
|
||||
return item.id === data.id;
|
||||
});
|
||||
|
||||
// 如果消息已完成且当前事件是完成事件(done=true 且无内容),直接忽略
|
||||
if (existingMessage?.is_completed && data.done && !data.content) {
|
||||
console.log('[Non-Agent] Ignoring duplicate completion event for completed message');
|
||||
return;
|
||||
}
|
||||
|
||||
fullContent.value += data.content;
|
||||
let obj = { ...data, content: '', role: 'assistant', showThink: false, is_completed: false };
|
||||
|
||||
@@ -451,13 +515,6 @@ onChunk((data) => {
|
||||
obj.content = fullContent.value;
|
||||
}
|
||||
|
||||
// 检查是否已有消息,如果没有则说明这是第一次,关闭 loading
|
||||
const existingMessage = messagesList.findLast((item) => {
|
||||
if (item.request_id === obj.id) {
|
||||
return true
|
||||
}
|
||||
return item.id === obj.id;
|
||||
});
|
||||
if (!existingMessage) {
|
||||
loading.value = false; // 消息即将创建,关闭 loading
|
||||
}
|
||||
@@ -704,8 +761,20 @@ const handleAgentChunk = (data) => {
|
||||
case 'answer':
|
||||
// 最终答案
|
||||
message.thinking = false;
|
||||
message.content = (message.content || '') + (data.content || '');
|
||||
fullContent.value += data.content || '';
|
||||
|
||||
console.log('[Answer Event] Received:', {
|
||||
has_content: !!data.content,
|
||||
content_length: data.content?.length || 0,
|
||||
done: data.done,
|
||||
current_message_content_length: message.content?.length || 0
|
||||
});
|
||||
|
||||
// 只有当有实际内容时才追加,避免空内容覆盖
|
||||
if (data.content) {
|
||||
message.content = (message.content || '') + data.content;
|
||||
fullContent.value += data.content;
|
||||
console.log('[Answer] Content appended, new length:', message.content.length);
|
||||
}
|
||||
|
||||
// Add or update answer event in agentEventStream
|
||||
if (!message.agentEventStream) message.agentEventStream = [];
|
||||
@@ -718,13 +787,19 @@ const handleAgentChunk = (data) => {
|
||||
done: false
|
||||
};
|
||||
message.agentEventStream.push(answerEvent);
|
||||
console.log('[Answer] Created new answer event in stream');
|
||||
}
|
||||
|
||||
answerEvent.content = message.content;
|
||||
answerEvent.done = data.done;
|
||||
// 只有当有实际内容时才更新 answerEvent.content
|
||||
if (data.content) {
|
||||
answerEvent.content = message.content;
|
||||
console.log('[Answer] answerEvent.content updated, length:', answerEvent.content.length);
|
||||
}
|
||||
|
||||
if (data.done) {
|
||||
console.log('[Agent] Answer done, content length:', message.content?.length || 0);
|
||||
// 只在第一次收到 done:true 时标记完成,忽略后续重复的完成事件
|
||||
if (data.done && !answerEvent.done) {
|
||||
answerEvent.done = true;
|
||||
console.log('[Agent] Answer done, content length:', message.content?.length || 0, 'answerEvent.content length:', answerEvent.content?.length || 0);
|
||||
|
||||
// 完成 - 关闭所有状态
|
||||
loading.value = false;
|
||||
@@ -735,9 +810,19 @@ const handleAgentChunk = (data) => {
|
||||
|
||||
// 标题生成已改为异步事件推送,不再需要在这里手动调用
|
||||
// 如果标题还未生成,前端会通过 SSE 事件接收
|
||||
} else if (data.done && answerEvent.done) {
|
||||
console.log('[Answer] Ignoring duplicate done event, current content preserved:', answerEvent.content?.length || 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'complete':
|
||||
// 整个流式响应完成事件 - 确保状态正确关闭
|
||||
console.log('[Agent] Complete event received');
|
||||
loading.value = false;
|
||||
isReplying.value = false;
|
||||
// 不清空 message.content,保持已有内容
|
||||
break;
|
||||
|
||||
case 'stop':
|
||||
// 停止事件 - 添加到事件流并标记对话完成
|
||||
console.log('[Agent] Stop event received');
|
||||
|
||||
Reference in New Issue
Block a user