From cbc63da8310178bafe700cdb8583c3933ab02a4d Mon Sep 17 00:00:00 2001 From: NamH Date: Thu, 28 Dec 2023 13:00:20 +0700 Subject: [PATCH 1/4] fix: #1183 Reveal in finder does not work on windows (#1239) * fix(OpenFile): #1183 reveal in finder does not work on windows Signed-off-by: James --------- Signed-off-by: James --- core/src/api/index.ts | 1 + core/src/core.ts | 8 +++++++ electron/handlers/app.ts | 7 ++++++ web/containers/CardSidebar/index.tsx | 9 +++++++- web/next.config.js | 3 +++ web/screens/Chat/Sidebar/index.tsx | 22 ++++++++----------- .../ExtensionsCatalog/index.tsx | 5 ----- web/types/index.d.ts | 3 +++ 8 files changed, 39 insertions(+), 19 deletions(-) diff --git a/core/src/api/index.ts b/core/src/api/index.ts index 64e781096..4f2b45f5f 100644 --- a/core/src/api/index.ts +++ b/core/src/api/index.ts @@ -10,6 +10,7 @@ export enum AppRoute { openAppDirectory = 'openAppDirectory', openFileExplore = 'openFileExplorer', relaunch = 'relaunch', + joinPath = 'joinPath' } export enum AppEvent { diff --git a/core/src/core.ts b/core/src/core.ts index f268233b7..0f20feb1e 100644 --- a/core/src/core.ts +++ b/core/src/core.ts @@ -44,6 +44,13 @@ const getUserSpace = (): Promise => global.core.api?.getUserSpace() const openFileExplorer: (path: string) => Promise = (path) => global.core.api?.openFileExplorer(path) +/** + * Joins multiple paths together. + * @param paths - The paths to join. + * @returns {Promise} A promise that resolves with the joined path. + */ +const joinPath: (paths: string[]) => Promise = (paths) => global.core.api?.joinPath(paths) + const getResourcePath: () => Promise = () => global.core.api?.getResourcePath() /** @@ -66,4 +73,5 @@ export { getUserSpace, openFileExplorer, getResourcePath, + joinPath, } diff --git a/electron/handlers/app.ts b/electron/handlers/app.ts index fc2d0ee59..ff88cd8f1 100644 --- a/electron/handlers/app.ts +++ b/electron/handlers/app.ts @@ -48,6 +48,13 @@ export function handleAppIPCs() { shell.openPath(url) }) + /** + * Joins multiple paths together, respect to the current OS. + */ + ipcMain.handle(AppRoute.joinPath, async (_event, paths: string[]) => + join(...paths) + ) + /** * Relaunches the app in production - reload window in development. * @param _event - The IPC event object. diff --git a/web/containers/CardSidebar/index.tsx b/web/containers/CardSidebar/index.tsx index 8c7fe3314..e3851214b 100644 --- a/web/containers/CardSidebar/index.tsx +++ b/web/containers/CardSidebar/index.tsx @@ -29,6 +29,13 @@ export default function CardSidebar({ useClickOutside(() => setMore(false), null, [menu, toggle]) + let openFolderTitle: string = 'Open Containing Folder' + if (isMac) { + openFolderTitle = 'Reveal in Finder' + } else if (isWindows) { + openFolderTitle = 'Reveal in File Explorer' + } + return (
- Reveal in Finder + {openFolderTitle}
{ const assistantId = activeThread.assistants[0]?.assistant_id switch (type) { case 'Thread': - filePath = join('threads', activeThread.id) + filePath = await joinPath(['threads', activeThread.id]) break case 'Model': if (!selectedModel) return - filePath = join('models', selectedModel.id) + filePath = await joinPath(['models', selectedModel.id]) break case 'Assistant': if (!assistantId) return - filePath = join('assistants', assistantId) + filePath = await joinPath(['assistants', assistantId]) break default: break } if (!filePath) return - - const fullPath = join(userSpace, filePath) + const fullPath = await joinPath([userSpace, filePath]) openFileExplorer(fullPath) } @@ -87,23 +84,22 @@ const Sidebar: React.FC = () => { const assistantId = activeThread.assistants[0]?.assistant_id switch (type) { case 'Thread': - filePath = join('threads', activeThread.id, 'thread.json') + filePath = await joinPath(['threads', activeThread.id, 'thread.json']) break case 'Model': if (!selectedModel) return - filePath = join('models', selectedModel.id, 'model.json') + filePath = await joinPath(['models', selectedModel.id, 'model.json']) break case 'Assistant': if (!assistantId) return - filePath = join('assistants', assistantId, 'assistant.json') + filePath = await joinPath(['assistants', assistantId, 'assistant.json']) break default: break } if (!filePath) return - - const fullPath = join(userSpace, filePath) + const fullPath = await joinPath([userSpace, filePath]) openFileExplorer(fullPath) } diff --git a/web/screens/Settings/CoreExtensions/ExtensionsCatalog/index.tsx b/web/screens/Settings/CoreExtensions/ExtensionsCatalog/index.tsx index b7ac59aa6..e2a6f3e13 100644 --- a/web/screens/Settings/CoreExtensions/ExtensionsCatalog/index.tsx +++ b/web/screens/Settings/CoreExtensions/ExtensionsCatalog/index.tsx @@ -5,8 +5,6 @@ import React, { useState, useEffect, useRef, useContext } from 'react' import { Button } from '@janhq/uikit' -import Loader from '@/containers/Loader' - import { FeatureToggleContext } from '@/context/FeatureToggle' import { useGetAppVersion } from '@/hooks/useGetAppVersion' @@ -18,7 +16,6 @@ import { extensionManager } from '@/extension' const ExtensionCatalog = () => { const [activeExtensions, setActiveExtensions] = useState([]) const [extensionCatalog, setExtensionCatalog] = useState([]) - const [isLoading, setIsLoading] = useState(false) const fileInputRef = useRef(null) const { version } = useGetAppVersion() const { experimentalFeatureEnabed } = useContext(FeatureToggleContext) @@ -95,8 +92,6 @@ const ExtensionCatalog = () => { } } - if (isLoading) return - return (
{extensionCatalog diff --git a/web/types/index.d.ts b/web/types/index.d.ts index 5e7b401ae..bd46b8a6f 100644 --- a/web/types/index.d.ts +++ b/web/types/index.d.ts @@ -8,6 +8,9 @@ declare global { declare const VERSION: string declare const ANALYTICS_ID: string declare const ANALYTICS_HOST: string + declare const isMac: boolean + declare const isWindows: boolean + declare const isLinux: boolean interface Core { api: APIFunctions events: EventEmitter From 7feaf9694dafc065be8e26e21aa380913784756b Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 28 Dec 2023 14:06:13 +0700 Subject: [PATCH 2/4] feat: Deprecate model.json ready state in favor of .download ext (#1238) * feat: Deprecate model.json ready state in favor of .download ext * refactor: resolve ts ignore * chore: fix warning * fix: path polyfill on Windows --- core/src/types/model/modelEntity.ts | 16 --- electron/handlers/download.ts | 9 +- .../conversational-extension/src/index.ts | 67 ++++----- .../inference-nitro-extension/src/index.ts | 2 +- .../inference-nitro-extension/src/module.ts | 53 +++++--- extensions/model-extension/src/index.ts | 127 +++++++++++------- .../BottomBar/DownloadingState/index.tsx | 16 ++- web/containers/ModalCancelDownload/index.tsx | 13 +- web/containers/Providers/EventListener.tsx | 67 +++++---- web/hooks/useDownloadModel.ts | 16 ++- web/hooks/useEngineSettings.ts | 13 +- web/screens/Chat/ChatBody/index.tsx | 8 +- web/utils/model.ts | 12 ++ web/utils/model_param.ts | 6 +- 14 files changed, 245 insertions(+), 180 deletions(-) create mode 100644 web/utils/model.ts diff --git a/core/src/types/model/modelEntity.ts b/core/src/types/model/modelEntity.ts index 1dd12c89b..23d27935e 100644 --- a/core/src/types/model/modelEntity.ts +++ b/core/src/types/model/modelEntity.ts @@ -67,13 +67,6 @@ export type Model = { */ description: string - /** - * The model state. - * Default: "to_download" - * Enum: "to_download" "downloading" "ready" "running" - */ - state?: ModelState - /** * The model settings. */ @@ -101,15 +94,6 @@ export type ModelMetadata = { cover?: string } -/** - * The Model transition states. - */ -export enum ModelState { - Downloading = 'downloading', - Ready = 'ready', - Running = 'running', -} - /** * The available model settings. */ diff --git a/electron/handlers/download.ts b/electron/handlers/download.ts index 6e64d23e2..145174ac2 100644 --- a/electron/handlers/download.ts +++ b/electron/handlers/download.ts @@ -3,7 +3,7 @@ import { DownloadManager } from './../managers/download' import { resolve, join } from 'path' import { WindowManager } from './../managers/window' import request from 'request' -import { createWriteStream } from 'fs' +import { createWriteStream, renameSync } from 'fs' import { DownloadEvent, DownloadRoute } from '@janhq/core' const progress = require('request-progress') @@ -48,6 +48,8 @@ export function handleDownloaderIPCs() { const userDataPath = join(app.getPath('home'), 'jan') const destination = resolve(userDataPath, fileName) const rq = request(url) + // downloading file to a temp file first + const downloadingTempFile = `${destination}.download` progress(rq, {}) .on('progress', function (state: any) { @@ -70,6 +72,9 @@ export function handleDownloaderIPCs() { }) .on('end', function () { if (DownloadManager.instance.networkRequests[fileName]) { + // Finished downloading, rename temp file to actual file + renameSync(downloadingTempFile, destination) + WindowManager?.instance.currentWindow?.webContents.send( DownloadEvent.onFileDownloadSuccess, { @@ -87,7 +92,7 @@ export function handleDownloaderIPCs() { ) } }) - .pipe(createWriteStream(destination)) + .pipe(createWriteStream(downloadingTempFile)) DownloadManager.instance.setRequest(fileName, rq) }) diff --git a/extensions/conversational-extension/src/index.ts b/extensions/conversational-extension/src/index.ts index 0fdf0b2d4..8aae791e8 100644 --- a/extensions/conversational-extension/src/index.ts +++ b/extensions/conversational-extension/src/index.ts @@ -1,7 +1,6 @@ -import { ExtensionType, fs } from '@janhq/core' +import { ExtensionType, fs, joinPath } from '@janhq/core' import { ConversationalExtension } from '@janhq/core' import { Thread, ThreadMessage } from '@janhq/core' -import { join } from 'path' /** * JSONConversationalExtension is a ConversationalExtension implementation that provides @@ -69,14 +68,14 @@ export default class JSONConversationalExtension */ async saveThread(thread: Thread): Promise { try { - const threadDirPath = join( + const threadDirPath = await joinPath([ JSONConversationalExtension._homeDir, - thread.id - ) - const threadJsonPath = join( + thread.id, + ]) + const threadJsonPath = await joinPath([ threadDirPath, - JSONConversationalExtension._threadInfoFileName - ) + JSONConversationalExtension._threadInfoFileName, + ]) await fs.mkdir(threadDirPath) await fs.writeFile(threadJsonPath, JSON.stringify(thread, null, 2)) Promise.resolve() @@ -89,20 +88,22 @@ export default class JSONConversationalExtension * Delete a thread with the specified ID. * @param threadId The ID of the thread to delete. */ - deleteThread(threadId: string): Promise { - return fs.rmdir(join(JSONConversationalExtension._homeDir, `${threadId}`)) + async deleteThread(threadId: string): Promise { + return fs.rmdir( + await joinPath([JSONConversationalExtension._homeDir, `${threadId}`]) + ) } async addNewMessage(message: ThreadMessage): Promise { try { - const threadDirPath = join( + const threadDirPath = await joinPath([ JSONConversationalExtension._homeDir, - message.thread_id - ) - const threadMessagePath = join( + message.thread_id, + ]) + const threadMessagePath = await joinPath([ threadDirPath, - JSONConversationalExtension._threadMessagesFileName - ) + JSONConversationalExtension._threadMessagesFileName, + ]) await fs.mkdir(threadDirPath) await fs.appendFile(threadMessagePath, JSON.stringify(message) + '\n') Promise.resolve() @@ -116,11 +117,14 @@ export default class JSONConversationalExtension messages: ThreadMessage[] ): Promise { try { - const threadDirPath = join(JSONConversationalExtension._homeDir, threadId) - const threadMessagePath = join( + const threadDirPath = await joinPath([ + JSONConversationalExtension._homeDir, + threadId, + ]) + const threadMessagePath = await joinPath([ threadDirPath, - JSONConversationalExtension._threadMessagesFileName - ) + JSONConversationalExtension._threadMessagesFileName, + ]) await fs.mkdir(threadDirPath) await fs.writeFile( threadMessagePath, @@ -140,11 +144,11 @@ export default class JSONConversationalExtension */ private async readThread(threadDirName: string): Promise { return fs.readFile( - join( + await joinPath([ JSONConversationalExtension._homeDir, threadDirName, - JSONConversationalExtension._threadInfoFileName - ) + JSONConversationalExtension._threadInfoFileName, + ]) ) } @@ -159,10 +163,10 @@ export default class JSONConversationalExtension const threadDirs: string[] = [] for (let i = 0; i < fileInsideThread.length; i++) { - const path = join( + const path = await joinPath([ JSONConversationalExtension._homeDir, - fileInsideThread[i] - ) + fileInsideThread[i], + ]) const isDirectory = await fs.isDirectory(path) if (!isDirectory) { console.debug(`Ignore ${path} because it is not a directory`) @@ -184,7 +188,10 @@ export default class JSONConversationalExtension async getAllMessages(threadId: string): Promise { try { - const threadDirPath = join(JSONConversationalExtension._homeDir, threadId) + const threadDirPath = await joinPath([ + JSONConversationalExtension._homeDir, + threadId, + ]) const isDir = await fs.isDirectory(threadDirPath) if (!isDir) { throw Error(`${threadDirPath} is not directory`) @@ -197,10 +204,10 @@ export default class JSONConversationalExtension throw Error(`${threadDirPath} not contains message file`) } - const messageFilePath = join( + const messageFilePath = await joinPath([ threadDirPath, - JSONConversationalExtension._threadMessagesFileName - ) + JSONConversationalExtension._threadMessagesFileName, + ]) const result = await fs.readLineByLine(messageFilePath) diff --git a/extensions/inference-nitro-extension/src/index.ts b/extensions/inference-nitro-extension/src/index.ts index 946d526dd..d19f3853c 100644 --- a/extensions/inference-nitro-extension/src/index.ts +++ b/extensions/inference-nitro-extension/src/index.ts @@ -111,7 +111,7 @@ export default class JanInferenceNitroExtension implements InferenceExtension { return; } const userSpacePath = await getUserSpace(); - const modelFullPath = join(userSpacePath, "models", model.id, model.id); + const modelFullPath = join(userSpacePath, "models", model.id); const nitroInitResult = await executeOnMain(MODULE, "initModel", { modelFullPath: modelFullPath, diff --git a/extensions/inference-nitro-extension/src/module.ts b/extensions/inference-nitro-extension/src/module.ts index 25836a875..37b9e5b3b 100644 --- a/extensions/inference-nitro-extension/src/module.ts +++ b/extensions/inference-nitro-extension/src/module.ts @@ -13,10 +13,11 @@ const NITRO_HTTP_LOAD_MODEL_URL = `${NITRO_HTTP_SERVER_URL}/inferences/llamacpp/ const NITRO_HTTP_UNLOAD_MODEL_URL = `${NITRO_HTTP_SERVER_URL}/inferences/llamacpp/unloadModel`; const NITRO_HTTP_VALIDATE_MODEL_URL = `${NITRO_HTTP_SERVER_URL}/inferences/llamacpp/modelstatus`; const NITRO_HTTP_KILL_URL = `${NITRO_HTTP_SERVER_URL}/processmanager/destroy`; +const SUPPORTED_MODEL_FORMAT = ".gguf"; // The subprocess instance for Nitro let subprocess = undefined; -let currentModelFile = undefined; +let currentModelFile: string = undefined; let currentSettings = undefined; /** @@ -37,6 +38,17 @@ function stopModel(): Promise { */ async function initModel(wrapper: any): Promise { currentModelFile = wrapper.modelFullPath; + const files: string[] = fs.readdirSync(currentModelFile); + + // Look for GGUF model file + const ggufBinFile = files.find( + (file) => + file === path.basename(currentModelFile) || + file.toLowerCase().includes(SUPPORTED_MODEL_FORMAT) + ); + + currentModelFile = path.join(currentModelFile, ggufBinFile); + if (wrapper.model.engine !== "nitro") { return Promise.resolve({ error: "Not a nitro model" }); } else { @@ -66,25 +78,26 @@ async function initModel(wrapper: any): Promise { async function loadModel(nitroResourceProbe: any | undefined) { // Gather system information for CPU physical cores and memory if (!nitroResourceProbe) nitroResourceProbe = await getResourcesInfo(); - return killSubprocess() - .then(() => tcpPortUsed.waitUntilFree(PORT, 300, 5000)) - // wait for 500ms to make sure the port is free for windows platform - .then(() => { - if (process.platform === "win32") { - return sleep(500); - } - else { - return sleep(0); - } - }) - .then(() => spawnNitroProcess(nitroResourceProbe)) - .then(() => loadLLMModel(currentSettings)) - .then(validateModelStatus) - .catch((err) => { - console.error("error: ", err); - // TODO: Broadcast error so app could display proper error message - return { error: err, currentModelFile }; - }); + return ( + killSubprocess() + .then(() => tcpPortUsed.waitUntilFree(PORT, 300, 5000)) + // wait for 500ms to make sure the port is free for windows platform + .then(() => { + if (process.platform === "win32") { + return sleep(500); + } else { + return sleep(0); + } + }) + .then(() => spawnNitroProcess(nitroResourceProbe)) + .then(() => loadLLMModel(currentSettings)) + .then(validateModelStatus) + .catch((err) => { + console.error("error: ", err); + // TODO: Broadcast error so app could display proper error message + return { error: err, currentModelFile }; + }) + ); } // Add function sleep diff --git a/extensions/model-extension/src/index.ts b/extensions/model-extension/src/index.ts index 16adced5d..9580afd9b 100644 --- a/extensions/model-extension/src/index.ts +++ b/extensions/model-extension/src/index.ts @@ -5,9 +5,11 @@ import { abortDownload, getResourcePath, getUserSpace, + InferenceEngine, + joinPath, } from '@janhq/core' -import { ModelExtension, Model, ModelState } from '@janhq/core' -import { join } from 'path' +import { basename } from 'path' +import { ModelExtension, Model } from '@janhq/core' /** * A extension for models @@ -15,6 +17,9 @@ import { join } from 'path' export default class JanModelExtension implements ModelExtension { private static readonly _homeDir = 'models' private static readonly _modelMetadataFileName = 'model.json' + private static readonly _supportedModelFormat = '.gguf' + private static readonly _incompletedModelFileName = '.download' + private static readonly _offlineInferenceEngine = InferenceEngine.nitro /** * Implements type from JanExtension. @@ -54,10 +59,10 @@ export default class JanModelExtension implements ModelExtension { // copy models folder from resources to home directory const resourePath = await getResourcePath() - const srcPath = join(resourePath, 'models') + const srcPath = await joinPath([resourePath, 'models']) const userSpace = await getUserSpace() - const destPath = join(userSpace, JanModelExtension._homeDir) + const destPath = await joinPath([userSpace, JanModelExtension._homeDir]) await fs.syncFile(srcPath, destPath) @@ -88,11 +93,18 @@ export default class JanModelExtension implements ModelExtension { */ async downloadModel(model: Model): Promise { // create corresponding directory - const directoryPath = join(JanModelExtension._homeDir, model.id) - await fs.mkdir(directoryPath) + const modelDirPath = await joinPath([JanModelExtension._homeDir, model.id]) + await fs.mkdir(modelDirPath) - // path to model binary - const path = join(directoryPath, model.id) + // try to retrieve the download file name from the source url + // if it fails, use the model ID as the file name + const extractedFileName = basename(model.source_url) + const fileName = extractedFileName + .toLowerCase() + .endsWith(JanModelExtension._supportedModelFormat) + ? extractedFileName + : model.id + const path = await joinPath([modelDirPath, fileName]) downloadFile(model.source_url, path) } @@ -103,10 +115,12 @@ export default class JanModelExtension implements ModelExtension { */ async cancelModelDownload(modelId: string): Promise { return abortDownload( - join(JanModelExtension._homeDir, modelId, modelId) - ).then(() => { - fs.deleteFile(join(JanModelExtension._homeDir, modelId, modelId)) - }) + await joinPath([JanModelExtension._homeDir, modelId, modelId]) + ).then(async () => + fs.deleteFile( + await joinPath([JanModelExtension._homeDir, modelId, modelId]) + ) + ) } /** @@ -116,27 +130,16 @@ export default class JanModelExtension implements ModelExtension { */ async deleteModel(modelId: string): Promise { try { - const dirPath = join(JanModelExtension._homeDir, modelId) + const dirPath = await joinPath([JanModelExtension._homeDir, modelId]) // remove all files under dirPath except model.json const files = await fs.listFiles(dirPath) - const deletePromises = files.map((fileName: string) => { + const deletePromises = files.map(async (fileName: string) => { if (fileName !== JanModelExtension._modelMetadataFileName) { - return fs.deleteFile(join(dirPath, fileName)) + return fs.deleteFile(await joinPath([dirPath, fileName])) } }) await Promise.allSettled(deletePromises) - - // update the state as default - const jsonFilePath = join( - dirPath, - JanModelExtension._modelMetadataFileName - ) - const json = await fs.readFile(jsonFilePath) - const model = JSON.parse(json) as Model - delete model.state - - await fs.writeFile(jsonFilePath, JSON.stringify(model, null, 2)) } catch (err) { console.error(err) } @@ -148,24 +151,14 @@ export default class JanModelExtension implements ModelExtension { * @returns A Promise that resolves when the model is saved. */ async saveModel(model: Model): Promise { - const jsonFilePath = join( + const jsonFilePath = await joinPath([ JanModelExtension._homeDir, model.id, - JanModelExtension._modelMetadataFileName - ) + JanModelExtension._modelMetadataFileName, + ]) try { - await fs.writeFile( - jsonFilePath, - JSON.stringify( - { - ...model, - state: ModelState.Ready, - }, - null, - 2 - ) - ) + await fs.writeFile(jsonFilePath, JSON.stringify(model, null, 2)) } catch (err) { console.error(err) } @@ -176,11 +169,34 @@ export default class JanModelExtension implements ModelExtension { * @returns A Promise that resolves with an array of all models. */ async getDownloadedModels(): Promise { - const models = await this.getModelsMetadata() - return models.filter((model) => model.state === ModelState.Ready) + return await this.getModelsMetadata( + async (modelDir: string, model: Model) => { + if (model.engine !== JanModelExtension._offlineInferenceEngine) { + return true + } + return await fs + .listFiles(await joinPath([JanModelExtension._homeDir, modelDir])) + .then((files: string[]) => { + // or model binary exists in the directory + // model binary name can match model ID or be a .gguf file and not be an incompleted model file + return ( + files.includes(modelDir) || + files.some( + (file) => + file + .toLowerCase() + .includes(JanModelExtension._supportedModelFormat) && + !file.endsWith(JanModelExtension._incompletedModelFileName) + ) + ) + }) + } + ) } - private async getModelsMetadata(): Promise { + private async getModelsMetadata( + selector?: (path: string, model: Model) => Promise + ): Promise { try { const filesUnderJanRoot = await fs.listFiles('') if (!filesUnderJanRoot.includes(JanModelExtension._homeDir)) { @@ -193,26 +209,35 @@ export default class JanModelExtension implements ModelExtension { const allDirectories: string[] = [] for (const file of files) { const isDirectory = await fs.isDirectory( - join(JanModelExtension._homeDir, file) + await joinPath([JanModelExtension._homeDir, file]) ) if (isDirectory) { allDirectories.push(file) } } - const readJsonPromises = allDirectories.map((dirName) => { - const jsonPath = join( + const readJsonPromises = allDirectories.map(async (dirName) => { + // filter out directories that don't match the selector + + // read model.json + const jsonPath = await joinPath([ JanModelExtension._homeDir, dirName, - JanModelExtension._modelMetadataFileName - ) - return this.readModelMetadata(jsonPath) + JanModelExtension._modelMetadataFileName, + ]) + let model = await this.readModelMetadata(jsonPath) + model = typeof model === 'object' ? model : JSON.parse(model) + + if (selector && !(await selector?.(dirName, model))) { + return + } + return model }) const results = await Promise.allSettled(readJsonPromises) const modelData = results.map((result) => { if (result.status === 'fulfilled') { try { - return JSON.parse(result.value) as Model + return result.value as Model } catch { console.debug(`Unable to parse model metadata: ${result.value}`) return undefined @@ -230,7 +255,7 @@ export default class JanModelExtension implements ModelExtension { } private readModelMetadata(path: string) { - return fs.readFile(join(path)) + return fs.readFile(path) } /** diff --git a/web/containers/Layout/BottomBar/DownloadingState/index.tsx b/web/containers/Layout/BottomBar/DownloadingState/index.tsx index 0648508d0..7aef36caf 100644 --- a/web/containers/Layout/BottomBar/DownloadingState/index.tsx +++ b/web/containers/Layout/BottomBar/DownloadingState/index.tsx @@ -1,7 +1,5 @@ import { Fragment } from 'react' -import { ExtensionType } from '@janhq/core' -import { ModelExtension } from '@janhq/core' import { Progress, Modal, @@ -12,14 +10,19 @@ import { ModalTrigger, } from '@janhq/uikit' +import { useAtomValue } from 'jotai' + +import useDownloadModel from '@/hooks/useDownloadModel' import { useDownloadState } from '@/hooks/useDownloadState' import { formatDownloadPercentage } from '@/utils/converter' -import { extensionManager } from '@/extension' +import { downloadingModelsAtom } from '@/helpers/atoms/Model.atom' export default function DownloadingState() { const { downloadStates } = useDownloadState() + const downloadingModels = useAtomValue(downloadingModelsAtom) + const { abortModelDownload } = useDownloadModel() const totalCurrentProgress = downloadStates .map((a) => a.size.transferred + a.size.transferred) @@ -73,9 +76,10 @@ export default function DownloadingState() { size="sm" onClick={() => { if (item?.modelId) { - extensionManager - .get(ExtensionType.Model) - ?.cancelModelDownload(item.modelId) + const model = downloadingModels.find( + (model) => model.id === item.modelId + ) + if (model) abortModelDownload(model) } }} > diff --git a/web/containers/ModalCancelDownload/index.tsx b/web/containers/ModalCancelDownload/index.tsx index d1a6f1a44..2a5626183 100644 --- a/web/containers/ModalCancelDownload/index.tsx +++ b/web/containers/ModalCancelDownload/index.tsx @@ -1,6 +1,5 @@ import { useMemo } from 'react' -import { ModelExtension, ExtensionType } from '@janhq/core' import { Model } from '@janhq/core' import { @@ -17,11 +16,12 @@ import { import { atom, useAtomValue } from 'jotai' +import useDownloadModel from '@/hooks/useDownloadModel' import { useDownloadState } from '@/hooks/useDownloadState' import { formatDownloadPercentage } from '@/utils/converter' -import { extensionManager } from '@/extension' +import { downloadingModelsAtom } from '@/helpers/atoms/Model.atom' type Props = { model: Model @@ -30,6 +30,7 @@ type Props = { export default function ModalCancelDownload({ model, isFromList }: Props) { const { modelDownloadStateAtom } = useDownloadState() + const downloadingModels = useAtomValue(downloadingModelsAtom) const downloadAtom = useMemo( () => atom((get) => get(modelDownloadStateAtom)[model.id]), // eslint-disable-next-line react-hooks/exhaustive-deps @@ -37,6 +38,7 @@ export default function ModalCancelDownload({ model, isFromList }: Props) { ) const downloadState = useAtomValue(downloadAtom) const cancelText = `Cancel ${formatDownloadPercentage(downloadState.percent)}` + const { abortModelDownload } = useDownloadModel() return ( @@ -80,9 +82,10 @@ export default function ModalCancelDownload({ model, isFromList }: Props) { themes="danger" onClick={() => { if (downloadState?.modelId) { - extensionManager - .get(ExtensionType.Model) - ?.cancelModelDownload(downloadState.modelId) + const model = downloadingModels.find( + (model) => model.id === downloadState.modelId + ) + if (model) abortModelDownload(model) } }} > diff --git a/web/containers/Providers/EventListener.tsx b/web/containers/Providers/EventListener.tsx index d73e5732d..046f2ecd2 100644 --- a/web/containers/Providers/EventListener.tsx +++ b/web/containers/Providers/EventListener.tsx @@ -1,34 +1,35 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { PropsWithChildren, useEffect, useRef } from 'react' +import { basename } from 'path' -import { ExtensionType } from '@janhq/core' -import { ModelExtension } from '@janhq/core' +import { PropsWithChildren, useEffect, useRef } from 'react' import { useAtomValue, useSetAtom } from 'jotai' import { useDownloadState } from '@/hooks/useDownloadState' import { useGetDownloadedModels } from '@/hooks/useGetDownloadedModels' +import { modelBinFileName } from '@/utils/model' + import EventHandler from './EventHandler' import { appDownloadProgress } from './Jotai' -import { extensionManager } from '@/extension/ExtensionManager' import { downloadingModelsAtom } from '@/helpers/atoms/Model.atom' export default function EventListenerWrapper({ children }: PropsWithChildren) { const setProgress = useSetAtom(appDownloadProgress) const models = useAtomValue(downloadingModelsAtom) const modelsRef = useRef(models) - useEffect(() => { - modelsRef.current = models - }, [models]) + const { setDownloadedModels, downloadedModels } = useGetDownloadedModels() const { setDownloadState, setDownloadStateSuccess, setDownloadStateFailed } = useDownloadState() const downloadedModelRef = useRef(downloadedModels) + useEffect(() => { + modelsRef.current = models + }, [models]) useEffect(() => { downloadedModelRef.current = downloadedModels }, [downloadedModels]) @@ -38,40 +39,36 @@ export default function EventListenerWrapper({ children }: PropsWithChildren) { window.electronAPI.onFileDownloadUpdate( (_event: string, state: any | undefined) => { if (!state) return - setDownloadState({ - ...state, - modelId: state.fileName.split('/').pop() ?? '', - }) + const model = modelsRef.current.find( + (model) => modelBinFileName(model) === basename(state.fileName) + ) + if (model) + setDownloadState({ + ...state, + modelId: model.id, + }) } ) - window.electronAPI.onFileDownloadError( - (_event: string, callback: any) => { - console.error('Download error', callback) - const modelId = callback.fileName.split('/').pop() ?? '' - setDownloadStateFailed(modelId) - } - ) + window.electronAPI.onFileDownloadError((_event: string, state: any) => { + console.error('Download error', state) + const model = modelsRef.current.find( + (model) => modelBinFileName(model) === basename(state.fileName) + ) + if (model) setDownloadStateFailed(model.id) + }) - window.electronAPI.onFileDownloadSuccess( - (_event: string, callback: any) => { - if (callback && callback.fileName) { - const modelId = callback.fileName.split('/').pop() ?? '' - - const model = modelsRef.current.find((e) => e.id === modelId) - - setDownloadStateSuccess(modelId) - - if (model) - extensionManager - .get(ExtensionType.Model) - ?.saveModel(model) - .then(() => { - setDownloadedModels([...downloadedModelRef.current, model]) - }) + window.electronAPI.onFileDownloadSuccess((_event: string, state: any) => { + if (state && state.fileName) { + const model = modelsRef.current.find( + (model) => modelBinFileName(model) === basename(state.fileName) + ) + if (model) { + setDownloadStateSuccess(model.id) + setDownloadedModels([...downloadedModelRef.current, model]) } } - ) + }) window.electronAPI.onAppUpdateDownloadUpdate( (_event: string, progress: any) => { diff --git a/web/hooks/useDownloadModel.ts b/web/hooks/useDownloadModel.ts index 7d5b2d1bd..bd587981c 100644 --- a/web/hooks/useDownloadModel.ts +++ b/web/hooks/useDownloadModel.ts @@ -1,7 +1,15 @@ -import { Model, ExtensionType, ModelExtension } from '@janhq/core' +import { + Model, + ExtensionType, + ModelExtension, + abortDownload, + joinPath, +} from '@janhq/core' import { useSetAtom } from 'jotai' +import { modelBinFileName } from '@/utils/model' + import { useDownloadState } from './useDownloadState' import { extensionManager } from '@/extension/ExtensionManager' @@ -33,8 +41,14 @@ export default function useDownloadModel() { .get(ExtensionType.Model) ?.downloadModel(model) } + const abortModelDownload = async (model: Model) => { + await abortDownload( + await joinPath(['models', model.id, modelBinFileName(model)]) + ) + } return { downloadModel, + abortModelDownload, } } diff --git a/web/hooks/useEngineSettings.ts b/web/hooks/useEngineSettings.ts index 50dcd0518..14f32d4b4 100644 --- a/web/hooks/useEngineSettings.ts +++ b/web/hooks/useEngineSettings.ts @@ -1,10 +1,10 @@ -import { join } from 'path' - -import { fs } from '@janhq/core' +import { fs, joinPath } from '@janhq/core' export const useEngineSettings = () => { const readOpenAISettings = async () => { - const settings = await fs.readFile(join('engines', 'openai.json')) + const settings = await fs.readFile( + await joinPath(['engines', 'openai.json']) + ) if (settings) { return JSON.parse(settings) } @@ -17,7 +17,10 @@ export const useEngineSettings = () => { }) => { const settings = await readOpenAISettings() settings.api_key = apiKey - await fs.writeFile(join('engines', 'openai.json'), JSON.stringify(settings)) + await fs.writeFile( + await joinPath(['engines', 'openai.json']), + JSON.stringify(settings) + ) } return { readOpenAISettings, saveOpenAISettings } } diff --git a/web/screens/Chat/ChatBody/index.tsx b/web/screens/Chat/ChatBody/index.tsx index 4d86e9e44..e1ae98b6e 100644 --- a/web/screens/Chat/ChatBody/index.tsx +++ b/web/screens/Chat/ChatBody/index.tsx @@ -116,7 +116,7 @@ const ChatBody: React.FC = () => { ) : ( {messages.map((message, index) => ( - <> +
{message.status === MessageStatus.Error && @@ -126,8 +126,8 @@ const ChatBody: React.FC = () => { className="mt-10 flex flex-col items-center" > - Oops! The generation was interrupted. Let's - give it another go! + Oops! The generation was interrupted. Let's give it + another go!
)} - +
))} )} diff --git a/web/utils/model.ts b/web/utils/model.ts new file mode 100644 index 000000000..5c5ef1264 --- /dev/null +++ b/web/utils/model.ts @@ -0,0 +1,12 @@ +import { basename } from 'path' + +import { Model } from '@janhq/core' + +export const modelBinFileName = (model: Model) => { + const modelFormatExt = '.gguf' + const extractedFileName = basename(model.source_url) ?? model.id + const fileName = extractedFileName.toLowerCase().endsWith(modelFormatExt) + ? extractedFileName + : model.id + return fileName +} diff --git a/web/utils/model_param.ts b/web/utils/model_param.ts index 3288fb40b..7d559c313 100644 --- a/web/utils/model_param.ts +++ b/web/utils/model_param.ts @@ -22,8 +22,7 @@ export const toRuntimeParams = ( for (const [key, value] of Object.entries(modelParams)) { if (key in defaultModelParams) { - // @ts-ignore - runtimeParams[key] = value + runtimeParams[key as keyof ModelRuntimeParams] = value } } @@ -46,8 +45,7 @@ export const toSettingParams = ( for (const [key, value] of Object.entries(modelParams)) { if (key in defaultSettingParams) { - // @ts-ignore - settingParams[key] = value + settingParams[key as keyof ModelSettingParams] = value } } From cfbc5674fe58229227254fc6313559bc45e2f947 Mon Sep 17 00:00:00 2001 From: hiento09 <136591877+hiento09@users.noreply.github.com> Date: Thu, 28 Dec 2023 15:34:11 +0700 Subject: [PATCH 3/4] Remove un-use resource checking to free ram on windows - monitor extension (#1241) Co-authored-by: Hien To --- extensions/inference-nitro-extension/src/module.ts | 6 +++--- extensions/monitoring-extension/src/module.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/extensions/inference-nitro-extension/src/module.ts b/extensions/inference-nitro-extension/src/module.ts index 37b9e5b3b..9d3d4452e 100644 --- a/extensions/inference-nitro-extension/src/module.ts +++ b/extensions/inference-nitro-extension/src/module.ts @@ -272,11 +272,11 @@ function spawnNitroProcess(nitroResourceProbe: any): Promise { function getResourcesInfo(): Promise { return new Promise(async (resolve) => { const cpu = await si.cpu(); - const mem = await si.mem(); + // const mem = await si.mem(); - const response = { + const response: ResourcesInfo = { numCpuPhysicalCore: cpu.physicalCores, - memAvailable: mem.available, + memAvailable: 0, }; resolve(response); }); diff --git a/extensions/monitoring-extension/src/module.ts b/extensions/monitoring-extension/src/module.ts index 6fe5881c2..cc10ea4f9 100644 --- a/extensions/monitoring-extension/src/module.ts +++ b/extensions/monitoring-extension/src/module.ts @@ -4,11 +4,11 @@ const getResourcesInfo = async () => new Promise(async (resolve) => { const cpu = await si.cpu(); const mem = await si.mem(); - const gpu = await si.graphics(); + // const gpu = await si.graphics(); const response = { cpu, mem, - gpu, + // gpu, }; resolve(response); }); From 5250061c11abb1b017f98a416912d6fddbcc731a Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 28 Dec 2023 17:57:39 +0700 Subject: [PATCH 4/4] feat: Jan Server, API and decoupled clients (#948) * chore: expose fs apis * chore: correct electron import path * update download api Signed-off-by: James * update chat_completion Signed-off-by: James * fix electron import Signed-off-by: James * feat: adding API support at 1337 (#991) Signed-off-by: James Co-authored-by: James * feat: Add /chat/completion api and handler * chore: add todo for modelList * chore: read engine.json for openai chat_completion (#1030) Signed-off-by: James Co-authored-by: James * refactor: move routes to shared node module * refactor: exported modules from core with types (#1172) * refactor: exported modules from core with types * fix: fix file reading args * refactor: fileManager handles * fix: app issues with server refactoring * refactor: shared server module (#1210) * chore: resolve main * chore: update makefile --------- Signed-off-by: James Co-authored-by: James Co-authored-by: NamH Co-authored-by: hiro --- Makefile | 1 + core/package.json | 21 ++ core/rollup.config.ts | 91 +++-- core/src/api/index.ts | 35 +- core/src/fs.ts | 77 ++-- core/src/node/api/HttpServer.ts | 8 + core/src/node/api/common/builder.ts | 335 ++++++++++++++++++ core/src/node/api/common/configuration.ts | 31 ++ core/src/node/api/index.ts | 2 + core/src/node/api/routes/common.ts | 42 +++ core/src/node/api/routes/download.ts | 54 +++ core/src/node/api/routes/extension.ts | 51 +++ core/src/node/api/routes/fs.ts | 27 ++ core/src/node/api/routes/index.ts | 6 + core/src/node/api/routes/thread.ts | 30 ++ core/src/node/api/routes/v1.ts | 21 ++ .../managers => core/src/node}/download.ts | 0 .../src/node}/extension/extension.ts | 26 +- .../src/node}/extension/index.ts | 49 ++- core/src/node/extension/manager.ts | 61 ++++ .../src/node}/extension/store.ts | 66 ++-- core/src/node/index.ts | 7 + .../managers => core/src/node}/module.ts | 13 +- core/tsconfig.json | 2 +- electron/handlers/app.ts | 10 +- electron/handlers/download.ts | 5 +- electron/handlers/extension.ts | 16 +- electron/handlers/fileManager.ts | 37 ++ electron/handlers/fs.ts | 242 +------------ electron/main.ts | 14 +- electron/managers/extension.ts | 85 ----- electron/package.json | 5 + electron/tsconfig.json | 3 + electron/utils/menu.ts | 3 +- electron/utils/migration.ts | 30 ++ extensions/assistant-extension/src/index.ts | 34 +- .../conversational-extension/src/index.ts | 63 ++-- .../inference-nitro-extension/src/index.ts | 20 +- .../inference-nitro-extension/src/module.ts | 4 + .../inference-openai-extension/src/index.ts | 17 +- .../src/index.ts | 16 +- extensions/model-extension/src/index.ts | 44 ++- package.json | 8 +- server/data/.gitkeep | 0 server/data/models/.gitkeep | 0 server/data/threads/.gitkeep | 0 server/icons/icon.png | Bin 38651 -> 0 bytes server/index.ts | 43 +++ server/lib/.gitkeep | 0 server/main.ts | 20 +- server/package.json | 27 +- server/tsconfig.json | 6 +- server/v1/assistants/index.ts | 8 - server/v1/chat/index.ts | 11 - server/v1/index.ts | 37 -- server/v1/models/downloadModel.ts | 23 -- server/v1/models/index.ts | 61 ---- server/v1/models/modelOp.ts | 11 - server/v1/threads/index.ts | 8 - web/extension/ExtensionManager.ts | 5 +- web/hooks/useEngineSettings.ts | 13 +- web/hooks/useSendChatMessage.ts | 3 +- web/next.config.js | 1 + web/screens/Chat/Sidebar/index.tsx | 2 + web/screens/Settings/Models/index.tsx | 2 +- web/services/cloudNativeService.ts | 76 ---- web/services/coreService.ts | 6 +- web/services/extensionService.ts | 7 +- web/services/restService.ts | 59 +++ web/types/index.d.ts | 1 + web/utils/json.ts | 9 + 71 files changed, 1261 insertions(+), 890 deletions(-) create mode 100644 core/src/node/api/HttpServer.ts create mode 100644 core/src/node/api/common/builder.ts create mode 100644 core/src/node/api/common/configuration.ts create mode 100644 core/src/node/api/index.ts create mode 100644 core/src/node/api/routes/common.ts create mode 100644 core/src/node/api/routes/download.ts create mode 100644 core/src/node/api/routes/extension.ts create mode 100644 core/src/node/api/routes/fs.ts create mode 100644 core/src/node/api/routes/index.ts create mode 100644 core/src/node/api/routes/thread.ts create mode 100644 core/src/node/api/routes/v1.ts rename {electron/managers => core/src/node}/download.ts (100%) rename {electron => core/src/node}/extension/extension.ts (90%) rename {electron => core/src/node}/extension/index.ts (72%) create mode 100644 core/src/node/extension/manager.ts rename {electron => core/src/node}/extension/store.ts (67%) create mode 100644 core/src/node/index.ts rename {electron/managers => core/src/node}/module.ts (59%) create mode 100644 electron/handlers/fileManager.ts delete mode 100644 electron/managers/extension.ts create mode 100644 electron/utils/migration.ts delete mode 100644 server/data/.gitkeep delete mode 100644 server/data/models/.gitkeep delete mode 100644 server/data/threads/.gitkeep delete mode 100644 server/icons/icon.png create mode 100644 server/index.ts delete mode 100644 server/lib/.gitkeep delete mode 100644 server/v1/assistants/index.ts delete mode 100644 server/v1/chat/index.ts delete mode 100644 server/v1/index.ts delete mode 100644 server/v1/models/downloadModel.ts delete mode 100644 server/v1/models/index.ts delete mode 100644 server/v1/models/modelOp.ts delete mode 100644 server/v1/threads/index.ts delete mode 100644 web/services/cloudNativeService.ts create mode 100644 web/services/restService.ts create mode 100644 web/utils/json.ts diff --git a/Makefile b/Makefile index b0f5ed543..a61ae92bb 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ ifeq ($(OS),Windows_NT) yarn config set network-timeout 300000 endif yarn build:core + yarn build:server yarn install yarn build:extensions diff --git a/core/package.json b/core/package.json index a599fc3c2..ce0714c27 100644 --- a/core/package.json +++ b/core/package.json @@ -22,6 +22,27 @@ "engines": { "node": ">=6.0.0" }, + "exports": { + ".": "./dist/core.umd.js", + "./sdk": "./dist/core.umd.js", + "./node": "./dist/node/index.cjs.js" + }, + "typesVersions": { + "*": { + ".": [ + "./dist/core.es5.js.map", + "./dist/types/index.d.ts" + ], + "sdk": [ + "./dist/core.es5.js.map", + "./dist/types/index.d.ts" + ], + "node": [ + "./dist/node/index.cjs.js.map", + "./dist/types/node/index.d.ts" + ] + } + }, "scripts": { "lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'", "prebuild": "rimraf dist", diff --git a/core/rollup.config.ts b/core/rollup.config.ts index 5e1762c96..d982db659 100644 --- a/core/rollup.config.ts +++ b/core/rollup.config.ts @@ -8,30 +8,69 @@ const pkg = require('./package.json') const libraryName = 'core' -export default { - input: `src/index.ts`, - output: [ - { file: pkg.main, name: libraryName, format: 'umd', sourcemap: true }, - { file: pkg.module, format: 'es', sourcemap: true }, - ], - // Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash') - external: [], - watch: { - include: 'src/**', - }, - plugins: [ - // Allow json resolution - json(), - // Compile TypeScript files - typescript({ useTsconfigDeclarationDir: true }), - // Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs) - commonjs(), - // Allow node_modules resolution, so you can use 'external' to control - // which external modules to include in the bundle - // https://github.com/rollup/rollup-plugin-node-resolve#usage - resolve(), +export default [ + { + input: `src/index.ts`, + output: [ + { file: pkg.main, name: libraryName, format: 'umd', sourcemap: true }, + { file: pkg.module, format: 'es', sourcemap: true }, + ], + // Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash') + external: ['path'], + watch: { + include: 'src/**', + }, + plugins: [ + // Allow json resolution + json(), + // Compile TypeScript files + typescript({ useTsconfigDeclarationDir: true }), + // Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs) + commonjs(), + // Allow node_modules resolution, so you can use 'external' to control + // which external modules to include in the bundle + // https://github.com/rollup/rollup-plugin-node-resolve#usage + resolve(), - // Resolve source maps to the original source - sourceMaps(), - ], -} + // Resolve source maps to the original source + sourceMaps(), + ], + }, + { + input: `src/node/index.ts`, + output: [{ file: 'dist/node/index.cjs.js', format: 'cjs', sourcemap: true }], + // Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash') + external: [ + 'fs/promises', + 'path', + 'pacote', + '@types/pacote', + '@npmcli/arborist', + 'ulid', + 'node-fetch', + 'fs', + 'request', + 'crypto', + 'url', + 'http', + ], + watch: { + include: 'src/node/**', + }, + plugins: [ + // Allow json resolution + json(), + // Compile TypeScript files + typescript({ useTsconfigDeclarationDir: true }), + // Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs) + commonjs(), + // Allow node_modules resolution, so you can use 'external' to control + // which external modules to include in the bundle + // https://github.com/rollup/rollup-plugin-node-resolve#usage + resolve(), + + // Resolve source maps to the original source + sourceMaps(), + ], + }, +] diff --git a/core/src/api/index.ts b/core/src/api/index.ts index 4f2b45f5f..23d5f6f87 100644 --- a/core/src/api/index.ts +++ b/core/src/api/index.ts @@ -5,7 +5,6 @@ export enum AppRoute { appDataPath = 'appDataPath', appVersion = 'appVersion', - getResourcePath = 'getResourcePath', openExternalUrl = 'openExternalUrl', openAppDirectory = 'openAppDirectory', openFileExplore = 'openFileExplorer', @@ -41,20 +40,20 @@ export enum ExtensionRoute { uninstallExtension = 'uninstallExtension', } export enum FileSystemRoute { - appendFile = 'appendFile', - copyFile = 'copyFile', - syncFile = 'syncFile', - deleteFile = 'deleteFile', - exists = 'exists', - getResourcePath = 'getResourcePath', + appendFileSync = 'appendFileSync', + copyFileSync = 'copyFileSync', + unlinkSync = 'unlinkSync', + existsSync = 'existsSync', + readdirSync = 'readdirSync', + mkdirSync = 'mkdirSync', + readFileSync = 'readFileSync', + rmdirSync = 'rmdirSync', + writeFileSync = 'writeFileSync', +} +export enum FileManagerRoute { + synceFile = 'syncFile', getUserSpace = 'getUserSpace', - isDirectory = 'isDirectory', - listFiles = 'listFiles', - mkdir = 'mkdir', - readFile = 'readFile', - readLineByLine = 'readLineByLine', - rmdir = 'rmdir', - writeFile = 'writeFile', + getResourcePath = 'getResourcePath', } export type ApiFunction = (...args: any[]) => any @@ -83,17 +82,23 @@ export type FileSystemRouteFunctions = { [K in FileSystemRoute]: ApiFunction } +export type FileManagerRouteFunctions = { + [K in FileManagerRoute]: ApiFunction +} + export type APIFunctions = AppRouteFunctions & AppEventFunctions & DownloadRouteFunctions & DownloadEventFunctions & ExtensionRouteFunctions & - FileSystemRouteFunctions + FileSystemRouteFunctions & + FileManagerRoute export const APIRoutes = [ ...Object.values(AppRoute), ...Object.values(DownloadRoute), ...Object.values(ExtensionRoute), ...Object.values(FileSystemRoute), + ...Object.values(FileManagerRoute), ] export const APIEvents = [...Object.values(AppEvent), ...Object.values(DownloadEvent)] diff --git a/core/src/fs.ts b/core/src/fs.ts index 6ada7d3e2..34d915ef1 100644 --- a/core/src/fs.ts +++ b/core/src/fs.ts @@ -1,89 +1,74 @@ /** * Writes data to a file at the specified path. - * @param {string} path - The path to the file. - * @param {string} data - The data to write to the file. * @returns {Promise} A Promise that resolves when the file is written successfully. */ -const writeFile: (path: string, data: string) => Promise = (path, data) => - global.core.api?.writeFile(path, data) - -/** - * 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 => global.core.api?.isDirectory(path) +const writeFileSync = (...args: any[]) => global.core.api?.writeFileSync(...args) /** * Reads the contents of a file at the specified path. - * @param {string} path - The path of the file to read. * @returns {Promise} A Promise that resolves with the contents of the file. */ -const readFile: (path: string) => Promise = (path) => global.core.api?.readFile(path) +const readFileSync = (...args: any[]) => global.core.api?.readFileSync(...args) /** * Check whether the file exists * @param {string} path * @returns {boolean} A boolean indicating whether the path is a file. */ -const exists = (path: string): Promise => global.core.api?.exists(path) +const existsSync = (...args: any[]) => global.core.api?.existsSync(...args) /** * List the directory files - * @param {string} path - The path of the directory to list files. * @returns {Promise} A Promise that resolves with the contents of the directory. */ -const listFiles: (path: string) => Promise = (path) => global.core.api?.listFiles(path) +const readdirSync = (...args: any[]) => global.core.api?.readdirSync(...args) /** * Creates a directory at the specified path. - * @param {string} path - The path of the directory to create. * @returns {Promise} A Promise that resolves when the directory is created successfully. */ -const mkdir: (path: string) => Promise = (path) => global.core.api?.mkdir(path) +const mkdirSync = (...args: any[]) => global.core.api?.mkdirSync(...args) /** * Removes a directory at the specified path. - * @param {string} path - The path of the directory to remove. * @returns {Promise} A Promise that resolves when the directory is removed successfully. */ -const rmdir: (path: string) => Promise = (path) => global.core.api?.rmdir(path) +const rmdirSync = (...args: any[]) => + global.core.api?.rmdirSync(...args, { recursive: true, force: true }) /** * Deletes a file from the local file system. * @param {string} path - The path of the file to delete. * @returns {Promise} A Promise that resolves when the file is deleted. */ -const deleteFile: (path: string) => Promise = (path) => global.core.api?.deleteFile(path) +const unlinkSync = (...args: any[]) => global.core.api?.unlinkSync(...args) /** * Appends data to a file at the specified path. - * @param path path to the file - * @param data data to append */ -const appendFile: (path: string, data: string) => Promise = (path, data) => - global.core.api?.appendFile(path, data) - -const copyFile: (src: string, dest: string) => Promise = (src, dest) => - global.core.api?.copyFile(src, dest) +const appendFileSync = (...args: any[]) => global.core.api?.appendFileSync(...args) +/** + * Synchronizes a file from a source path to a destination path. + * @param {string} src - The source path of the file to be synchronized. + * @param {string} dest - The destination path where the file will be synchronized to. + * @returns {Promise} - A promise that resolves when the file has been successfully synchronized. + */ const syncFile: (src: string, dest: string) => Promise = (src, dest) => global.core.api?.syncFile(src, dest) -/** - * Reads a file line by line. - * @param {string} path - The path of the file to read. - * @returns {Promise} A promise that resolves to the lines of the file. - */ -const readLineByLine: (path: string) => Promise = (path) => - global.core.api?.readLineByLine(path) +/** + * Copy file sync. + */ +const copyFileSync = (...args: any[]) => global.core.api?.copyFileSync(...args) + +// TODO: Export `dummy` fs functions automatically +// Currently adding these manually export const fs = { - isDirectory, - writeFile, - readFile, - exists, - listFiles, - mkdir, - rmdir, - deleteFile, - appendFile, - readLineByLine, - copyFile, + writeFileSync, + readFileSync, + existsSync, + readdirSync, + mkdirSync, + rmdirSync, + unlinkSync, + appendFileSync, + copyFileSync, syncFile, } diff --git a/core/src/node/api/HttpServer.ts b/core/src/node/api/HttpServer.ts new file mode 100644 index 000000000..32d597717 --- /dev/null +++ b/core/src/node/api/HttpServer.ts @@ -0,0 +1,8 @@ +export interface HttpServer { + post: (route: string, handler: (req: any, res: any) => Promise) => void + get: (route: string, handler: (req: any, res: any) => Promise) => void + patch: (route: string, handler: (req: any, res: any) => Promise) => void + put: (route: string, handler: (req: any, res: any) => Promise) => void + delete: (route: string, handler: (req: any, res: any) => Promise) => void + register: (router: any, opts?: any) => void +} diff --git a/core/src/node/api/common/builder.ts b/core/src/node/api/common/builder.ts new file mode 100644 index 000000000..4081e859f --- /dev/null +++ b/core/src/node/api/common/builder.ts @@ -0,0 +1,335 @@ +import fs from 'fs' +import { JanApiRouteConfiguration, RouteConfiguration } from './configuration' +import { join } from 'path' +import { Model, ThreadMessage } from './../../../index' + +import fetch from 'node-fetch' +import { ulid } from 'ulid' +import request from 'request' + +const progress = require('request-progress') +const os = require('os') + +const path = join(os.homedir(), 'jan') + +export const getBuilder = async (configuration: RouteConfiguration) => { + const directoryPath = join(path, configuration.dirName) + + try { + if (!(await fs.existsSync(directoryPath))) { + console.debug('model folder not found') + return [] + } + + const files: string[] = await fs.readdirSync(directoryPath) + + const allDirectories: string[] = [] + for (const file of files) { + if (file === '.DS_Store') continue + allDirectories.push(file) + } + + const readJsonPromises = allDirectories.map(async (dirName) => { + const jsonPath = join(directoryPath, dirName, configuration.metadataFileName) + return await readModelMetadata(jsonPath) + }) + + const results = await Promise.all(readJsonPromises) + const modelData = results + .map((result: any) => { + try { + return JSON.parse(result) + } catch (err) { + console.error(err) + } + }) + .filter((e: any) => !!e) + + return modelData + } catch (err) { + console.error(err) + return [] + } +} + +const readModelMetadata = async (path: string) => { + return fs.readFileSync(path, 'utf-8') +} + +export const retrieveBuilder = async (configuration: RouteConfiguration, id: string) => { + const data = await getBuilder(configuration) + const filteredData = data.filter((d: any) => d.id === id)[0] + + if (!filteredData) { + return {} + } + + return filteredData +} + +export const deleteBuilder = async (configuration: RouteConfiguration, id: string) => { + if (configuration.dirName === 'assistants' && id === 'jan') { + return { + message: 'Cannot delete Jan assistant', + } + } + + const directoryPath = join(path, configuration.dirName) + try { + const data = await retrieveBuilder(configuration, id) + if (!data || !data.keys) { + return { + message: 'Not found', + } + } + + const myPath = join(directoryPath, id) + fs.rmdirSync(myPath, { recursive: true }) + return { + id: id, + object: configuration.delete.object, + deleted: true, + } + } catch (ex) { + console.error(ex) + } +} + +export const getMessages = async (threadId: string) => { + const threadDirPath = join(path, 'threads', threadId) + const messageFile = 'messages.jsonl' + try { + const files: string[] = await fs.readdirSync(threadDirPath) + if (!files.includes(messageFile)) { + throw Error(`${threadDirPath} not contains message file`) + } + + const messageFilePath = join(threadDirPath, messageFile) + + const lines = fs + .readFileSync(messageFilePath, 'utf-8') + .toString() + .split('\n') + .filter((line: any) => line !== '') + + const messages: ThreadMessage[] = [] + lines.forEach((line: string) => { + messages.push(JSON.parse(line) as ThreadMessage) + }) + return messages + } catch (err) { + console.error(err) + return [] + } +} + +export const retrieveMesasge = async (threadId: string, messageId: string) => { + const messages = await getMessages(threadId) + const filteredMessages = messages.filter((m) => m.id === messageId) + if (!filteredMessages || filteredMessages.length === 0) { + return { + message: 'Not found', + } + } + + return filteredMessages[0] +} + +export const createThread = async (thread: any) => { + const threadMetadataFileName = 'thread.json' + // TODO: add validation + if (!thread.assistants || thread.assistants.length === 0) { + return { + message: 'Thread must have at least one assistant', + } + } + + const threadId = generateThreadId(thread.assistants[0].assistant_id) + try { + const updatedThread = { + ...thread, + id: threadId, + created: Date.now(), + updated: Date.now(), + } + const threadDirPath = join(path, 'threads', updatedThread.id) + const threadJsonPath = join(threadDirPath, threadMetadataFileName) + + if (!fs.existsSync(threadDirPath)) { + fs.mkdirSync(threadDirPath) + } + + await fs.writeFileSync(threadJsonPath, JSON.stringify(updatedThread, null, 2)) + return updatedThread + } catch (err) { + return { + error: err, + } + } +} + +export const updateThread = async (threadId: string, thread: any) => { + const threadMetadataFileName = 'thread.json' + const currentThreadData = await retrieveBuilder(JanApiRouteConfiguration.threads, threadId) + if (!currentThreadData) { + return { + message: 'Thread not found', + } + } + // we don't want to update the id and object + delete thread.id + delete thread.object + + const updatedThread = { + ...currentThreadData, + ...thread, + updated: Date.now(), + } + try { + const threadDirPath = join(path, 'threads', updatedThread.id) + const threadJsonPath = join(threadDirPath, threadMetadataFileName) + + await fs.writeFileSync(threadJsonPath, JSON.stringify(updatedThread, null, 2)) + return updatedThread + } catch (err) { + return { + message: err, + } + } +} + +const generateThreadId = (assistantId: string) => { + return `${assistantId}_${(Date.now() / 1000).toFixed(0)}` +} + +export const createMessage = async (threadId: string, message: any) => { + const threadMessagesFileName = 'messages.jsonl' + // TODO: add validation + try { + const msgId = ulid() + const createdAt = Date.now() + const threadMessage: ThreadMessage = { + ...message, + id: msgId, + thread_id: threadId, + created: createdAt, + updated: createdAt, + object: 'thread.message', + } + + const threadDirPath = join(path, 'threads', threadId) + const threadMessagePath = join(threadDirPath, threadMessagesFileName) + + if (!fs.existsSync(threadDirPath)) { + fs.mkdirSync(threadDirPath) + } + fs.appendFileSync(threadMessagePath, JSON.stringify(threadMessage) + '\n') + return threadMessage + } catch (err) { + return { + message: err, + } + } +} + +export const downloadModel = async (modelId: string) => { + const model = await retrieveBuilder(JanApiRouteConfiguration.models, modelId) + if (!model || model.object !== 'model') { + return { + message: 'Model not found', + } + } + + const directoryPath = join(path, 'models', modelId) + if (!fs.existsSync(directoryPath)) { + fs.mkdirSync(directoryPath) + } + + // path to model binary + const modelBinaryPath = join(directoryPath, modelId) + const rq = request(model.source_url) + + progress(rq, {}) + .on('progress', function (state: any) { + console.log('progress', JSON.stringify(state, null, 2)) + }) + .on('error', function (err: Error) { + console.error('error', err) + }) + .on('end', function () { + console.log('end') + }) + .pipe(fs.createWriteStream(modelBinaryPath)) + + return { + message: `Starting download ${modelId}`, + } +} + +export const chatCompletions = async (request: any, reply: any) => { + const modelList = await getBuilder(JanApiRouteConfiguration.models) + const modelId = request.body.model + + const matchedModels = modelList.filter((model: Model) => model.id === modelId) + if (matchedModels.length === 0) { + const error = { + error: { + message: `The model ${request.body.model} does not exist`, + type: 'invalid_request_error', + param: null, + code: 'model_not_found', + }, + } + reply.code(404).send(error) + return + } + + const requestedModel = matchedModels[0] + const engineConfiguration = await getEngineConfiguration(requestedModel.engine) + + let apiKey: string | undefined = undefined + let apiUrl: string = 'http://127.0.0.1:3928/inferences/llamacpp/chat_completion' // default nitro url + + if (engineConfiguration) { + apiKey = engineConfiguration.api_key + apiUrl = engineConfiguration.full_url + } + + reply.raw.writeHead(200, { + 'Content-Type': 'text/event-stream', + 'Cache-Control': 'no-cache', + Connection: 'keep-alive', + }) + + const headers: Record = { + 'Content-Type': 'application/json', + } + + if (apiKey) { + headers['Authorization'] = `Bearer ${apiKey}` + headers['api-key'] = apiKey + } + console.log(apiUrl) + console.log(JSON.stringify(headers)) + const response = await fetch(apiUrl, { + method: 'POST', + headers: headers, + body: JSON.stringify(request.body), + }) + if (response.status !== 200) { + console.error(response) + return + } else { + response.body.pipe(reply.raw) + } +} + +const getEngineConfiguration = async (engineId: string) => { + if (engineId !== 'openai') { + return undefined + } + const directoryPath = join(path, 'engines') + const filePath = join(directoryPath, `${engineId}.json`) + const data = await fs.readFileSync(filePath, 'utf-8') + return JSON.parse(data) +} diff --git a/core/src/node/api/common/configuration.ts b/core/src/node/api/common/configuration.ts new file mode 100644 index 000000000..88e5ffb61 --- /dev/null +++ b/core/src/node/api/common/configuration.ts @@ -0,0 +1,31 @@ +export const JanApiRouteConfiguration: Record = { + models: { + dirName: 'models', + metadataFileName: 'model.json', + delete: { + object: 'model', + }, + }, + assistants: { + dirName: 'assistants', + metadataFileName: 'assistant.json', + delete: { + object: 'assistant', + }, + }, + threads: { + dirName: 'threads', + metadataFileName: 'thread.json', + delete: { + object: 'thread', + }, + }, +} + +export type RouteConfiguration = { + dirName: string + metadataFileName: string + delete: { + object: string + } +} diff --git a/core/src/node/api/index.ts b/core/src/node/api/index.ts new file mode 100644 index 000000000..4c3041ba3 --- /dev/null +++ b/core/src/node/api/index.ts @@ -0,0 +1,2 @@ +export * from './HttpServer' +export * from './routes' diff --git a/core/src/node/api/routes/common.ts b/core/src/node/api/routes/common.ts new file mode 100644 index 000000000..194429c66 --- /dev/null +++ b/core/src/node/api/routes/common.ts @@ -0,0 +1,42 @@ +import { AppRoute } from '../../../api' +import { HttpServer } from '../HttpServer' +import { join } from 'path' +import { + chatCompletions, + deleteBuilder, + downloadModel, + getBuilder, + retrieveBuilder, +} from '../common/builder' + +import { JanApiRouteConfiguration } from '../common/configuration' + +export const commonRouter = async (app: HttpServer) => { + // Common Routes + Object.keys(JanApiRouteConfiguration).forEach((key) => { + app.get(`/${key}`, async (_request) => getBuilder(JanApiRouteConfiguration[key])) + + app.get(`/${key}/:id`, async (request: any) => + retrieveBuilder(JanApiRouteConfiguration[key], request.params.id), + ) + + app.delete(`/${key}/:id`, async (request: any) => + deleteBuilder(JanApiRouteConfiguration[key], request.params.id), + ) + }) + + // Download Model Routes + app.get(`/models/download/:modelId`, async (request: any) => + downloadModel(request.params.modelId), + ) + + // Chat Completion Routes + app.post(`/chat/completions`, async (request: any, reply: any) => chatCompletions(request, reply)) + + // App Routes + app.post(`/app/${AppRoute.joinPath}`, async (request: any, reply: any) => { + const args = JSON.parse(request.body) as any[] + console.debug('joinPath: ', ...args[0]) + reply.send(JSON.stringify(join(...args[0]))) + }) +} diff --git a/core/src/node/api/routes/download.ts b/core/src/node/api/routes/download.ts new file mode 100644 index 000000000..26945bada --- /dev/null +++ b/core/src/node/api/routes/download.ts @@ -0,0 +1,54 @@ +import { DownloadRoute } from '../../../api' +import { join } from 'path' +import { userSpacePath, DownloadManager, HttpServer } from '../../index' +import { createWriteStream } from 'fs' + +const request = require('request') +const progress = require('request-progress') + +export const downloadRouter = async (app: HttpServer) => { + app.post(`/${DownloadRoute.downloadFile}`, async (req, res) => { + const body = JSON.parse(req.body as any) + const normalizedArgs = body.map((arg: any) => { + if (typeof arg === 'string' && arg.includes('file:/')) { + return join(userSpacePath, arg.replace('file:/', '')) + } + return arg + }) + + const localPath = normalizedArgs[1] + const fileName = localPath.split('/').pop() ?? '' + + const rq = request(normalizedArgs[0]) + progress(rq, {}) + .on('progress', function (state: any) { + console.log('download onProgress', state) + }) + .on('error', function (err: Error) { + console.log('download onError', err) + }) + .on('end', function () { + console.log('download onEnd') + }) + .pipe(createWriteStream(normalizedArgs[1])) + + DownloadManager.instance.setRequest(fileName, rq) + }) + + app.post(`/${DownloadRoute.abortDownload}`, async (req, res) => { + const body = JSON.parse(req.body as any) + const normalizedArgs = body.map((arg: any) => { + if (typeof arg === 'string' && arg.includes('file:/')) { + return join(userSpacePath, arg.replace('file:/', '')) + } + return arg + }) + + const localPath = normalizedArgs[0] + const fileName = localPath.split('/').pop() ?? '' + console.debug('fileName', fileName) + const rq = DownloadManager.instance.networkRequests[fileName] + DownloadManager.instance.networkRequests[fileName] = undefined + rq?.abort() + }) +} diff --git a/core/src/node/api/routes/extension.ts b/core/src/node/api/routes/extension.ts new file mode 100644 index 000000000..abd6a6f0c --- /dev/null +++ b/core/src/node/api/routes/extension.ts @@ -0,0 +1,51 @@ +import { join, extname } from 'path' +import { ExtensionRoute } from '../../../api' +import { + userSpacePath, + ModuleManager, + getActiveExtensions, + installExtensions, + HttpServer, +} from '../../index' +import { readdirSync } from 'fs' + +export const extensionRouter = async (app: HttpServer) => { + // TODO: Share code between node projects + app.post(`/${ExtensionRoute.getActiveExtensions}`, async (req, res) => { + const activeExtensions = await getActiveExtensions() + res.status(200).send(activeExtensions) + }) + + app.post(`/${ExtensionRoute.baseExtensions}`, async (req, res) => { + const baseExtensionPath = join(__dirname, '..', '..', '..', 'pre-install') + const extensions = readdirSync(baseExtensionPath) + .filter((file) => extname(file) === '.tgz') + .map((file) => join(baseExtensionPath, file)) + + res.status(200).send(extensions) + }) + + app.post(`/${ExtensionRoute.installExtension}`, async (req, res) => { + const extensions = req.body as any + const installed = await installExtensions(JSON.parse(extensions)[0]) + return JSON.parse(JSON.stringify(installed)) + }) + + app.post(`/${ExtensionRoute.invokeExtensionFunc}`, async (req, res) => { + const args = JSON.parse(req.body as any) + console.debug(args) + const module = await import(join(userSpacePath, 'extensions', args[0])) + + ModuleManager.instance.setModule(args[0], module) + const method = args[1] + if (typeof module[method] === 'function') { + // remove first item from args + const newArgs = args.slice(2) + console.log(newArgs) + return module[method](...args.slice(2)) + } else { + console.debug(module[method]) + console.error(`Function "${method}" does not exist in the module.`) + } + }) +} diff --git a/core/src/node/api/routes/fs.ts b/core/src/node/api/routes/fs.ts new file mode 100644 index 000000000..f0cf1eeed --- /dev/null +++ b/core/src/node/api/routes/fs.ts @@ -0,0 +1,27 @@ +import { FileSystemRoute } from '../../../api' +import { join } from 'path' +import { HttpServer, userSpacePath } from '../../index' + +export const fsRouter = async (app: HttpServer) => { + const moduleName = 'fs' + // Generate handlers for each fs route + Object.values(FileSystemRoute).forEach((route) => { + app.post(`/${route}`, async (req, res) => { + const body = JSON.parse(req.body as any) + try { + const result = await import(moduleName).then((mdl) => { + return mdl[route]( + ...body.map((arg: any) => + typeof arg === 'string' && arg.includes('file:/') + ? join(userSpacePath, arg.replace('file:/', '')) + : arg, + ), + ) + }) + res.status(200).send(result) + } catch (ex) { + console.log(ex) + } + }) + }) +} diff --git a/core/src/node/api/routes/index.ts b/core/src/node/api/routes/index.ts new file mode 100644 index 000000000..e6edc62f7 --- /dev/null +++ b/core/src/node/api/routes/index.ts @@ -0,0 +1,6 @@ +export * from './download' +export * from './extension' +export * from './fs' +export * from './thread' +export * from './common' +export * from './v1' diff --git a/core/src/node/api/routes/thread.ts b/core/src/node/api/routes/thread.ts new file mode 100644 index 000000000..4066d2716 --- /dev/null +++ b/core/src/node/api/routes/thread.ts @@ -0,0 +1,30 @@ +import { HttpServer } from '../HttpServer' +import { + createMessage, + createThread, + getMessages, + retrieveMesasge, + updateThread, +} from '../common/builder' + +export const threadRouter = async (app: HttpServer) => { + // create thread + app.post(`/`, async (req, res) => createThread(req.body)) + + app.get(`/:threadId/messages`, async (req, res) => getMessages(req.params.threadId)) + + // retrieve message + app.get(`/:threadId/messages/:messageId`, async (req, res) => + retrieveMesasge(req.params.threadId, req.params.messageId), + ) + + // create message + app.post(`/:threadId/messages`, async (req, res) => + createMessage(req.params.threadId as any, req.body as any), + ) + + // modify thread + app.patch(`/:threadId`, async (request: any) => + updateThread(request.params.threadId, request.body), + ) +} diff --git a/core/src/node/api/routes/v1.ts b/core/src/node/api/routes/v1.ts new file mode 100644 index 000000000..4f637e392 --- /dev/null +++ b/core/src/node/api/routes/v1.ts @@ -0,0 +1,21 @@ +import { HttpServer } from '../HttpServer' +import { commonRouter, threadRouter, fsRouter, extensionRouter, downloadRouter } from './index' + +export const v1Router = async (app: HttpServer) => { + // MARK: External Routes + app.register(commonRouter) + app.register(threadRouter, { + prefix: '/thread', + }) + + // MARK: Internal Application Routes + app.register(fsRouter, { + prefix: '/fs', + }) + app.register(extensionRouter, { + prefix: '/extension', + }) + app.register(downloadRouter, { + prefix: '/download', + }) +} diff --git a/electron/managers/download.ts b/core/src/node/download.ts similarity index 100% rename from electron/managers/download.ts rename to core/src/node/download.ts diff --git a/electron/extension/extension.ts b/core/src/node/extension/extension.ts similarity index 90% rename from electron/extension/extension.ts rename to core/src/node/extension/extension.ts index 1bd11611d..0460eca5e 100644 --- a/electron/extension/extension.ts +++ b/core/src/node/extension/extension.ts @@ -1,14 +1,14 @@ -import { rmdir } from 'fs/promises' +import { rmdirSync } from 'fs' import { resolve, join } from 'path' import { manifest, extract } from 'pacote' import * as Arborist from '@npmcli/arborist' -import { ExtensionManager } from './../managers/extension' +import { ExtensionManager } from './manager' /** * An NPM package that can be used as an extension. * Used to hold all the information and functions necessary to handle the extension lifecycle. */ -class Extension { +export default class Extension { /** * @property {string} origin Original specification provided to fetch the package. * @property {Object} installOptions Options provided to pacote when fetching the manifest. @@ -56,10 +56,7 @@ class Extension { * @type {string} */ get specifier() { - return ( - this.origin + - (this.installOptions.version ? '@' + this.installOptions.version : '') - ) + return this.origin + (this.installOptions.version ? '@' + this.installOptions.version : '') } /** @@ -85,9 +82,7 @@ class Extension { this.main = mnf.main this.description = mnf.description } catch (error) { - throw new Error( - `Package ${this.origin} does not contain a valid manifest: ${error}` - ) + throw new Error(`Package ${this.origin} does not contain a valid manifest: ${error}`) } return true @@ -107,7 +102,7 @@ class Extension { await extract( this.specifier, join(ExtensionManager.instance.extensionsPath ?? '', this.name ?? ''), - this.installOptions + this.installOptions, ) // Set the url using the custom extensions protocol @@ -180,11 +175,8 @@ class Extension { * @returns {Promise} */ async uninstall() { - const extPath = resolve( - ExtensionManager.instance.extensionsPath ?? '', - this.name ?? '' - ) - await rmdir(extPath, { recursive: true }) + const extPath = resolve(ExtensionManager.instance.extensionsPath ?? '', this.name ?? '') + await rmdirSync(extPath, { recursive: true }) this.emitUpdate() } @@ -200,5 +192,3 @@ class Extension { return this } } - -export default Extension diff --git a/electron/extension/index.ts b/core/src/node/extension/index.ts similarity index 72% rename from electron/extension/index.ts rename to core/src/node/extension/index.ts index c6a6cc0c0..e685fc0ae 100644 --- a/electron/extension/index.ts +++ b/core/src/node/extension/index.ts @@ -1,5 +1,5 @@ import { readFileSync } from 'fs' -import { protocol } from 'electron' + import { normalize } from 'path' import Extension from './extension' @@ -12,18 +12,8 @@ import { getActiveExtensions, addExtension, } from './store' -import { ExtensionManager } from './../managers/extension' +import { ExtensionManager } from './manager' -/** - * Sets up the required communication between the main and renderer processes. - * Additionally sets the extensions up using {@link useExtensions} if a extensionsPath is provided. - * @param {Object} options configuration for setting up the renderer facade. - * @param {confirmInstall} [options.confirmInstall] Function to validate that a extension should be installed. - * @param {Boolean} [options.useFacade=true] Whether to make a facade to the extensions available in the renderer. - * @param {string} [options.extensionsPath] Optional path to the extensions folder. - * @returns {extensionManager|Object} A set of functions used to manage the extension lifecycle if useExtensions is provided. - * @function - */ export function init(options: any) { // Create extensions protocol to serve extensions to renderer registerExtensionProtocol() @@ -41,13 +31,24 @@ export function init(options: any) { * @private * @returns {boolean} Whether the protocol registration was successful */ -function registerExtensionProtocol() { - return protocol.registerFileProtocol('extension', (request, callback) => { - const entry = request.url.substr('extension://'.length - 1) +async function registerExtensionProtocol() { + let electron: any = undefined - const url = normalize(ExtensionManager.instance.extensionsPath + entry) - callback({ path: url }) - }) + try { + const moduleName = "electron" + electron = await import(moduleName) + } catch (err) { + console.error('Electron is not available') + } + + if (electron) { + return electron.protocol.registerFileProtocol('extension', (request: any, callback: any) => { + const entry = request.url.substr('extension://'.length - 1) + + const url = normalize(ExtensionManager.instance.extensionsPath + entry) + callback({ path: url }) + }) + } } /** @@ -57,8 +58,7 @@ function registerExtensionProtocol() { * @returns {extensionManager} A set of functions used to manage the extension lifecycle. */ export function useExtensions(extensionsPath: string) { - if (!extensionsPath) - throw Error('A path to the extensions folder is required to use extensions') + if (!extensionsPath) throw Error('A path to the extensions folder is required to use extensions') // Store the path to the extensions folder ExtensionManager.instance.setExtensionsPath(extensionsPath) @@ -69,7 +69,7 @@ export function useExtensions(extensionsPath: string) { // Read extension list from extensions folder const extensions = JSON.parse( - readFileSync(ExtensionManager.instance.getExtensionsFile(), 'utf-8') + readFileSync(ExtensionManager.instance.getExtensionsFile(), 'utf-8'), ) try { // Create and store a Extension instance for each extension in list @@ -82,7 +82,7 @@ export function useExtensions(extensionsPath: string) { throw new Error( 'Could not successfully rebuild list of installed extensions.\n' + error + - '\nPlease check the extensions.json file in the extensions folder.' + '\nPlease check the extensions.json file in the extensions folder.', ) } @@ -111,7 +111,6 @@ function loadExtension(ext: any) { }) } } - addExtension(extension, false) extension.subscribe('pe-persist', persistExtensions) } @@ -123,7 +122,7 @@ function loadExtension(ext: any) { export function getStore() { if (!ExtensionManager.instance.extensionsPath) { throw new Error( - 'The extension path has not yet been set up. Please run useExtensions before accessing the store' + 'The extension path has not yet been set up. Please run useExtensions before accessing the store', ) } @@ -134,4 +133,4 @@ export function getStore() { getActiveExtensions, removeExtension, } -} +} \ No newline at end of file diff --git a/core/src/node/extension/manager.ts b/core/src/node/extension/manager.ts new file mode 100644 index 000000000..9173504a7 --- /dev/null +++ b/core/src/node/extension/manager.ts @@ -0,0 +1,61 @@ +import { join, resolve } from "path"; + +import { existsSync, mkdirSync, writeFileSync } from "fs"; +import { init } from "./index"; +import { homedir } from "os" +/** + * Manages extension installation and migration. + */ + +export const userSpacePath = join(homedir(), "jan"); + +export class ExtensionManager { + public static instance: ExtensionManager = new ExtensionManager(); + + extensionsPath: string | undefined = join(userSpacePath, "extensions"); + + constructor() { + if (ExtensionManager.instance) { + return ExtensionManager.instance; + } + } + + /** + * Sets up the extensions by initializing the `extensions` module with the `confirmInstall` and `extensionsPath` options. + * The `confirmInstall` function always returns `true` to allow extension installation. + * The `extensionsPath` option specifies the path to install extensions to. + */ + setupExtensions() { + init({ + // Function to check from the main process that user wants to install a extension + confirmInstall: async (_extensions: string[]) => { + return true; + }, + // Path to install extension to + extensionsPath: join(userSpacePath, "extensions"), + }); + } + + setExtensionsPath(extPath: string) { + // Create folder if it does not exist + let extDir; + try { + extDir = resolve(extPath); + if (extDir.length < 2) throw new Error(); + + if (!existsSync(extDir)) mkdirSync(extDir); + + const extensionsJson = join(extDir, "extensions.json"); + if (!existsSync(extensionsJson)) + writeFileSync(extensionsJson, "{}", "utf8"); + + this.extensionsPath = extDir; + } catch (error) { + throw new Error("Invalid path provided to the extensions folder"); + } + } + + getExtensionsFile() { + return join(this.extensionsPath ?? "", "extensions.json"); + } +} diff --git a/electron/extension/store.ts b/core/src/node/extension/store.ts similarity index 67% rename from electron/extension/store.ts rename to core/src/node/extension/store.ts index 4857ef27a..8e11c2bb2 100644 --- a/electron/extension/store.ts +++ b/core/src/node/extension/store.ts @@ -1,16 +1,6 @@ -/** - * Provides access to the extensions stored by Extension Store - * @typedef {Object} extensionManager - * @prop {getExtension} getExtension - * @prop {getAllExtensions} getAllExtensions - * @prop {getActiveExtensions} getActiveExtensions - * @prop {installExtensions} installExtensions - * @prop {removeExtension} removeExtension - */ - -import { writeFileSync } from 'fs' -import Extension from './extension' -import { ExtensionManager } from './../managers/extension' +import { writeFileSync } from "fs"; +import Extension from "./extension"; +import { ExtensionManager } from "./manager"; /** * @module store @@ -21,7 +11,7 @@ import { ExtensionManager } from './../managers/extension' * Register of installed extensions * @type {Object.} extension - List of installed extensions */ -const extensions: Record = {} +const extensions: Record = {}; /** * Get a extension from the stored extensions. @@ -31,10 +21,10 @@ const extensions: Record = {} */ export function getExtension(name: string) { if (!Object.prototype.hasOwnProperty.call(extensions, name)) { - throw new Error(`Extension ${name} does not exist`) + throw new Error(`Extension ${name} does not exist`); } - return extensions[name] + return extensions[name]; } /** @@ -43,7 +33,7 @@ export function getExtension(name: string) { * @alias extensionManager.getAllExtensions */ export function getAllExtensions() { - return Object.values(extensions) + return Object.values(extensions); } /** @@ -52,7 +42,7 @@ export function getAllExtensions() { * @alias extensionManager.getActiveExtensions */ export function getActiveExtensions() { - return Object.values(extensions).filter((extension) => extension.active) + return Object.values(extensions).filter((extension) => extension.active); } /** @@ -63,9 +53,9 @@ export function getActiveExtensions() { * @alias extensionManager.removeExtension */ export function removeExtension(name: string, persist = true) { - const del = delete extensions[name] - if (persist) persistExtensions() - return del + const del = delete extensions[name]; + if (persist) persistExtensions(); + return del; } /** @@ -75,10 +65,10 @@ export function removeExtension(name: string, persist = true) { * @returns {void} */ export function addExtension(extension: Extension, persist = true) { - if (extension.name) extensions[extension.name] = extension + if (extension.name) extensions[extension.name] = extension; if (persist) { - persistExtensions() - extension.subscribe('pe-persist', persistExtensions) + persistExtensions(); + extension.subscribe("pe-persist", persistExtensions); } } @@ -87,15 +77,15 @@ export function addExtension(extension: Extension, persist = true) { * @returns {void} */ export function persistExtensions() { - const persistData: Record = {} + const persistData: Record = {}; for (const name in extensions) { - persistData[name] = extensions[name] + persistData[name] = extensions[name]; } writeFileSync( ExtensionManager.instance.getExtensionsFile(), JSON.stringify(persistData), - 'utf8' - ) + "utf8" + ); } /** @@ -106,25 +96,25 @@ export function persistExtensions() { * @alias extensionManager.installExtensions */ export async function installExtensions(extensions: any, store = true) { - const installed: Extension[] = [] + const installed: Extension[] = []; for (const ext of extensions) { // Set install options and activation based on input type - const isObject = typeof ext === 'object' - const spec = isObject ? [ext.specifier, ext] : [ext] - const activate = isObject ? ext.activate !== false : true + const isObject = typeof ext === "object"; + const spec = isObject ? [ext.specifier, ext] : [ext]; + const activate = isObject ? ext.activate !== false : true; // Install and possibly activate extension - const extension = new Extension(...spec) - await extension._install() - if (activate) extension.setActive(true) + const extension = new Extension(...spec); + await extension._install(); + if (activate) extension.setActive(true); // Add extension to store if needed - if (store) addExtension(extension) - installed.push(extension) + if (store) addExtension(extension); + installed.push(extension); } // Return list of all installed extensions - return installed + return installed; } /** diff --git a/core/src/node/index.ts b/core/src/node/index.ts new file mode 100644 index 000000000..49c2c3c26 --- /dev/null +++ b/core/src/node/index.ts @@ -0,0 +1,7 @@ +export * from './extension/index' +export * from './extension/extension' +export * from './extension/manager' +export * from './extension/store' +export * from './download' +export * from './module' +export * from './api' diff --git a/electron/managers/module.ts b/core/src/node/module.ts similarity index 59% rename from electron/managers/module.ts rename to core/src/node/module.ts index dc16d0d22..2201fb400 100644 --- a/electron/managers/module.ts +++ b/core/src/node/module.ts @@ -1,16 +1,14 @@ -import { dispose } from "./../utils/disposable"; - /** * Manages imported modules. */ export class ModuleManager { - public requiredModules: Record = {}; + public requiredModules: Record = {} - public static instance: ModuleManager = new ModuleManager(); + public static instance: ModuleManager = new ModuleManager() constructor() { if (ModuleManager.instance) { - return ModuleManager.instance; + return ModuleManager.instance } } @@ -20,14 +18,13 @@ export class ModuleManager { * @param {any | undefined} nodule - The module to set, or undefined to clear the module. */ setModule(moduleName: string, nodule: any | undefined) { - this.requiredModules[moduleName] = nodule; + this.requiredModules[moduleName] = nodule } /** * Clears all imported modules. */ clearImportedModules() { - dispose(this.requiredModules); - this.requiredModules = {}; + this.requiredModules = {} } } diff --git a/core/tsconfig.json b/core/tsconfig.json index 19b2d29ad..81991f46a 100644 --- a/core/tsconfig.json +++ b/core/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "moduleResolution": "node", "target": "es5", - "module": "es2015", + "module": "ES2020", "lib": ["es2015", "es2016", "es2017", "dom"], "strict": true, "sourceMap": true, diff --git a/electron/handlers/app.ts b/electron/handlers/app.ts index ff88cd8f1..2966ef888 100644 --- a/electron/handlers/app.ts +++ b/electron/handlers/app.ts @@ -1,11 +1,13 @@ import { app, ipcMain, shell, nativeTheme } from 'electron' -import { ModuleManager } from './../managers/module' import { join } from 'path' -import { ExtensionManager } from './../managers/extension' import { WindowManager } from './../managers/window' import { userSpacePath } from './../utils/path' import { AppRoute } from '@janhq/core' import { getResourcePath } from './../utils/path' +import { + ExtensionManager, + ModuleManager, +} from '@janhq/core/node' export function handleAppIPCs() { /** @@ -26,10 +28,6 @@ export function handleAppIPCs() { shell.openPath(userSpacePath) }) - ipcMain.handle(AppRoute.getResourcePath, async (_event) => { - return getResourcePath() - }) - /** * Opens a URL in the user's default browser. * @param _event - The IPC event object. diff --git a/electron/handlers/download.ts b/electron/handlers/download.ts index 145174ac2..9c49b44f3 100644 --- a/electron/handlers/download.ts +++ b/electron/handlers/download.ts @@ -1,11 +1,11 @@ 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, renameSync } from 'fs' import { DownloadEvent, DownloadRoute } from '@janhq/core' const progress = require('request-progress') +import { DownloadManager } from '@janhq/core/node' export function handleDownloaderIPCs() { /** @@ -46,6 +46,9 @@ export function handleDownloaderIPCs() { */ ipcMain.handle(DownloadRoute.downloadFile, async (_event, url, fileName) => { const userDataPath = join(app.getPath('home'), 'jan') + if (typeof fileName === 'string' && fileName.includes('file:/')) { + fileName = fileName.replace('file:/', '') + } const destination = resolve(userDataPath, fileName) const rq = request(url) // downloading file to a temp file first diff --git a/electron/handlers/extension.ts b/electron/handlers/extension.ts index f89206eac..ad8b59a99 100644 --- a/electron/handlers/extension.ts +++ b/electron/handlers/extension.ts @@ -1,15 +1,15 @@ import { ipcMain, webContents } from 'electron' import { readdirSync } from 'fs' -import { ModuleManager } from './../managers/module' import { join, extname } from 'path' + import { - getActiveExtensions, - getAllExtensions, installExtensions, -} from './../extension/store' -import { getExtension } from './../extension/store' -import { removeExtension } from './../extension/store' -import Extension from './../extension/extension' + getExtension, + removeExtension, + getActiveExtensions, + ModuleManager +} from '@janhq/core/node' + import { getResourcePath, userSpacePath } from './../utils/path' import { ExtensionRoute } from '@janhq/core' @@ -81,7 +81,7 @@ export function handleExtensionIPCs() { ExtensionRoute.updateExtension, async (e, extensions, reload) => { // Update all provided extensions - const updated: Extension[] = [] + const updated: any[] = [] for (const ext of extensions) { const extension = getExtension(ext) const res = await extension.update() diff --git a/electron/handlers/fileManager.ts b/electron/handlers/fileManager.ts new file mode 100644 index 000000000..5dc448344 --- /dev/null +++ b/electron/handlers/fileManager.ts @@ -0,0 +1,37 @@ +import { ipcMain } from 'electron' +// @ts-ignore +import reflect from '@alumna/reflect' + +import { FileManagerRoute, getResourcePath } from '@janhq/core' +import { userSpacePath } from './../utils/path' + +/** + * Handles file system extensions operations. + */ +export function handleFileMangerIPCs() { + // Handles the 'synceFile' IPC event. This event is triggered to synchronize a file from a source path to a destination path. + ipcMain.handle( + FileManagerRoute.synceFile, + async (_event, src: string, dest: string) => { + return reflect({ + src, + dest, + recursive: true, + delete: false, + overwrite: true, + errorOnExist: false, + }) + } + ) + + // Handles the 'getUserSpace' IPC event. This event is triggered to get the user space path. + ipcMain.handle( + FileManagerRoute.getUserSpace, + (): Promise => Promise.resolve(userSpacePath) + ) + + // Handles the 'getResourcePath' IPC event. This event is triggered to get the resource path. + ipcMain.handle(FileManagerRoute.getResourcePath, async (_event) => { + return getResourcePath() + }) +} diff --git a/electron/handlers/fs.ts b/electron/handlers/fs.ts index f8ac089e4..fdfaba606 100644 --- a/electron/handlers/fs.ts +++ b/electron/handlers/fs.ts @@ -1,238 +1,24 @@ import { ipcMain } from 'electron' -import * as fs from 'fs' -import fse from 'fs-extra' -import { join } from 'path' -import readline from 'readline' -import { userSpacePath } from './../utils/path' -import { FileSystemRoute } from '@janhq/core' -const reflect = require('@alumna/reflect') +import { FileSystemRoute } from '@janhq/core' +import { userSpacePath } from '../utils/path' +import { join } from 'path' /** * Handles file system operations. */ export function handleFsIPCs() { - /** - * Gets the path to the user data directory. - * @param event - The event object. - * @returns A promise that resolves with the path to the user data directory. - */ - ipcMain.handle( - FileSystemRoute.getUserSpace, - (): Promise => Promise.resolve(userSpacePath) - ) - - /** - * Checks whether the path is a directory. - * @param event - The event object. - * @param path - The path to check. - * @returns A promise that resolves with a boolean indicating whether the path is a directory. - */ - ipcMain.handle( - FileSystemRoute.isDirectory, - (_event, path: string): Promise => { - const fullPath = join(userSpacePath, path) - return Promise.resolve( - fs.existsSync(fullPath) && fs.lstatSync(fullPath).isDirectory() + const moduleName = 'fs' + Object.values(FileSystemRoute).forEach((route) => { + ipcMain.handle(route, async (event, ...args) => { + return import(moduleName).then((mdl) => + mdl[route]( + ...args.map((arg) => + typeof arg === 'string' && arg.includes('file:/') + ? join(userSpacePath, arg.replace('file:/', '')) + : arg + ) + ) ) - } - ) - - /** - * Reads a file from the user data directory. - * @param event - The event object. - * @param path - The path of the file to read. - * @returns A promise that resolves with the contents of the file. - */ - ipcMain.handle( - FileSystemRoute.readFile, - async (event, path: string): Promise => { - return new Promise((resolve, reject) => { - fs.readFile(join(userSpacePath, path), 'utf8', (err, data) => { - if (err) { - reject(err) - } else { - resolve(data) - } - }) - }) - } - ) - - /** - * Checks whether a file exists in the user data directory. - * @param event - The event object. - * @param path - The path of the file to check. - * @returns A promise that resolves with a boolean indicating whether the file exists. - */ - ipcMain.handle(FileSystemRoute.exists, async (_event, path: string) => { - return new Promise((resolve, reject) => { - const fullPath = join(userSpacePath, path) - fs.existsSync(fullPath) ? resolve(true) : resolve(false) }) }) - - /** - * Writes data to a file in the user data directory. - * @param event - The event object. - * @param path - The path of the file to write to. - * @param data - The data to write to the file. - * @returns A promise that resolves when the file has been written. - */ - ipcMain.handle( - FileSystemRoute.writeFile, - async (event, path: string, data: string): Promise => { - try { - await fs.writeFileSync(join(userSpacePath, path), data, 'utf8') - } catch (err) { - console.error(`writeFile ${path} result: ${err}`) - } - } - ) - - /** - * Creates a directory in the user data directory. - * @param event - The event object. - * @param path - The path of the directory to create. - * @returns A promise that resolves when the directory has been created. - */ - ipcMain.handle( - FileSystemRoute.mkdir, - async (event, path: string): Promise => { - try { - fs.mkdirSync(join(userSpacePath, path), { recursive: true }) - } catch (err) { - console.error(`mkdir ${path} result: ${err}`) - } - } - ) - - /** - * Removes a directory in the user data directory. - * @param event - The event object. - * @param path - The path of the directory to remove. - * @returns A promise that resolves when the directory is removed successfully. - */ - ipcMain.handle( - FileSystemRoute.rmdir, - async (event, path: string): Promise => { - try { - await fs.rmSync(join(userSpacePath, path), { recursive: true }) - } catch (err) { - console.error(`rmdir ${path} result: ${err}`) - } - } - ) - - /** - * Lists the files in a directory in the user data directory. - * @param event - The event object. - * @param path - The path of the directory to list files from. - * @returns A promise that resolves with an array of file names. - */ - ipcMain.handle( - FileSystemRoute.listFiles, - async (event, path: string): Promise => { - return new Promise((resolve, reject) => { - fs.readdir(join(userSpacePath, path), (err, files) => { - if (err) { - reject(err) - } else { - resolve(files) - } - }) - }) - } - ) - - /** - * Deletes a file from the user data folder. - * @param _event - The IPC event object. - * @param filePath - The path to the file to delete. - * @returns A string indicating the result of the operation. - */ - ipcMain.handle(FileSystemRoute.deleteFile, async (_event, filePath) => { - try { - await fs.unlinkSync(join(userSpacePath, filePath)) - } catch (err) { - console.error(`unlink ${filePath} result: ${err}`) - } - }) - - /** - * Appends data to a file in the user data directory. - * @param event - The event object. - * @param path - The path of the file to append to. - * @param data - The data to append to the file. - * @returns A promise that resolves when the file has been written. - */ - ipcMain.handle( - FileSystemRoute.appendFile, - async (_event, path: string, data: string) => { - try { - await fs.appendFileSync(join(userSpacePath, path), data, 'utf8') - } catch (err) { - console.error(`appendFile ${path} result: ${err}`) - } - } - ) - - ipcMain.handle( - FileSystemRoute.syncFile, - async (_event, src: string, dest: string) => { - console.debug(`Copying file from ${src} to ${dest}`) - - return reflect({ - src, - dest, - recursive: true, - delete: false, - overwrite: true, - errorOnExist: false, - }) - } - ) - - ipcMain.handle( - FileSystemRoute.copyFile, - async (_event, src: string, dest: string) => { - console.debug(`Copying file from ${src} to ${dest}`) - - return fse.copySync(src, dest, { - overwrite: false, - recursive: true, - errorOnExist: false, - }) - } - ) - - /** - * Reads a file line by line. - * @param event - The event object. - * @param path - The path of the file to read. - * @returns A promise that resolves with the contents of the file. - */ - ipcMain.handle( - FileSystemRoute.readLineByLine, - async (_event, path: string) => { - const fullPath = join(userSpacePath, path) - - return new Promise((res, rej) => { - try { - const readInterface = readline.createInterface({ - input: fs.createReadStream(fullPath), - }) - const lines: any = [] - readInterface - .on('line', function (line) { - lines.push(line) - }) - .on('close', function () { - res(lines) - }) - } catch (err) { - rej(err) - } - }) - } - ) } diff --git a/electron/main.ts b/electron/main.ts index 542875312..fae3a1ffa 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -7,27 +7,34 @@ import { createUserSpace } from './utils/path' * Managers **/ import { WindowManager } from './managers/window' -import { ModuleManager } from './managers/module' -import { ExtensionManager } from './managers/extension' +import { ExtensionManager, ModuleManager } from '@janhq/core/node' /** * IPC Handlers **/ import { handleDownloaderIPCs } from './handlers/download' import { handleExtensionIPCs } from './handlers/extension' +import { handleFileMangerIPCs } from './handlers/fileManager' import { handleAppIPCs } from './handlers/app' import { handleAppUpdates } from './handlers/update' import { handleFsIPCs } from './handlers/fs' +import { migrateExtensions } from './utils/migration' + +/** + * Server + */ +import { startServer } from '@janhq/server' app .whenReady() .then(createUserSpace) - .then(ExtensionManager.instance.migrateExtensions) + .then(migrateExtensions) .then(ExtensionManager.instance.setupExtensions) .then(setupMenu) .then(handleIPCs) .then(handleAppUpdates) .then(createMainWindow) + .then(startServer) .then(() => { app.on('activate', () => { if (!BrowserWindow.getAllWindows().length) { @@ -80,4 +87,5 @@ function handleIPCs() { handleDownloaderIPCs() handleExtensionIPCs() handleAppIPCs() + handleFileMangerIPCs() } diff --git a/electron/managers/extension.ts b/electron/managers/extension.ts deleted file mode 100644 index 7eef24877..000000000 --- a/electron/managers/extension.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { app } from 'electron' -import { init } from './../extension' -import { join, resolve } from 'path' -import { rmdir } from 'fs' -import Store from 'electron-store' -import { existsSync, mkdirSync, writeFileSync } from 'fs' -import { userSpacePath } from './../utils/path' -/** - * Manages extension installation and migration. - */ -export class ExtensionManager { - public static instance: ExtensionManager = new ExtensionManager() - - extensionsPath: string | undefined = undefined - - constructor() { - if (ExtensionManager.instance) { - return ExtensionManager.instance - } - } - - /** - * Sets up the extensions by initializing the `extensions` module with the `confirmInstall` and `extensionsPath` options. - * The `confirmInstall` function always returns `true` to allow extension installation. - * The `extensionsPath` option specifies the path to install extensions to. - */ - setupExtensions() { - init({ - // Function to check from the main process that user wants to install a extension - confirmInstall: async (_extensions: string[]) => { - return true - }, - // Path to install extension to - extensionsPath: join(userSpacePath, 'extensions'), - }) - } - - /** - * Migrates the extensions by deleting the `extensions` directory in the user data path. - * If the `migrated_version` key in the `Store` object does not match the current app version, - * the function deletes the `extensions` directory and sets the `migrated_version` key to the current app version. - * @returns A Promise that resolves when the migration is complete. - */ - migrateExtensions() { - return new Promise((resolve) => { - const store = new Store() - if (store.get('migrated_version') !== app.getVersion()) { - console.debug('start migration:', store.get('migrated_version')) - const fullPath = join(userSpacePath, 'extensions') - - rmdir(fullPath, { recursive: true }, function (err) { - if (err) console.error(err) - store.set('migrated_version', app.getVersion()) - console.debug('migrate extensions done') - resolve(undefined) - }) - } else { - resolve(undefined) - } - }) - } - - setExtensionsPath(extPath: string) { - // Create folder if it does not exist - let extDir - try { - extDir = resolve(extPath) - if (extDir.length < 2) throw new Error() - - if (!existsSync(extDir)) mkdirSync(extDir) - - const extensionsJson = join(extDir, 'extensions.json') - if (!existsSync(extensionsJson)) - writeFileSync(extensionsJson, '{}', 'utf8') - - this.extensionsPath = extDir - } catch (error) { - throw new Error('Invalid path provided to the extensions folder') - } - } - - getExtensionsFile() { - return join(this.extensionsPath ?? '', 'extensions.json') - } -} diff --git a/electron/package.json b/electron/package.json index 864934d56..3cc5e7680 100644 --- a/electron/package.json +++ b/electron/package.json @@ -72,15 +72,20 @@ "dependencies": { "@alumna/reflect": "^1.1.3", "@janhq/core": "link:./core", + "@janhq/server": "link:./server", "@npmcli/arborist": "^7.1.0", "@types/request": "^2.48.12", "@uiball/loaders": "^1.3.0", "electron-store": "^8.1.0", "electron-updater": "^6.1.7", "fs-extra": "^11.2.0", + "node-fetch": "2", "pacote": "^17.0.4", "request": "^2.88.2", "request-progress": "^3.0.0", + "rimraf": "^5.0.5", + "typescript": "^5.3.3", + "ulid": "^2.3.0", "use-debounce": "^9.0.4" }, "devDependencies": { diff --git a/electron/tsconfig.json b/electron/tsconfig.json index 3cc218f93..11c9d8577 100644 --- a/electron/tsconfig.json +++ b/electron/tsconfig.json @@ -15,6 +15,9 @@ "paths": { "*": ["node_modules/*"] }, "typeRoots": ["node_modules/@types"] }, + "ts-node": { + "esm": true + }, "include": ["./**/*.ts"], "exclude": ["core", "build", "dist", "tests", "node_modules"] } diff --git a/electron/utils/menu.ts b/electron/utils/menu.ts index 65e009aef..97a2afd98 100644 --- a/electron/utils/menu.ts +++ b/electron/utils/menu.ts @@ -1,5 +1,5 @@ // @ts-nocheck -const { app, Menu, dialog } = require("electron"); +import { app, Menu, dialog, shell } from "electron"; const isMac = process.platform === "darwin"; const { autoUpdater } = require("electron-updater"); import { compareSemanticVersions } from "./versionDiff"; @@ -97,7 +97,6 @@ const template: (Electron.MenuItemConstructorOptions | Electron.MenuItem)[] = [ { label: "Learn More", click: async () => { - const { shell } = require("electron"); await shell.openExternal("https://jan.ai/"); }, }, diff --git a/electron/utils/migration.ts b/electron/utils/migration.ts new file mode 100644 index 000000000..cf66270c6 --- /dev/null +++ b/electron/utils/migration.ts @@ -0,0 +1,30 @@ +import { app } from 'electron' +import { join } from 'path' + +import { rmdir } from 'fs' +import Store from 'electron-store' +import { userSpacePath } from './path' +/** + * Migrates the extensions by deleting the `extensions` directory in the user data path. + * If the `migrated_version` key in the `Store` object does not match the current app version, + * the function deletes the `extensions` directory and sets the `migrated_version` key to the current app version. + * @returns A Promise that resolves when the migration is complete. + */ +export function migrateExtensions() { + return new Promise((resolve) => { + const store = new Store() + if (store.get('migrated_version') !== app.getVersion()) { + console.debug('start migration:', store.get('migrated_version')) + const fullPath = join(userSpacePath, 'extensions') + + rmdir(fullPath, { recursive: true }, function (err) { + if (err) console.error(err) + store.set('migrated_version', app.getVersion()) + console.debug('migrate extensions done') + resolve(undefined) + }) + } else { + resolve(undefined) + } + }) +} diff --git a/extensions/assistant-extension/src/index.ts b/extensions/assistant-extension/src/index.ts index 9b48ce6c9..0c5745b3d 100644 --- a/extensions/assistant-extension/src/index.ts +++ b/extensions/assistant-extension/src/index.ts @@ -3,17 +3,18 @@ import { AssistantExtension } from "@janhq/core"; import { join } from "path"; export default class JanAssistantExtension implements AssistantExtension { - private static readonly _homeDir = "assistants"; + private static readonly _homeDir = "file://assistants"; type(): ExtensionType { return ExtensionType.Assistant; } - onLoad(): void { + async onLoad() { // making the assistant directory - fs.mkdir(JanAssistantExtension._homeDir).then(() => { - this.createJanAssistant(); - }); + if (!(await fs.existsSync(JanAssistantExtension._homeDir))) + fs.mkdirSync(JanAssistantExtension._homeDir).then(() => { + this.createJanAssistant(); + }); } /** @@ -23,12 +24,12 @@ export default class JanAssistantExtension implements AssistantExtension { async createAssistant(assistant: Assistant): Promise { const assistantDir = join(JanAssistantExtension._homeDir, assistant.id); - await fs.mkdir(assistantDir); + if (!(await fs.existsSync(assistantDir))) await fs.mkdirSync(assistantDir); // store the assistant metadata json const assistantMetadataPath = join(assistantDir, "assistant.json"); try { - await fs.writeFile( + await fs.writeFileSync( assistantMetadataPath, JSON.stringify(assistant, null, 2) ); @@ -41,18 +42,14 @@ export default class JanAssistantExtension implements AssistantExtension { // get all the assistant directories // get all the assistant metadata json const results: Assistant[] = []; - const allFileName: string[] = await fs.listFiles( + const allFileName: string[] = await fs.readdirSync( JanAssistantExtension._homeDir ); for (const fileName of allFileName) { const filePath = join(JanAssistantExtension._homeDir, fileName); - const isDirectory = await fs.isDirectory(filePath); - if (!isDirectory) { - // if not a directory, ignore - continue; - } - const jsonFiles: string[] = (await fs.listFiles(filePath)).filter( + if (filePath.includes(".DS_Store")) continue; + const jsonFiles: string[] = (await fs.readdirSync(filePath)).filter( (file: string) => file === "assistant.json" ); @@ -61,9 +58,12 @@ export default class JanAssistantExtension implements AssistantExtension { continue; } - const assistant: Assistant = JSON.parse( - await fs.readFile(join(filePath, jsonFiles[0])) + const content = await fs.readFileSync( + join(filePath, jsonFiles[0]), + "utf-8" ); + const assistant: Assistant = + typeof content === "object" ? content : JSON.parse(content); results.push(assistant); } @@ -78,7 +78,7 @@ export default class JanAssistantExtension implements AssistantExtension { // remove the directory const assistantDir = join(JanAssistantExtension._homeDir, assistant.id); - await fs.rmdir(assistantDir); + await fs.rmdirSync(assistantDir); return Promise.resolve(); } diff --git a/extensions/conversational-extension/src/index.ts b/extensions/conversational-extension/src/index.ts index 8aae791e8..6531c489a 100644 --- a/extensions/conversational-extension/src/index.ts +++ b/extensions/conversational-extension/src/index.ts @@ -9,7 +9,7 @@ import { Thread, ThreadMessage } from '@janhq/core' export default class JSONConversationalExtension implements ConversationalExtension { - private static readonly _homeDir = 'threads' + private static readonly _homeDir = 'file://threads' private static readonly _threadInfoFileName = 'thread.json' private static readonly _threadMessagesFileName = 'messages.jsonl' @@ -23,8 +23,9 @@ export default class JSONConversationalExtension /** * Called when the extension is loaded. */ - onLoad() { - fs.mkdir(JSONConversationalExtension._homeDir) + async onLoad() { + if (!(await fs.existsSync(JSONConversationalExtension._homeDir))) + fs.mkdirSync(JSONConversationalExtension._homeDir) console.debug('JSONConversationalExtension loaded') } @@ -47,7 +48,9 @@ export default class JSONConversationalExtension const convos = promiseResults .map((result) => { if (result.status === 'fulfilled') { - return JSON.parse(result.value) as Thread + return typeof result.value === 'object' + ? result.value + : JSON.parse(result.value) } }) .filter((convo) => convo != null) @@ -76,8 +79,11 @@ export default class JSONConversationalExtension threadDirPath, JSONConversationalExtension._threadInfoFileName, ]) - await fs.mkdir(threadDirPath) - await fs.writeFile(threadJsonPath, JSON.stringify(thread, null, 2)) + if (!(await fs.existsSync(threadDirPath))) { + await fs.mkdirSync(threadDirPath) + } + + await fs.writeFileSync(threadJsonPath, JSON.stringify(thread)) Promise.resolve() } catch (err) { Promise.reject(err) @@ -89,8 +95,9 @@ export default class JSONConversationalExtension * @param threadId The ID of the thread to delete. */ async deleteThread(threadId: string): Promise { - return fs.rmdir( - await joinPath([JSONConversationalExtension._homeDir, `${threadId}`]) + return fs.rmdirSync( + await joinPath([JSONConversationalExtension._homeDir, `${threadId}`]), + { recursive: true } ) } @@ -104,8 +111,9 @@ export default class JSONConversationalExtension threadDirPath, JSONConversationalExtension._threadMessagesFileName, ]) - await fs.mkdir(threadDirPath) - await fs.appendFile(threadMessagePath, JSON.stringify(message) + '\n') + if (!(await fs.existsSync(threadDirPath))) + await fs.mkdirSync(threadDirPath) + await fs.appendFileSync(threadMessagePath, JSON.stringify(message) + '\n') Promise.resolve() } catch (err) { Promise.reject(err) @@ -125,8 +133,9 @@ export default class JSONConversationalExtension threadDirPath, JSONConversationalExtension._threadMessagesFileName, ]) - await fs.mkdir(threadDirPath) - await fs.writeFile( + if (!(await fs.existsSync(threadDirPath))) + await fs.mkdirSync(threadDirPath) + await fs.writeFileSync( threadMessagePath, messages.map((msg) => JSON.stringify(msg)).join('\n') + (messages.length ? '\n' : '') @@ -143,12 +152,13 @@ export default class JSONConversationalExtension * @returns data of the thread */ private async readThread(threadDirName: string): Promise { - return fs.readFile( + return fs.readFileSync( await joinPath([ JSONConversationalExtension._homeDir, threadDirName, JSONConversationalExtension._threadInfoFileName, - ]) + ]), + 'utf-8' ) } @@ -157,23 +167,19 @@ export default class JSONConversationalExtension * @private */ private async getValidThreadDirs(): Promise { - const fileInsideThread: string[] = await fs.listFiles( + const fileInsideThread: string[] = await fs.readdirSync( JSONConversationalExtension._homeDir ) const threadDirs: string[] = [] for (let i = 0; i < fileInsideThread.length; i++) { + if (fileInsideThread[i].includes('.DS_Store')) continue const path = await joinPath([ JSONConversationalExtension._homeDir, fileInsideThread[i], ]) - const isDirectory = await fs.isDirectory(path) - if (!isDirectory) { - console.debug(`Ignore ${path} because it is not a directory`) - continue - } - const isHavingThreadInfo = (await fs.listFiles(path)).includes( + const isHavingThreadInfo = (await fs.readdirSync(path)).includes( JSONConversationalExtension._threadInfoFileName ) if (!isHavingThreadInfo) { @@ -192,12 +198,8 @@ export default class JSONConversationalExtension JSONConversationalExtension._homeDir, threadId, ]) - const isDir = await fs.isDirectory(threadDirPath) - if (!isDir) { - throw Error(`${threadDirPath} is not directory`) - } - const files: string[] = await fs.listFiles(threadDirPath) + const files: string[] = await fs.readdirSync(threadDirPath) if ( !files.includes(JSONConversationalExtension._threadMessagesFileName) ) { @@ -209,7 +211,14 @@ export default class JSONConversationalExtension JSONConversationalExtension._threadMessagesFileName, ]) - const result = await fs.readLineByLine(messageFilePath) + const result = await fs + .readFileSync(messageFilePath, 'utf-8') + .then((content) => + content + .toString() + .split('\n') + .filter((line) => line !== '') + ) const messages: ThreadMessage[] = [] result.forEach((line: string) => { diff --git a/extensions/inference-nitro-extension/src/index.ts b/extensions/inference-nitro-extension/src/index.ts index d19f3853c..e1ccc9f2a 100644 --- a/extensions/inference-nitro-extension/src/index.ts +++ b/extensions/inference-nitro-extension/src/index.ts @@ -17,9 +17,9 @@ import { ThreadMessage, events, executeOnMain, - getUserSpace, fs, Model, + joinPath, } from "@janhq/core"; import { InferenceExtension } from "@janhq/core"; import { requestInference } from "./helpers/sse"; @@ -58,8 +58,9 @@ export default class JanInferenceNitroExtension implements InferenceExtension { /** * Subscribes to events emitted by the @janhq/core package. */ - onLoad(): void { - fs.mkdir(JanInferenceNitroExtension._homeDir); + async onLoad() { + if (!(await fs.existsSync(JanInferenceNitroExtension._homeDir))) + fs.mkdirSync(JanInferenceNitroExtension._homeDir); this.writeDefaultEngineSettings(); // Events subscription @@ -91,12 +92,12 @@ export default class JanInferenceNitroExtension implements InferenceExtension { JanInferenceNitroExtension._homeDir, JanInferenceNitroExtension._engineMetadataFileName ); - if (await fs.exists(engineFile)) { - JanInferenceNitroExtension._engineSettings = JSON.parse( - await fs.readFile(engineFile) - ); + if (await fs.existsSync(engineFile)) { + const engine = await fs.readFileSync(engineFile, "utf-8"); + JanInferenceNitroExtension._engineSettings = + typeof engine === "object" ? engine : JSON.parse(engine); } else { - await fs.writeFile( + await fs.writeFileSync( engineFile, JSON.stringify(JanInferenceNitroExtension._engineSettings, null, 2) ); @@ -110,8 +111,7 @@ export default class JanInferenceNitroExtension implements InferenceExtension { if (model.engine !== "nitro") { return; } - const userSpacePath = await getUserSpace(); - const modelFullPath = join(userSpacePath, "models", model.id); + const modelFullPath = await joinPath(["models", model.id]); const nitroInitResult = await executeOnMain(MODULE, "initModel", { modelFullPath: modelFullPath, diff --git a/extensions/inference-nitro-extension/src/module.ts b/extensions/inference-nitro-extension/src/module.ts index 9d3d4452e..b8706a7dc 100644 --- a/extensions/inference-nitro-extension/src/module.ts +++ b/extensions/inference-nitro-extension/src/module.ts @@ -38,6 +38,10 @@ function stopModel(): Promise { */ async function initModel(wrapper: any): Promise { currentModelFile = wrapper.modelFullPath; + const janRoot = path.join(require("os").homedir(), "jan"); + if (!currentModelFile.includes(janRoot)) { + currentModelFile = path.join(janRoot, currentModelFile); + } const files: string[] = fs.readdirSync(currentModelFile); // Look for GGUF model file diff --git a/extensions/inference-openai-extension/src/index.ts b/extensions/inference-openai-extension/src/index.ts index 80d211d95..d612e474e 100644 --- a/extensions/inference-openai-extension/src/index.ts +++ b/extensions/inference-openai-extension/src/index.ts @@ -29,7 +29,7 @@ import { join } from "path"; * It also subscribes to events emitted by the @janhq/core package and handles new message requests. */ export default class JanInferenceOpenAIExtension implements InferenceExtension { - private static readonly _homeDir = "engines"; + private static readonly _homeDir = "file://engines"; private static readonly _engineMetadataFileName = "openai.json"; private static _currentModel: OpenAIModel; @@ -53,8 +53,9 @@ export default class JanInferenceOpenAIExtension implements InferenceExtension { /** * Subscribes to events emitted by the @janhq/core package. */ - onLoad(): void { - fs.mkdir(JanInferenceOpenAIExtension._homeDir); + async onLoad() { + if (!(await fs.existsSync(JanInferenceOpenAIExtension._homeDir))) + fs.mkdirSync(JanInferenceOpenAIExtension._homeDir); JanInferenceOpenAIExtension.writeDefaultEngineSettings(); // Events subscription @@ -85,12 +86,12 @@ export default class JanInferenceOpenAIExtension implements InferenceExtension { JanInferenceOpenAIExtension._homeDir, JanInferenceOpenAIExtension._engineMetadataFileName ); - if (await fs.exists(engineFile)) { - JanInferenceOpenAIExtension._engineSettings = JSON.parse( - await fs.readFile(engineFile) - ); + if (await fs.existsSync(engineFile)) { + const engine = await fs.readFileSync(engineFile, 'utf-8'); + JanInferenceOpenAIExtension._engineSettings = + typeof engine === "object" ? engine : JSON.parse(engine); } else { - await fs.writeFile( + await fs.writeFileSync( engineFile, JSON.stringify(JanInferenceOpenAIExtension._engineSettings, null, 2) ); diff --git a/extensions/inference-triton-trtllm-extension/src/index.ts b/extensions/inference-triton-trtllm-extension/src/index.ts index 0720ed3ac..7ef5270e7 100644 --- a/extensions/inference-triton-trtllm-extension/src/index.ts +++ b/extensions/inference-triton-trtllm-extension/src/index.ts @@ -57,9 +57,9 @@ export default class JanInferenceTritonTrtLLMExtension /** * Subscribes to events emitted by the @janhq/core package. */ - onLoad(): void { - fs.mkdir(JanInferenceTritonTrtLLMExtension._homeDir); - JanInferenceTritonTrtLLMExtension.writeDefaultEngineSettings(); + async onLoad() { + if (!(await fs.existsSync(JanInferenceTritonTrtLLMExtension._homeDir))) + JanInferenceTritonTrtLLMExtension.writeDefaultEngineSettings(); // Events subscription events.on(EventName.OnMessageSent, (data) => @@ -98,12 +98,12 @@ export default class JanInferenceTritonTrtLLMExtension JanInferenceTritonTrtLLMExtension._homeDir, JanInferenceTritonTrtLLMExtension._engineMetadataFileName ); - if (await fs.exists(engine_json)) { - JanInferenceTritonTrtLLMExtension._engineSettings = JSON.parse( - await fs.readFile(engine_json) - ); + if (await fs.existsSync(engine_json)) { + const engine = await fs.readFileSync(engine_json, "utf-8"); + JanInferenceTritonTrtLLMExtension._engineSettings = + typeof engine === "object" ? engine : JSON.parse(engine); } else { - await fs.writeFile( + await fs.writeFileSync( engine_json, JSON.stringify( JanInferenceTritonTrtLLMExtension._engineSettings, diff --git a/extensions/model-extension/src/index.ts b/extensions/model-extension/src/index.ts index 9580afd9b..38cf28ef0 100644 --- a/extensions/model-extension/src/index.ts +++ b/extensions/model-extension/src/index.ts @@ -15,7 +15,7 @@ import { ModelExtension, Model } from '@janhq/core' * A extension for models */ export default class JanModelExtension implements ModelExtension { - private static readonly _homeDir = 'models' + private static readonly _homeDir = 'file://models' private static readonly _modelMetadataFileName = 'model.json' private static readonly _supportedModelFormat = '.gguf' private static readonly _incompletedModelFileName = '.download' @@ -46,11 +46,11 @@ export default class JanModelExtension implements ModelExtension { private async copyModelsToHomeDir() { try { - if ( - localStorage.getItem(`${EXTENSION_NAME}-version`) === VERSION && - (await fs.exists(JanModelExtension._homeDir)) - ) { - console.debug('Model already migrated') + // list all of the files under the home directory + + if (fs.existsSync(JanModelExtension._homeDir)) { + // ignore if the model is already downloaded + console.debug('Models already persisted.') return } @@ -94,7 +94,7 @@ export default class JanModelExtension implements ModelExtension { async downloadModel(model: Model): Promise { // create corresponding directory const modelDirPath = await joinPath([JanModelExtension._homeDir, model.id]) - await fs.mkdir(modelDirPath) + if (!(await fs.existsSync(modelDirPath))) await fs.mkdirSync(modelDirPath) // try to retrieve the download file name from the source url // if it fails, use the model ID as the file name @@ -116,11 +116,11 @@ export default class JanModelExtension implements ModelExtension { async cancelModelDownload(modelId: string): Promise { return abortDownload( await joinPath([JanModelExtension._homeDir, modelId, modelId]) - ).then(async () => - fs.deleteFile( + ).then(async () => { + fs.unlinkSync( await joinPath([JanModelExtension._homeDir, modelId, modelId]) ) - ) + }) } /** @@ -133,10 +133,10 @@ export default class JanModelExtension implements ModelExtension { const dirPath = await joinPath([JanModelExtension._homeDir, modelId]) // remove all files under dirPath except model.json - const files = await fs.listFiles(dirPath) + const files = await fs.readdirSync(dirPath) const deletePromises = files.map(async (fileName: string) => { if (fileName !== JanModelExtension._modelMetadataFileName) { - return fs.deleteFile(await joinPath([dirPath, fileName])) + return fs.unlinkSync(await joinPath([dirPath, fileName])) } }) await Promise.allSettled(deletePromises) @@ -158,7 +158,7 @@ export default class JanModelExtension implements ModelExtension { ]) try { - await fs.writeFile(jsonFilePath, JSON.stringify(model, null, 2)) + await fs.writeFileSync(jsonFilePath, JSON.stringify(model, null, 2)) } catch (err) { console.error(err) } @@ -175,7 +175,7 @@ export default class JanModelExtension implements ModelExtension { return true } return await fs - .listFiles(await joinPath([JanModelExtension._homeDir, modelDir])) + .readdirSync(await joinPath([JanModelExtension._homeDir, modelDir])) .then((files: string[]) => { // or model binary exists in the directory // model binary name can match model ID or be a .gguf file and not be an incompleted model file @@ -198,22 +198,17 @@ export default class JanModelExtension implements ModelExtension { selector?: (path: string, model: Model) => Promise ): Promise { try { - const filesUnderJanRoot = await fs.listFiles('') - if (!filesUnderJanRoot.includes(JanModelExtension._homeDir)) { + if (!(await fs.existsSync(JanModelExtension._homeDir))) { console.debug('model folder not found') return [] } - const files: string[] = await fs.listFiles(JanModelExtension._homeDir) + const files: string[] = await fs.readdirSync(JanModelExtension._homeDir) const allDirectories: string[] = [] for (const file of files) { - const isDirectory = await fs.isDirectory( - await joinPath([JanModelExtension._homeDir, file]) - ) - if (isDirectory) { - allDirectories.push(file) - } + if (file === '.DS_Store') continue + allDirectories.push(file) } const readJsonPromises = allDirectories.map(async (dirName) => { @@ -247,6 +242,7 @@ export default class JanModelExtension implements ModelExtension { return undefined } }) + return modelData.filter((e) => !!e) } catch (err) { console.error(err) @@ -255,7 +251,7 @@ export default class JanModelExtension implements ModelExtension { } private readModelMetadata(path: string) { - return fs.readFile(path) + return fs.readFileSync(path, 'utf-8') } /** diff --git a/package.json b/package.json index c12ae1281..e4a1cae89 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "uikit", "core", "electron", - "web" + "web", + "server" ], "nohoist": [ "uikit", @@ -16,7 +17,9 @@ "electron", "electron/**", "web", - "web/**" + "web/**", + "server", + "server/**" ] }, "scripts": { @@ -28,6 +31,7 @@ "test-local": "yarn lint && yarn build:test && yarn test", "dev:uikit": "yarn workspace @janhq/uikit install && yarn workspace @janhq/uikit dev", "build:uikit": "yarn workspace @janhq/uikit install && yarn workspace @janhq/uikit build", + "build:server": "cd server && yarn install && yarn run build", "build:core": "cd core && yarn install && yarn run build", "build:web": "yarn workspace jan-web build && cpx \"web/out/**\" \"electron/renderer/\"", "build:electron": "cpx \"models/**\" \"electron/models/\" && yarn workspace jan build", diff --git a/server/data/.gitkeep b/server/data/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/server/data/models/.gitkeep b/server/data/models/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/server/data/threads/.gitkeep b/server/data/threads/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/server/icons/icon.png b/server/icons/icon.png deleted file mode 100644 index 289f99ded85e1e3cc91b17af6b607d2cafd97cd7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 38651 zcmd3NWmjCmwr%4Uf(Q5D5G=U6yL%EW1b4UK1a~L627 z(G9z*man;Hg()jap&}C?gFqlu8EJ7<5C|Ih6&i#H4?M2jW*>nElAW}UBM5ZD{q_T8 z&oB)90>XEa&~{R@HFI+P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W>|c{iiwILDM! zQiR02*0prCu_02O=6@z!wTso<-m}|9f6HcKvDejb{5L56_&1&PoQ->;iEvKMhl9uQ zA^1l?ZRgaSPoT%cNl4won_Kcm_Acd9F?`th`g~e>@~byJJtxu19hZ^H`c5yoc3?cp$?mK9gvp1a26Cd1Uq zux!-Y_-50@OL>Mz@Iol^pYadR_oh~fjr9Q90%E`)E0fjC37cMaUp-nh|tqXA;3^ks*_Vm=g5G>%N5MG zsK(q4e*v{PbW?1(Ukub-Zdz*so~i_<(><~r?xZm$%qoSQ_s3M zDsXPC*>7vFt=O`uTD*7#!$lqmcUjmx6!kXX?TJ6)~s3bp?C^vT{}CCZ>2N_HE9397Ulg z*;K7>L;#Dzw+D^}32T~s!qCJi3sM5p%O1toW{LThpsYB;x|ex_t#$8Sx1S$nyx#g0 zXo4&#vA5pxOC?MwkHE^jtz0nt&S;Zu9WrJi@5;h{cif?)CPxbC9Y80;&3{@T z+Hclh&OwF#AzFL%(nKJ>TEFi}pL13F45p0ikUz0q-Sj;vI>%)gZF`st{&UwEbQfVj zy@@>(%RKT0t-X}8mu*w)GffIEdfrnWKWj_a=8Mg_OL#kowq;ed?$l>QX+`-wIkEbt z&hm}FCx&lWLCcwW=0;ZclK+KkEzv!T&>G9^ik8^5 zjzRyHpKn}e`WaI-&Q}{E-LyVJ7>CvE`Clz@&D6JwF-s=IFBD&KY>7syw&h&CV!pU} zwEIUlo|1U06D%IV@V5_7d+47RwXMGCt7>`eRgKe|Ab~OE5oP~tCsD#a1UsR2^;92( zTcTBrLW7>%y|-SzNmYm5uNvZY { + childContext.register(require("@fastify/static"), { + root: + process.env.EXTENSION_ROOT || + path.join(require("os").homedir(), "jan", "extensions"), + wildcard: false, + }); + + done(); + }, + { prefix: "extensions" } +); +server.register(v1Router, { prefix: "/v1" }); + +export const startServer = () => { + server + .listen({ + port: JAN_API_PORT, + host: JAN_API_HOST, + }) + .then(() => { + console.log( + `JAN API listening at: http://${JAN_API_HOST}:${JAN_API_PORT}` + ); + }); +}; + +export const stopServer = () => { + server.close(); +}; diff --git a/server/lib/.gitkeep b/server/lib/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/server/main.ts b/server/main.ts index 707c3fb4b..c3eb69135 100644 --- a/server/main.ts +++ b/server/main.ts @@ -1,19 +1,3 @@ -import fastify from 'fastify' -import dotenv from 'dotenv' -import v1API from './v1' -const server = fastify() - -dotenv.config() -server.register(v1API, {prefix: "/api/v1"}) - - -const JAN_API_PORT = Number.parseInt(process.env.JAN_API_PORT || '1337') -const JAN_API_HOST = process.env.JAN_API_HOST || "0.0.0.0" - -server.listen({ - port: JAN_API_PORT, - host: JAN_API_HOST -}).then(() => { - console.log(`JAN API listening at: http://${JAN_API_HOST}:${JAN_API_PORT}`); -}) +import { startServer } from "./index"; +startServer(); diff --git a/server/package.json b/server/package.json index 1fd06a482..2d350a317 100644 --- a/server/package.json +++ b/server/package.json @@ -1,32 +1,37 @@ { - "name": "jan-server", + "name": "@janhq/server", "version": "0.1.3", - "main": "./build/main.js", + "main": "build/index.js", + "types": "build/index.d.ts", "author": "Jan ", "license": "AGPL-3.0", "homepage": "https://jan.ai", "description": "Use offline LLMs with your own data. Run open source models like Llama2 or Falcon on your internal computers/servers.", - "build": "", + "files": [ + "build/**" + ], "scripts": { "lint": "eslint . --ext \".js,.jsx,.ts,.tsx\"", "test:e2e": "playwright test --workers=1", - "dev": "nodemon .", + "dev": "tsc --watch & node --watch build/main.js", "build": "tsc" }, "dependencies": { + "@fastify/cors": "^8.4.2", + "@fastify/static": "^6.12.0", + "@janhq/core": "link:./core", + "dotenv": "^16.3.1", + "fastify": "^4.24.3", + "request": "^2.88.2", + "request-progress": "^3.0.0" }, "devDependencies": { "@types/body-parser": "^1.19.5", "@types/npmcli__arborist": "^5.6.4", "@typescript-eslint/eslint-plugin": "^6.7.3", "@typescript-eslint/parser": "^6.7.3", - "dotenv": "^16.3.1", "eslint-plugin-react": "^7.33.2", - "fastify": "^4.24.3", - "nodemon": "^3.0.1", - "run-script-os": "^1.1.6" - }, - "installConfig": { - "hoistingLimits": "workspaces" + "run-script-os": "^1.1.6", + "typescript": "^5.2.2" } } diff --git a/server/tsconfig.json b/server/tsconfig.json index 3363fdba6..2c4fc4a64 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -13,10 +13,12 @@ "allowJs": true, "skipLibCheck": true, "paths": { "*": ["node_modules/*"] }, - "typeRoots": ["node_modules/@types"] + "typeRoots": ["node_modules/@types"], + "ignoreDeprecations": "5.0", + "declaration": true }, // "sourceMap": true, - + "include": ["./**/*.ts"], "exclude": ["core", "build", "dist", "tests", "node_modules"] } diff --git a/server/v1/assistants/index.ts b/server/v1/assistants/index.ts deleted file mode 100644 index c722195d0..000000000 --- a/server/v1/assistants/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' - -const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { - //TODO: Add controllers for assistants here - // app.get("/", controller) - // app.post("/", controller) -} -export default router; \ No newline at end of file diff --git a/server/v1/chat/index.ts b/server/v1/chat/index.ts deleted file mode 100644 index cb5fbf120..000000000 --- a/server/v1/chat/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' - -const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { - //TODO: Add controllers for here - // app.get("/", controller) - - app.post("/", (req, res) => { - req.body - }) -} -export default router; \ No newline at end of file diff --git a/server/v1/index.ts b/server/v1/index.ts deleted file mode 100644 index 89d73200b..000000000 --- a/server/v1/index.ts +++ /dev/null @@ -1,37 +0,0 @@ -import assistantsAPI from './assistants' -import chatCompletionAPI from './chat' -import modelsAPI from './models' -import threadsAPI from './threads' - -import { FastifyInstance, FastifyPluginAsync } from 'fastify' - -const router: FastifyPluginAsync = async (app: FastifyInstance, opts) => { - app.register( - assistantsAPI, - { - prefix: "/assistants" - } - ) - - app.register( - chatCompletionAPI, - { - prefix: "/chat/completion" - } - ) - - app.register( - modelsAPI, - { - prefix: "/models" - } - ) - - app.register( - threadsAPI, - { - prefix: "/threads" - } - ) -} -export default router; \ No newline at end of file diff --git a/server/v1/models/downloadModel.ts b/server/v1/models/downloadModel.ts deleted file mode 100644 index d564a2207..000000000 --- a/server/v1/models/downloadModel.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { RouteHandlerMethod, FastifyRequest, FastifyReply } from 'fastify' -import { MODEL_FOLDER_PATH } from "./index" -import fs from 'fs/promises' - -const controller: RouteHandlerMethod = async (req: FastifyRequest, res: FastifyReply) => { - //TODO: download models impl - //Mirror logic from JanModelExtension.downloadModel? - let model = req.body.model; - - // Fetching logic - // const directoryPath = join(MODEL_FOLDER_PATH, model.id) - // await fs.mkdir(directoryPath) - - // const path = join(directoryPath, model.id) - // downloadFile(model.source_url, path) - // TODO: Different model downloader from different model vendor - - res.status(200).send({ - status: "Ok" - }) -} - -export default controller; \ No newline at end of file diff --git a/server/v1/models/index.ts b/server/v1/models/index.ts deleted file mode 100644 index 22c551300..000000000 --- a/server/v1/models/index.ts +++ /dev/null @@ -1,61 +0,0 @@ - -export const MODEL_FOLDER_PATH = "./data/models" -export const _modelMetadataFileName = 'model.json' - -import fs from 'fs/promises' -import { Model } from '@janhq/core' -import { join } from 'path' - -// map string => model object -let modelIndex = new Map(); -async function buildModelIndex(){ - let modelIds = await fs.readdir(MODEL_FOLDER_PATH); - // TODO: read modelFolders to get model info, mirror JanModelExtension? - try{ - for(let modelId in modelIds){ - let path = join(MODEL_FOLDER_PATH, modelId) - let fileData = await fs.readFile(join(path, _modelMetadataFileName)) - modelIndex.set(modelId, JSON.parse(fileData.toString("utf-8")) as Model) - } - } - catch(err){ - console.error("build model index failed. ", err); - } -} -buildModelIndex() - -import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' -import downloadModelController from './downloadModel' -import { startModel, stopModel } from './modelOp' - -const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { - //TODO: Add controllers declaration here - - ///////////// CRUD //////////////// - // Model listing - app.get("/", async (req, res) => { - res.status(200).send( - modelIndex.values() - ) - }) - - // Retrieve model info - app.get("/:id", (req, res) => { - res.status(200).send( - modelIndex.get(req.params.id) - ) - }) - - // Delete model - app.delete("/:id", (req, res) => { - modelIndex.delete(req.params) - - // TODO: delete on disk - }) - - ///////////// Other ops //////////////// - app.post("/", downloadModelController) - app.put("/start", startModel) - app.put("/stop", stopModel) -} -export default router; \ No newline at end of file diff --git a/server/v1/models/modelOp.ts b/server/v1/models/modelOp.ts deleted file mode 100644 index f2c7ffe75..000000000 --- a/server/v1/models/modelOp.ts +++ /dev/null @@ -1,11 +0,0 @@ -import {FastifyRequest, FastifyReply} from 'fastify' - -export async function startModel(req: FastifyRequest, res: FastifyReply): Promise { - - -} - -export async function stopModel(req: FastifyRequest, res: FastifyReply): Promise { - - -} \ No newline at end of file diff --git a/server/v1/threads/index.ts b/server/v1/threads/index.ts deleted file mode 100644 index e63f9e8d8..000000000 --- a/server/v1/threads/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { FastifyInstance, FastifyPluginAsync, FastifyPluginOptions } from 'fastify' - -const router: FastifyPluginAsync = async (app: FastifyInstance, opts: FastifyPluginOptions) => { - //TODO: Add controllers declaration here - - // app.get() -} -export default router; \ No newline at end of file diff --git a/web/extension/ExtensionManager.ts b/web/extension/ExtensionManager.ts index 4a53cb491..43847f344 100644 --- a/web/extension/ExtensionManager.ts +++ b/web/extension/ExtensionManager.ts @@ -81,7 +81,10 @@ export class ExtensionManager { */ async activateExtension(extension: Extension) { // Import class - await import(/* webpackIgnore: true */ extension.url).then( + const extensionUrl = window.electronAPI + ? extension.url + : extension.url.replace('extension://', `${API_BASE_URL}/extensions/`) + await import(/* webpackIgnore: true */ extensionUrl).then( (extensionClass) => { // Register class if it has a default export if ( diff --git a/web/hooks/useEngineSettings.ts b/web/hooks/useEngineSettings.ts index 14f32d4b4..349275e96 100644 --- a/web/hooks/useEngineSettings.ts +++ b/web/hooks/useEngineSettings.ts @@ -2,11 +2,14 @@ import { fs, joinPath } from '@janhq/core' export const useEngineSettings = () => { const readOpenAISettings = async () => { - const settings = await fs.readFile( - await joinPath(['engines', 'openai.json']) + if (!fs.existsSync(await joinPath(['file://engines', 'openai.json']))) + return {} + const settings = await fs.readFileSync( + await joinPath(['file://engines', 'openai.json']), + 'utf-8' ) if (settings) { - return JSON.parse(settings) + return typeof settings === 'object' ? settings : JSON.parse(settings) } return {} } @@ -17,8 +20,8 @@ export const useEngineSettings = () => { }) => { const settings = await readOpenAISettings() settings.api_key = apiKey - await fs.writeFile( - await joinPath(['engines', 'openai.json']), + await fs.writeFileSync( + await joinPath(['file://engines', 'openai.json']), JSON.stringify(settings) ) } diff --git a/web/hooks/useSendChatMessage.ts b/web/hooks/useSendChatMessage.ts index fb9761d60..f7643dfea 100644 --- a/web/hooks/useSendChatMessage.ts +++ b/web/hooks/useSendChatMessage.ts @@ -13,7 +13,6 @@ import { events, Model, ConversationalExtension, - ModelRuntimeParams, } from '@janhq/core' import { useAtom, useAtomValue, useSetAtom } from 'jotai' @@ -173,7 +172,7 @@ export default function useSendChatMessage() { updateThreadInitSuccess(activeThread.id) updateThread(updatedThread) - extensionManager + await extensionManager .get(ExtensionType.Conversational) ?.saveThread(updatedThread) } diff --git a/web/next.config.js b/web/next.config.js index b5a583f18..455ba70fc 100644 --- a/web/next.config.js +++ b/web/next.config.js @@ -32,6 +32,7 @@ const nextConfig = { JSON.stringify(process.env.ANALYTICS_ID) ?? JSON.stringify('xxx'), ANALYTICS_HOST: JSON.stringify(process.env.ANALYTICS_HOST) ?? JSON.stringify('xxx'), + API_BASE_URL: JSON.stringify('http://localhost:1337'), isMac: process.platform === 'darwin', isWindows: process.platform === 'win32', isLinux: process.platform === 'linux', diff --git a/web/screens/Chat/Sidebar/index.tsx b/web/screens/Chat/Sidebar/index.tsx index 35b2eb765..81833cd1d 100644 --- a/web/screens/Chat/Sidebar/index.tsx +++ b/web/screens/Chat/Sidebar/index.tsx @@ -51,6 +51,7 @@ const Sidebar: React.FC = () => { let filePath = undefined const assistantId = activeThread.assistants[0]?.assistant_id switch (type) { + case 'Engine': case 'Thread': filePath = await joinPath(['threads', activeThread.id]) break @@ -83,6 +84,7 @@ const Sidebar: React.FC = () => { let filePath = undefined const assistantId = activeThread.assistants[0]?.assistant_id switch (type) { + case 'Engine': case 'Thread': filePath = await joinPath(['threads', activeThread.id, 'thread.json']) break diff --git a/web/screens/Settings/Models/index.tsx b/web/screens/Settings/Models/index.tsx index 79859c275..3c5a0c6e3 100644 --- a/web/screens/Settings/Models/index.tsx +++ b/web/screens/Settings/Models/index.tsx @@ -15,7 +15,7 @@ export default function Models() { const [searchValue, setsearchValue] = useState('') const filteredDownloadedModels = downloadedModels.filter((x) => { - return x.name.toLowerCase().includes(searchValue.toLowerCase()) + return x.name?.toLowerCase().includes(searchValue.toLowerCase()) }) return ( diff --git a/web/services/cloudNativeService.ts b/web/services/cloudNativeService.ts deleted file mode 100644 index a300ac02d..000000000 --- a/web/services/cloudNativeService.ts +++ /dev/null @@ -1,76 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { toast } from 'react-toastify' -const API_BASE_PATH: string = '/api/v1' - -export function openExternalUrl(url: string) { - window?.open(url, '_blank') -} - -export async function appVersion() { - return Promise.resolve(VERSION) -} - -export function invokeExtensionFunc( - modulePath: string, - extensionFunc: string, - ...args: any -): Promise { - return fetchApi(modulePath, extensionFunc, args).catch((err: Error) => { - throw err - }) -} - -export async function downloadFile(downloadUrl: string, fileName: string) { - return fetchApi('', 'downloadFile', { - downloadUrl: downloadUrl, - fileName: fileName, - }).catch((err: Error) => { - throw err - }) -} - -export async function deleteFile(fileName: string) { - return fetchApi('', 'deleteFile', fileName).catch((err: Error) => { - throw err - }) -} - -export async function fetchApi( - modulePath: string, - extensionFunc: string, - args: any -): Promise { - const response = await fetch(API_BASE_PATH + '/invokeFunction', { - method: 'POST', - body: JSON.stringify({ - modulePath: modulePath, - method: extensionFunc, - args: args, - }), - headers: { contentType: 'application/json', Authorization: '' }, - }) - - if (!response.ok) { - const json = await response.json() - if (json && json.error) { - toast.error(json.error, { - position: 'bottom-left', - autoClose: 5000, - hideProgressBar: false, - closeOnClick: true, - pauseOnHover: true, - draggable: true, - progress: undefined, - theme: 'light', - }) - } - return null - } - const text = await response.text() - try { - const json = JSON.parse(text) - return Promise.resolve(json) - } catch (err) { - return Promise.resolve(text) - } -} diff --git a/web/services/coreService.ts b/web/services/coreService.ts index 89b6dcfd1..c010c6cec 100644 --- a/web/services/coreService.ts +++ b/web/services/coreService.ts @@ -1,5 +1,5 @@ -import * as restAPI from './cloudNativeService' import { EventEmitter } from './eventsService' +import { restAPI } from './restService' export const setupCoreServices = () => { if (typeof window === 'undefined') { console.debug('undefine', window) @@ -10,9 +10,7 @@ export const setupCoreServices = () => { if (!window.core) { window.core = { events: new EventEmitter(), - api: window.electronAPI ?? { - ...restAPI, - }, + api: window.electronAPI ?? restAPI, } } } diff --git a/web/services/extensionService.ts b/web/services/extensionService.ts index d79bda065..b18fb3cc1 100644 --- a/web/services/extensionService.ts +++ b/web/services/extensionService.ts @@ -15,13 +15,10 @@ export const isCoreExtensionInstalled = () => { return true } export const setupBaseExtensions = async () => { - if ( - typeof window === 'undefined' || - typeof window.electronAPI === 'undefined' - ) { + if (typeof window === 'undefined') { return } - const baseExtensions = await window.electronAPI.baseExtensions() + const baseExtensions = await window.core?.api.baseExtensions() if ( !extensionManager.get(ExtensionType.Conversational) || diff --git a/web/services/restService.ts b/web/services/restService.ts new file mode 100644 index 000000000..6fd7ad069 --- /dev/null +++ b/web/services/restService.ts @@ -0,0 +1,59 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { + AppRoute, + DownloadRoute, + ExtensionRoute, + FileSystemRoute, +} from '@janhq/core' + +import { safeJsonParse } from '@/utils/json' + +// Function to open an external URL in a new browser window +export function openExternalUrl(url: string) { + window?.open(url, '_blank') +} + +// Async function to get the application version +export async function appVersion() { + return Promise.resolve(VERSION) +} + +// Define API routes based on different route types +export const APIRoutes = [ + ...Object.values(AppRoute).map((r) => ({ path: 'app', route: r })), + ...Object.values(DownloadRoute).map((r) => ({ path: `download`, route: r })), + ...Object.values(ExtensionRoute).map((r) => ({ + path: `extension`, + route: r, + })), + ...Object.values(FileSystemRoute).map((r) => ({ path: `fs`, route: r })), +] + +// Define the restAPI object with methods for each API route +export const restAPI = { + ...Object.values(APIRoutes).reduce((acc, proxy) => { + return { + ...acc, + [proxy.route]: (...args: any) => { + // For each route, define a function that sends a request to the API + return fetch(`${API_BASE_URL}/v1/${proxy.path}/${proxy.route}`, { + method: 'POST', + body: JSON.stringify(args), + headers: { contentType: 'application/json' }, + }).then(async (res) => { + try { + if (proxy.path === 'fs') { + const text = await res.text() + return safeJsonParse(text) ?? text + } + return await res.json() + } catch (err) { + console.debug('Op: ', proxy, args, err) + } + }) + }, + } + }, {}), + openExternalUrl, + appVersion, +} diff --git a/web/types/index.d.ts b/web/types/index.d.ts index bd46b8a6f..328fc8f53 100644 --- a/web/types/index.d.ts +++ b/web/types/index.d.ts @@ -8,6 +8,7 @@ declare global { declare const VERSION: string declare const ANALYTICS_ID: string declare const ANALYTICS_HOST: string + declare const API_BASE_URL: string declare const isMac: boolean declare const isWindows: boolean declare const isLinux: boolean diff --git a/web/utils/json.ts b/web/utils/json.ts new file mode 100644 index 000000000..89115aa5f --- /dev/null +++ b/web/utils/json.ts @@ -0,0 +1,9 @@ +export const safeJsonParse = (str: string) => { + try { + const jsonValue: T = JSON.parse(str) + + return jsonValue + } catch { + return undefined + } +}