Skip to content

Commit

Permalink
refactor: move file to jan root (janhq#598)
Browse files Browse the repository at this point in the history
* feat: move necessary files to jan root

Signed-off-by: James <[email protected]>

* chore: check model dir

---------

Signed-off-by: James <[email protected]>
Co-authored-by: James <[email protected]>
Co-authored-by: Louis <[email protected]>
  • Loading branch information
3 people authored Nov 16, 2023
1 parent 9c5c03b commit 52d56a8
Show file tree
Hide file tree
Showing 54 changed files with 608 additions and 658 deletions.
4 changes: 2 additions & 2 deletions core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type MessageHistory = {
* The `NewMessageRequest` type defines the shape of a new message request object.
*/
export type NewMessageRequest = {
_id?: string;
id?: string;
conversationId?: string;
user?: string;
avatar?: string;
Expand All @@ -34,7 +34,7 @@ export type NewMessageRequest = {
* The `NewMessageRequest` type defines the shape of a new message request object.
*/
export type NewMessageResponse = {
_id?: string;
id?: string;
conversationId?: string;
user?: string;
avatar?: string;
Expand Down
17 changes: 17 additions & 0 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ const writeFile: (path: string, data: string) => Promise<any> = (path, data) =>
window.coreAPI?.writeFile(path, data) ??
window.electronAPI?.writeFile(path, data);

/**
* Gets the user space path.
* @returns {Promise<any>} A Promise that resolves with the user space path.
*/
const getUserSpace = (): Promise<string> =>
window.coreAPI?.getUserSpace() ?? window.electronAPI?.getUserSpace();

/**
* Checks whether the path is a directory.
* @param path - The path to check.
* @returns {boolean} A boolean indicating whether the path is a directory.
*/
const isDirectory = (path: string): Promise<boolean> =>
window.coreAPI?.isDirectory(path) ?? window.electronAPI?.isDirectory(path);

/**
* Reads the contents of a file at the specified path.
* @param {string} path - The path of the file to read.
Expand Down Expand Up @@ -48,6 +63,8 @@ const deleteFile: (path: string) => Promise<any> = (path) =>
window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path);

export const fs = {
isDirectory,
getUserSpace,
writeFile,
readFile,
listFiles,
Expand Down
9 changes: 3 additions & 6 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ export { events } from "./events";
* Events types exports.
* @module
*/
export {
EventName,
NewMessageRequest,
NewMessageResponse,
MessageHistory,
} from "./events";
export * from "./events";

export * from "./types/index";

/**
* Filesystem module exports.
Expand Down
59 changes: 54 additions & 5 deletions core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Conversation {
_id: string;
id: string;
modelId?: string;
botId?: string;
name: string;
Expand All @@ -8,11 +8,23 @@ export interface Conversation {
createdAt?: string;
updatedAt?: string;
messages: Message[];
lastMessage?: string;
}

export interface Message {
id: string;
message?: string;
user?: string;
_id: string;
createdAt?: string;
updatedAt?: string;
}

export interface RawMessage {
id?: string;
conversationId?: string;
user?: string;
avatar?: string;
message?: string;
createdAt?: string;
updatedAt?: string;
}
Expand All @@ -22,7 +34,7 @@ export interface Model {
* Combination of owner and model name.
* Being used as file name. MUST be unique.
*/
_id: string;
id: string;
name: string;
quantMethod: string;
bits: number;
Expand Down Expand Up @@ -51,7 +63,7 @@ export interface Model {
tags: string[];
}
export interface ModelCatalog {
_id: string;
id: string;
name: string;
shortDescription: string;
avatarUrl: string;
Expand All @@ -74,7 +86,7 @@ export type ModelVersion = {
* Combination of owner and model name.
* Being used as file name. Should be unique.
*/
_id: string;
id: string;
name: string;
quantMethod: string;
bits: number;
Expand All @@ -89,3 +101,40 @@ export type ModelVersion = {
startDownloadAt?: number;
finishDownloadAt?: number;
};

export interface ChatMessage {
id: string;
conversationId: string;
messageType: MessageType;
messageSenderType: MessageSenderType;
senderUid: string;
senderName: string;
senderAvatarUrl: string;
text: string | undefined;
imageUrls?: string[] | undefined;
createdAt: number;
status: MessageStatus;
}

export enum MessageType {
Text = "Text",
Image = "Image",
ImageWithText = "ImageWithText",
Error = "Error",
}

export enum MessageSenderType {
Ai = "assistant",
User = "user",
}

export enum MessageStatus {
Ready = "ready",
Pending = "pending",
}

export type ConversationState = {
hasMore: boolean;
waitingForResponse: boolean;
error?: Error;
};
8 changes: 8 additions & 0 deletions electron/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": false,
"singleQuote": true,
"quoteProps": "consistent",
"trailingComma": "es5",
"endOfLine": "auto",
"plugins": ["prettier-plugin-tailwindcss"]
}
96 changes: 49 additions & 47 deletions electron/handlers/download.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,108 @@
import { app, ipcMain } from "electron";
import { DownloadManager } from "../managers/download";
import { resolve, join } from "path";
import { WindowManager } from "../managers/window";
import request from "request";
import { createWriteStream, unlink } from "fs";
const progress = require("request-progress");
import { app, ipcMain } from 'electron'
import { DownloadManager } from '../managers/download'
import { resolve, join } from 'path'
import { WindowManager } from '../managers/window'
import request from 'request'
import { createWriteStream, unlink } from 'fs'
const progress = require('request-progress')

export function handleDownloaderIPCs() {
/**
* Handles the "pauseDownload" IPC message by pausing the download associated with the provided fileName.
* @param _event - The IPC event object.
* @param fileName - The name of the file being downloaded.
*/
ipcMain.handle("pauseDownload", async (_event, fileName) => {
DownloadManager.instance.networkRequests[fileName]?.pause();
});
ipcMain.handle('pauseDownload', async (_event, fileName) => {
DownloadManager.instance.networkRequests[fileName]?.pause()
})

/**
* Handles the "resumeDownload" IPC message by resuming the download associated with the provided fileName.
* @param _event - The IPC event object.
* @param fileName - The name of the file being downloaded.
*/
ipcMain.handle("resumeDownload", async (_event, fileName) => {
DownloadManager.instance.networkRequests[fileName]?.resume();
});
ipcMain.handle('resumeDownload', async (_event, fileName) => {
DownloadManager.instance.networkRequests[fileName]?.resume()
})

/**
* Handles the "abortDownload" IPC message by aborting the download associated with the provided fileName.
* The network request associated with the fileName is then removed from the networkRequests object.
* @param _event - The IPC event object.
* @param fileName - The name of the file being downloaded.
*/
ipcMain.handle("abortDownload", async (_event, fileName) => {
const rq = DownloadManager.instance.networkRequests[fileName];
DownloadManager.instance.networkRequests[fileName] = undefined;
const userDataPath = app.getPath("userData");
const fullPath = join(userDataPath, fileName);
rq?.abort();
let result = "NULL";
ipcMain.handle('abortDownload', async (_event, fileName) => {
const rq = DownloadManager.instance.networkRequests[fileName]
DownloadManager.instance.networkRequests[fileName] = undefined
const userDataPath = app.getPath('userData')
const fullPath = join(userDataPath, fileName)
rq?.abort()
let result = 'NULL'
unlink(fullPath, function (err) {
if (err && err.code == "ENOENT") {
result = `File not exist: ${err}`;
if (err && err.code == 'ENOENT') {
result = `File not exist: ${err}`
} else if (err) {
result = `File delete error: ${err}`;
result = `File delete error: ${err}`
} else {
result = "File deleted successfully";
result = 'File deleted successfully'
}
console.log(`Delete file ${fileName} from ${fullPath} result: ${result}`);
});
});
console.debug(
`Delete file ${fileName} from ${fullPath} result: ${result}`
)
})
})

/**
* Downloads a file from a given URL.
* @param _event - The IPC event object.
* @param url - The URL to download the file from.
* @param fileName - The name to give the downloaded file.
*/
ipcMain.handle("downloadFile", async (_event, url, fileName) => {
const userDataPath = app.getPath("userData");
const destination = resolve(userDataPath, fileName);
const rq = request(url);
ipcMain.handle('downloadFile', async (_event, url, fileName) => {
const userDataPath = join(app.getPath('home'), 'jan')
const destination = resolve(userDataPath, fileName)
const rq = request(url)

progress(rq, {})
.on("progress", function (state: any) {
.on('progress', function (state: any) {
WindowManager?.instance.currentWindow?.webContents.send(
"FILE_DOWNLOAD_UPDATE",
'FILE_DOWNLOAD_UPDATE',
{
...state,
fileName,
}
);
)
})
.on("error", function (err: Error) {
.on('error', function (err: Error) {
WindowManager?.instance.currentWindow?.webContents.send(
"FILE_DOWNLOAD_ERROR",
'FILE_DOWNLOAD_ERROR',
{
fileName,
err,
}
);
)
})
.on("end", function () {
.on('end', function () {
if (DownloadManager.instance.networkRequests[fileName]) {
WindowManager?.instance.currentWindow?.webContents.send(
"FILE_DOWNLOAD_COMPLETE",
'FILE_DOWNLOAD_COMPLETE',
{
fileName,
}
);
DownloadManager.instance.setRequest(fileName, undefined);
)
DownloadManager.instance.setRequest(fileName, undefined)
} else {
WindowManager?.instance.currentWindow?.webContents.send(
"FILE_DOWNLOAD_ERROR",
'FILE_DOWNLOAD_ERROR',
{
fileName,
err: "Download cancelled",
err: 'Download cancelled',
}
);
)
}
})
.pipe(createWriteStream(destination));
.pipe(createWriteStream(destination))

DownloadManager.instance.setRequest(fileName, rq);
});
DownloadManager.instance.setRequest(fileName, rq)
})
}
Loading

0 comments on commit 52d56a8

Please sign in to comment.