Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/web/src/assistant/lifecycle-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export interface LifecycleServiceInputs {
* behavior. Optional so existing `setInputs` callers/tests need no change.
*/
selectedPlatformAssistantId?: string | null;
/**
* Whether the organization store has resolved an active org ID.
* Platform API calls require the Vellum-Organization-Id header;
* `respondToInputs` defers `checkAssistant` until this is true.
*/
hasOrganization?: boolean;
}

const NOOP_REDIRECT = (_: string) => {};
Expand Down Expand Up @@ -194,6 +200,7 @@ class AssistantLifecycleService {
if (this.inputs.hasPlatformSession) {
setSelfHostedConnection(null);
}
if (!this.inputs.hasOrganization) return;
await this.checkAssistant();
}

Expand Down
12 changes: 9 additions & 3 deletions apps/web/src/assistant/use-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ export function useAssistantLifecycle({
}: UseAssistantLifecycleOptions): void {
const queryClient = useQueryClient();

const currentOrganizationId =
useOrganizationStore.use.currentOrganizationId();

// Whether to query the server-side status at all. Gateway-auth
// mode and "local mode without platform session" short-circuit
// to local states without ever calling /assistant/.
// Platform API calls require the Vellum-Organization-Id header;
// wait for the org store to resolve before firing them.
const shouldQueryServer =
isAuthenticated(sessionStatus) &&
!isGatewayAuthMode() &&
(hasPlatformSession || !isLocalMode());
(hasPlatformSession || !isLocalMode()) &&
!!currentOrganizationId;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Gate the imperative lifecycle fetch on org readiness

For authenticated platform sessions while currentOrganizationId is still hydrating, this only disables the passive useAssistantQuery; the effect below still calls lifecycleService.respondToInputs(), which immediately runs checkAssistant()/fetchQuery() and hits assistantsList without the Vellum-Organization-Id header. In the common case where selectedPlatformAssistantId remains null (multi-assistant disabled), the effect also won't rerun when the org later arrives, so the startup 400 can still put the lifecycle into an error/stuck state instead of recovering.

Useful? React with 👍 / 👎.


// Which platform assistant the user has selected, gated by the
// multi-platform-assistant flag. When the flag is off (or no
Expand All @@ -67,8 +73,6 @@ export function useAssistantLifecycle({
// pre-multi-assistant behavior.
const multiAssistantEnabled =
useClientFeatureFlagStore.use.multiPlatformAssistant();
const currentOrganizationId =
useOrganizationStore.use.currentOrganizationId();
const byOrg =
useResolvedAssistantsStore.use.selectedPlatformAssistantByOrg();
const selectedPlatformAssistantId =
Expand Down Expand Up @@ -97,6 +101,7 @@ export function useAssistantLifecycle({
resolveOnboardingRedirect,
queryClient,
selectedPlatformAssistantId,
hasOrganization: !!currentOrganizationId,
});
void lifecycleService.respondToInputs();
}, [
Expand All @@ -108,6 +113,7 @@ export function useAssistantLifecycle({
resolveOnboardingRedirect,
queryClient,
selectedPlatformAssistantId,
currentOrganizationId,
]);

// Hand poll results to the service — it decides whether to
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/root-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { useElectronFeatureFlagBridge } from "@/runtime/electron-feature-flags";
import { TimezoneSync } from "@/components/timezone-sync";
import { retireAssistant } from "@/assistant/retire-service";
import { selectPlatformAssistant } from "@/assistant/select-platform-assistant";
import { useOrganizationStore } from "@/stores/organization-store";
import { CreateAssistantDialog } from "@/components/create-assistant-dialog";
import { ConfirmDialog } from "@vellumai/design-library/components/confirm-dialog";
import { toast } from "@vellumai/design-library/components/toast";
Expand Down Expand Up @@ -90,7 +91,8 @@ export function RootLayout() {
const isSessionInitializing = useIsSessionInitializing();
const hasPlatformSession = useHasPlatformSession();
const isNonProduction = useEnvironmentStore.use.isNonProduction();
useClientFeatureFlagSync(hasPlatformSession && !isSessionInitializing);
const hasOrganization = !!useOrganizationStore.use.currentOrganizationId();
useClientFeatureFlagSync(hasPlatformSession && !isSessionInitializing && hasOrganization);
useAssistantLifecycle({
sessionStatus,
isRetired: false,
Expand Down