Skip to content

Commit

Permalink
Merge branch 'dev' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo authored Nov 30, 2024
2 parents 44b7198 + 39cdbba commit 0f403ff
Show file tree
Hide file tree
Showing 138 changed files with 2,332 additions and 1,296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ jobs:
with:
args: |
Jan App ${{ inputs.build_reason }} build artifact version {{ VERSION }}:
- Windows: https://delta.jan.ai/nightly/jan-win-x64-{{ VERSION }}.exe
- macOS Intel: https://delta.jan.ai/nightly/jan-mac-x64-{{ VERSION }}.dmg
- macOS Apple Silicon: https://delta.jan.ai/nightly/jan-mac-arm64-{{ VERSION }}.dmg
- Linux Deb: https://delta.jan.ai/nightly/jan-linux-amd64-{{ VERSION }}.deb
- Linux AppImage: https://delta.jan.ai/nightly/jan-linux-x86_64-{{ VERSION }}.AppImage
- Windows: https://delta.jan.ai/nightly/jan-nightly-win-x64-{{ VERSION }}.exe
- macOS Intel: https://delta.jan.ai/nightly/jan-nightly-mac-x64-{{ VERSION }}.dmg
- macOS Apple Silicon: https://delta.jan.ai/nightly/jan-nightly-mac-arm64-{{ VERSION }}.dmg
- Linux Deb: https://delta.jan.ai/nightly/jan-nightly-linux-amd64-{{ VERSION }}.deb
- Linux AppImage: https://delta.jan.ai/nightly/jan-nightly-linux-x86_64-{{ VERSION }}.AppImage
- Github action run: https://github.com/janhq/jan/actions/runs/{{ GITHUB_RUN_ID }}
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ coverage
.yarnrc
test_results.html
*.tsbuildinfo
electron/shared/**
13 changes: 0 additions & 13 deletions core/src/browser/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { joinPath } from './core'
import { openFileExplorer } from './core'
import { getJanDataFolderPath } from './core'
import { abortDownload } from './core'
import { getFileSize } from './core'
import { executeOnMain } from './core'

describe('test core apis', () => {
Expand Down Expand Up @@ -66,18 +65,6 @@ describe('test core apis', () => {
expect(result).toBe('aborted')
})

it('should get file size', async () => {
const url = 'http://example.com/file'
globalThis.core = {
api: {
getFileSize: jest.fn().mockResolvedValue(1024),
},
}
const result = await getFileSize(url)
expect(globalThis.core.api.getFileSize).toHaveBeenCalledWith(url)
expect(result).toBe(1024)
})

it('should execute function on main process', async () => {
const extension = 'testExtension'
const method = 'testMethod'
Expand Down
10 changes: 0 additions & 10 deletions core/src/browser/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ const downloadFile: (downloadRequest: DownloadRequest, network?: NetworkConfig)
network
) => globalThis.core?.api?.downloadFile(downloadRequest, network)

/**
* Get unit in bytes for a remote file.
*
* @param url - The url of the file.
* @returns {Promise<number>} - A promise that resolves with the file size.
*/
const getFileSize: (url: string) => Promise<number> = (url: string) =>
globalThis.core.api?.getFileSize(url)

