Skip to content

Commit 867b011

Browse files
chore: remove dengerous console.log
1 parent 31639b0 commit 867b011

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

src/ai/explain.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import Groq from "groq-sdk";
2-
import Enquirer from "enquirer";
3-
import keytar from "keytar";
4-
import { prompt as aiPrompt } from "./prompt";
5-
import ora from "ora";
6-
import { ACCOUNT_NAME, SERVICE_NAME } from "../utils/constants";
7-
import { HttpsProxyAgent } from "https-proxy-agent";
1+
import Enquirer from 'enquirer';
2+
import Groq from 'groq-sdk';
3+
import { HttpsProxyAgent } from 'https-proxy-agent';
4+
import keytar from 'keytar';
5+
import ora from 'ora';
6+
import { ACCOUNT_NAME, SERVICE_NAME } from '../utils/constants';
7+
import { prompt as aiPrompt } from './prompt';
88

99
function isValidUrl(input: string) {
1010
try {
@@ -30,21 +30,20 @@ async function getApiKey() {
3030
}
3131

3232
// 3. Prompt user
33-
const { apiKey: newKey } = await prompt({
34-
type: "password",
35-
name: "apiKey",
36-
message: "Enter your Groq API key:",
37-
validate: (
38-
value: string,
39-
) => (value.trim() === "" ? "API key cannot be empty" : true),
40-
initial: "",
33+
const { apiKey: newKey } = (await prompt({
34+
type: 'password',
35+
name: 'apiKey',
36+
message: 'Enter your Groq API key:',
37+
validate: (value: string) =>
38+
value.trim() === '' ? 'API key cannot be empty' : true,
39+
initial: '',
4140
result: (value: string) => value.trim(),
4241
stdin: process.stdin,
4342
stdout: process.stdout,
44-
}) as { apiKey: string };
43+
})) as { apiKey: string };
4544

4645
await keytar.setPassword(SERVICE_NAME, ACCOUNT_NAME, newKey);
47-
console.log("\n✅ API key saved securely!");
46+
console.log('\n✅ API key saved securely!');
4847
return newKey;
4948
} catch (error) {
5049
console.error(error);
@@ -58,24 +57,24 @@ async function getHttpProxyAgent(): Promise<
5857
const envProxy = process.env.HTTPS_PROXY || process.env.https_proxy;
5958

6059
if (envProxy) {
61-
console.log("Using proxy from environment variables");
60+
console.log('Using proxy from environment variables');
6261
return new HttpsProxyAgent(envProxy);
6362
}
6463

6564
const { setProxy } = await Enquirer.prompt<{ setProxy: boolean }>({
66-
type: "confirm",
67-
name: "setProxy",
68-
message: "Do you want to set a proxy?",
65+
type: 'confirm',
66+
name: 'setProxy',
67+
message: 'Do you want to set a proxy?',
6968
initial: false,
7069
});
7170

7271
if (!setProxy) return undefined;
7372

7473
const { proxyAddr } = await Enquirer.prompt<{ proxyAddr: string }>({
75-
type: "input",
76-
name: "proxyAddr",
77-
message: "Enter proxy address (e.g., http://user:pass@localhost:7890):",
78-
validate: (value) => isValidUrl(value) || "Invalid URL format",
74+
type: 'input',
75+
name: 'proxyAddr',
76+
message: 'Enter proxy address (e.g., http://user:pass@localhost:7890):',
77+
validate: (value) => isValidUrl(value) || 'Invalid URL format',
7978
});
8079

8180
return new HttpsProxyAgent(proxyAddr);
@@ -85,31 +84,31 @@ async function getHttpProxyAgent(): Promise<
8584
export async function explain(regex: string) {
8685
try {
8786
const apiKey = await getApiKey();
88-
console.log("apiKey: ", apiKey);
8987
const httpAgent = await getHttpProxyAgent();
9088

9189
const groq = new Groq({ apiKey, ...(httpAgent ? { httpAgent } : {}) });
9290

93-
const spinner = ora("loading ....").start();
91+
const spinner = ora('loading ....').start();
9492
const completion = await groq.chat.completions.create({
95-
model: "openai/gpt-oss-20b",
93+
model: 'openai/gpt-oss-20b',
9694
messages: [
97-
{ role: "system", content: "You are a regex explainer." },
98-
{ role: "user", content: aiPrompt(regex) },
95+
{ role: 'system', content: 'You are a regex explainer.' },
96+
{ role: 'user', content: aiPrompt(regex) },
9997
],
10098
});
10199

102100
spinner.stop().clear();
103101

104-
console.log("\n📖 Explanation:\n");
102+
console.log('\n📖 Explanation:\n');
105103
if (
106-
completion.choices && completion.choices[0] &&
104+
completion.choices &&
105+
completion.choices[0] &&
107106
completion.choices[0].message
108107
) {
109108
console.log(completion.choices[0].message.content);
110109
} else {
111110
console.error(
112-
"Error: Completion result is undefined or malformed.",
111+
'Error: Completion result is undefined or malformed.',
113112
);
114113
}
115114
} catch (error) {

0 commit comments

Comments
 (0)