Skip to content

Commit 3467930

Browse files
committed
Build/Refactor: lint pre-commit hook and reformat repo to spec (danny-avila#314)
* build/refactor: move lint/prettier packages to project root, install husky, add pre-commit hook * refactor: reformat files * build: put full eslintrc back with all rules
1 parent 3341e71 commit 3467930

File tree

157 files changed

+4879
-2446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+4879
-2446
lines changed

.eslintrc.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es2021: true,
5+
node: true,
6+
commonjs: true,
7+
es6: true
8+
},
9+
extends: [
10+
'eslint:recommended',
11+
'plugin:react/recommended',
12+
'plugin:react-hooks/recommended',
13+
'prettier'
14+
],
15+
parser: '@typescript-eslint/parser',
16+
parserOptions: {
17+
ecmaVersion: 'latest',
18+
sourceType: 'module',
19+
ecmaFeatures: {
20+
jsx: true
21+
}
22+
},
23+
plugins: ['react', 'react-hooks', '@typescript-eslint'],
24+
rules: {
25+
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': 'allow-with-description' }],
26+
indent: ['error', 2, { SwitchCase: 1 }],
27+
'max-len': [
28+
'error',
29+
{
30+
code: 150,
31+
ignoreStrings: true,
32+
ignoreTemplateLiterals: true,
33+
ignoreComments: true
34+
}
35+
],
36+
'linebreak-style': 0,
37+
// "arrow-parens": [2, "as-needed", { requireForBlockBody: true }],
38+
// 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }],
39+
'no-console': 'off',
40+
'import/extensions': 'off',
41+
'no-use-before-define': [
42+
'error',
43+
{
44+
functions: false
45+
}
46+
],
47+
'no-promise-executor-return': 'off',
48+
'no-param-reassign': 'off',
49+
'no-continue': 'off',
50+
'no-restricted-syntax': 'off',
51+
'react/prop-types': ['off'],
52+
'react/display-name': ['off']
53+
},
54+
overrides: [
55+
{
56+
files: ['**/*.ts', '**/*.tsx'],
57+
rules: {
58+
'no-unused-vars': 'off', // off because it conflicts with '@typescript-eslint/no-unused-vars'
59+
'react/display-name': 'off',
60+
'@typescript-eslint/no-unused-vars': 'warn'
61+
}
62+
},
63+
{
64+
files: ['rollup.config.js', '.eslintrc.js', 'jest.config.js'],
65+
env: {
66+
node: true
67+
}
68+
},
69+
{
70+
files: [
71+
'**/*.test.js',
72+
'**/*.test.jsx',
73+
'**/*.test.ts',
74+
'**/*.test.tsx',
75+
'**/*.spec.js',
76+
'**/*.spec.jsx',
77+
'**/*.spec.ts',
78+
'**/*.spec.tsx',
79+
'setupTests.js'
80+
],
81+
env: {
82+
jest: true,
83+
node: true
84+
},
85+
rules: {
86+
'react/display-name': 'off',
87+
'react/prop-types': 'off',
88+
'react/no-unescaped-entities': 'off'
89+
}
90+
},
91+
{
92+
files: '**/*.+(ts)',
93+
parser: '@typescript-eslint/parser',
94+
parserOptions: {
95+
project: './client/tsconfig.json'
96+
},
97+
plugins: ['@typescript-eslint/eslint-plugin'],
98+
extends: [
99+
'plugin:@typescript-eslint/eslint-recommended',
100+
'plugin:@typescript-eslint/recommended'
101+
]
102+
}
103+
],
104+
settings: {
105+
react: {
106+
createClass: 'createReactClass', // Regex for Component Factory to use,
107+
// default to "createReactClass"
108+
pragma: 'React', // Pragma to use, default to "React"
109+
fragment: 'Fragment', // Fragment to use (may be a property of <pragma>), default to "Fragment"
110+
version: 'detect' // React version. "detect" automatically picks the version you have installed.
111+
}
112+
}
113+
};

.husky/pre-commit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
npx lint-staged
5+

.prettierrc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
printWidth: 100,
3+
useTabs: false,
4+
tabWidth: 2,
5+
semi: true,
6+
singleQuote: true,
7+
// bracketSpacing: false,
8+
trailingComma: 'none',
9+
arrowParens: 'always',
10+
embeddedLanguageFormatting: 'auto',
11+
insertPragma: false,
12+
proseWrap: 'preserve',
13+
quoteProps: 'as-needed',
14+
requirePragma: false,
15+
rangeStart: 0,
16+
endOfLine: 'auto',
17+
jsxBracketSameLine: false,
18+
jsxSingleQuote: false,
19+
};

api/.eslintrc.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

api/.prettierrc

Lines changed: 0 additions & 22 deletions
This file was deleted.

