feat(chat): 添加深度思考模式支持

refactor
钟良源 3 weeks ago
parent df345447ca
commit 84bd2d1617

@ -41,12 +41,17 @@ document.addEventListener('DOMContentLoaded', () => {
return; return;
} }
// 获取深度思考开关状态
const deepThinkingCheckbox = document.getElementById('deep-thinking-checkbox');
const enableDeepThinking = deepThinkingCheckbox ? deepThinkingCheckbox.checked : false;
// 发送消息 // 发送消息
vscode.postMessage({ vscode.postMessage({
command: 'ask', command: 'ask',
text, text,
fileContent: selectedCodeForContext, fileContent: selectedCodeForContext,
fileContentPath: contextFilePath // 添加文件路径 fileContentPath: contextFilePath, // 添加文件路径
enableDeepThinking: enableDeepThinking // 添加深度思考标志
}); });
// 切换按钮为停止按钮 // 切换按钮为停止按钮

@ -64,46 +64,62 @@ code {
/* 表单区域 */ /* 表单区域 */
#chat-form { #chat-form {
display: flex;
gap: 10px;
margin-top: auto; margin-top: auto;
padding: 10px 0; padding: 10px 0;
} }
.input-container {
display: flex;
align-items: center;
gap: 10px;
padding: 8px;
background-color: var(--vscode-input-background);
border: 1px solid var(--vscode-dropdown-border);
border-radius: 6px;
transition: border-color 0.2s;
}
.input-container:focus-within {
border-color: var(--vscode-focusBorder);
box-shadow: 0 0 0 1px var(--vscode-focusBorder);
}
#user-input { #user-input {
flex: 1; flex: 1;
padding: 8px 12px; padding: 6px 8px;
border: 1px solid var(--vscode-dropdown-border); border: none;
border-radius: 4px; background-color: transparent;
background-color: var(--vscode-input-background);
color: var(--vscode-input-foreground); color: var(--vscode-input-foreground);
font-family: var(--vscode-font-family); font-family: var(--vscode-font-family);
font-size: var(--vscode-font-size); font-size: var(--vscode-font-size);
outline: none;
} }
#user-input:focus { #user-input::placeholder {
outline: none; color: var(--vscode-input-placeholderForeground);
border-color: var(--vscode-focusBorder);
} }
#chat-form button { #chat-form button {
padding: 8px 16px; padding: 6px 16px;
background-color: #007acc; background-color: var(--vscode-button-background);
color: white; color: var(--vscode-button-foreground);
border: none; border: none;
border-radius: 4px; border-radius: 4px;
cursor: pointer; cursor: pointer;
font-size: 14px; font-size: 13px;
font-weight: 500;
transition: background-color 0.2s; transition: background-color 0.2s;
white-space: nowrap;
} }
#chat-form button:hover { #chat-form button:hover {
background-color: #005a9e; background-color: var(--vscode-button-hoverBackground);
} }
/* 停止按钮样式 */ /* 停止按钮样式 */
#chat-form button.stop-button { #chat-form button.stop-button {
background-color: #f44336; background-color: #f44336;
color: white;
} }
#chat-form button.stop-button:hover { #chat-form button.stop-button:hover {
@ -501,3 +517,33 @@ code {
color: var(--vscode-descriptionForeground); color: var(--vscode-descriptionForeground);
font-size: 12px; font-size: 12px;
} }
/* 深度思考开关样式 */
.deep-thinking-toggle {
display: flex;
align-items: center;
gap: 6px;
cursor: pointer;
font-size: 12px;
white-space: nowrap;
user-select: none;
padding: 4px 8px;
border-radius: 4px;
transition: background-color 0.2s;
}
.deep-thinking-toggle:hover {
background-color: var(--vscode-list-hoverBackground);
}
.deep-thinking-toggle input[type="checkbox"] {
cursor: pointer;
width: 14px;
height: 14px;
margin: 0;
}
.deep-thinking-toggle .toggle-label {
color: var(--vscode-foreground);
font-weight: 500;
}

