Skip to content

Commit c927593

Browse files
chore: fix eslint errors
1 parent ec84815 commit c927593

11 files changed

Lines changed: 178 additions & 154 deletions

.eslintrc.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,12 @@ module.exports = {
2424
'no-plusplus': ['error', { 'allowForLoopAfterthoughts': true }],
2525
'no-console': 'off',
2626
'import/extensions': 'off',
27+
'no-use-before-define': ['error', {
28+
'functions': false,
29+
}],
30+
'no-promise-executor-return': 'off',
31+
'no-param-reassign': 'off',
32+
'no-continue': 'off',
33+
'no-restricted-syntax': 'off',
2734
},
2835
};

bin/cli.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
import fs from 'fs';
33
import { pathToFileURL } from 'url';
44
import { KeyvFile } from 'keyv-file';
5-
import ChatGPTClient from '../src/ChatGPTClient.js';
65
import boxen from 'boxen';
76
import ora from 'ora';
87
import clipboard from 'clipboardy';
98
import inquirer from 'inquirer';
109
import inquirerAutocompletePrompt from 'inquirer-autocomplete-prompt';
10+
import ChatGPTClient from '../src/ChatGPTClient.js';
1111
import BingAIClient from '../src/BingAIClient.js';
1212

13-
const arg = process.argv.find((arg) => arg.startsWith('--settings'));
14-
let path;
15-
if (arg) {
16-
path = arg.split('=')[1];
17-
} else {
18-
path = './settings.js';
19-
}
13+
const arg = process.argv.find(_arg => _arg.startsWith('--settings'));
14+
const path = arg?.split('=')[1] ?? './settings.js';
2015

