diff --git a/core/src/node/api/restful/common.ts b/core/src/node/api/restful/common.ts index 39f7b8d8b..989104e03 100644 --- a/core/src/node/api/restful/common.ts +++ b/core/src/node/api/restful/common.ts @@ -1,7 +1,6 @@ import { HttpServer } from '../HttpServer' import { chatCompletions, - deleteBuilder, downloadModel, getBuilder, retrieveBuilder, @@ -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) => { @@ -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 @@ -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 - } + app.post(`/models/start`, async (request: any, reply: any) => models(request, reply)) - return startModel(request.params.modelId, settingParams) - }) - - 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)) diff --git a/core/src/node/api/restful/helper/builder.test.ts b/core/src/node/api/restful/helper/builder.test.ts index f21257098..cfaee6007 100644 --- a/core/src/node/api/restful/helper/builder.test.ts +++ b/core/src/node/api/restful/helper/builder.test.ts @@ -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, @@ -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) diff --git a/core/src/node/api/restful/helper/builder.ts b/core/src/node/api/restful/helper/builder.ts index c3493a8be..e081708cf 100644 --- a/core/src/node/api/restful/helper/builder.ts +++ b/core/src/node/api/restful/helper/builder.ts @@ -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 => { const threadDirPath = join(getJanDataFolderPath(), 'threads', threadId) const messageFile = 'messages.jsonl' @@ -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), diff --git a/core/src/node/api/restful/helper/startStopModel.test.ts b/core/src/node/api/restful/helper/startStopModel.test.ts deleted file mode 100644 index 7c1a56cf1..000000000 --- a/core/src/node/api/restful/helper/startStopModel.test.ts +++ /dev/null @@ -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() - }) -}) diff --git a/core/src/node/api/restful/helper/startStopModel.ts b/core/src/node/api/restful/helper/startStopModel.ts deleted file mode 100644 index 2e9db6d15..000000000 --- a/core/src/node/api/restful/helper/startStopModel.ts +++ /dev/null @@ -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 }), - }) -}