@@ -13,6 +13,9 @@ import { TrustedHostsInput } from '@/containers/TrustedHostsInput'
1313import { useLocalApiServer } from '@/hooks/useLocalApiServer'
1414import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
1515import { useAppState } from '@/hooks/useAppState'
16+ import { useModelProvider } from '@/hooks/useModelProvider'
17+ import { startModel } from '@/services/models'
18+ import { localStorageKey } from '@/constants/localStorage'
1619import { windowKey } from '@/constants/windows'
1720import { IconLogs } from '@tabler/icons-react'
1821import { cn } from '@/lib/utils'
@@ -32,6 +35,8 @@ function LocalAPIServer() {
3235 setCorsEnabled,
3336 verboseLogs,
3437 setVerboseLogs,
38+ enableOnStartup,
39+ setEnableOnStartup,
3540 serverHost,
3641 serverPort,
3742 apiPrefix,
@@ -40,6 +45,7 @@ function LocalAPIServer() {
4045 } = useLocalApiServer ( )
4146
4247 const { serverStatus, setServerStatus } = useAppState ( )
48+ const { selectedModel, selectedProvider, getProviderByName } = useModelProvider ( )
4349 const [ showApiKeyError , setShowApiKeyError ] = useState ( false )
4450 const [ isApiKeyEmpty , setIsApiKeyEmpty ] = useState (
4551 ! apiKey || apiKey . toString ( ) . trim ( ) . length === 0
@@ -60,6 +66,54 @@ function LocalAPIServer() {
6066 setIsApiKeyEmpty ( ! isValid )
6167 }
6268
69+ const getLastUsedModel = ( ) : { provider : string ; model : string } | null => {
70+ try {
71+ const stored = localStorage . getItem ( localStorageKey . lastUsedModel )
72+ return stored ? JSON . parse ( stored ) : null
73+ } catch ( error ) {
74+ console . debug ( 'Failed to get last used model from localStorage:' , error )
75+ return null
76+ }
77+ }
78+
79+ // Helper function to determine which model to start
80+ const getModelToStart = ( ) => {
81+ // Use last used model if available
82+ const lastUsedModel = getLastUsedModel ( )
83+ if ( lastUsedModel ) {
84+ const provider = getProviderByName ( lastUsedModel . provider )
85+ if (
86+ provider &&
87+ provider . models . some ( ( m ) => m . id === lastUsedModel . model )
88+ ) {
89+ return { model : lastUsedModel . model , provider }
90+ }
91+ }
92+
93+ // Use selected model if available
94+ if ( selectedModel && selectedProvider ) {
95+ const provider = getProviderByName ( selectedProvider )
96+ if ( provider ) {
97+ return { model : selectedModel . id , provider }
98+ }
99+ }
100+
101+ // Use first model from llamacpp provider
102+ const llamacppProvider = getProviderByName ( 'llamacpp' )
103+ if (
104+ llamacppProvider &&
105+ llamacppProvider . models &&
106+ llamacppProvider . models . length > 0
107+ ) {
108+ return {
109+ model : llamacppProvider . models [ 0 ] . id ,
110+ provider : llamacppProvider ,
111+ }
112+ }
113+
114+ return null
115+ }
116+
63117 const toggleAPIServer = async ( ) => {
64118 // Validate API key before starting server
65119 if ( serverStatus === 'stopped' ) {
@@ -68,19 +122,33 @@ function LocalAPIServer() {
68122 return
69123 }
70124 setShowApiKeyError ( false )
71- }
72125
73- setServerStatus ( 'pending' )
74- if ( serverStatus === 'stopped' ) {
75- window . core ?. api
76- ?. startServer ( {
77- host : serverHost ,
78- port : serverPort ,
79- prefix : apiPrefix ,
80- apiKey,
81- trustedHosts,
82- isCorsEnabled : corsEnabled ,
83- isVerboseEnabled : verboseLogs ,
126+ const modelToStart = getModelToStart ( )
127+ // Only start server if we have a model to load
128+ if ( ! modelToStart ) {
129+ console . warn (
130+ 'Cannot start Local API Server: No model available to load'
131+ )
132+ return
133+ }
134+
135+ setServerStatus ( 'pending' )
136+
137+ // Start the model first
138+ startModel ( modelToStart . provider , modelToStart . model )
139+ . then ( ( ) => {
140+ console . log ( `Model ${ modelToStart . model } started successfully` )
141+
142+ // Then start the server
143+ return window . core ?. api ?. startServer ( {
144+ host : serverHost ,
145+ port : serverPort ,
146+ prefix : apiPrefix ,
147+ apiKey,
148+ trustedHosts,
149+ isCorsEnabled : corsEnabled ,
150+ isVerboseEnabled : verboseLogs ,
151+ } )
84152 } )
85153 . then ( ( ) => {
86154 setServerStatus ( 'running' )
@@ -90,6 +158,7 @@ function LocalAPIServer() {
90158 setServerStatus ( 'stopped' )
91159 } )
92160 } else {
161+ setServerStatus ( 'pending' )
93162 window . core ?. api
94163 ?. stopServer ( )
95164 . then ( ( ) => {
@@ -199,6 +268,26 @@ function LocalAPIServer() {
199268 />
200269 </ Card >
201270
271+ { /* Startup Configuration */ }
272+ < Card title = { t ( 'settings:localApiServer.startupConfiguration' ) } >
273+ < CardItem
274+ title = { t ( 'settings:localApiServer.runOnStartup' ) }
275+ description = { t ( 'settings:localApiServer.runOnStartupDesc' ) }
276+ actions = {
277+ < Switch
278+ checked = { enableOnStartup }
279+ onCheckedChange = { ( checked ) => {
280+ if ( ! apiKey || apiKey . toString ( ) . trim ( ) . length === 0 ) {
281+ setShowApiKeyError ( true )
282+ return
283+ }
284+ setEnableOnStartup ( checked )
285+ } }
286+ />
287+ }
288+ />
289+ </ Card >
290+
202291 { /* Server Configuration */ }
203292 < Card title = { t ( 'settings:localApiServer.serverConfiguration' ) } >
204293 < CardItem
0 commit comments