fix(assistant): no status request while neither AI addon is on

Every page of an install with the assistant and AI parsing off asked /api/ai/status and logged the 404 the addon guard answers. The store asks only while an addon opens the route and the install is not managed, and one hook on the desktop window and both phone screens asks again when the assistant or AI parsing is switched.
This commit is contained in:
Maurice
2026-10-25 00:11:22 +00:00
parent 4051af58a7
commit 2d398568da
13 changed files with 196 additions and 18 deletions
+45 -1
View File
@@ -1,5 +1,5 @@
/**
* FE-AI-STORE-001 to FE-AI-STORE-012
* FE-AI-STORE-001 to FE-AI-STORE-014
*
* The store runs a turn end to end against a faked repo: the request it
* sends, how the stream lands in the chat, and what each way of failing
@@ -9,11 +9,14 @@
*/
import type { AiStatusResponse, AssistantMessage, AssistantThreadResponse, UiStreamChunk } from '@trek/shared';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { seedStore } from '../../tests/helpers/store';
import { AssistantRequestError } from '../api/assistant';
import { aiConnectionsRepo } from '../repo/aiConnectionsRepo';
import { assistantRepo } from '../repo/assistantRepo';
import { OnlineOnlyError } from '../repo/onlineOnly';
import { useAddonStore } from './addonStore';
import { chatOf, useAssistantStore } from './assistantStore';
import { useAuthStore } from './authStore';
import { useSettingsStore } from './settingsStore';
const TRIP = 4;
@@ -60,6 +63,10 @@ async function* hangingStream(first: UiStreamChunk[], signal: AbortSignal): Asyn
});
}
/** The addon list as it lands from the server, with these addons on. */
const addonsOn = (...ids: string[]) =>
seedStore(useAddonStore, { loaded: true, addons: ids.map((id) => ({ id, enabled: true })) });
const chat = () => chatOf(useAssistantStore.getState(), TRIP);
const store = () => useAssistantStore.getState();
@@ -76,6 +83,7 @@ beforeEach(() => {
lastApplied: null,
});
useSettingsStore.setState((s) => ({ settings: { ...s.settings, language: 'de' } }));
useAuthStore.setState({ managed: false });
});
afterEach(() => vi.restoreAllMocks());
@@ -311,6 +319,7 @@ describe('assistantStore: threads, window and status', () => {
});
it('FE-AI-STORE-012: status fails closed, offline keeps the last answer, and the privacy notice is acknowledged', async () => {
addonsOn('ai_assistant');
vi.spyOn(aiConnectionsRepo, 'status').mockResolvedValueOnce(STATUS);
await store().loadStatus();
expect(store().status).toEqual(STATUS);
@@ -328,4 +337,39 @@ describe('assistantStore: threads, window and status', () => {
await store().loadStatus();
expect(store().status).toBeNull();
});
it('FE-AI-STORE-013: the status is asked for only while the assistant or AI parsing is on', async () => {
const status = vi.spyOn(aiConnectionsRepo, 'status').mockResolvedValue(STATUS);
useAddonStore.setState({ addons: [], loaded: false });
await store().loadStatus();
addonsOn('mcp');
await store().loadStatus();
expect(status).not.toHaveBeenCalled();
expect(store().status).toBeNull();
addonsOn('llm_parsing');
await store().loadStatus();
expect(status).toHaveBeenCalledTimes(1);
expect(store().status).toEqual(STATUS);
addonsOn('ai_assistant');
await store().loadStatus();
expect(status).toHaveBeenCalledTimes(2);
// Switched off again, the assistant goes away without a request.
addonsOn();
await store().loadStatus();
expect(status).toHaveBeenCalledTimes(2);
expect(store().status).toBeNull();
});
it('FE-AI-STORE-014: a managed install, which refuses /api/ai, is not asked whatever its addons say', async () => {
const status = vi.spyOn(aiConnectionsRepo, 'status').mockResolvedValue(STATUS);
useAssistantStore.setState({ status: STATUS });
useAuthStore.setState({ managed: true });
addonsOn('ai_assistant', 'llm_parsing');
await store().loadStatus();
expect(status).not.toHaveBeenCalled();
expect(store().status).toBeNull();
});
});
+25 -1
View File
@@ -25,6 +25,7 @@ import type { Assignment } from '../types';
import { browserTimeZone } from '../utils/localDate';
import { randomId } from '../utils/randomId';
import type { RouteProfileFocus } from '../utils/routeGeometry';
import { useAddonStore } from './addonStore';
import { assistantScopeOf, assistantTargetOf, type AssistantChatKey } from './assistantChatKey';
import {
blockDays,
@@ -36,6 +37,7 @@ import {
type WindowRect,
} from './assistantMapModel';
import { applyChunk, closeUndo, markDecisions } from './assistantMessages';
import { useAuthStore } from './authStore';
import { useSettingsStore } from './settingsStore';
import { useTripStore } from './tripStore';
@@ -120,7 +122,10 @@ interface UndoBlockResult {
}
interface AssistantState {
/** What the assistant runs on for this user; null until loaded, and the assistant stays hidden until then. */
/**
* What the assistant runs on for this user; null until loaded, while no addon
* opens /api/ai (aiRoutesOpen) and on a managed install, and the assistant stays hidden then.
*/
status: AiStatusResponse | null;
window: { open: boolean; minimized: boolean };
/** The chats by `AssistantChatKey`: a trip's id, or a journey's key. */
@@ -316,6 +321,19 @@ export function chatOf(state: Pick<AssistantState, 'byTrip'>, key: AssistantChat
return state.byTrip[key] ?? EMPTY_CHAT;
}
/**
* Whether the addons open /api/ai: the server opens it while the assistant or
* AI parsing is on, the two sharing one connection, and answers 404 while
* both are off. Closed until the addon list is in. A selector on the addon
* store; a managed install refuses /api/ai whatever it says (loadStatus).
*/
export function aiRoutesOpen({
loaded,
isEnabled,
}: Pick<ReturnType<typeof useAddonStore.getState>, 'loaded' | 'isEnabled'>): boolean {
return loaded && (isEnabled('ai_assistant') || isEnabled('llm_parsing'));
}
function httpStatus(err: unknown): number | undefined {
return (err as { response?: { status?: number } } | null)?.response?.status;
}
@@ -568,6 +586,12 @@ function backToAfter(byTrip: Record<number, AssistantTripChat>): Record<number,
function shellActions(ctx: Ctx) {
return {
loadStatus: async () => {
// While /api/ai is closed the request could only get a 404, or the 403 of a managed install: nothing is asked,
// and the assistant stays hidden. The managed flag comes with the app config, which can land after the addons.
if (useAuthStore.getState().managed || !aiRoutesOpen(useAddonStore.getState())) {
ctx.set({ status: null });
return;
}
try {
ctx.set({ status: await aiConnectionsRepo.status() });
} catch (err) {