Skip to content

Commit

Permalink
fix: app issues with server refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Dec 26, 2023
1 parent 249e013 commit 1f641f3
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion core/src/node/api/common/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const getMessages = async (threadId: string) => {
const messageFilePath = join(threadDirPath, messageFile)

const lines = fs
.readFileSync(messageFilePath)
.readFileSync(messageFilePath, 'utf-8')

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
This path depends on a
user-provided value
.
.toString()
.split('\n')
.filter((line: any) => line !== '')
Expand Down
15 changes: 9 additions & 6 deletions core/src/node/api/routes/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { join } from 'path'
import { HttpServer, userSpacePath } from '../../index'

export const fsRouter = async (app: HttpServer) => {
const moduleName = "fs"
const moduleName = 'fs'
// Generate handlers for each fs route
Object.values(FileSystemRoute).forEach((route) => {
app.post(`/${route}`, async (req, res) => {
const body = JSON.parse(req.body as any)
try {
const result = await import(moduleName).then(mdl => { return mdl[route](
...body.map((arg: any) =>
arg.includes('file:/') ? join(userSpacePath, arg.replace('file:/', '')) : arg,
),
)
const result = await import(moduleName).then((mdl) => {
return mdl[route](
...body.map((arg: any) =>
typeof arg === 'string' && arg.includes('file:/')
? join(userSpacePath, arg.replace('file:/', ''))
: arg,
),
)
})
res.status(200).send(result)
} catch (ex) {
Expand Down
2 changes: 1 addition & 1 deletion electron/handlers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function handleFsIPCs() {
return import(moduleName).then((mdl) =>
mdl[route](
...args.map((arg) =>
arg.includes('file:/')
typeof arg === 'string' && arg.includes('file:/')
? join(userSpacePath, arg.replace('file:/', ''))
: arg
)
Expand Down
8 changes: 6 additions & 2 deletions extensions/assistant-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class JanAssistantExtension implements AssistantExtension {
/**
* Called when the extension is unloaded.
*/
onUnload(): void { }
onUnload(): void {}

async createAssistant(assistant: Assistant): Promise<void> {
const assistantDir = join(JanAssistantExtension._homeDir, assistant.id);
Expand Down Expand Up @@ -48,6 +48,7 @@ export default class JanAssistantExtension implements AssistantExtension {
for (const fileName of allFileName) {
const filePath = join(JanAssistantExtension._homeDir, fileName);

if (filePath.includes(".DS_Store")) continue;
const jsonFiles: string[] = (await fs.readdirSync(filePath)).filter(
(file: string) => file === "assistant.json"
);
Expand All @@ -57,7 +58,10 @@ export default class JanAssistantExtension implements AssistantExtension {
continue;
}

const content = await fs.readFileSync(join(filePath, jsonFiles[0]), 'utf-8');
const content = await fs.readFileSync(
join(filePath, jsonFiles[0]),
"utf-8"
);
const assistant: Assistant =
typeof content === "object" ? content : JSON.parse(content);

Expand Down
25 changes: 16 additions & 9 deletions extensions/conversational-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { join } from 'path'
* functionality for managing threads.
*/
export default class JSONConversationalExtension
implements ConversationalExtension {
implements ConversationalExtension
{
private static readonly _homeDir = 'file://threads'
private static readonly _threadInfoFileName = 'thread.json'
private static readonly _threadMessagesFileName = 'messages.jsonl'
Expand Down Expand Up @@ -96,7 +97,8 @@ export default class JSONConversationalExtension
*/
deleteThread(threadId: string): Promise<void> {
return fs.rmdirSync(
join(JSONConversationalExtension._homeDir, `${threadId}`)
join(JSONConversationalExtension._homeDir, `${threadId}`),
{ recursive: true }
)
}

Expand Down Expand Up @@ -153,7 +155,8 @@ export default class JSONConversationalExtension
JSONConversationalExtension._homeDir,
threadDirName,
JSONConversationalExtension._threadInfoFileName
)
),
'utf-8'
)
}

Expand All @@ -168,6 +171,8 @@ export default class JSONConversationalExtension

const threadDirs: string[] = []
for (let i = 0; i < fileInsideThread.length; i++) {
if (fileInsideThread[i].includes('.DS_Store')) continue

const path = join(
JSONConversationalExtension._homeDir,
fileInsideThread[i]
Expand Down Expand Up @@ -202,12 +207,14 @@ export default class JSONConversationalExtension
JSONConversationalExtension._threadMessagesFileName
)

const result = await fs.readFileSync(messageFilePath, 'utf-8').then((content) =>
content
.toString()
.split('\n')
.filter((line) => line !== '')
)
const result = await fs
.readFileSync(messageFilePath, 'utf-8')
.then((content) =>
content
.toString()
.split('\n')
.filter((line) => line !== '')
)

const messages: ThreadMessage[] = []
result.forEach((line: string) => {
Expand Down
7 changes: 3 additions & 4 deletions extensions/inference-nitro-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
ThreadMessage,
events,
executeOnMain,
getUserSpace,
fs,
Model,
} from "@janhq/core";
Expand Down Expand Up @@ -93,9 +92,9 @@ export default class JanInferenceNitroExtension implements InferenceExtension {
JanInferenceNitroExtension._engineMetadataFileName
);
if (await fs.existsSync(engineFile)) {
JanInferenceNitroExtension._engineSettings = JSON.parse(
await fs.readFileSync(engineFile)
);
const engine = await fs.readFileSync(engineFile, 'utf-8');
JanInferenceNitroExtension._engineSettings =
typeof engine === "object" ? engine : JSON.parse(engine);
} else {
await fs.writeFileSync(
engineFile,
Expand Down
4 changes: 4 additions & 0 deletions extensions/inference-nitro-extension/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function stopModel(): Promise<void> {
*/
async function initModel(wrapper: any): Promise<ModelOperationResponse> {
currentModelFile = wrapper.modelFullPath;
const janRoot = path.join(require("os").homedir(), "jan");
if (!currentModelFile.includes(janRoot)) {
currentModelFile = path.join(janRoot, currentModelFile);
}
if (wrapper.model.engine !== "nitro") {
return Promise.resolve({ error: "Not a nitro model" });
} else {
Expand Down
11 changes: 3 additions & 8 deletions extensions/inference-openai-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,9 @@ export default class JanInferenceOpenAIExtension implements InferenceExtension {
JanInferenceOpenAIExtension._engineMetadataFileName
);
if (await fs.existsSync(engineFile)) {
try {
JanInferenceOpenAIExtension._engineSettings = JSON.parse(
await fs.readFileSync(engineFile)
);
} catch {
JanInferenceOpenAIExtension._engineSettings =
await fs.readFileSync(engineFile);
}
const engine = await fs.readFileSync(engineFile, 'utf-8');
JanInferenceOpenAIExtension._engineSettings =
typeof engine === "object" ? engine : JSON.parse(engine);
} else {
await fs.writeFileSync(
engineFile,
Expand Down
12 changes: 6 additions & 6 deletions extensions/inference-triton-trtllm-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export default class JanInferenceTritonTrtLLMExtension
/**
* Subscribes to events emitted by the @janhq/core package.
*/
onLoad(): void {
fs.mkdirSync(JanInferenceTritonTrtLLMExtension._homeDir);
JanInferenceTritonTrtLLMExtension.writeDefaultEngineSettings();
async onLoad() {
if (!(await fs.existsSync(JanInferenceTritonTrtLLMExtension._homeDir)))
JanInferenceTritonTrtLLMExtension.writeDefaultEngineSettings();

// Events subscription
events.on(EventName.OnMessageSent, (data) =>
Expand Down Expand Up @@ -99,9 +99,9 @@ export default class JanInferenceTritonTrtLLMExtension
JanInferenceTritonTrtLLMExtension._engineMetadataFileName
);
if (await fs.existsSync(engine_json)) {
JanInferenceTritonTrtLLMExtension._engineSettings = JSON.parse(
await fs.readFileSync(engine_json)
);
const engine = await fs.readFileSync(engine_json, "utf-8");
JanInferenceTritonTrtLLMExtension._engineSettings =
typeof engine === "object" ? engine : JSON.parse(engine);
} else {
await fs.writeFileSync(
engine_json,
Expand Down
10 changes: 7 additions & 3 deletions web/hooks/useEngineSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { fs } from '@janhq/core'

export const useEngineSettings = () => {
const readOpenAISettings = async () => {
const settings = await fs.readFileSync(join('engines', 'openai.json'))
if (!fs.existsSync(join('file://engines', 'openai.json'))) return {}
const settings = await fs.readFileSync(
join('file://engines', 'openai.json'),
'utf-8'
)
if (settings) {
return JSON.parse(settings)
return typeof settings === 'object' ? settings : JSON.parse(settings)
}
return {}
}
Expand All @@ -18,7 +22,7 @@ export const useEngineSettings = () => {
const settings = await readOpenAISettings()
settings.api_key = apiKey
await fs.writeFileSync(
join('engines', 'openai.json'),
join('file://engines', 'openai.json'),
JSON.stringify(settings)
)
}
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useSendChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default function useSendChatMessage() {
updateThreadInitSuccess(activeThread.id)
updateThread(updatedThread)

extensionManager
await extensionManager
.get<ConversationalExtension>(ExtensionType.Conversational)
?.saveThread(updatedThread)
}
Expand Down
4 changes: 2 additions & 2 deletions web/screens/Chat/ChatBody/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ const ChatBody: React.FC = () => {
className="mt-10 flex flex-col items-center"
>
<span className="mb-3 text-center text-sm font-medium text-gray-500">
Oops! The generation was interrupted. Let&apos;s
give it another go!
Oops! The generation was interrupted. Let&apos;s give it
another go!
</span>
<Button
className="w-min"
Expand Down

0 comments on commit 1f641f3

Please sign in to comment.