Skip to content

Commit 52ae15b

Browse files
authored
Merge pull request #120 from covespace/use_devchat_token
Use devchat token
2 parents 23277e2 + dc1cab2 commit 52ae15b

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@
6969
"description": "The max number of tokens of a prompt.",
7070
"when": "DevChat.llmModel == 'OpenAI'"
7171
},
72-
"DevChat.OpenAI.apiKey": {
72+
"DevChat.API_KEY": {
7373
"type": "string",
7474
"default": "",
75-
"description": "Open API Key",
75+
"description": "API key for accessing the LLM model",
7676
"when": "DevChat.llmModel == 'OpenAI'"
7777
},
78-
"DevChat.OpenAI.EndPoint": {
78+
"DevChat.API_ENDPOINT": {
7979
"type": "string",
8080
"default": "",
81-
"description": "The OpenAI API endpoint URL.",
81+
"description": "API endpoint URL",
8282
"when": "DevChat.llmModel == 'OpenAI'"
8383
},
8484
"DevChat.DevChatPath": {
@@ -155,7 +155,7 @@
155155
},
156156
{
157157
"command": "DevChat.OPENAI_API_KEY",
158-
"title": "OPENAI_API_KEY",
158+
"title": "DEVCHAT_API_KEY",
159159
"category": "DevChat"
160160
},
161161
{

src/contributes/commands.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function checkOpenaiApiKey() {
3838
const secretStorage: vscode.SecretStorage = ExtensionContextHolder.context!.secrets;
3939
let openaiApiKey = await secretStorage.get("devchat_OPENAI_API_KEY");
4040
if (!openaiApiKey) {
41-
openaiApiKey = vscode.workspace.getConfiguration('DevChat').get('OpenAI.apiKey');
41+
openaiApiKey = vscode.workspace.getConfiguration('DevChat').get('API_KEY');
4242
}
4343
if (!openaiApiKey) {
4444
openaiApiKey = process.env.OPENAI_API_KEY;
@@ -50,7 +50,7 @@ export async function checkOpenaiApiKey() {
5050
}
5151

5252
function checkOpenaiKey() {
53-
let openaiApiKey = vscode.workspace.getConfiguration('DevChat').get('OpenAI.apiKey');
53+
let openaiApiKey = vscode.workspace.getConfiguration('DevChat').get('API_KEY');
5454
if (!openaiApiKey) {
5555
openaiApiKey = process.env.OPENAI_API_KEY;
5656
}
@@ -60,8 +60,8 @@ function checkOpenaiKey() {
6060
placeHolder: 'Please input your OpenAI API key (or DevChat access key)'
6161
}).then((value) => {
6262
if (value) {
63-
// Set API Key
64-
vscode.workspace.getConfiguration('DevChat').update('OpenAI.apiKey', value, true);
63+
// 设置用户输入的API Key
64+
vscode.workspace.getConfiguration('DevChat').update('API_KEY', value, true);
6565
}
6666
});
6767
return false;

src/toolwrapper/devchat.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class DevChat {
8888
const secretStorage: vscode.SecretStorage = ExtensionContextHolder.context!.secrets;
8989
let openaiApiKey = await secretStorage.get("devchat_OPENAI_API_KEY");
9090
if (!openaiApiKey) {
91-
openaiApiKey = vscode.workspace.getConfiguration('DevChat').get('OpenAI.apiKey');
91+
openaiApiKey = vscode.workspace.getConfiguration('DevChat').get('API_KEY');
9292
}
9393
if (!openaiApiKey) {
9494
openaiApiKey = process.env.OPENAI_API_KEY;
@@ -146,6 +146,22 @@ class DevChat {
146146
isError: false,
147147
};
148148
}
149+
150+
apiEndpoint(apiKey: string | undefined): any {
151+
let openAiApiBase: string | undefined = undefined;
152+
if (apiKey?.startsWith("DC.")) {
153+
// TODO add devchat proxy
154+
openAiApiBase = "https://xw4ymuy6qj.ap-southeast-1.awsapprunner.com/api/v1";
155+
}
156+
157+
if (vscode.workspace.getConfiguration('DevChat').get('API_ENDPOINT')) {
158+
openAiApiBase = vscode.workspace.getConfiguration('DevChat').get('API_ENDPOINT');
159+
}
160+
161+
const openAiApiBaseObject = openAiApiBase ? { OPENAI_API_BASE: openAiApiBase } : {};
162+
return openAiApiBaseObject;
163+
}
164+
149165
async chat(content: string, options: ChatOptions = {}, onData: (data: ChatResponse) => void): Promise<ChatResponse> {
150166
const args = await this.buildArgs(options);
151167
args.push(content);
@@ -158,8 +174,8 @@ class DevChat {
158174
}
159175

160176

161-
const openaiApiBase = vscode.workspace.getConfiguration('DevChat').get('OpenAI.EndPoint');
162-
const openaiApiBaseObject = openaiApiBase ? { OPENAI_API_BASE: openaiApiBase } : {};
177+
// 如果配置了devchat的TOKEN,那么就需要使用默认的代理
178+
let openAiApiBaseObject = this.apiEndpoint(openaiApiKey);
163179

164180
const openaiModel = vscode.workspace.getConfiguration('DevChat').get('OpenAI.model');
165181
const openaiTemperature = vscode.workspace.getConfiguration('DevChat').get('OpenAI.temperature');
@@ -196,16 +212,19 @@ class DevChat {
196212
onData(data);
197213
};
198214

199-
logger.channel()?.info(`Running devchat with args: ${args.join(" ")}`);
200-
const { exitCode: code, stdout, stderr } = await this.commandRun.spawnAsync(devChat, args, {
215+
const spawnAsyncOptions = {
201216
maxBuffer: 10 * 1024 * 1024, // Set maxBuffer to 10 MB
202217
cwd: workspaceDir,
203218
env: {
204219
...process.env,
205220
OPENAI_API_KEY: openaiApiKey,
206-
...openaiApiBaseObject
221+
...openAiApiBaseObject
207222
},
208-
}, onStdoutPartial, undefined, undefined, undefined);
223+
};
224+
225+
logger.channel()?.info(`Running devchat with args: ${args.join(" ")}`);
226+
logger.channel()?.info(`Running devchat with env: ${JSON.stringify(openAiApiBaseObject)}`);
227+
const { exitCode: code, stdout, stderr } = await this.commandRun.spawnAsync(devChat, args, spawnAsyncOptions, onStdoutPartial, undefined, undefined, undefined);
209228

210229
if (stderr) {
211230
const errorMessage = stderr.trim().match(/Error(.+)/)?.[1];

0 commit comments

Comments
 (0)