api/app/clients/bingai.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const askBing = async ({
2323

2424
const bingAIClient = new BingAIClient({
2525
// "_U" cookie from bing.com
26-
userToken: process.env.BINGAI_TOKEN == 'user_provided' ? token : process.env.BINGAI_TOKEN ?? null,
26+
userToken:
27+
process.env.BINGAI_TOKEN == 'user_provided' ? token : process.env.BINGAI_TOKEN ?? null,
2728
// If the above doesn't work, provide all your cookies as a string instead
2829
// cookies: '',
2930
debug: false,

api/app/clients/chatgpt-browser.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ const browserClient = async ({
1818

1919
const clientOptions = {
2020
// Warning: This will expose your access token to a third party. Consider the risks before using this.
21-
reverseProxyUrl: process.env.CHATGPT_REVERSE_PROXY || 'https://ai.fakeopen.com/api/conversation',
21+
reverseProxyUrl:
22+
process.env.CHATGPT_REVERSE_PROXY || 'https://ai.fakeopen.com/api/conversation',
2223
// Access token from https://chat.openai.com/api/auth/session
23-
accessToken: process.env.CHATGPT_TOKEN == 'user_provided' ? token : process.env.CHATGPT_TOKEN ?? null,
24+
accessToken:
25+
process.env.CHATGPT_TOKEN == 'user_provided' ? token : process.env.CHATGPT_TOKEN ?? null,
2426
model: model,
2527
debug: false,
2628
proxy: process.env.PROXY || null,

api/app/clients/chatgpt-client.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require('dotenv').config();
22
const { KeyvFile } = require('keyv-file');
33
const { genAzureEndpoint } = require('../../utils/genAzureEndpoints');
4-
const tiktoken = require("@dqbd/tiktoken");
4+
const tiktoken = require('@dqbd/tiktoken');
55
const encoding_for_model = tiktoken.encoding_for_model;
66

77
const askClient = async ({
@@ -27,7 +27,7 @@ const askClient = async ({
2727

2828
const azure = process.env.AZURE_OPENAI_API_KEY ? true : false;
2929
if (promptPrefix == null) {
30-
promptText = "You are ChatGPT, a large language model trained by OpenAI.";
30+
promptText = 'You are ChatGPT, a large language model trained by OpenAI.';
3131
} else {
3232
promptText = promptPrefix;
3333
}
@@ -45,7 +45,7 @@ const askClient = async ({
4545
},
4646
chatGptLabel,
4747
promptPrefix,
48-
proxy: process.env.PROXY || null,
48+
proxy: process.env.PROXY || null
4949
// debug: true
5050
};
5151

@@ -77,16 +77,16 @@ const askClient = async ({
7777
const res = await client.sendMessage(text, { ...options, userId });
7878
// return res;
7979
// create a new response object that includes the token counts
80-
const newRes = {
81-
...res,
82-
usage: {
83-
prompt_tokens: prompt_tokens.length,
84-
completion_tokens: text_tokens.length,
85-
total_tokens: prompt_tokens.length + text_tokens.length
86-
}
87-
};
80+
const newRes = {
81+
...res,
82+
usage: {
83+
prompt_tokens: prompt_tokens.length,
84+
completion_tokens: text_tokens.length,
85+
total_tokens: prompt_tokens.length + text_tokens.length
86+
}
87+
};
8888

89-
return newRes;
89+
return newRes;
9090
};
9191

9292
module.exports = { askClient };

api/app/google/GoogleClient.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const TextStream = require('../stream');
33
const { google } = require('googleapis');
44
const { Agent, ProxyAgent } = require('undici');
55
const { getMessages, saveMessage, saveConvo } = require('../../models');
6-
const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('@dqbd/tiktoken');
6+
const {
7+
encoding_for_model: encodingForModel,
8+
get_encoding: getEncoding
9+
} = require('@dqbd/tiktoken');
710

811
const tokenizersCache = {};
912

@@ -65,7 +68,8 @@ class GoogleAgent {
6568
// The max prompt tokens is determined by the max context tokens minus the max response tokens.
6669
// Earlier messages will be dropped until the prompt is within the limit.
6770
this.maxResponseTokens = this.modelOptions.maxOutputTokens || 1024;
68-
this.maxPromptTokens = this.options.maxPromptTokens || this.maxContextTokens - this.maxResponseTokens;
71+
this.maxPromptTokens =
72+
this.options.maxPromptTokens || this.maxContextTokens - this.maxResponseTokens;
6973

7074
if (this.maxPromptTokens + this.maxResponseTokens > this.maxContextTokens) {
7175
throw new Error(
@@ -291,7 +295,10 @@ class GoogleAgent {
291295
try {
292296
const result = await this.getCompletion(message, messages, opts.abortController);
293297
blocked = result?.predictions?.[0]?.safetyAttributes?.blocked;
294-
reply = result?.predictions?.[0]?.candidates?.[0]?.content || result?.predictions?.[0]?.content || '';
298+
reply =
299+
result?.predictions?.[0]?.candidates?.[0]?.content ||
300+
result?.predictions?.[0]?.content ||
301+
'';
295302
if (blocked === true) {
296303
reply = `Google blocked a proper response to your message:\n${JSON.stringify(
297304
result.predictions[0].safetyAttributes

api/app/stream.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ class TextStream extends Readable {
1616
if (this.currentIndex < this.text.length) {
1717
setTimeout(() => {
1818
const remainingChars = this.text.length - this.currentIndex;
19-
const chunkSize = Math.min(
20-
this.randomInt(minChunkSize, maxChunkSize + 1),
21-
remainingChars
22-
);
19+
const chunkSize = Math.min(this.randomInt(minChunkSize, maxChunkSize + 1), remainingChars);
2320

2421
const chunk = this.text.slice(this.currentIndex, this.currentIndex + chunkSize);
2522
this.push(chunk);

0 commit comments

Comments
 (0)