Skip to content

Commit

Permalink
fix: incompatible browser dependency (janhq#2439)
Browse files Browse the repository at this point in the history
* fix: incompatible browser dependency

* fix: update model extension to use rollup

* fix: test timeout
  • Loading branch information
louis-jan authored Mar 21, 2024
1 parent 5b1b730 commit ff7ec39
Show file tree
Hide file tree
Showing 40 changed files with 174 additions and 145 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ build: check-file-counts
clean:
ifeq ($(OS),Windows_NT)
powershell -Command "Get-ChildItem -Path . -Include node_modules, .next, dist, build, out -Recurse -Directory | Remove-Item -Recurse -Force"
powershell -Command "Get-ChildItem -Path . -Include package-lock.json -Recurse -File | Remove-Item -Recurse -Force"
powershell -Command "Remove-Item -Recurse -Force ./pre-install/*.tgz"
powershell -Command "Remove-Item -Recurse -Force ./electron/pre-install/*.tgz"
rmdir /s /q "%USERPROFILE%\jan\extensions"
powershell -Command "if (Test-Path \"$($env:USERPROFILE)\jan\extensions\") { Remove-Item -Path \"$($env:USERPROFILE)\jan\extensions\" -Recurse -Force }"
else ifeq ($(shell uname -s),Linux)
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
find . -name ".next" -type d -exec rm -rf '{}' +
find . -name "dist" -type d -exec rm -rf '{}' +
find . -name "build" -type d -exec rm -rf '{}' +
find . -name "out" -type d -exec rm -rf '{}' +
find . -name "packake-lock.json" -type f -exec rm -rf '{}' +
rm -rf ./pre-install/*.tgz
rm -rf ./electron/pre-install/*.tgz
rm -rf "~/jan/extensions"
Expand All @@ -72,6 +74,7 @@ else
find . -name "dist" -type d -exec rm -rf '{}' +
find . -name "build" -type d -exec rm -rf '{}' +
find . -name "out" -type d -exec rm -rf '{}' +
find . -name "packake-lock.json" -type f -exec rm -rf '{}' +
rm -rf ./pre-install/*.tgz
rm -rf ./electron/pre-install/*.tgz
rm -rf ~/jan/extensions
Expand Down
16 changes: 6 additions & 10 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
],
"homepage": "https://jan.ai",
"license": "AGPL-3.0",
"main": "dist/core.umd.js",
"module": "dist/core.es5.js",
"main": "dist/core.es5.js",
"module": "dist/core.cjs.js",
"typings": "dist/types/index.d.ts",
"files": [
"dist",
"types"
],
"author": "Jan <[email protected]>",
"exports": {
".": "./dist/core.umd.js",
"./sdk": "./dist/core.umd.js",
".": "./dist/core.es5.js",
"./node": "./dist/node/index.cjs.js"
},
"typesVersions": {
Expand All @@ -27,24 +26,21 @@
"./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'",
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
"test": "jest",
"prebuild": "rimraf dist",
"build": "tsc --module commonjs && rollup -c rollup.config.ts",
"start": "rollup -c rollup.config.ts -w"
},
"devDependencies": {
"@rollup/plugin-replace": "^5.0.5",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.4",
"eslint": "8.57.0",
Expand All @@ -63,6 +59,6 @@
},
"dependencies": {
"rxjs": "^7.8.1",
"ulid": "^2.3.0"
"ulidx": "^2.3.0"
}
}
19 changes: 12 additions & 7 deletions core/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import commonjs from 'rollup-plugin-commonjs'
import sourceMaps from 'rollup-plugin-sourcemaps'
import typescript from 'rollup-plugin-typescript2'
import json from 'rollup-plugin-json'
import replace from '@rollup/plugin-replace'

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 },
// { file: pkg.main, name: libraryName, format: 'umd', sourcemap: true },
{ file: pkg.main, format: 'es', sourcemap: true },
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: ['path'],
Expand All @@ -30,7 +29,13 @@ export default [
// 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(),
replace({
'node:crypto': 'crypto',
'delimiters': ['"', '"'],
}),
resolve({
browser: true,
}),

// Resolve source maps to the original source
sourceMaps(),
Expand All @@ -46,7 +51,7 @@ export default [
'pacote',
'@types/pacote',
'@npmcli/arborist',
'ulid',
'ulidx',
'node-fetch',
'fs',
'request',
Expand All @@ -64,7 +69,7 @@ export default [
// Allow json resolution
json(),
// Compile TypeScript files
typescript({ useTsconfigDeclarationDir: true, exclude: ['src/*.ts', 'src/extensions/**'] }),
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
Expand Down
28 changes: 14 additions & 14 deletions core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const executeOnMain: (extension: string, method: string, ...args: any[]) => Prom
extension,
method,
...args
) => global.core?.api?.invokeExtensionFunc(extension, method, ...args)
) => globalThis.core?.api?.invokeExtensionFunc(extension, method, ...args)

/**
* Downloads a file from a URL and saves it to the local file system.
Expand All @@ -26,44 +26,44 @@ const executeOnMain: (extension: string, method: string, ...args: any[]) => Prom
const downloadFile: (downloadRequest: DownloadRequest, network?: NetworkConfig) => Promise<any> = (
downloadRequest,
network
) => global.core?.api?.downloadFile(downloadRequest, network)
) => globalThis.core?.api?.downloadFile(downloadRequest, network)

/**
* Aborts the download of a specific file.
* @param {string} fileName - The name of the file whose download is to be aborted.
* @returns {Promise<any>} A promise that resolves when the download has been aborted.
*/
const abortDownload: (fileName: string) => Promise<any> = (fileName) =>
global.core.api?.abortDownload(fileName)
globalThis.core.api?.abortDownload(fileName)

/**
* Gets Jan's data folder path.
*
* @returns {Promise<string>} A Promise that resolves with Jan's data folder path.
*/
const getJanDataFolderPath = (): Promise<string> => global.core.api?.getJanDataFolderPath()
const getJanDataFolderPath = (): Promise<string> => globalThis.core.api?.getJanDataFolderPath()

/**
* Opens the file explorer at a specific path.
* @param {string} path - The path to open in the file explorer.
* @returns {Promise<any>} A promise that resolves when the file explorer is opened.
*/
const openFileExplorer: (path: string) => Promise<any> = (path) =>
global.core.api?.openFileExplorer(path)
globalThis.core.api?.openFileExplorer(path)

/**
* Joins multiple paths together.
* @param paths - The paths to join.
* @returns {Promise<string>} A promise that resolves with the joined path.
*/
const joinPath: (paths: string[]) => Promise<string> = (paths) => global.core.api?.joinPath(paths)
const joinPath: (paths: string[]) => Promise<string> = (paths) => globalThis.core.api?.joinPath(paths)

/**
* Retrive the basename from an url.
* @param path - The path to retrieve.
* @returns {Promise<string>} A promise that resolves with the basename.
*/
const baseName: (paths: string) => Promise<string> = (path) => global.core.api?.baseName(path)
const baseName: (paths: string) => Promise<string> = (path) => globalThis.core.api?.baseName(path)

/**
* Opens an external URL in the default web browser.
Expand All @@ -72,28 +72,28 @@ const baseName: (paths: string) => Promise<string> = (path) => global.core.api?.
* @returns {Promise<any>} - A promise that resolves when the URL has been successfully opened.
*/
const openExternalUrl: (url: string) => Promise<any> = (url) =>
global.core.api?.openExternalUrl(url)
globalThis.core.api?.openExternalUrl(url)

/**
* Gets the resource path of the application.
*
* @returns {Promise<string>} - A promise that resolves with the resource path.
*/
const getResourcePath: () => Promise<string> = () => global.core.api?.getResourcePath()
const getResourcePath: () => Promise<string> = () => globalThis.core.api?.getResourcePath()

/**
* Gets the user's home path.
* @returns return user's home path
*/
const getUserHomePath = (): Promise<string> => global.core.api?.getUserHomePath()
const getUserHomePath = (): Promise<string> => globalThis.core.api?.getUserHomePath()

/**
* Log to file from browser processes.
*
* @param message - Message to log.
*/
const log: (message: string, fileName?: string) => void = (message, fileName) =>
global.core.api?.log(message, fileName)
globalThis.core.api?.log(message, fileName)

/**
* Check whether the path is a subdirectory of another path.
Expand All @@ -104,14 +104,14 @@ const log: (message: string, fileName?: string) => void = (message, fileName) =>
* @returns {Promise<boolean>} - A promise that resolves with a boolean indicating whether the path is a subdirectory.
*/
const isSubdirectory: (from: string, to: string) => Promise<boolean> = (from: string, to: string) =>
global.core.api?.isSubdirectory(from, to)
globalThis.core.api?.isSubdirectory(from, to)

/**
* Get system information
* @returns {Promise<any>} - A promise that resolves with the system information.
*/
const systemInformation: () => Promise<SystemInformation> = () =>
global.core.api?.systemInformation()
globalThis.core.api?.systemInformation()

/**
* Show toast message from browser processes.
Expand All @@ -120,7 +120,7 @@ const systemInformation: () => Promise<SystemInformation> = () =>
* @returns
*/
const showToast: (title: string, message: string) => void = (title, message) =>
global.core.api?.showToast(title, message)
globalThis.core.api?.showToast(title, message)
/**
* Register extension point function type definition
*/
Expand Down
6 changes: 3 additions & 3 deletions core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param handler The handler function to call when the event is observed.
*/
const on: (eventName: string, handler: Function) => void = (eventName, handler) => {
global.core?.events?.on(eventName, handler)
globalThis.core?.events?.on(eventName, handler)
}

/**
Expand All @@ -15,7 +15,7 @@ const on: (eventName: string, handler: Function) => void = (eventName, handler)
* @param handler The handler function to call when the event is observed.
*/
const off: (eventName: string, handler: Function) => void = (eventName, handler) => {
global.core?.events?.off(eventName, handler)
globalThis.core?.events?.off(eventName, handler)
}

/**
Expand All @@ -25,7 +25,7 @@ const off: (eventName: string, handler: Function) => void = (eventName, handler)
* @param object The object to pass to the event callback.
*/
const emit: (eventName: string, object: any) => void = (eventName, object) => {
global.core?.events?.emit(eventName, object)
globalThis.core?.events?.emit(eventName, object)
}

export const events = {
Expand Down
2 changes: 1 addition & 1 deletion core/src/extensions/ai-engines/OAIEngine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { requestInference } from './helpers/sse'
import { ulid } from 'ulid'
import { ulid } from 'ulidx'
import { AIEngine } from './AIEngine'
import {
ChatCompletionRole,
Expand Down
30 changes: 15 additions & 15 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FileStat } from './types'
* Writes data to a file at the specified path.
* @returns {Promise<any>} A Promise that resolves when the file is written successfully.
*/
const writeFileSync = (...args: any[]) => global.core.api?.writeFileSync(...args)
const writeFileSync = (...args: any[]) => globalThis.core.api?.writeFileSync(...args)

/**
* Writes blob data to a file at the specified path.
Expand All @@ -13,52 +13,52 @@ const writeFileSync = (...args: any[]) => global.core.api?.writeFileSync(...args
* @returns
*/
const writeBlob: (path: string, data: string) => Promise<any> = (path, data) =>
global.core.api?.writeBlob(path, data)
globalThis.core.api?.writeBlob(path, data)

/**
* Reads the contents of a file at the specified path.
* @returns {Promise<any>} A Promise that resolves with the contents of the file.
*/
const readFileSync = (...args: any[]) => global.core.api?.readFileSync(...args)
const readFileSync = (...args: any[]) => globalThis.core.api?.readFileSync(...args)
/**
* Check whether the file exists
* @param {string} path
* @returns {boolean} A boolean indicating whether the path is a file.
*/
const existsSync = (...args: any[]) => global.core.api?.existsSync(...args)
const existsSync = (...args: any[]) => globalThis.core.api?.existsSync(...args)
/**
* List the directory files
* @returns {Promise<any>} A Promise that resolves with the contents of the directory.
*/
const readdirSync = (...args: any[]) => global.core.api?.readdirSync(...args)
const readdirSync = (...args: any[]) => globalThis.core.api?.readdirSync(...args)
/**
* Creates a directory at the specified path.
* @returns {Promise<any>} A Promise that resolves when the directory is created successfully.
*/
const mkdirSync = (...args: any[]) => global.core.api?.mkdirSync(...args)
const mkdirSync = (...args: any[]) => globalThis.core.api?.mkdirSync(...args)

const mkdir = (...args: any[]) => global.core.api?.mkdir(...args)
const mkdir = (...args: any[]) => globalThis.core.api?.mkdir(...args)

/**
* Removes a directory at the specified path.
* @returns {Promise<any>} A Promise that resolves when the directory is removed successfully.
*/
const rmdirSync = (...args: any[]) =>
global.core.api?.rmdirSync(...args, { recursive: true, force: true })
globalThis.core.api?.rmdirSync(...args, { recursive: true, force: true })

const rm = (path: string) => global.core.api?.rm(path)
const rm = (path: string) => globalThis.core.api?.rm(path)

/**
* Deletes a file from the local file system.
* @param {string} path - The path of the file to delete.
* @returns {Promise<any>} A Promise that resolves when the file is deleted.
*/
const unlinkSync = (...args: any[]) => global.core.api?.unlinkSync(...args)
const unlinkSync = (...args: any[]) => globalThis.core.api?.unlinkSync(...args)

/**
* Appends data to a file at the specified path.
*/
const appendFileSync = (...args: any[]) => global.core.api?.appendFileSync(...args)
const appendFileSync = (...args: any[]) => globalThis.core.api?.appendFileSync(...args)

/**
* Synchronizes a file from a source path to a destination path.
Expand All @@ -67,15 +67,15 @@ const appendFileSync = (...args: any[]) => global.core.api?.appendFileSync(...ar
* @returns {Promise<any>} - A promise that resolves when the file has been successfully synchronized.
*/
const syncFile: (src: string, dest: string) => Promise<any> = (src, dest) =>
global.core.api?.syncFile(src, dest)
globalThis.core.api?.syncFile(src, dest)

/**
* Copy file sync.
*/
const copyFileSync = (...args: any[]) => global.core.api?.copyFileSync(...args)
const copyFileSync = (...args: any[]) => globalThis.core.api?.copyFileSync(...args)

const copyFile: (src: string, dest: string) => Promise<void> = (src, dest) =>
global.core.api?.copyFile(src, dest)
globalThis.core.api?.copyFile(src, dest)

/**
* Gets the file's stats.
Expand All @@ -87,7 +87,7 @@ const copyFile: (src: string, dest: string) => Promise<void> = (src, dest) =>
const fileStat: (path: string, outsideJanDataFolder?: boolean) => Promise<FileStat | undefined> = (
path,
outsideJanDataFolder
) => global.core.api?.fileStat(path, outsideJanDataFolder)
) => globalThis.core.api?.fileStat(path, outsideJanDataFolder)

// TODO: Export `dummy` fs functions automatically
// Currently adding these manually
Expand Down
2 changes: 1 addition & 1 deletion core/src/node/api/restful/helper/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const createMessage = async (threadId: string, message: any) => {
const threadMessagesFileName = 'messages.jsonl'

try {
const { ulid } = require('ulid')
const { ulid } = require('ulidx')
const msgId = ulid()
const createdAt = Date.now()
const threadMessage: ThreadMessage = {
Expand Down
Loading

0 comments on commit ff7ec39

Please sign in to comment.