/**
* Aborts the download of a specific file.
* @param {string} fileName - The name of the file whose download is to be aborted.
Expand Down Expand Up @@ -167,7 +158,6 @@ export {
getUserHomePath,
systemInformation,
showToast,
getFileSize,
dirName,
FileStat,
}
1 change: 0 additions & 1 deletion core/src/browser/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export abstract class BaseExtension implements ExtensionType {
for (const model of models) {
ModelManager.instance().register(model)
}
events.emit(ModelEvent.OnModelsUpdate, {})
}

/**
Expand Down
6 changes: 4 additions & 2 deletions core/src/browser/extensions/engines/helpers/sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ export function requestInference(
errorCode = ErrorCode.InvalidApiKey
}
const error = {
message: data.error?.message ?? 'Error occurred.',
message: data.error?.message ?? data.message ?? 'Error occurred.',
code: errorCode,
}
subscriber.error(error)
subscriber.complete()
return
}
if (model.parameters?.stream === false) {
// There could be overriden stream parameter in the model
// that is set in request body (transformed payload)
if (requestBody?.stream === false || model.parameters?.stream === false) {
const data = await response.json()
if (transformResponse) {
subscriber.next(transformResponse(data))
Expand Down
1 change: 1 addition & 0 deletions core/src/browser/extensions/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export abstract class ModelExtension extends BaseExtension implements ModelInter
return ExtensionTypeEnum.Model
}

abstract configurePullOptions(configs: { [key: string]: any }): Promise<any>
abstract getModels(): Promise<Model[]>
abstract pullModel(model: string, id?: string, name?: string): Promise<void>
abstract cancelModelPull(modelId: string): Promise<void>
Expand Down
17 changes: 5 additions & 12 deletions core/src/node/api/processors/download.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ jest.mock('fs', () => ({
createWriteStream: jest.fn(),
}))

const requestMock = jest.fn((options, callback) => {
callback(new Error('Test error'), null)
})
jest.mock('request', () => requestMock)

jest.mock('request-progress', () => {
return jest.fn().mockImplementation(() => {
return {
Expand Down Expand Up @@ -54,18 +59,6 @@ describe('Downloader', () => {
beforeEach(() => {
jest.resetAllMocks()
})
it('should handle getFileSize errors correctly', async () => {
const observer = jest.fn()
const url = 'http://example.com/file'

const downloader = new Downloader(observer)
const requestMock = jest.fn((options, callback) => {
callback(new Error('Test error'), null)
})
jest.mock('request', () => requestMock)

await expect(downloader.getFileSize(observer, url)).rejects.toThrow('Test error')
})

it('should pause download correctly', () => {
const observer = jest.fn()
Expand Down
21 changes: 0 additions & 21 deletions core/src/node/api/processors/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,4 @@ export class Downloader implements Processor {
pauseDownload(_observer: any, fileName: any) {
DownloadManager.instance.networkRequests[fileName]?.pause()
}

async getFileSize(_observer: any, url: string): Promise<number> {
return new Promise((resolve, reject) => {
const request = require('request')
request(
{
url,
method: 'HEAD',
},
function (err: any, response: any) {
if (err) {
console.error('Getting file size failed:', err)
reject(err)
} else {
const size: number = response.headers['content-length'] ?? -1
resolve(size)
}
}
)
})
}
}
34 changes: 15 additions & 19 deletions core/src/node/api/restful/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { HttpServer } from '../HttpServer'
import {
chatCompletions,
deleteBuilder,
downloadModel,
getBuilder,
retrieveBuilder,
Expand All @@ -14,8 +13,6 @@ import {
} from './helper/builder'

import { JanApiRouteConfiguration } from './helper/configuration'
import { startModel, stopModel } from './helper/startStopModel'
import { ModelSettingParams } from '../../../types'

export const commonRouter = async (app: HttpServer) => {
const normalizeData = (data: any) => {
Expand All @@ -28,19 +25,25 @@ export const commonRouter = async (app: HttpServer) => {
// Read & Delete :: Threads | Models | Assistants
Object.keys(JanApiRouteConfiguration).forEach((key) => {
app.get(`/${key}`, async (_req, _res) => {
if (key === 'models') {
if (key.includes('models')) {
return models(_req, _res)
}
return getBuilder(JanApiRouteConfiguration[key]).then(normalizeData)
})

app.get(`/${key}/:id`, async (request: any) =>
retrieveBuilder(JanApiRouteConfiguration[key], request.params.id)
)
app.get(`/${key}/:id`, async (_req: any, _res: any) => {
if (key.includes('models')) {
return models(_req, _res)
}
return retrieveBuilder(JanApiRouteConfiguration[key], _req.params.id)
})

app.delete(`/${key}/:id`, async (request: any) =>
deleteBuilder(JanApiRouteConfiguration[key], request.params.id)
)
app.delete(`/${key}/:id`, async (_req: any, _res: any) => {
if (key.includes('models')) {
return models(_req, _res)
}
return retrieveBuilder(JanApiRouteConfiguration[key], _req.params.id)
})
})

// Threads
Expand Down Expand Up @@ -70,16 +73,9 @@ export const commonRouter = async (app: HttpServer) => {
})
)

app.put(`/models/:modelId/start`, async (request: any) => {
let settingParams: ModelSettingParams | undefined = undefined
if (Object.keys(request.body).length !== 0) {
settingParams = JSON.parse(request.body) as ModelSettingParams
}

return startModel(request.params.modelId, settingParams)
})
app.post(`/models/start`, async (request: any, reply: any) => models(request, reply))

app.put(`/models/:modelId/stop`, async (request: any) => stopModel(request.params.modelId))
app.post(`/models/stop`, async (request: any, reply: any) => models(request, reply))

// Chat Completion
app.post(`/chat/completions`, async (request: any, reply: any) => chatCompletions(request, reply))
Expand Down
40 changes: 1 addition & 39 deletions core/src/node/api/restful/helper/builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
import {
existsSync,
readdirSync,
readFileSync,
writeFileSync,
mkdirSync,
appendFileSync,
rmdirSync,
} from 'fs'
import { join } from 'path'
import { existsSync, readdirSync, readFileSync, writeFileSync, mkdirSync, appendFileSync } from 'fs'
import {
getBuilder,
retrieveBuilder,
deleteBuilder,
getMessages,
retrieveMessage,
createThread,
Expand Down Expand Up @@ -82,34 +72,6 @@ describe('builder helper functions', () => {
})
})

describe('deleteBuilder', () => {
it('should return a message if trying to delete Jan assistant', async () => {
const result = await deleteBuilder({ ...mockConfiguration, dirName: 'assistants' }, 'jan')
expect(result).toEqual({ message: 'Cannot delete Jan assistant' })
})

it('should return a message if data is not found', async () => {
;(existsSync as jest.Mock).mockReturnValue(true)
;(readdirSync as jest.Mock).mockReturnValue(['file1'])
;(readFileSync as jest.Mock).mockReturnValue(JSON.stringify({ id: 'model1' }))

const result = await deleteBuilder(mockConfiguration, 'nonexistentId')
expect(result).toEqual({ message: 'Not found' })
})

it('should delete the directory and return success message', async () => {
;(existsSync as jest.Mock).mockReturnValue(true)
;(readdirSync as jest.Mock).mockReturnValue(['file1'])
;(readFileSync as jest.Mock).mockReturnValue(JSON.stringify({ id: 'model1' }))

const result = await deleteBuilder(mockConfiguration, 'model1')
expect(rmdirSync).toHaveBeenCalledWith(join('/mock/path', 'mockDir', 'model1'), {
recursive: true,
})
expect(result).toEqual({ id: 'model1', object: 'mockObject', deleted: true })
})
})

describe('getMessages', () => {
it('should return an empty array if message file does not exist', async () => {
;(existsSync as jest.Mock).mockReturnValue(false)
Expand Down
30 changes: 1 addition & 29 deletions core/src/node/api/restful/helper/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,6 @@ export const retrieveBuilder = async (configuration: RouteConfiguration, id: str
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(getJanDataFolderPath(), configuration.dirName)
try {
const data = await retrieveBuilder(configuration, id)
if (!data) {
return {
message: 'Not found',
}
}

const objectPath = join(directoryPath, id)
rmdirSync(objectPath, { recursive: true })
return {
id: id,
object: configuration.delete.object,
deleted: true,
}
} catch (ex) {
console.error(ex)
}
}

export const getMessages = async (threadId: string): Promise<ThreadMessage[]> => {
const threadDirPath = join(getJanDataFolderPath(), 'threads', threadId)
const messageFile = 'messages.jsonl'
Expand Down Expand Up @@ -308,7 +280,7 @@ export const models = async (request: any, reply: any) => {
'Content-Type': 'application/json',
}

const response = await fetch(`${CORTEX_API_URL}/models`, {
const response = await fetch(`${CORTEX_API_URL}/models${request.url.split('/models')[1] ?? ""}`, {
method: request.method,
headers: headers,
body: JSON.stringify(request.body),
Expand Down
10 changes: 0 additions & 10 deletions core/src/node/api/restful/helper/startStopModel.test.ts

This file was deleted.

25 changes: 0 additions & 25 deletions core/src/node/api/restful/helper/startStopModel.ts

This file was deleted.

2 changes: 1 addition & 1 deletion core/src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum NativeRoute {

quickAskSizeUpdated = 'quickAskSizeUpdated',
ackDeepLink = 'ackDeepLink',
factoryReset = 'factoryReset'
}

/**
Expand Down Expand Up @@ -65,7 +66,6 @@ export enum DownloadRoute {
pauseDownload = 'pauseDownload',
resumeDownload = 'resumeDownload',
getDownloadProgress = 'getDownloadProgress',
getFileSize = 'getFileSize',
}

export enum DownloadEvent {
Expand Down
Loading

0 comments on commit 0f403ff

Please sign in to comment.