refactor: remove FS call from frontend - remove any casting

This commit is contained in:
Louis 2025-04-07 13:34:14 +07:00
parent 7e2498cc79
commit a10cec1a66
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
2 changed files with 17 additions and 14 deletions

View File

@ -13,6 +13,8 @@ import {
ContentType, ContentType,
EngineManager, EngineManager,
InferenceEngine, InferenceEngine,
MessageStatus,
ChatCompletionRole,
} from '@janhq/core' } from '@janhq/core'
import { extractInferenceParams, extractModelLoadParams } from '@janhq/core' import { extractInferenceParams, extractModelLoadParams } from '@janhq/core'
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai' import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai'
@ -20,7 +22,7 @@ import { OpenAI } from 'openai'
import { import {
ChatCompletionMessageParam, ChatCompletionMessageParam,
ChatCompletionRole, ChatCompletionRole as OpenAIChatCompletionRole,
ChatCompletionTool, ChatCompletionTool,
} from 'openai/resources/chat' } from 'openai/resources/chat'
@ -261,7 +263,7 @@ export default function useSendChatMessage() {
const response = await openai.chat.completions.create({ const response = await openai.chat.completions.create({
messages: (data.messages ?? []).map((e) => { messages: (data.messages ?? []).map((e) => {
return { return {
role: e.role as ChatCompletionRole, role: e.role as OpenAIChatCompletionRole,
content: e.content, content: e.content,
} }
}) as ChatCompletionMessageParam[], }) as ChatCompletionMessageParam[],
@ -276,24 +278,24 @@ export default function useSendChatMessage() {
thread_id: activeThreadRef.current.id, thread_id: activeThreadRef.current.id,
assistant_id: activeAssistantRef.current.assistant_id, assistant_id: activeAssistantRef.current.assistant_id,
attachments: [], attachments: [],
role: response.choices[0].message.role as any, role: response.choices[0].message.role as ChatCompletionRole,
content: [ content: [
{ {
type: ContentType.Text, type: ContentType.Text,
text: { text: {
value: response.choices[0].message.content value: response.choices[0].message.content
? (response.choices[0].message.content as any) ? response.choices[0].message.content
: '', : '',
annotations: [], annotations: [],
}, },
}, },
], ],
status: 'ready' as any, status: MessageStatus.Ready,
created_at: Date.now(), created_at: Date.now(),
completed_at: Date.now(), completed_at: Date.now(),
} }
requestBuilder.pushAssistantMessage( requestBuilder.pushAssistantMessage(
(response.choices[0].message.content as any) ?? '' response.choices[0].message.content ?? ''
) )
events.emit(MessageEvent.OnMessageUpdate, newMessage) events.emit(MessageEvent.OnMessageUpdate, newMessage)
} }
@ -307,7 +309,7 @@ export default function useSendChatMessage() {
thread_id: activeThreadRef.current.id, thread_id: activeThreadRef.current.id,
assistant_id: activeAssistantRef.current.assistant_id, assistant_id: activeAssistantRef.current.assistant_id,
attachments: [], attachments: [],
role: 'assistant' as any, role: ChatCompletionRole.Assistant,
content: [ content: [
{ {
type: ContentType.Text, type: ContentType.Text,
@ -317,7 +319,7 @@ export default function useSendChatMessage() {
}, },
}, },
], ],
status: 'pending' as any, status: MessageStatus.Pending,
created_at: Date.now(), created_at: Date.now(),
completed_at: Date.now(), completed_at: Date.now(),
} }
@ -336,7 +338,7 @@ export default function useSendChatMessage() {
thread_id: activeThreadRef.current.id, thread_id: activeThreadRef.current.id,
assistant_id: activeAssistantRef.current.assistant_id, assistant_id: activeAssistantRef.current.assistant_id,
attachments: [], attachments: [],
role: 'assistant' as any, role: ChatCompletionRole.Assistant,
content: [ content: [
{ {
type: ContentType.Text, type: ContentType.Text,
@ -348,7 +350,7 @@ export default function useSendChatMessage() {
}, },
}, },
], ],
status: 'ready' as any, status: MessageStatus.Ready,
created_at: Date.now(), created_at: Date.now(),
completed_at: Date.now(), completed_at: Date.now(),
} }

View File

@ -2,7 +2,6 @@ import { useCallback } from 'react'
import { useTheme } from 'next-themes' import { useTheme } from 'next-themes'
import { fs, joinPath } from '@janhq/core'
import { Button, ScrollArea, Select, Switch } from '@janhq/joi' import { Button, ScrollArea, Select, Switch } from '@janhq/joi'
import { useAtom, useAtomValue } from 'jotai' import { useAtom, useAtomValue } from 'jotai'
@ -51,9 +50,11 @@ export default function AppearanceOptions() {
const handleClickTheme = useCallback( const handleClickTheme = useCallback(
async (e: string) => { async (e: string) => {
setSelectedIdTheme(e) setSelectedIdTheme(e)
const janThemesPath = await joinPath([janDataFolderPath, 'themes']) const theme: Theme = JSON.parse(
const filePath = await joinPath([`${janThemesPath}/${e}`, `theme.json`]) await window.core.api.readTheme({
const theme: Theme = JSON.parse(await fs.readFileSync(filePath, 'utf-8')) themeName: e,
})
)
setThemeData(theme) setThemeData(theme)
setTheme(String(theme?.nativeTheme)) setTheme(String(theme?.nativeTheme))
}, },