|
|
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'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
// 如果用户配置了基础URL,使用基础URL加上默认路径
|
|
|
if (userBaseUrl) {
|
|
|
return {
|
|
|
url: `${userBaseUrl}/comp/api/v1/chat/completions-stream`,
|
|
|
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}/comp/api/v1/chat/completions-stream`,
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
};
|
|
|
} catch (e) {
|
|
|
// 如果解析失败,使用默认地址
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 检查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`,
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
} catch (e) {
|
|
|
// 解析失败则继续使用默认配置
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 默认配置
|
|
|
return {
|
|
|
url: 'https://p13-ai.ngsk.tech:7001/comp/api/v1/chat/completions-stream',
|
|
|
apiKey: userApiKey || 'dev-token'
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export async function callQwenAPI(
|
|
|
question: string,
|
|
|
history: any[],
|
|
|
fileContent: string,
|
|
|
context: vscode.ExtensionContext,
|
|
|
filename: string = ""
|
|
|
): Promise<any> {
|
|
|
const { url, apiKey } = getApiConfig(context);
|
|
|
|
|
|
const messages = [
|
|
|
{
|
|
|
role: 'system',
|
|
|
content: `你是一个代码助手,请根据当前文件内容和历史对话回答问题。请使用中文回答。`
|
|
|
},
|
|
|
...history,
|
|
|
{
|
|
|
role: 'user',
|
|
|
content: question
|
|
|
}
|
|
|
];
|
|
|
|
|
|
console.log("messages:", messages)
|
|
|
|
|
|
try {
|
|
|
const params = {
|
|
|
method: 'POST',
|
|
|
headers: {
|
|
|
'Authorization': `Bearer ${apiKey}`,
|
|
|
'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
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
console.log("params:", JSON.stringify(params))
|
|
|
|
|
|
// @ts-ignore
|
|
|
const response = await fetch(url, params);
|
|
|
|
|
|
if (!response.ok) {
|
|
|
console.log("请求失败:", response)
|
|
|
throw new Error(`API 请求失败: ${response.status} ${response.statusText}`);
|
|
|
}
|
|
|
|
|
|
return response;
|
|
|
} catch (error: any) {
|
|
|
console.error(error);
|
|
|
// 提供更具体的错误信息
|
|
|
if (error.message.includes('terminated') || error.message.includes('other side closed')) {
|
|
|
throw new Error('网络连接中断,请检查网络状况或稍后重试。');
|
|
|
} else if (error.name === 'TypeError') {
|
|
|
throw new Error(`网络错误: ${error.message}`);
|
|
|
}
|
|
|
throw error;
|
|
|
}
|
|
|
} |