@@ -243,22 +243,6 @@ export default class Serve extends Command {
243243 // No translations and no explicit i18n plugin — this is fine, kernel fallback works
244244 }
245245
246- // 3c. Auto-register AIServicePlugin if not already present
247- const hasAiPlugin = plugins . some (
248- ( p : any ) => p . name === 'com.objectstack.service-ai'
249- || p . constructor ?. name === 'AIServicePlugin'
250- ) ;
251- if ( ! hasAiPlugin ) {
252- try {
253- const aiPkg = '@objectstack/service-ai' ;
254- const { AIServicePlugin } = await import ( /* webpackIgnore: true */ aiPkg ) ;
255- await kernel . use ( new AIServicePlugin ( ) ) ;
256- trackPlugin ( 'AIService' ) ;
257- } catch {
258- // @objectstack /service-ai not installed — skip
259- }
260- }
261-
262246 // Add HTTP server plugin BEFORE config plugins so that the
263247 // http-server service is available for any plugin that needs it
264248 // during init/start (e.g. AuthPlugin).
@@ -334,6 +318,24 @@ export default class Serve extends Command {
334318 }
335319 }
336320
321+ // 4. Auto-register AIServicePlugin if not already loaded by config plugins.
322+ // Registered AFTER Dispatcher so that the ai:routes hook listener is
323+ // already in place when AIServicePlugin.start() fires the hook.
324+ const hasAIPlugin = plugins . some (
325+ ( p : any ) => p . name === 'com.objectstack.service-ai'
326+ || p . constructor ?. name === 'AIServicePlugin'
327+ ) ;
328+ if ( ! hasAIPlugin ) {
329+ try {
330+ const aiPkg = '@objectstack/service-ai' ;
331+ const { AIServicePlugin } = await import ( /* webpackIgnore: true */ aiPkg ) ;
332+ await kernel . use ( new AIServicePlugin ( ) ) ;
333+ trackPlugin ( 'AIService' ) ;
334+ } catch {
335+ // @objectstack /service-ai not installed — AI features unavailable
336+ }
337+ }
338+
337339 // ── Studio UI ─────────────────────────────────────────────────
338340 // In dev mode, Studio UI is enabled by default (use --no-ui to disable).
339341 // Always serves the pre-built dist/ — no Vite dev server, no extra port.
0 commit comments