|
|
|
|
@ -2,69 +2,74 @@ import vscode from "vscode";
|
|
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
|
|
// 获取API配置的函数
|
|
|
|
|
function getApiConfig(context: vscode.ExtensionContext) {
|
|
|
|
|
// 从用户配置中获取设置
|
|
|
|
|
const config = vscode.workspace.getConfiguration('ai-chat.api');
|
|
|
|
|
const userUrl = config.get<string>('url');
|
|
|
|
|
const userBaseUrl = config.get<string>('baseUrl');
|
|
|
|
|
const userApiKey = config.get<string>('key');
|
|
|
|
|
|
|
|
|
|
// 如果用户配置了完整URL,优先使用
|
|
|
|
|
if (userUrl) {
|
|
|
|
|
return {
|
|
|
|
|
url: userUrl,
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
function getApiConfig(context: vscode.ExtensionContext, enableDeepThinking: boolean = false) {
|
|
|
|
|
// 从用户配置中获取设置
|
|
|
|
|
const config = vscode.workspace.getConfiguration('ai-chat.api');
|
|
|
|
|
const userUrl = config.get<string>('url');
|
|
|
|
|
const userBaseUrl = config.get<string>('baseUrl');
|
|
|
|
|
const userApiKey = config.get<string>('key');
|
|
|
|
|
|
|
|
|
|
// 如果用户配置了基础URL,使用基础URL加上默认路径
|
|
|
|
|
if (userBaseUrl) {
|
|
|
|
|
return {
|
|
|
|
|
url: `${userBaseUrl}/comp/api/v1/chat/completions-stream`,
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
// 根据是否启用深度思考选择不同的端点
|
|
|
|
|
const endpoint = enableDeepThinking
|
|
|
|
|
? '/comp/api/v1/chat/completions-stream-cot'
|
|
|
|
|
: '/comp/api/v1/chat/completions-stream';
|
|
|
|
|
|
|
|
|
|
// 检测code-server环境
|
|
|
|
|
const codeServerUrl = process.env.CODE_SERVER_URL;
|
|
|
|
|
if (codeServerUrl) {
|
|
|
|
|
// 在code-server环境中,尝试使用相对路径或者基于当前地址的API地址
|
|
|
|
|
try {
|
|
|
|
|
const baseUrl = new URL(codeServerUrl);
|
|
|
|
|
return {
|
|
|
|
|
url: `${baseUrl.origin}/comp/api/v1/chat/completions-stream`,
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// 如果解析失败,使用默认地址
|
|
|
|
|
// 如果用户配置了完整URL,优先使用
|
|
|
|
|
if (userUrl) {
|
|
|
|
|
return {
|
|
|
|
|
url: userUrl,
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查CODE_SERVER_CONFIG环境变量
|
|
|
|
|
const codeServerConfig = process.env.CODE_SERVER_CONFIG;
|
|
|
|
|
if (codeServerConfig) {
|
|
|
|
|
try {
|
|
|
|
|
// 尝试从配置中解析bindAddr
|
|
|
|
|
const configObj = JSON.parse(codeServerConfig);
|
|
|
|
|
if (configObj.bindAddr) {
|
|
|
|
|
const [host, port] = configObj.bindAddr.split(':');
|
|
|
|
|
if (host && port) {
|
|
|
|
|
return {
|
|
|
|
|
url: `http://${host}:${port}/comp/api/v1/chat/completions-stream`,
|
|
|
|
|
// 如果用户配置了基础URL,使用基础URL加上对应端点
|
|
|
|
|
if (userBaseUrl) {
|
|
|
|
|
return {
|
|
|
|
|
url: `${userBaseUrl}${endpoint}`,
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检测code-server环境
|
|
|
|
|
const codeServerUrl = process.env.CODE_SERVER_URL;
|
|
|
|
|
if (codeServerUrl) {
|
|
|
|
|
// 在code-server环境中,尝试使用相对路径或者基于当前地址的API地址
|
|
|
|
|
try {
|
|
|
|
|
const baseUrl = new URL(codeServerUrl);
|
|
|
|
|
return {
|
|
|
|
|
url: `${baseUrl.origin}${endpoint}`,
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// 如果解析失败,使用默认地址
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// 解析失败则继续使用默认配置
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 默认配置
|
|
|
|
|
return {
|
|
|
|
|
url: 'https://p13-ai.ngsk.tech:7001/comp/api/v1/chat/completions-stream',
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
// 检查CODE_SERVER_CONFIG环境变量
|
|
|
|
|
const codeServerConfig = process.env.CODE_SERVER_CONFIG;
|
|
|
|
|
if (codeServerConfig) {
|
|
|
|
|
try {
|
|
|
|
|
// 尝试从配置中解析bindAddr
|
|
|
|
|
const configObj = JSON.parse(codeServerConfig);
|
|
|
|
|
if (configObj.bindAddr) {
|
|
|
|
|
const [host, port] = configObj.bindAddr.split(':');
|
|
|
|
|
if (host && port) {
|
|
|
|
|
return {
|
|
|
|
|
url: `http://${host}:${port}${endpoint}`,
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// 解析失败则继续使用默认配置
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 默认配置
|
|
|
|
|
return {
|
|
|
|
|
url: `https://p13-ai.ngsk.tech:7001${endpoint}`,
|
|
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function callQwenAPI(
|
|
|
|
|
@ -72,9 +77,10 @@ export async function callQwenAPI(
|
|
|
|
|
history: any[],
|
|
|
|
|
fileContent: string,
|
|
|
|
|
context: vscode.ExtensionContext,
|
|
|
|
|
filename: string = ""
|
|
|
|
|
filename: string = "",
|
|
|
|
|
enableDeepThinking: boolean = false
|
|
|
|
|
): Promise<any> {
|
|
|
|
|
const { url, apiKey } = getApiConfig(context);
|
|
|
|
|
const {url, apiKey} = getApiConfig(context, enableDeepThinking);
|
|
|
|
|
|
|
|
|
|
const messages = [
|
|
|
|
|
{
|
|
|
|
|
@ -91,6 +97,21 @@ export async function callQwenAPI(
|
|
|
|
|
console.log("messages:", messages)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const requestBody: any = {
|
|
|
|
|
model: 'Qwen2_5_Coder',
|
|
|
|
|
messages,
|
|
|
|
|
context_file: {
|
|
|
|
|
filename: filename,
|
|
|
|
|
section_type: "code",
|
|
|
|
|
content: fileContent
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 如果启用深度思考,添加 enable_cot 参数
|
|
|
|
|
if (enableDeepThinking) {
|
|
|
|
|
requestBody.enable_cot = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
@ -98,15 +119,7 @@ export async function callQwenAPI(
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
// 'Accept': 'text/event-stream'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
model: 'Qwen2_5_Coder',
|
|
|
|
|
messages,
|
|
|
|
|
context_file: {
|
|
|
|
|
filename: filename,
|
|
|
|
|
section_type: "code",
|
|
|
|
|
content: fileContent
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
body: JSON.stringify(requestBody)
|
|
|
|
|
}
|
|
|
|
|
console.log("params:", JSON.stringify(params))
|
|
|
|
|
|
|
|
|
|
|