Skip to content

Commit

Permalink
Merge branch 'main' into add-vision
Browse files Browse the repository at this point in the history
  • Loading branch information
lectrician1 committed Feb 19, 2024
2 parents 95d4405 + bd5f25b commit 3a91653
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ One click deploy with Vercel

1. Ensure that you have the following installed:

- [node.js](https://nodejs.org/en/)
- [yarn](https://yarnpkg.com/) or [npm](https://www.npmjs.com/)
- [node.js](https://nodejs.org/en/) (v14.18.0 or above)
- [yarn](https://yarnpkg.com/) or [npm](https://www.npmjs.com/) (6.14.15 or above)

2. Clone this [repository](https://github.com/ztjhz/BetterChatGPT) by running `git clone https://github.com/ztjhz/BetterChatGPT.git`
3. Navigate into the directory by running `cd BetterChatGPT`
Expand Down
162 changes: 146 additions & 16 deletions electron/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const path = require('path');

const {dialog, app, BrowserWindow, Tray, Menu } = require('electron');
const {
app,
shell,
clipboard,
dialog,
download,
BrowserWindow,
Tray,
Menu,
MenuItem,
} = require('electron');
const isDev = require('electron-is-dev');
const { autoUpdater } = require('electron-updater');
let win = null;
Expand All @@ -13,11 +23,130 @@ const PORT = isDev ? '5173' : '51735';
const ICON = 'icon-rounded.png';
const ICON_TEMPLATE = 'iconTemplate.png';

const setupLinksLeftClick = (win) => {
win.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
};

const setupContextMenu = (win) => {
win.webContents.on('context-menu', (_, params) => {
const { x, y, linkURL, selectionText } = params;

const template = [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ type: 'separator' },
{ role: 'selectAll' },
{ type: 'separator' },
{ role: 'toggleDevTools' },
];

const spellingMenu = [];

if (selectionText && !linkURL) {
// Add each spelling suggestion
for (const suggestion of params.dictionarySuggestions) {
spellingMenu.push(
new MenuItem({
label: suggestion,
click: () => win.webContents.replaceMisspelling(suggestion),
})
);
}

// Allow users to add the misspelled word to the dictionary
if (params.misspelledWord) {
spellingMenu.push(
new MenuItem({
label: 'Add to dictionary',
click: () =>
win.webContents.session.addWordToSpellCheckerDictionary(
params.misspelledWord
),
})
);
}

if (spellingMenu.length > 0) {
spellingMenu.push({ type: 'separator' });
}

template.push(
{ type: 'separator' },
{
label: `Search Google for "${selectionText}"`,
click: () => {
shell.openExternal(
`https://www.google.com/search?q=${encodeURIComponent(
selectionText
)}`
);
},
},
{
label: `Search DuckDuckGo for "${selectionText}"`,
click: () => {
shell.openExternal(
`https://duckduckgo.com/?q=${encodeURIComponent(selectionText)}`
);
},
}
);
}

if (linkURL) {
template.push(
{ type: 'separator' },
{
label: 'Open Link in Browser',
click: () => {
shell.openExternal(linkURL);
},
},
{
label: 'Copy Link Address',
click: () => {
clipboard.writeText(linkURL);
},
},
{
label: 'Save Link As...',
click: () => {
dialog.showSaveDialog(
win,
{ defaultPath: path.basename(linkURL) },
(filePath) => {
if (filePath) {
download(win, linkURL, { filename: filePath });
}
}
);
},
}
);
}

Menu.buildFromTemplate([...spellingMenu, ...template]).popup({
window: win,
x,
y,
});
});
};

function createWindow() {
autoUpdater.checkForUpdatesAndNotify();

win = new BrowserWindow({
autoHideMenuBar: true,
autoHideMenuBar: true,
show: false,
icon: assetPath(ICON),
});
Expand All @@ -35,26 +164,27 @@ function createWindow() {
win.webContents.openDevTools({ mode: 'detach' });
}

setupLinksLeftClick(win);
setupContextMenu(win);

return win;
}

const assetPath = (asset) => {
return path.join(
__dirname,
isDev ? `../public/${asset}` : `../dist/${asset}`
)
}

const createTray = (window) => {
const tray = new Tray(
assetPath(!isMacOS ? ICON : ICON_TEMPLATE)
);
};

const createTray = (win) => {
const tray = new Tray(assetPath(!isMacOS ? ICON : ICON_TEMPLATE));
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show',
click: () => {
win.maximize();
window.show();
win.show();
},
},
{
Expand All @@ -68,7 +198,7 @@ const createTray = (window) => {

tray.on('click', () => {
win.maximize();
window.show();
win.show();
});
tray.setToolTip('Better ChatGPT');
tray.setContextMenu(contextMenu);
Expand All @@ -91,18 +221,18 @@ process.on('uncaughtException', (error) => {
});

if (!instanceLock) {
app.quit()
app.quit();
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (win) {
if (win.isMinimized()) win.restore()
win.focus()
if (win.isMinimized()) win.restore();
win.focus();
}
})
});

app.whenReady().then(() => {
win = createWindow()
})
win = createWindow();
});
}

