Skip to content

Commit

Permalink
Merge pull request #4 from mlx-chat/MLC-7
Browse files Browse the repository at this point in the history
[MLC-7] repo: Move all app related items to /app
  • Loading branch information
ParkerSm1th authored Feb 27, 2024
2 parents fc8b829 + 1645334 commit 3b9da01
Show file tree
Hide file tree
Showing 32 changed files with 116 additions and 136 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: 16

- name: Install App Deps
run: npm i --ignore-scripts
working-directory: ./app
- name: Lint App
working-directory: ./app
run: npm run lint
19 changes: 9 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
node_modules/
.pnp/
.pnp.js

# testing
/coverage
coverage/

# next.js
/.next/
/out/
.next/
out/

# production
/build
/main/out
/main/tailwind.css
/dist
build/
app/main/tailwind.css
dist/

# misc
.DS_Store
Expand Down Expand Up @@ -165,4 +164,4 @@ venv.bak/
dmypy.json

# Pyre type checker
.pyre/
.pyre/
5 changes: 1 addition & 4 deletions .eslintrc.cjs → app/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,7 @@ module.exports = {
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
project: ['./tsconfig.json'], // Specify it only for TypeScript files
sourceType: 'module',
tsconfigRootDir: __dirname,
project: './app/tsconfig.json',
},
plugins: [
'react',
Expand Down
File renamed without changes.
File renamed without changes.
121 changes: 66 additions & 55 deletions main/main.ts → app/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,65 +23,76 @@ function handleSetTitle(event: any, title: string) {

// Python Server
export class ServerManager {
private serverProcess: any | null = null;

private findOpenPort(startingPort: number): Promise<number> {
return new Promise<number>((resolve) => {
const server = net.createServer();

server.listen(startingPort, () => {
const port = (server.address() as net.AddressInfo).port;
server.close(() => resolve(port));
});

server.on('error', (err: any) => err.code === 'EADDRINUSE' && resolve(this.findOpenPort(startingPort + 1)));
private serverProcess: any | null = null;

private findOpenPort(startingPort: number): Promise<number> {
return new Promise<number>((resolve) => {
const server = net.createServer();

server.listen(startingPort, () => {
const port = (server.address() as net.AddressInfo).port;
server.close(() => resolve(port));
});
}

private runPythonServer(model: string, port: number): any {
const args = [`--model ${model}`, '--host 127.0.0.1', `--port ${port}`];
const modifiedArgs = args.flatMap(arg => arg.split(/\s+/));

const pythonProcess = spawn('python', ['-m', 'server.server', ...modifiedArgs]);
pythonProcess.stdout.on('data', (data: Buffer) => console.log("Server output:", data.toString('utf8')));
pythonProcess.stderr.on('data', (data: Buffer) => console.log(`Server error: ${data.toString('utf8')}`));

return pythonProcess;
}

start(model: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.stop();

this.findOpenPort(8080).then((port) => {
console.log(`Starting server for model: ${model} on port: ${port}`);
this.serverProcess = this.runPythonServer(model, port);

this.serverProcess.on('close', (code: number | null) => {
console.log(`Server process exited with code ${code}`);
this.serverProcess = null;
});

this.serverProcess.on('error', (err: any) => {
console.error(`Error in server process: ${err}`);
this.serverProcess = null;
reject(err);
});

resolve();

server.on(
'error',
(err: any) => err.code === 'EADDRINUSE' && resolve(this.findOpenPort(startingPort + 1)),
);
});
}

private runPythonServer(model: string, port: number): any {
const args = [`--model ${model}`, '--host 127.0.0.1', `--port ${port}`];
const modifiedArgs = args.flatMap(arg => arg.split(/\s+/));

const pythonProcess = spawn('python', ['-m', 'server.server', ...modifiedArgs], {
cwd: '../',
});
pythonProcess.stdout.on(
'data',
(data: Buffer) => console.log('Server output:', data.toString('utf8')),
);
pythonProcess.stderr.on(
'data',
(data: Buffer) => console.log(`Server error: ${data.toString('utf8')}`),
);

return pythonProcess;
}

start(model: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.stop();

this.findOpenPort(8080).then((port) => {
console.log(`Starting server for model: ${model} on port: ${port}`);
this.serverProcess = this.runPythonServer(model, port);

this.serverProcess.on('close', (code: number | null) => {
console.log(`Server process exited with code ${code}`);
this.serverProcess = null;
});

this.serverProcess.on('error', (err: any) => {
console.error(`Error in server process: ${err}`);
this.serverProcess = null;
reject(err);
});

resolve();
});
}

stop(): void {
if (this.serverProcess) {
console.log('Stopping the server...');
this.serverProcess.kill();
this.serverProcess = null;
}
});
}

stop(): void {
if (this.serverProcess) {
console.log('Stopping the server...');
this.serverProcess.kill();
this.serverProcess = null;
}
}

}

// Loading Screen
let splash: BrowserWindow | null;
const createSplashScreen = () => {
Expand Down Expand Up @@ -164,7 +175,7 @@ app.whenReady().then(() => {
.then(() => console.log('Server started successfully'))
.catch(error => console.error('Error starting server:', error));
});

createSplashScreen();

// createWindow();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
60 changes: 36 additions & 24 deletions src/app/page.tsx → app/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use client';

import React, {
useCallback,
useEffect,
useState,
} from 'react';
Expand All @@ -24,25 +23,25 @@ import {
export default function Home() {
const [selectedDirectory, setSelectedDirectory] = useState<string | null>(null);
const [message, setMessage] = useState<string | null>(null);
const [chatHistory, setChatHistory] = useState<{ role: string; content: any }[]>([]);
const [chatHistory, setChatHistory] = useState<{ role: string; content: string | null; }[]>([]);
const [selectedModel, setSelectedModel] = useState<string | null>(null);

function handleOpen() {
if (typeof window !== 'undefined' && global?.window) {
if (typeof window !== 'undefined') {
window.electronAPI.selectDirectory();
}
}

useEffect(() => {
window.electronAPI.onSelectDirectory(customData => {
window.electronAPI.onSelectDirectory((customData) => {
setSelectedDirectory(customData[0]);
});
}, []);

const handleModelChange = (selectedModel: string | null) => {
setSelectedModel(selectedModel);
if (typeof window !== 'undefined' && global?.window && selectedModel) {
window.electronAPI.startServer(selectedModel);
const handleModelChange = (model: string | null) => {
setSelectedModel(model);
if (typeof window !== 'undefined' && model) {
window.electronAPI.startServer(model);
}
};

Expand All @@ -63,13 +62,13 @@ export default function Home() {
body: JSON.stringify({
messages: newHistory,
temperature: 0.0,
max_tokens: 10,
// eslint-disable-next-line @typescript-eslint/naming-convention
max_tokens: 100,
}),
});

const responseData = await response.json();
const assistantResponse = responseData.choices[0].message.content;
console.log(assistantResponse);

setChatHistory([
...newHistory,
Expand All @@ -78,9 +77,9 @@ export default function Home() {

setMessage('');
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error fetching data:', error);
}
console.log(chatHistory);
}
};

Expand Down Expand Up @@ -124,19 +123,32 @@ export default function Home() {
</div>
</div>
<div className='flex-grow min-w-full bg-slate-100 rounded-sm dark:bg-zinc-900 border flex'>
{chatHistory.length ? (<div className='flex flex-col flex-grow gap-4 p-4'>
{chatHistory.map((chat, index) => (
<div key={index} className={`flex ${chat.role === 'user' ? 'justify-end' : 'justify-start'}`}>
<div className={`p-4 rounded-sm ${chat.role === 'user' ? 'bg-slate-200 dark:bg-zinc-800' : 'bg-slate-300 dark:bg-zinc-700'}`}>
<p>{chat.content}</p>
</div>
{chatHistory.length
? (
<div className='flex flex-col flex-grow gap-4 p-4'>
{chatHistory.map((chat, index) => (
<div
key={index}
className={`flex ${chat.role === 'user' ? 'justify-end' : 'justify-start'}`}
>
<div
className={`p-4 rounded-sm ${
chat.role === 'user'
? 'bg-slate-200 dark:bg-zinc-800'
: 'bg-slate-300 dark:bg-zinc-700'
}`}
>
<p>{chat.content}</p>
</div>
</div>
))}
</div>
))}
</div>) : (
<div className='flex justify-center items-center min-h-full align-middle flex-grow'>
<h1 className='text-md text-zinc-500 dark:text-zinc-500'>No messages yet</h1>
</div>
)}
)
: (
<div className='flex justify-center items-center min-h-full align-middle flex-grow'>
<h1 className='text-md text-zinc-500 dark:text-zinc-500'>No messages yet</h1>
</div>
)}
</div>
<div className='flex justify-center'>
<Input
Expand All @@ -148,4 +160,4 @@ export default function Home() {
</div>
</main>
);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tsconfig.json → app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"main/preload.ts",
"main/main.ts",
"out/types/**/*.ts",
".eslintrc.cjs",
],
"exclude": [
"node_modules",
Expand Down
18 changes: 0 additions & 18 deletions src/lib/hooks.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/lib/store.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/lib/utils.ts

This file was deleted.

0 comments on commit 3b9da01

Please sign in to comment.