Skip to content

Commit

Permalink
Merge pull request #3158 from janhq/dev
Browse files Browse the repository at this point in the history
chore: release 0.5.2 to main
  • Loading branch information
Van-QA authored Jul 15, 2024
2 parents 852ff18 + a36b3fe commit 506cbb8
Show file tree
Hide file tree
Showing 44 changed files with 638 additions and 306 deletions.
16 changes: 0 additions & 16 deletions core/src/browser/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,6 @@ const unlinkSync = (...args: any[]) => globalThis.core.api?.unlinkSync(...args)
*/
const appendFileSync = (...args: any[]) => globalThis.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) =>
globalThis.core.api?.syncFile(src, dest)

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

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

Expand All @@ -95,9 +81,7 @@ export const fs = {
rm,
unlinkSync,
appendFileSync,
copyFileSync,
copyFile,
syncFile,
fileStat,
writeBlob,
}
3 changes: 2 additions & 1 deletion core/src/node/api/processors/download.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve, sep } from 'path'
import { DownloadEvent } from '../../../types/api'
import { normalizeFilePath } from '../../helper/path'
import { normalizeFilePath, validatePath } from '../../helper/path'
import { getJanDataFolderPath } from '../../helper'
import { DownloadManager } from '../../helper/download'
import { createWriteStream, renameSync } from 'fs'
Expand Down Expand Up @@ -37,6 +37,7 @@ export class Downloader implements Processor {
const modelId = array.pop() ?? ''

const destination = resolve(getJanDataFolderPath(), normalizedPath)
validatePath(destination)
const rq = request({ url, strictSSL, proxy })

// Put request to download manager instance
Expand Down
39 changes: 29 additions & 10 deletions core/src/node/api/processors/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path'
import { normalizeFilePath } from '../../helper/path'
import { join, resolve } from 'path'
import { normalizeFilePath, validatePath } from '../../helper/path'
import { getJanDataFolderPath } from '../../helper'
import { Processor } from './Processor'
import fs from 'fs'
Expand All @@ -15,17 +15,29 @@ export class FileSystem implements Processor {
process(route: string, ...args: any): any {
const instance = this as any
const func = instance[route]

if (func) {
return func(...args)
} else {
return import(FileSystem.moduleName).then((mdl) =>
mdl[route](
...args.map((arg: any) => {
return typeof arg === 'string' &&
(arg.startsWith(`file:/`) || arg.startsWith(`file:\\`))
? join(getJanDataFolderPath(), normalizeFilePath(arg))
: arg
...args.map((arg: any, index: number) => {
if(index !== 0) {
return arg
}
if (index === 0 && typeof arg !== 'string') {
throw new Error(`Invalid argument ${JSON.stringify(args)}`)
}
const path =
(arg.startsWith(`file:/`) || arg.startsWith(`file:\\`))
? join(getJanDataFolderPath(), normalizeFilePath(arg))
: arg

if(path.startsWith(`http://`) || path.startsWith(`https://`)) {
return path
}
const absolutePath = resolve(path)
validatePath(absolutePath)
return absolutePath
})
)
)
Expand All @@ -42,8 +54,11 @@ export class FileSystem implements Processor {
path = join(getJanDataFolderPath(), normalizeFilePath(path))
}

const absolutePath = resolve(path)
validatePath(absolutePath)

return new Promise((resolve, reject) => {
fs.rm(path, { recursive: true, force: true }, (err) => {
fs.rm(absolutePath, { recursive: true, force: true }, (err) => {
if (err) {
reject(err)
} else {
Expand All @@ -63,8 +78,11 @@ export class FileSystem implements Processor {
path = join(getJanDataFolderPath(), normalizeFilePath(path))
}

const absolutePath = resolve(path)
validatePath(absolutePath)

return new Promise((resolve, reject) => {
fs.mkdir(path, { recursive: true }, (err) => {
fs.mkdir(absolutePath, { recursive: true }, (err) => {
if (err) {
reject(err)
} else {
Expand All @@ -73,4 +91,5 @@ export class FileSystem implements Processor {
})
})
}

}
21 changes: 6 additions & 15 deletions core/src/node/api/processors/fsExt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path'
import fs from 'fs'
import { appResourcePath, normalizeFilePath } from '../../helper/path'
import { appResourcePath, normalizeFilePath, validatePath } from '../../helper/path'
import { getJanDataFolderPath, getJanDataFolderPath as getPath } from '../../helper'
import { Processor } from './Processor'
import { FileStat } from '../../../types'
Expand All @@ -18,19 +18,6 @@ export class FSExt implements Processor {
return func(...args)
}

// Handles the 'syncFile' IPC event. This event is triggered to synchronize a file from a source path to a destination path.
syncFile(src: string, dest: string) {
const reflect = require('@alumna/reflect')
return reflect({
src,
dest,
recursive: true,
delete: false,
overwrite: true,
errorOnExist: false,
})
}

// Handles the 'getJanDataFolderPath' IPC event. This event is triggered to get the user space path.
getJanDataFolderPath() {
return Promise.resolve(getPath())
Expand Down Expand Up @@ -70,14 +57,18 @@ export class FSExt implements Processor {
writeBlob(path: string, data: any) {
try {
const normalizedPath = normalizeFilePath(path)

const dataBuffer = Buffer.from(data, 'base64')
fs.writeFileSync(join(getJanDataFolderPath(), normalizedPath), dataBuffer)
const writePath = join(getJanDataFolderPath(), normalizedPath)
validatePath(writePath)
fs.writeFileSync(writePath, dataBuffer)
} catch (err) {
console.error(`writeFile ${path} result: ${err}`)
}
}

copyFile(src: string, dest: string): Promise<void> {
validatePath(dest)
return new Promise((resolve, reject) => {
fs.copyFile(src, dest, (err) => {
if (err) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/node/helper/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import childProcess from 'child_process'
const configurationFileName = 'settings.json'

// TODO: do no specify app name in framework module
const defaultJanDataFolder = join(os.homedir(), 'jan')
// TODO: do not default the os.homedir
const defaultJanDataFolder = join(os?.homedir() || '', 'jan')
const defaultAppConfig: AppConfiguration = {
data_folder: defaultJanDataFolder,
quick_ask: false,
Expand Down
2 changes: 1 addition & 1 deletion core/src/node/helper/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class DownloadManager {
// store the download information with key is model id
public downloadProgressMap: Record<string, DownloadState> = {}

// store the download infomation with key is normalized file path
// store the download information with key is normalized file path
public downloadInfo: Record<string, DownloadState> = {}

constructor() {
Expand Down
11 changes: 10 additions & 1 deletion core/src/node/helper/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'path'
import { join, resolve } from 'path'
import { getJanDataFolderPath } from './config'

/**
* Normalize file path
Expand Down Expand Up @@ -33,3 +34,11 @@ export async function appResourcePath(): Promise<string> {
// server
return join(global.core.appPath(), '../../..')
}

export function validatePath(path: string) {
const janDataFolderPath = getJanDataFolderPath()
const absolutePath = resolve(__dirname, path)
if (!absolutePath.startsWith(janDataFolderPath)) {
throw new Error(`Invalid path: ${absolutePath}`)
}
}
2 changes: 0 additions & 2 deletions core/src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export enum ExtensionRoute {
}
export enum FileSystemRoute {
appendFileSync = 'appendFileSync',
copyFileSync = 'copyFileSync',
unlinkSync = 'unlinkSync',
existsSync = 'existsSync',
readdirSync = 'readdirSync',
Expand All @@ -100,7 +99,6 @@ export enum FileSystemRoute {
writeFileSync = 'writeFileSync',
}
export enum FileManagerRoute {
syncFile = 'syncFile',
copyFile = 'copyFile',
getJanDataFolderPath = 'getJanDataFolderPath',
getResourcePath = 'getResourcePath',
Expand Down
1 change: 1 addition & 0 deletions core/src/types/assistant/assistantEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export type AssistantTool = {
type: string
enabled: boolean
useTimeWeightedRetriever?: boolean
settings: any
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/types/message/messageEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export enum MessageStatus {
export enum ErrorCode {
InvalidApiKey = 'invalid_api_key',

AuthenticationError = 'authentication_error',

InsufficientQuota = 'insufficient_quota',

InvalidRequestError = 'invalid_request_error',
Expand Down
32 changes: 31 additions & 1 deletion electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
* @module preload
*/

import { APIEvents, APIRoutes } from '@janhq/core/node'
import { APIEvents, APIRoutes, AppConfiguration, getAppConfigurations, updateAppConfiguration } from '@janhq/core/node'
import { contextBridge, ipcRenderer } from 'electron'
import { readdirSync } from 'fs'

const interfaces: { [key: string]: (...args: any[]) => any } = {}

// Loop over each route in APIRoutes
APIRoutes.forEach((method) => {
// For each method, create a function on the interfaces object
// This function invokes the method on the ipcRenderer with any provided arguments

interfaces[method] = (...args: any[]) => ipcRenderer.invoke(method, ...args)

})

// Loop over each method in APIEvents
Expand All @@ -22,6 +25,33 @@ APIEvents.forEach((method) => {
// The handler for the event is provided as an argument to the function
interfaces[method] = (handler: any) => ipcRenderer.on(method, handler)
})


interfaces['changeDataFolder'] = async path => {
const appConfiguration: AppConfiguration = await ipcRenderer.invoke('getAppConfigurations')
const currentJanDataFolder = appConfiguration.data_folder
appConfiguration.data_folder = path
const reflect = require('@alumna/reflect')
const { err } = await reflect({
src: currentJanDataFolder,
dest: path,
recursive: true,
delete: false,
overwrite: true,
errorOnExist: false,
})
if (err) {
console.error(err)
throw err
}
await ipcRenderer.invoke('updateAppConfiguration', appConfiguration)
}

interfaces['isDirectoryEmpty'] = async path => {
const dirChildren = await readdirSync(path)
return dirChildren.filter((x) => x !== '.DS_Store').length === 0
}

// Expose the 'interfaces' object in the main world under the name 'electronAPI'
// This allows the renderer process to access these methods directly
contextBridge.exposeInMainWorld('electronAPI', {
Expand Down
1 change: 1 addition & 0 deletions extensions/assistant-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export default class JanAssistantExtension extends AssistantExtension {
{
type: 'retrieval',
enabled: false,
useTimeWeightedRetriever: false,
settings: {
top_k: 2,
chunk_size: 1024,
Expand Down
12 changes: 8 additions & 4 deletions extensions/assistant-extension/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ export function toolRetrievalUpdateTextSplitter(
export async function toolRetrievalIngestNewDocument(
file: string,
model: string,
engine: string
engine: string,
useTimeWeighted: boolean
) {
const filePath = path.join(getJanDataFolderPath(), normalizeFilePath(file))
const threadPath = path.dirname(filePath.replace('files', ''))
retrieval.updateEmbeddingEngine(model, engine)
return retrieval
.ingestAgentKnowledge(filePath, `${threadPath}/memory`)
.ingestAgentKnowledge(filePath, `${threadPath}/memory`, useTimeWeighted)
.catch((err) => {
console.error(err)
})
Expand All @@ -33,8 +34,11 @@ export async function toolRetrievalLoadThreadMemory(threadId: string) {
})
}

export async function toolRetrievalQueryResult(query: string) {
return retrieval.generateResult(query).catch((err) => {
export async function toolRetrievalQueryResult(
query: string,
useTimeWeighted: boolean = false
) {
return retrieval.generateResult(query, useTimeWeighted).catch((err) => {
console.error(err)
})
}
Loading

0 comments on commit 506cbb8

Please sign in to comment.