const createServer = () => {
Expand Down
2 changes: 2 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const getChatCompletion = async (
const modelmapping: Partial<Record<ModelOptions, string>> = {
'gpt-3.5-turbo': 'gpt-35-turbo',
'gpt-3.5-turbo-16k': 'gpt-35-turbo-16k',
'gpt-3.5-turbo-1106': 'gpt-35-turbo-1106',
'gpt-3.5-turbo-0125': 'gpt-35-turbo-0125',
};

const model = modelmapping[config.model] || config.model;
Expand Down
18 changes: 17 additions & 1 deletion src/components/PopupModal/PopupModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
import { useTranslation } from 'react-i18next';

Expand Down Expand Up @@ -36,6 +36,22 @@ const PopupModal = ({
else _handleClose();
};

const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
if (handleClickBackdrop) handleClickBackdrop();
else handleClose ? handleClose() : setIsModalOpen(false);
} else if (event.key === 'Enter') {
if (handleConfirm) handleConfirm();
}
};

useEffect(() => {
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [handleConfirm, handleClose, handleClickBackdrop]);

if (modalRoot) {
return ReactDOM.createPortal(
<div className='fixed top-0 left-0 z-[999] w-full p-4 overflow-x-hidden overflow-y-auto h-full flex justify-center items-center'>
Expand Down
11 changes: 11 additions & 0 deletions src/constants/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const modelOptions: ModelOptions[] = [
'gpt-3.5-turbo',
'gpt-3.5-turbo-16k',
'gpt-3.5-turbo-1106',
'gpt-3.5-turbo-0125',
'gpt-4',
'gpt-4-32k',
'gpt-4-1106-preview',
Expand All @@ -39,13 +40,15 @@ export const modelMaxToken = {
'gpt-3.5-turbo-16k': 16384,
'gpt-3.5-turbo-16k-0613': 16384,
'gpt-3.5-turbo-1106': 16384,
'gpt-3.5-turbo-0125': 16384,
'gpt-4': 8192,
'gpt-4-0314': 8192,
'gpt-4-0613': 8192,
'gpt-4-32k': 32768,
'gpt-4-32k-0314': 32768,
'gpt-4-32k-0613': 32768,
'gpt-4-1106-preview': 128000,
'gpt-4-0125-preview': 128000,
'gpt-4-vision-preview': 128000
};

Expand Down Expand Up @@ -74,6 +77,10 @@ export const modelCost = {
prompt: { price: 0.001, unit: 1000 },
completion: { price: 0.0015, unit: 1000 },
},
'gpt-3.5-turbo-0125': {
prompt: { price: 0.0005, unit: 1000 },
completion: { price: 0.0015, unit: 1000 },
},
'gpt-4': {
prompt: { price: 0.03, unit: 1000 },
completion: { price: 0.06, unit: 1000 },
Expand Down Expand Up @@ -102,6 +109,10 @@ export const modelCost = {
prompt: { price: 0.01, unit: 1000 },
completion: { price: 0.03, unit: 1000 },
},
'gpt-4-0125-preview': {
prompt: { price: 0.01, unit: 1000 },
completion: { price: 0.03, unit: 1000 },
},
'gpt-4-vision-preview': {
prompt: { price: 0.01, unit: 1000 },
completion: { price: 0.03, unit: 1000 },
Expand Down
11 changes: 10 additions & 1 deletion src/types/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ export interface Folder {
color?: string;
}

export type ModelOptions = 'gpt-4' | 'gpt-4-32k' | 'gpt-4-1106-preview' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k' | 'gpt-3.5-turbo-1106' | 'gpt-4-vision-preview' ;
export type ModelOptions =
| 'gpt-4'
| 'gpt-4-32k'
| 'gpt-4-1106-preview'
| 'gpt-4-0125-preview'
| 'gpt-3.5-turbo'
| 'gpt-3.5-turbo-16k'
| 'gpt-3.5-turbo-1106'
| 'gpt-3.5-turbo-0125'
| 'gpt-4-vision-preview';
// | 'gpt-3.5-turbo-0301';
// | 'gpt-4-0314'
// | 'gpt-4-32k-0314'
Expand Down

0 comments on commit 3a91653

Please sign in to comment.