2116
let settings;
2217
if (fs.existsSync(path)) {
@@ -25,9 +20,9 @@ if (fs.existsSync(path)) {
2520
settings = (await import(pathToFileURL(fullPath).toString())).default;
2621
} else {
2722
if (arg) {
28-
console.error(`Error: the file specified by the --settings parameter does not exist.`);
23+
console.error('Error: the file specified by the --settings parameter does not exist.');
2924
} else {
30-
console.error(`Error: the settings.js file does not exist.`);
25+
console.error('Error: the settings.js file does not exist.');
3126
}
3227
process.exit(1);
3328
}
@@ -95,7 +90,9 @@ switch (clientToUse) {
9590
break;
9691
}
9792

98-
console.log(tryBoxen('ChatGPT CLI', { padding: 0.7, margin: 1, borderStyle: 'double', dimBorder: true }));
93+
console.log(tryBoxen('ChatGPT CLI', {
94+
padding: 0.7, margin: 1, borderStyle: 'double', dimBorder: true,
95+
}));
9996

10097
await conversation();
10198

@@ -116,13 +113,13 @@ async function conversation() {
116113
prompt.ui.activePrompt.firstRender = false;
117114
// The below is a hack to allow selecting items from the autocomplete menu while also being able to submit messages.
118115
// This basically simulates a hybrid between having `suggestOnly: false` and `suggestOnly: true`.
119-
await new Promise((resolve) => setTimeout(resolve, 0));
116+
await new Promise(resolve => setTimeout(resolve, 0));
120117
prompt.ui.activePrompt.opt.source = (answers, input) => {
121118
if (!input) {
122119
return [];
123120
}
124121
prompt.ui.activePrompt.opt.suggestOnly = !input.startsWith('!');
125-
return availableCommands.filter((command) => command.value.startsWith(input));
122+
return availableCommands.filter(command => command.value.startsWith(input));
126123
};
127124
let { message } = await prompt;
128125
message = message.trim();
@@ -143,6 +140,8 @@ async function conversation() {
143140
return deleteAllConversations();
144141
case '!exit':
145142
return true;
143+
default:
144+
return conversation();
146145
}
147146
}
148147
return onMessage(message);
@@ -172,7 +171,9 @@ async function onMessage(message) {
172171
...conversationData,
173172
onProgress: (token) => {
174173
reply += token;
175-
const output = tryBoxen(`${reply.trim()}█`, { title: aiLabel, padding: 0.7, margin: 1, dimBorder: true });
174+
const output = tryBoxen(`${reply.trim()}█`, {
175+
title: aiLabel, padding: 0.7, margin: 1, dimBorder: true,
176+
});
176177
spinner.text = `${spinnerPrefix}\n${output}`;
177178
},
178179
});
@@ -192,10 +193,10 @@ async function onMessage(message) {
192193
conversationData = {
193194
parentMessageId: response.messageId,
194195
jailbreakConversationId: response.jailbreakConversationId,
195-
//conversationId: response.conversationId,
196-
//conversationSignature: response.conversationSignature,
197-
//clientId: response.clientId,
198-
//invocationId: response.invocationId,
196+
// conversationId: response.conversationId,
197+
// conversationSignature: response.conversationSignature,
198+
// clientId: response.clientId,
199+
// invocationId: response.invocationId,
199200
};
200201
break;
201202
default:
@@ -206,7 +207,9 @@ async function onMessage(message) {
206207
break;
207208
}
208209
await client.conversationsCache.set('lastConversation', conversationData);
209-
const output = tryBoxen(responseText, { title: aiLabel, padding: 0.7, margin: 1, dimBorder: true });
210+
const output = tryBoxen(responseText, {
211+
title: aiLabel, padding: 0.7, margin: 1, dimBorder: true,
212+
});
210213
console.log(output);
211214
} catch (error) {
212215
spinner.stop();
@@ -271,7 +274,7 @@ async function copyConversation() {
271274
// get the last message ID
272275
const lastMessageId = messages[messages.length - 1].id;
273276
const orderedMessages = ChatGPTClient.getMessagesForConversation(messages, lastMessageId);
274-
const conversationString = orderedMessages.map((message) => `#### ${message.role}:\n${message.message}`).join('\n\n');
277+
const conversationString = orderedMessages.map(message => `#### ${message.role}:\n${message.message}`).join('\n\n');
275278
try {
276279
await clipboard.write(`${conversationString}\n\n----\nMade with ChatGPT CLI: <https://github.com/waylaidwanderer/node-chatgpt-api>`);
277280
logSuccess('Copied conversation to clipboard.');
@@ -282,15 +285,21 @@ async function copyConversation() {
282285
}
283286

284287
function logError(message) {
285-
console.log(tryBoxen(message, { title: 'Error', padding: 0.7, margin: 1, borderColor: 'red' }));
288+
console.log(tryBoxen(message, {
289+
title: 'Error', padding: 0.7, margin: 1, borderColor: 'red',
290+
}));
286291
}
287292

288293
function logSuccess(message) {
289-
console.log(tryBoxen(message, { title: 'Success', padding: 0.7, margin: 1, borderColor: 'green' }));
294+
console.log(tryBoxen(message, {
295+
title: 'Success', padding: 0.7, margin: 1, borderColor: 'green',
296+
}));
290297
}
291298

292299
function logWarning(message) {
293-
console.log(tryBoxen(message, { title: 'Warning', padding: 0.7, margin: 1, borderColor: 'yellow' }));
300+
console.log(tryBoxen(message, {
301+
title: 'Warning', padding: 0.7, margin: 1, borderColor: 'yellow',
302+
}));
294303
}
295304

296305
/**

bin/server.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
#!/usr/bin/env node
22
import fastify from 'fastify';
33
import cors from '@fastify/cors';
4-
import { FastifySSEPlugin } from "@waylaidwanderer/fastify-sse-v2";
4+
import { FastifySSEPlugin } from '@waylaidwanderer/fastify-sse-v2';
55
import fs from 'fs';
6-
import { pathToFileURL } from 'url'
6+
import { pathToFileURL } from 'url';
7+
import { KeyvFile } from 'keyv-file';
78
import ChatGPTClient from '../src/ChatGPTClient.js';
89
import ChatGPTBrowserClient from '../src/ChatGPTBrowserClient.js';
910
import BingAIClient from '../src/BingAIClient.js';
10-
import { KeyvFile } from 'keyv-file';
1111

12-
const arg = process.argv.find((arg) => arg.startsWith('--settings'));
13-
let path;
14-
if (arg) {
15-
path = arg.split('=')[1];
16-
} else {
17-
path = './settings.js';
18-
}
12+
const arg = process.argv.find(_arg => _arg.startsWith('--settings'));
13+
const path = arg?.split('=')[1] ?? './settings.js';
1914

2015
let settings;
2116
if (fs.existsSync(path)) {
@@ -24,9 +19,9 @@ if (fs.existsSync(path)) {
2419
settings = (await import(pathToFileURL(fullPath).toString())).default;
2520
} else {
2621
if (arg) {
27-
console.error(`Error: the file specified by the --settings parameter does not exist.`);
22+
console.error('Error: the file specified by the --settings parameter does not exist.');
2823
} else {
29-
console.error(`Error: the settings.js file does not exist.`);
24+
console.error('Error: the settings.js file does not exist.');
3025
}
3126
process.exit(1);
3227
}
@@ -152,7 +147,7 @@ server.post('/conversation', async (request, reply) => {
152147

153148
server.listen({
154149
port: settings.apiOptions?.port || settings.port || 3000,
155-
host: settings.apiOptions?.host || 'localhost'
150+
host: settings.apiOptions?.host || 'localhost',
156151
}, (error) => {
157152
if (error) {
158153
console.error(error);
@@ -164,8 +159,8 @@ function nextTick() {
164159
return new Promise(resolve => setTimeout(resolve, 0));
165160
}
166161

167-
function getClient(clientToUse) {
168-
switch (clientToUse) {
162+
function getClient(clientToUseForMessage) {
163+
switch (clientToUseForMessage) {
169164
case 'bing':
170165
return new BingAIClient(settings.bingAiClient);
171166
case 'chatgpt-browser':
@@ -180,7 +175,7 @@ function getClient(clientToUse) {
180175
settings.cacheOptions,
181176
);
182177
default:
183-
throw new Error(`Invalid clientToUse: ${clientToUse}`);
178+
throw new Error(`Invalid clientToUse: ${clientToUseForMessage}`);
184179
}
185180
}
186181

@@ -189,9 +184,9 @@ function getClient(clientToUse) {
189184
* `settings.js` > `apiOptions.perMessageClientOptionsWhitelist`.
190185
* Returns original object if no whitelist is set.
191186
* @param {*} inputOptions
192-
* @param clientToUse
187+
* @param clientToUseForMessage
193188
*/
194-
function filterClientOptions(inputOptions, clientToUse) {
189+
function filterClientOptions(inputOptions, clientToUseForMessage) {
195190
if (!inputOptions || !perMessageClientOptionsWhitelist) {
196191
return null;
197192
}
@@ -202,25 +197,25 @@ function filterClientOptions(inputOptions, clientToUse) {
202197
&& inputOptions.clientToUse
203198
&& perMessageClientOptionsWhitelist.validClientsToUse.includes(inputOptions.clientToUse)
204199
) {
205-
clientToUse = inputOptions.clientToUse;
200+
clientToUseForMessage = inputOptions.clientToUse;
206201
} else {
207-
inputOptions.clientToUse = clientToUse;
202+
inputOptions.clientToUse = clientToUseForMessage;
208203
}
209204

210-
const whitelist = perMessageClientOptionsWhitelist[clientToUse];
205+
const whitelist = perMessageClientOptionsWhitelist[clientToUseForMessage];
211206
if (!whitelist) {
212207
// No whitelist, return all options
213208
return inputOptions;
214209
}
215210

216211
const outputOptions = {};
217212

218-
for (let property in inputOptions) {
213+
for (const property of Object.keys(inputOptions)) {
219214
const allowed = whitelist.includes(property);
220215

221216
if (!allowed && typeof inputOptions[property] === 'object') {
222217
// Check for nested properties
223-
for (let nestedProp in inputOptions[property]) {
218+
for (const nestedProp of Object.keys(inputOptions[property])) {
224219
const nestedAllowed = whitelist.includes(`${property}.${nestedProp}`);
225220
if (nestedAllowed) {
226221
outputOptions[property] = outputOptions[property] || {};

demos/use-api-server-streaming.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ try {
2626
throw new Error(`Failed to send message. HTTP ${response.status} - ${response.statusText}`);
2727
},
2828
onclose() {
29-
throw new Error(`Failed to send message. Server closed the connection unexpectedly.`);
29+
throw new Error('Failed to send message. Server closed the connection unexpectedly.');
3030
},
3131
onerror(err) {
3232
throw err;

demos/use-browser-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const response3 = await chatGptClient.sendMessage('Now write it in French.', {
2626
parentMessageId: response2.messageId,
2727
// If you want streamed responses, you can set the `onProgress` callback to receive the response as it's generated.
2828
// You will receive one token at a time, so you will need to concatenate them yourself.
29-
onProgress: (token) => process.stdout.write(token),
29+
onProgress: token => process.stdout.write(token),
3030
});
3131
console.log();
3232
console.log(response3.response); // Les chats sont les meilleurs animaux de compagnie du monde.

demos/use-client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ response = await chatGptClient.sendMessage('Now write it in French.', {
5252
parentMessageId: response.messageId,
5353
// If you want streamed responses, you can set the `onProgress` callback to receive the response as it's generated.
5454
// You will receive one token at a time, so you will need to concatenate them yourself.
55-
onProgress: (token) => process.stdout.write(token),
55+
onProgress: token => process.stdout.write(token),
5656
});
5757
console.log();
5858
console.log(response.response); // Doux et élégant, avec des yeux qui brillent,\nLes chats sont des créatures de grâce suprême.\n...
@@ -62,7 +62,7 @@ response = await chatGptClient.sendMessage('Repeat my 2nd message verbatim.', {
6262
parentMessageId: response.messageId,
6363
// If you want streamed responses, you can set the `onProgress` callback to receive the response as it's generated.
6464
// You will receive one token at a time, so you will need to concatenate them yourself.
65-
onProgress: (token) => process.stdout.write(token),
65+
onProgress: token => process.stdout.write(token),
6666
});
6767
console.log();
6868
console.log(response.response); // "Write a short poem about cats."

settings.example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@ export default {
9797
// (Optional) Possible options: "chatgpt", "bing".
9898
// clientToUse: 'bing',
9999
},
100-
}
100+
};

0 commit comments

Comments
 (0)