chore: remove a couple of deprecated server apis

This commit is contained in:
Louis 2024-11-25 23:53:36 +07:00
parent 148a1e5d32
commit 1a75459004
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
5 changed files with 17 additions and 122 deletions

View File

@ -1,7 +1,6 @@
import { HttpServer } from '../HttpServer' import { HttpServer } from '../HttpServer'
import { import {
chatCompletions, chatCompletions,
deleteBuilder,
downloadModel, downloadModel,
getBuilder, getBuilder,
retrieveBuilder, retrieveBuilder,
@ -14,8 +13,6 @@ import {
} from './helper/builder' } from './helper/builder'
import { JanApiRouteConfiguration } from './helper/configuration' import { JanApiRouteConfiguration } from './helper/configuration'
import { startModel, stopModel } from './helper/startStopModel'
import { ModelSettingParams } from '../../../types'
export const commonRouter = async (app: HttpServer) => { export const commonRouter = async (app: HttpServer) => {
const normalizeData = (data: any) => { const normalizeData = (data: any) => {
@ -28,19 +25,25 @@ export const commonRouter = async (app: HttpServer) => {
// Read & Delete :: Threads | Models | Assistants // Read & Delete :: Threads | Models | Assistants
Object.keys(JanApiRouteConfiguration).forEach((key) => { Object.keys(JanApiRouteConfiguration).forEach((key) => {
app.get(`/${key}`, async (_req, _res) => { app.get(`/${key}`, async (_req, _res) => {
if (key === 'models') { if (key.includes('models')) {
return models(_req, _res) return models(_req, _res)
} }
return getBuilder(JanApiRouteConfiguration[key]).then(normalizeData) return getBuilder(JanApiRouteConfiguration[key]).then(normalizeData)
}) })
app.get(`/${key}/:id`, async (request: any) => app.get(`/${key}/:id`, async (_req: any, _res: any) => {
retrieveBuilder(JanApiRouteConfiguration[key], request.params.id) if (key.includes('models')) {
) return models(_req, _res)
}
return retrieveBuilder(JanApiRouteConfiguration[key], _req.params.id)
})
app.delete(`/${key}/:id`, async (request: any) => app.delete(`/${key}/:id`, async (_req: any, _res: any) => {
deleteBuilder(JanApiRouteConfiguration[key], request.params.id) if (key.includes('models')) {
) return models(_req, _res)
}
return retrieveBuilder(JanApiRouteConfiguration[key], _req.params.id)
})
}) })
// Threads // Threads
@ -70,16 +73,9 @@ export const commonRouter = async (app: HttpServer) => {
}) })
) )
app.put(`/models/:modelId/start`, async (request: any) => { app.post(`/models/start`, async (request: any, reply: any) => models(request, reply))
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/stop`, async (request: any, reply: any) => models(request, reply))
})
app.put(`/models/:modelId/stop`, async (request: any) => stopModel(request.params.modelId))
// Chat Completion // Chat Completion
app.post(`/chat/completions`, async (request: any, reply: any) => chatCompletions(request, reply)) app.post(`/chat/completions`, async (request: any, reply: any) => chatCompletions(request, reply))

View File

@ -1,17 +1,7 @@
import { import { existsSync, readdirSync, readFileSync, writeFileSync, mkdirSync, appendFileSync } from 'fs'
existsSync,
readdirSync,
readFileSync,
writeFileSync,
mkdirSync,
appendFileSync,
rmdirSync,
} from 'fs'
import { join } from 'path'
import { import {
getBuilder, getBuilder,
retrieveBuilder, retrieveBuilder,
deleteBuilder,
getMessages, getMessages,
retrieveMessage, retrieveMessage,
createThread, createThread,
@ -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', () => { describe('getMessages', () => {
it('should return an empty array if message file does not exist', async () => { it('should return an empty array if message file does not exist', async () => {
;(existsSync as jest.Mock).mockReturnValue(false) ;(existsSync as jest.Mock).mockReturnValue(false)

View File

@ -73,34 +73,6 @@ export const retrieveBuilder = async (configuration: RouteConfiguration, id: str
return filteredData 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[]> => { export const getMessages = async (threadId: string): Promise<ThreadMessage[]> => {
const threadDirPath = join(getJanDataFolderPath(), 'threads', threadId) const threadDirPath = join(getJanDataFolderPath(), 'threads', threadId)
const messageFile = 'messages.jsonl' const messageFile = 'messages.jsonl'
@ -308,7 +280,7 @@ export const models = async (request: any, reply: any) => {
'Content-Type': 'application/json', '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, method: request.method,
headers: headers, headers: headers,
body: JSON.stringify(request.body), body: JSON.stringify(request.body),

View File

@ -1,10 +0,0 @@
import { startModel } from './startStopModel'
describe('startModel', () => {
it('test_startModel_error', async () => {
const modelId = 'testModelId'
const settingParams = undefined
expect(startModel(modelId, settingParams)).resolves.toThrow()
})
})

View File

@ -1,25 +0,0 @@
import { ModelSettingParams } from '../../../../types'
import { CORTEX_DEFAULT_PORT, LOCAL_HOST } from './consts'
/**
* Start a model
* @param modelId
* @param settingParams
* @returns
*/
export const startModel = async (modelId: string, settingParams?: ModelSettingParams) => {
return fetch(`http://${LOCAL_HOST}:${CORTEX_DEFAULT_PORT}/v1/models/start`, {
method: 'POST',
body: JSON.stringify({ model: modelId, ...settingParams }),
})
}
/*
* Stop model.
*/
export const stopModel = async (modelId: string) => {
return fetch(`http://${LOCAL_HOST}:${CORTEX_DEFAULT_PORT}/v1/models/stop`, {
method: 'POST',
body: JSON.stringify({ model: modelId }),
})
}