@ -74,13 +74,14 @@ export class MessageHandler {
const fileContent = message.fileContent; const fileContent = message.fileContent;
const fileName = message.fileContentPath ? path.basename(message.fileContentPath) : "current_file.txt"; const fileName = message.fileContentPath ? path.basename(message.fileContentPath) : "current_file.txt";
const history = currentSessionHistory; const history = currentSessionHistory;
const enableDeepThinking = message.enableDeepThinking || false;
currentSessionHistory = [...history, {role: 'user', content: question}]; currentSessionHistory = [...history, {role: 'user', content: question}];
this.provider._postMessage({command: 'addMessage', role: 'user', content: question}); this.provider._postMessage({command: 'addMessage', role: 'user', content: question});
try { try {
const response = await callQwenAPI(question, history, fileContent, this.context, fileName); const response = await callQwenAPI(question, history, fileContent, this.context, fileName, enableDeepThinking);
// 发送开始流式传输的消息 // 发送开始流式传输的消息
this.provider._postMessage({command: 'startStream', role: 'assistant'}); this.provider._postMessage({command: 'startStream', role: 'assistant'});
@ -98,7 +99,7 @@ export class MessageHandler {
if (done) break; if (done) break;
const chunk = decoder.decode(value, {stream: true}); const chunk = decoder.decode(value, {stream: true});
console.log("chunk:",chunk)
// 检查是否包含event: end事件 // 检查是否包含event: end事件
let processedChunk = chunk; let processedChunk = chunk;
if (chunk.includes('event: end')) { if (chunk.includes('event: end')) {

@ -2,13 +2,18 @@ import vscode from "vscode";
import path from "path"; import path from "path";
// 获取API配置的函数 // 获取API配置的函数
function getApiConfig(context: vscode.ExtensionContext) { function getApiConfig(context: vscode.ExtensionContext, enableDeepThinking: boolean = false) {
// 从用户配置中获取设置 // 从用户配置中获取设置
const config = vscode.workspace.getConfiguration('ai-chat.api'); const config = vscode.workspace.getConfiguration('ai-chat.api');
const userUrl = config.get<string>('url'); const userUrl = config.get<string>('url');
const userBaseUrl = config.get<string>('baseUrl'); const userBaseUrl = config.get<string>('baseUrl');
const userApiKey = config.get<string>('key'); const userApiKey = config.get<string>('key');
// 根据是否启用深度思考选择不同的端点
const endpoint = enableDeepThinking
? '/comp/api/v1/chat/completions-stream-cot'
: '/comp/api/v1/chat/completions-stream';
// 如果用户配置了完整URL优先使用 // 如果用户配置了完整URL优先使用
if (userUrl) { if (userUrl) {
return { return {
@ -17,10 +22,10 @@ function getApiConfig(context: vscode.ExtensionContext) {
}; };
} }
// 如果用户配置了基础URL使用基础URL加上默认路径 // 如果用户配置了基础URL使用基础URL加上对应端点
if (userBaseUrl) { if (userBaseUrl) {
return { return {
url: `${userBaseUrl}/comp/api/v1/chat/completions-stream`, url: `${userBaseUrl}${endpoint}`,
apiKey: userApiKey || 'dev-token' apiKey: userApiKey || 'dev-token'
}; };
} }
@ -32,7 +37,7 @@ function getApiConfig(context: vscode.ExtensionContext) {
try { try {
const baseUrl = new URL(codeServerUrl); const baseUrl = new URL(codeServerUrl);
return { return {
url: `${baseUrl.origin}/comp/api/v1/chat/completions-stream`, url: `${baseUrl.origin}${endpoint}`,
apiKey: userApiKey || 'dev-token' apiKey: userApiKey || 'dev-token'
}; };
} catch (e) { } catch (e) {
@ -50,7 +55,7 @@ function getApiConfig(context: vscode.ExtensionContext) {
const [host, port] = configObj.bindAddr.split(':'); const [host, port] = configObj.bindAddr.split(':');
if (host && port) { if (host && port) {
return { return {
url: `http://${host}:${port}/comp/api/v1/chat/completions-stream`, url: `http://${host}:${port}${endpoint}`,
apiKey: userApiKey || 'dev-token' apiKey: userApiKey || 'dev-token'
}; };
} }
@ -62,7 +67,7 @@ function getApiConfig(context: vscode.ExtensionContext) {
// 默认配置 // 默认配置
return { return {
url: 'https://p13-ai.ngsk.tech:7001/comp/api/v1/chat/completions-stream', url: `https://p13-ai.ngsk.tech:7001${endpoint}`,
apiKey: userApiKey || 'dev-token' apiKey: userApiKey || 'dev-token'
}; };
} }
@ -72,9 +77,10 @@ export async function callQwenAPI(
history: any[], history: any[],
fileContent: string, fileContent: string,
context: vscode.ExtensionContext, context: vscode.ExtensionContext,
filename: string = "" filename: string = "",
enableDeepThinking: boolean = false
): Promise<any> { ): Promise<any> {
const { url, apiKey } = getApiConfig(context); const {url, apiKey} = getApiConfig(context, enableDeepThinking);
const messages = [ const messages = [
{ {
@ -91,14 +97,7 @@ export async function callQwenAPI(
console.log("messages:", messages) console.log("messages:", messages)
try { try {
const params = { const requestBody: any = {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
// 'Accept': 'text/event-stream'
},
body: JSON.stringify({
model: 'Qwen2_5_Coder', model: 'Qwen2_5_Coder',
messages, messages,
context_file: { context_file: {
@ -106,7 +105,21 @@ export async function callQwenAPI(
section_type: "code", section_type: "code",
content: fileContent content: fileContent
} }
}) };
// 如果启用深度思考,添加 enable_cot 参数
if (enableDeepThinking) {
requestBody.enable_cot = true;
}
const params = {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
// 'Accept': 'text/event-stream'
},
body: JSON.stringify(requestBody)
} }
console.log("params:", JSON.stringify(params)) console.log("params:", JSON.stringify(params))

@ -75,8 +75,14 @@ export function getWebviewContent(styleUri: vscode.Uri, scriptUri: vscode.Uri,hi
<!-- --> <!-- -->
<form id="chat-form"> <form id="chat-form">
<div class="input-container">
<input type="text" id="user-input" placeholder="输入你的问题..."/> <input type="text" id="user-input" placeholder="输入你的问题..."/>
<label class="deep-thinking-toggle" title="启用深度思考模式">
<input type="checkbox" id="deep-thinking-checkbox"/>
<span class="toggle-label"></span>
</label>
<button type="submit" id="send-btn"></button> <button type="submit" id="send-btn"></button>
</div>
</form> </form>
<div id="loading" class="hidden">AI ...</div> <div id="loading" class="hidden">AI ...</div>
</div> </div>

Loading…
Cancel
Save