Skip to content

Commit

Permalink
feat: Jan Server, API and decoupled clients (janhq#948)
Browse files Browse the repository at this point in the history
* chore: expose fs apis

* chore: correct electron import path

* update download api

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

* update chat_completion

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

* fix electron import

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

* feat: adding API support at 1337 (janhq#991)

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

* feat: Add /chat/completion api and handler

* chore: add todo for modelList

* chore: read engine.json for openai chat_completion (janhq#1030)

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

* refactor: move routes to shared node module

* refactor: exported modules from core with types (janhq#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 (janhq#1210)

* chore: resolve main

* chore: update makefile

---------

Signed-off-by: James <[email protected]>
Co-authored-by: James <[email protected]>
Co-authored-by: NamH <[email protected]>
Co-authored-by: hiro <[email protected]>
  • Loading branch information
4 people authored Dec 28, 2023
1 parent cfbc567 commit 5250061
Show file tree
Hide file tree
Showing 71 changed files with 1,258 additions and 887 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
89 changes: 64 additions & 25 deletions core/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/**',
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(),
],
},
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(),
{
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(),
],
}
// Resolve source maps to the original source
sourceMaps(),
],
},
]
35 changes: 20 additions & 15 deletions core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
export enum AppRoute {
appDataPath = 'appDataPath',
appVersion = 'appVersion',
getResourcePath = 'getResourcePath',
openExternalUrl = 'openExternalUrl',
openAppDirectory = 'openAppDirectory',
openFileExplore = 'openFileExplorer',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)]
73 changes: 29 additions & 44 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
@@ -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<any>} A Promise that resolves when the file is written successfully.
*/
const writeFile: (path: string, data: string) => Promise<any> = (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<boolean> => 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<any>} A Promise that resolves with the contents of the file.
*/
const readFile: (path: string) => Promise<any> = (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<boolean> => 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<any>} A Promise that resolves with the contents of the directory.
*/
const listFiles: (path: string) => Promise<any> = (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<any>} A Promise that resolves when the directory is created successfully.
*/
const mkdir: (path: string) => Promise<any> = (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<any>} A Promise that resolves when the directory is removed successfully.
*/
const rmdir: (path: string) => Promise<any> = (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<any>} A Promise that resolves when the file is deleted.
*/
const deleteFile: (path: string) => Promise<any> = (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<any> = (path, data) =>
global.core.api?.appendFile(path, data)

const copyFile: (src: string, dest: string) => Promise<any> = (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<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)

/**
* Reads a file line by line.
* @param {string} path - The path of the file to read.
* @returns {Promise<any>} A promise that resolves to the lines of the file.
* Copy file sync.
*/
const readLineByLine: (path: string) => Promise<any> = (path) =>
global.core.api?.readLineByLine(path)
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,
}
8 changes: 8 additions & 0 deletions core/src/node/api/HttpServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface HttpServer {
post: (route: string, handler: (req: any, res: any) => Promise<any>) => void
get: (route: string, handler: (req: any, res: any) => Promise<any>) => void
patch: (route: string, handler: (req: any, res: any) => Promise<any>) => void
put: (route: string, handler: (req: any, res: any) => Promise<any>) => void
delete: (route: string, handler: (req: any, res: any) => Promise<any>) => void
register: (router: any, opts?: any) => void
}
Loading

0 comments on commit 5250061

Please sign in to comment.