From f511fe4fcf4ccd96c208e504028aef4f57816dfb Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 1 Dec 2026 00:40:57 +0100 Subject: [PATCH] fix(assistant): preserve managed status gating Only request managed status for the assistant addon. Cover the configured attachment path and keep operator-only gateway configuration out of the self-hosted environment reference. --- .../components/Assistant/useAssistantStatus.ts | 4 +++- client/src/store/assistantStore.test.ts | 9 +++++++-- client/src/store/assistantStore.ts | 16 ++++++++-------- .../tests/unit/app-config/env-reference.test.ts | 3 +++ .../assistant-attachments.controller.test.ts | 11 +++++++++-- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/client/src/components/Assistant/useAssistantStatus.ts b/client/src/components/Assistant/useAssistantStatus.ts index b905919c3..3b25372bc 100644 --- a/client/src/components/Assistant/useAssistantStatus.ts +++ b/client/src/components/Assistant/useAssistantStatus.ts @@ -2,6 +2,7 @@ import { useEffect } from 'react'; import { useAddonStore } from '../../store/addonStore'; import { aiRoutesOpen, useAssistantStore } from '../../store/assistantStore'; +import { useAuthStore } from '../../store/authStore'; /** * Asks for the assistant's status when a view of it mounts, and again when @@ -13,7 +14,8 @@ import { aiRoutesOpen, useAssistantStore } from '../../store/assistantStore'; */ export function useAssistantStatus(): void { const loadStatus = useAssistantStore((s) => s.loadStatus); - const routesOpen = useAddonStore(aiRoutesOpen); + const managed = useAuthStore((s) => s.managed); + const routesOpen = useAddonStore((s) => aiRoutesOpen(s, managed)); const assistantOn = useAddonStore((s) => s.isEnabled('ai_assistant')); useEffect(() => { diff --git a/client/src/store/assistantStore.test.ts b/client/src/store/assistantStore.test.ts index e8f46fe6e..294c3d51a 100644 --- a/client/src/store/assistantStore.test.ts +++ b/client/src/store/assistantStore.test.ts @@ -363,13 +363,18 @@ describe('assistantStore: threads, window and status', () => { expect(store().status).toBeNull(); }); - it('FE-AI-STORE-014: a managed install, which refuses /api/ai, is not asked whatever its addons say', async () => { + it('FE-AI-STORE-014: a managed install only asks for status when its assistant addon is on', async () => { const status = vi.spyOn(aiConnectionsRepo, 'status').mockResolvedValue(STATUS); useAssistantStore.setState({ status: STATUS }); useAuthStore.setState({ managed: true }); - addonsOn('ai_assistant', 'llm_parsing'); + addonsOn('llm_parsing'); await store().loadStatus(); expect(status).not.toHaveBeenCalled(); expect(store().status).toBeNull(); + + addonsOn('ai_assistant', 'llm_parsing'); + await store().loadStatus(); + expect(status).toHaveBeenCalledOnce(); + expect(store().status).toEqual(STATUS); }); }); diff --git a/client/src/store/assistantStore.ts b/client/src/store/assistantStore.ts index a873452da..bed8cc9b0 100644 --- a/client/src/store/assistantStore.ts +++ b/client/src/store/assistantStore.ts @@ -37,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'; @@ -324,13 +325,13 @@ export function chatOf(state: Pick, key: AssistantChat * 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. The server's status response decides availability on managed installs. + * store. On managed installs only the assistant addon opens the status route. */ -export function aiRoutesOpen({ - loaded, - isEnabled, -}: Pick, 'loaded' | 'isEnabled'>): boolean { - return loaded && (isEnabled('ai_assistant') || isEnabled('llm_parsing')); +export function aiRoutesOpen( + { loaded, isEnabled }: Pick, 'loaded' | 'isEnabled'>, + managed: boolean, +): boolean { + return loaded && (isEnabled('ai_assistant') || (!managed && isEnabled('llm_parsing'))); } function httpStatus(err: unknown): number | undefined { @@ -585,8 +586,7 @@ function backToAfter(byTrip: Record): Record { - // When both addons are off the status route is closed. - if (!aiRoutesOpen(useAddonStore.getState())) { + if (!aiRoutesOpen(useAddonStore.getState(), useAuthStore.getState().managed)) { ctx.set({ status: null }); return; } diff --git a/server/tests/unit/app-config/env-reference.test.ts b/server/tests/unit/app-config/env-reference.test.ts index 70a97caac..4b1d3fcc3 100644 --- a/server/tests/unit/app-config/env-reference.test.ts +++ b/server/tests/unit/app-config/env-reference.test.ts @@ -24,6 +24,9 @@ const NOT_OPERATOR_SETTINGS: Record = { APP_VERSION: 'set by the image build; the VERSION file wins over it', TREK_DB_FILE: 'points the Playwright harness at a throwaway database', TREK_MANAGED: 'managed hosting only, deliberately undocumented for self-hosters', + TREK_MANAGED_ASSISTANT_URL: 'managed hosting only, the operator gateway URL', + TREK_MANAGED_ASSISTANT_TOKEN: 'managed hosting only, the instance credential for the operator gateway', + TREK_MANAGED_ASSISTANT_MODEL: 'managed hosting only, the operator-selected assistant model', MAPBOX_ACCESS_TOKEN: 'managed hosting only (read with TREK_MANAGED), deliberately undocumented for self-hosters', CARTO_API_KEY: 'managed hosting only (read with TREK_MANAGED), deliberately undocumented for self-hosters', PLACES_API_BASE: "managed hosting's egress gateway for the Places calls, deliberately undocumented for self-hosters", diff --git a/server/tests/unit/nest/assistant/assistant-attachments.controller.test.ts b/server/tests/unit/nest/assistant/assistant-attachments.controller.test.ts index 657dbd220..bfc64385f 100644 --- a/server/tests/unit/nest/assistant/assistant-attachments.controller.test.ts +++ b/server/tests/unit/nest/assistant/assistant-attachments.controller.test.ts @@ -48,13 +48,16 @@ class FakeResponse extends EventEmitter { } } -function make(opts: { addon?: boolean; managed?: boolean; trip?: boolean } = {}) { +function make(opts: { addon?: boolean; managed?: boolean; managedAssistant?: boolean; trip?: boolean } = {}) { const service = { attach: vi.fn(async (_u: number, _t: number, _b: unknown, _f: unknown, _c: TurnChannel) => undefined), }; const trips = { findAccessible: vi.fn(async () => (opts.trip === false ? undefined : { id: 5, user_id: 7 })) }; const addons = { isAddonEnabled: vi.fn(async () => opts.addon !== false) }; - const env = { isManaged: () => opts.managed === true }; + const env = { + isManaged: () => opts.managed === true, + env: () => ({ managed: { assistant: opts.managedAssistant ? { model: 'test-model' } : undefined } }), + }; const controller = new AssistantAttachmentsController( service as unknown as AssistantService, trips as unknown as TripAccessService, @@ -105,6 +108,10 @@ describe('AssistantAttachmentsController', () => { }); expect(stranger.trips.findAccessible).toHaveBeenCalledWith('5', 7); for (const t of [off, managed, stranger]) expect(t.service.attach).not.toHaveBeenCalled(); + + const managedWithAssistant = make({ managed: true, managedAssistant: true }); + await managedWithAssistant.controller.attach(user, '5', file('a.pdf'), BODY, res); + expect(managedWithAssistant.service.attach).toHaveBeenCalledOnce(); }); it('ASTATT-003: a missing, too large or unknown file is refused before the turn', async () => {