Skip to content

Commit 6041d61

Browse files
authored
Merge pull request #4475 from janhq/fix/remote-engine-issues
fix: remote engine revamp issues
2 parents df6110b + f328c35 commit 6041d61

File tree

11 files changed

+43
-16
lines changed

11 files changed

+43
-16
lines changed

web/containers/ModelDropdown/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,22 @@ const ModelDropdown = ({
194194
const modelId = activeAssistant?.model?.id
195195

196196
const model = downloadedModels.find((model) => model.id === modelId)
197-
setSelectedModel(model)
197+
if (model) {
198+
if (
199+
engines?.[model.engine]?.[0].type === 'local' ||
200+
(engines?.[model.engine]?.[0].api_key?.length ?? 0) > 0
201+
)
202+
setSelectedModel(model)
203+
} else {
204+
setSelectedModel(undefined)
205+
}
198206
}, [
199207
recommendedModel,
200208
activeThread,
201209
downloadedModels,
202210
setSelectedModel,
203211
activeAssistant?.model?.id,
212+
engines,
204213
])
205214

206215
const isLocalEngine = useCallback(

web/containers/Providers/ModelHandler.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ export default function ModelHandler() {
336336

337337
// Check model engine; we don't want to generate a title when it's not a local engine. remote model using first promp
338338
if (
339-
!isLocalEngine(engines, activeModelRef.current?.engine as InferenceEngine)
339+
activeModelRef.current?.engine !== InferenceEngine.cortex &&
340+
activeModelRef.current?.engine !== InferenceEngine.cortex_llamacpp
340341
) {
341342
const updatedThread: Thread = {
342343
...thread,
@@ -396,9 +397,7 @@ export default function ModelHandler() {
396397

397398
// 2. Update the title with the result of the inference
398399
setTimeout(() => {
399-
const engine = EngineManager.instance().get(
400-
messageRequest.model?.engine ?? activeModelRef.current?.engine ?? ''
401-
)
400+
const engine = EngineManager.instance().get(InferenceEngine.cortex)
402401
engine?.inference(messageRequest)
403402
}, 1000)
404403
}

web/hooks/useActiveModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export function useActiveModel() {
159159
}
160160
if (!activeModel) return
161161

162-
const engine = EngineManager.instance().get(activeModel.engine)
162+
const engine = EngineManager.instance().get(InferenceEngine.cortex)
163163
engine?.stopInference()
164164
}, [activeModel, stateModel, stopModel])
165165

web/hooks/useConfigurations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const useConfigurations = () => {
3333

3434
useEffect(() => {
3535
configurePullOptions()
36+
// eslint-disable-next-line react-hooks/exhaustive-deps
3637
}, [])
3738

3839
return {

web/hooks/useRecommendedModel.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { atom, useAtomValue } from 'jotai'
77
import { activeModelAtom } from './useActiveModel'
88

99
import { activeAssistantAtom } from '@/helpers/atoms/Assistant.atom'
10+
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
1011
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
1112
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'
1213

@@ -30,6 +31,7 @@ export default function useRecommendedModel() {
3031
const activeThread = useAtomValue(activeThreadAtom)
3132
const downloadedModels = useAtomValue(downloadedModelsAtom)
3233
const activeAssistant = useAtomValue(activeAssistantAtom)
34+
const engines = useAtomValue(installedEnginesAtom)
3335

3436
const getAndSortDownloadedModels = useCallback(async (): Promise<Model[]> => {
3537
const models = downloadedModels.sort((a, b) =>
@@ -45,7 +47,12 @@ export default function useRecommendedModel() {
4547
const getRecommendedModel = useCallback(async (): Promise<
4648
Model | undefined
4749
> => {
48-
const models = await getAndSortDownloadedModels()
50+
const models = (await getAndSortDownloadedModels()).filter((e: Model) =>
51+
engines?.[e.engine]?.[0].type === 'local' ||
52+
(engines?.[e.engine]?.[0].api_key?.length ?? 0) > 0
53+
? true
54+
: false
55+
)
4956

5057
if (!activeThread || !activeAssistant) return
5158
const modelId = activeAssistant.model.id
@@ -63,10 +70,8 @@ export default function useRecommendedModel() {
6370
}
6471

6572
// sort the model, for display purpose
66-
6773
if (models.length === 0) {
6874
// if we have no downloaded models, then can't recommend anything
69-
console.debug("No downloaded models, can't recommend anything")
7075
setRecommendedModel(undefined)
7176
return
7277
}
@@ -94,7 +99,7 @@ export default function useRecommendedModel() {
9499

95100
setRecommendedModel(lastUsedModel)
96101
// eslint-disable-next-line react-hooks/exhaustive-deps
97-
}, [getAndSortDownloadedModels, activeThread])
102+
}, [getAndSortDownloadedModels, activeThread, engines])
98103

99104
useEffect(() => {
100105
getRecommendedModel()

web/hooks/useStarterScreen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
12
import { useMemo } from 'react'
23

34
import { InferenceEngine, EngineConfig } from '@janhq/core'

web/screens/Hub/ModelList/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useAtomValue } from 'jotai'
66

77
import ModelItem from '@/screens/Hub/ModelList/ModelItem'
88

9+
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
910
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
1011

1112
type Props = {
@@ -14,6 +15,7 @@ type Props = {
1415

1516
const ModelList = ({ models }: Props) => {
1617
const downloadedModels = useAtomValue(downloadedModelsAtom)
18+
const engines = useAtomValue(installedEnginesAtom)
1719
const sortedModels: Model[] = useMemo(() => {
1820
const featuredModels: Model[] = []
1921
const remoteModels: Model[] = []
@@ -22,7 +24,7 @@ const ModelList = ({ models }: Props) => {
2224
models.forEach((m) => {
2325
if (m.metadata?.tags?.includes('Featured')) {
2426
featuredModels.push(m)
25-
} else if (m.format === 'api') {
27+
} else if (engines?.[m.engine]?.[0]?.type === 'remote') {
2628
remoteModels.push(m)
2729
} else if (downloadedModels.map((m) => m.id).includes(m.id)) {
2830
localModels.push(m)
@@ -40,7 +42,7 @@ const ModelList = ({ models }: Props) => {
4042
...remainingModels,
4143
...remoteModels,
4244
]
43-
}, [models, downloadedModels])
45+
}, [models, downloadedModels, engines])
4446

4547
return (
4648
<div className="relative h-full w-full flex-shrink-0">

web/screens/LocalServer/LocalServerLeftPanel/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Fragment, useCallback, useState } from 'react'
22

3-
import { EngineManager, Model, ModelSettingParams } from '@janhq/core'
3+
import {
4+
EngineManager,
5+
InferenceEngine,
6+
Model,
7+
ModelSettingParams,
8+
} from '@janhq/core'
49
import { Button, Tooltip, Select, Input, Checkbox } from '@janhq/joi'
510

611
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
@@ -94,7 +99,7 @@ const LocalServerLeftPanel = () => {
9499
localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')
95100
setFirstTimeVisitAPIServer(false)
96101
}
97-
const engine = EngineManager.instance().get((model as Model).engine)
102+
const engine = EngineManager.instance().get(InferenceEngine.cortex)
98103
engine?.loadModel(model as Model)
99104
// startModel(selectedModel.id, false).catch((e) => console.error(e))
100105
setIsLoading(false)

web/screens/Settings/Engines/RemoteEngineSettings.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
/* eslint-disable react/no-unescaped-entities */
3+
/* eslint-disable @typescript-eslint/no-unused-vars */
34

45
import React, { useCallback, useRef, useState, useEffect } from 'react'
56

67
import {
78
EngineConfig as OriginalEngineConfig,
89
InferenceEngine,
10+
events,
11+
EngineEvent,
912
} from '@janhq/core'
1013

1114
interface EngineConfig extends OriginalEngineConfig {
@@ -64,6 +67,7 @@ const RemoteEngineSettings = ({
6467
set(updatedEngine, field, value)
6568
await updateEngine(name, updatedEngine)
6669
mutate()
70+
events.emit(EngineEvent.OnEngineUpdate, {})
6771
}, 300)
6872
},
6973
[engine, name, mutate]

web/screens/Thread/ThreadCenterPanel/AssistantSetting/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const AssistantSetting: React.FC<Props> = ({ componentData }) => {
8888
setEngineParamsUpdate,
8989
stopModel,
9090
updateThreadMetadata,
91+
resetGenerating,
9192
]
9293
)
9394

0 commit comments

Comments
 (0)