Skip to content

Commit 7a36ed2

Browse files
authored
Merge pull request #6681 from menloresearch/fix/local-api-server
fix: local api server auto start first model when last used missing
2 parents c5a5968 + d102165 commit 7a36ed2

11 files changed

Lines changed: 213 additions & 161 deletions

File tree

web-app/src/containers/DropdownModelProvider.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { predefinedProviders } from '@/consts/providers'
2424
import { useServiceHub } from '@/hooks/useServiceHub'
2525
import { PlatformFeatures } from '@/lib/platform/const'
2626
import { PlatformFeature } from '@/lib/platform/types'
27+
import { getLastUsedModel } from '@/utils/getModelToStart'
2728

2829
type DropdownModelProviderProps = {
2930
model?: ThreadModel
@@ -39,16 +40,6 @@ interface SearchableModel {
3940
}
4041

4142
// Helper functions for localStorage
42-
const getLastUsedModel = (): { provider: string; model: string } | null => {
43-
try {
44-
const stored = localStorage.getItem(localStorageKey.lastUsedModel)
45-
return stored ? JSON.parse(stored) : null
46-
} catch (error) {
47-
console.debug('Failed to get last used model from localStorage:', error)
48-
return null
49-
}
50-
}
51-
5243
const setLastUsedModel = (provider: string, model: string) => {
5344
try {
5445
localStorage.setItem(

web-app/src/locales/de-DE/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@
169169
"serverLogs": "Server Logs",
170170
"serverLogsDesc": "Zeige detaillierte Logs des lokalen API-Servers an.",
171171
"openLogs": "Logs öffnen",
172+
"swaggerDocs": "API-Dokumentation",
173+
"swaggerDocsDesc": "Zeige interaktive API-Dokumentation (Swagger UI) an.",
174+
"openDocs": "Dokumentation öffnen",
175+
"startupConfiguration": "Startkonfiguration",
176+
"runOnStartup": "Auto start",
177+
"runOnStartupDesc": "Starte den lokalen API-Server automatisch beim Anwendungsstart. Verwendet das zuletzt verwendete Modell oder wählt das erste verfügbare Modell, falls nicht verfügbar.",
172178
"serverConfiguration": "Server Konfiguration",
173179
"serverHost": "Server Host",
174180
"serverHostDesc": "Netzwerkadresse für den Server.",

web-app/src/locales/en/settings.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,12 @@
169169
"serverLogs": "Server Logs",
170170
"serverLogsDesc": "View detailed logs of the local API server.",
171171
"openLogs": "Open Logs",
172+
"swaggerDocs": "API Documentation",
173+
"swaggerDocsDesc": "View interactive API documentation (Swagger UI).",
174+
"openDocs": "Open Docs",
172175
"startupConfiguration": "Startup Configuration",
173-
"runOnStartup": "Enable by default on startup",
174-
"runOnStartupDesc": "Automatically start the Local API Server when the application launches.",
176+
"runOnStartup": "Auto start",
177+
"runOnStartupDesc": "Automatically start the Local API Server when the application launches. Uses last used model, or picks the first available model if unavailable.",
175178
"serverConfiguration": "Server Configuration",
176179
"serverHost": "Server Host",
177180
"serverHostDesc": "Network address for the server.",

web-app/src/locales/id/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@
167167
"serverLogs": "Log Server",
168168
"serverLogsDesc": "Lihat log terperinci dari server API lokal.",
169169
"openLogs": "Buka Log",
170+
"swaggerDocs": "Dokumentasi API",
171+
"swaggerDocsDesc": "Lihat dokumentasi API interaktif (Swagger UI).",
172+
"openDocs": "Buka Dokumentasi",
173+
"startupConfiguration": "Konfigurasi Startup",
174+
"runOnStartup": "Auto start",
175+
"runOnStartupDesc": "Mulai Server API Lokal secara otomatis saat aplikasi diluncurkan. Menggunakan model terakhir yang digunakan, atau memilih model pertama yang tersedia jika tidak tersedia.",
170176
"serverConfiguration": "Konfigurasi Server",
171177
"serverHost": "Host Server",
172178
"serverHostDesc": "Alamat jaringan untuk server.",

web-app/src/locales/pl/settings.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@
167167
"serverLogs": "Dzienniki Serwera",
168168
"serverLogsDesc": "Wyświetl szczegółowe dzienniki lokalnego serwera API.",
169169
"openLogs": "Otwórz Dzienniki",
170+
"swaggerDocs": "Dokumentacja API",
171+
"swaggerDocsDesc": "Wyświetl interaktywną dokumentację API (Swagger UI).",
172+
"openDocs": "Otwórz Dokumentację",
170173
"startupConfiguration": "Konfiguracja Startowa",
171-
"runOnStartup": "Domyślnie włączaj przy starcie",
172-
"runOnStartupDesc": "Automatycznie uruchamiaj lokalny serwer API podczas uruchamiania aplikacji.",
174+
"runOnStartup": "Auto start",
175+
"runOnStartupDesc": "Automatycznie uruchamiaj lokalny serwer API podczas uruchamiania aplikacji. Używa ostatnio używanego modelu lub wybiera pierwszy dostępny model, jeśli nie jest dostępny.",
173176
"serverConfiguration": "Konfiguracja Serwera",
174177
"serverHost": "Host",
175178
"serverHostDesc": "Adres sieciowy serwera.",

web-app/src/locales/vn/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@
169169
"serverLogs": "Nhật ký máy chủ",
170170
"serverLogsDesc": "Xem nhật ký chi tiết của máy chủ API cục bộ.",
171171
"openLogs": "Mở nhật ký",
172+
"swaggerDocs": "Tài liệu API",
173+
"swaggerDocsDesc": "Xem tài liệu API tương tác (Swagger UI).",
174+
"openDocs": "Mở tài liệu",
175+
"startupConfiguration": "Cấu hình khởi động",
176+
"runOnStartup": "Auto start",
177+
"runOnStartupDesc": "Tự động khởi động Máy chủ API Cục bộ khi ứng dụng khởi chạy. Sử dụng mô hình đã dùng gần nhất hoặc chọn mô hình đầu tiên có sẵn nếu không khả dụng.",
172178
"serverConfiguration": "Cấu hình máy chủ",
173179
"serverHost": "Máy chủ lưu trữ",
174180
"serverHostDesc": "Địa chỉ mạng cho máy chủ.",

web-app/src/locales/zh-CN/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@
169169
"serverLogs": "服务器日志",
170170
"serverLogsDesc": "查看本地 API 服务器的详细日志。",
171171
"openLogs": "打开日志",
172+
"swaggerDocs": "API 文档",
173+
"swaggerDocsDesc": "查看交互式 API 文档(Swagger UI)。",
174+
"openDocs": "打开文档",
175+
"startupConfiguration": "启动配置",
176+
"runOnStartup": "Auto start",
177+
"runOnStartupDesc": "应用程序启动时自动启动本地 API 服务器。使用上次使用的模型,如果不可用则选择第一个可用模型。",
172178
"serverConfiguration": "服务器配置",
173179
"serverHost": "服务器主机",
174180
"serverHostDesc": "服务器的网络地址。",

web-app/src/locales/zh-TW/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@
167167
"serverLogs": "伺服器日誌",
168168
"serverLogsDesc": "檢視本機 API 伺服器的詳細日誌。",
169169
"openLogs": "開啟日誌",
170+
"swaggerDocs": "API 文件",
171+
"swaggerDocsDesc": "查看互動式 API 文件(Swagger UI)。",
172+
"openDocs": "開啟文件",
173+
"startupConfiguration": "啟動設定",
174+
"runOnStartup": "Auto start",
175+
"runOnStartupDesc": "應用程式啟動時自動啟動本機 API 伺服器。使用上次使用的模型,如果不可用則選擇第一個可用模型。",
170176
"serverConfiguration": "伺服器設定",
171177
"serverHost": "伺服器主機",
172178
"serverHostDesc": "伺服器的網路位址。",

web-app/src/providers/DataProvider.tsx

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { useThreads } from '@/hooks/useThreads'
1212
import { useLocalApiServer } from '@/hooks/useLocalApiServer'
1313
import { useAppState } from '@/hooks/useAppState'
1414
import { AppEvent, events } from '@janhq/core'
15-
import { localStorageKey } from '@/constants/localStorage'
1615
import { SystemEvent } from '@/types/events'
16+
import { getModelToStart } from '@/utils/getModelToStart'
1717

1818
export function DataProvider() {
1919
const { setProviders, selectedModel, selectedProvider, getProviderByName } =
@@ -66,12 +66,15 @@ export function DataProvider() {
6666

6767
// Listen for deep link events
6868
let unsubscribe = () => {}
69-
serviceHub.events().listen(SystemEvent.DEEP_LINK, (event) => {
70-
const deep_link = event.payload as string
71-
handleDeepLink([deep_link])
72-
}).then((unsub) => {
73-
unsubscribe = unsub
74-
})
69+
serviceHub
70+
.events()
71+
.listen(SystemEvent.DEEP_LINK, (event) => {
72+
const deep_link = event.payload as string
73+
handleDeepLink([deep_link])
74+
})
75+
.then((unsub) => {
76+
unsubscribe = unsub
77+
})
7578
return () => {
7679
unsubscribe()
7780
}
@@ -109,54 +112,6 @@ export function DataProvider() {
109112
})
110113
}, [serviceHub, setProviders])
111114

112-
const getLastUsedModel = (): { provider: string; model: string } | null => {
113-
try {
114-
const stored = localStorage.getItem(localStorageKey.lastUsedModel)
115-
return stored ? JSON.parse(stored) : null
116-
} catch (error) {
117-
console.debug('Failed to get last used model from localStorage:', error)
118-
return null
119-
}
120-
}
121-
122-
// Helper function to determine which model to start
123-
const getModelToStart = () => {
124-
// Use last used model if available
125-
const lastUsedModel = getLastUsedModel()
126-
if (lastUsedModel) {
127-
const provider = getProviderByName(lastUsedModel.provider)
128-
if (
129-
provider &&
130-
provider.models.some((m) => m.id === lastUsedModel.model)
131-
) {
132-
return { model: lastUsedModel.model, provider }
133-
}
134-
}
135-
136-
// Use selected model if available
137-
if (selectedModel && selectedProvider) {
138-
const provider = getProviderByName(selectedProvider)
139-
if (provider) {
140-
return { model: selectedModel.id, provider }
141-
}
142-
}
143-
144-
// Use first model from llamacpp provider
145-
const llamacppProvider = getProviderByName('llamacpp')
146-
if (
147-
llamacppProvider &&
148-
llamacppProvider.models &&
149-
llamacppProvider.models.length > 0
150-
) {
151-
return {
152-
model: llamacppProvider.models[0].id,
153-
provider: llamacppProvider,
154-
}
155-
}
156-
157-
return null
158-
}
159-
160115
// Auto-start Local API Server on app startup if enabled
161116
useEffect(() => {
162117
if (enableOnStartup) {
@@ -166,7 +121,11 @@ export function DataProvider() {
166121
return
167122
}
168123

169-
const modelToStart = getModelToStart()
124+
const modelToStart = getModelToStart({
125+
selectedModel,
126+
selectedProvider,
127+
getProviderByName,
128+
})
170129

171130
// Only start server if we have a model to load
172131
if (!modelToStart) {

0 commit comments

Comments
 (0)