Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 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,13 @@ export interface LifecycleServiceInputs {
* behavior. Optional so existing `setInputs` callers/tests need no change.
*/
selectedPlatformAssistantId?: string | null;
/**
* Whether the org store has hydrated (or no platform session exists).
* Platform API calls require the Vellum-Organization-Id header;
* `respondToInputs` defers `checkAssistant` until this is true.
* Mirrors `useIsOrgReady()` from the React layer.
*/
isOrgReady?: boolean;
}

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

Expand Down
14 changes: 11 additions & 3 deletions apps/web/src/assistant/use-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { lifecycleService } from "@/assistant/lifecycle-service";
import { useAssistantQuery } from "@/assistant/queries";
import { isGatewayAuthMode } from "@/lib/auth/gateway-session";
import { isLocalMode } from "@/lib/local-mode";
import { useIsOrgReady } from "@/hooks/use-is-org-ready";
import { isAuthenticated, type SessionStatus } from "@/stores/session-status";
import { useClientFeatureFlagStore } from "@/stores/client-feature-flag-store";
import { useResolvedAssistantsStore } from "@/stores/resolved-assistants-store";
Expand Down Expand Up @@ -52,13 +53,20 @@ export function useAssistantLifecycle({
}: UseAssistantLifecycleOptions): void {
const queryClient = useQueryClient();

const isOrgReady = useIsOrgReady();
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()) &&
isOrgReady;

// 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 +75,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 +103,7 @@ export function useAssistantLifecycle({
resolveOnboardingRedirect,
queryClient,
selectedPlatformAssistantId,
isOrgReady,
});
void lifecycleService.respondToInputs();
}, [
Expand All @@ -108,6 +115,7 @@ export function useAssistantLifecycle({
resolveOnboardingRedirect,
queryClient,
selectedPlatformAssistantId,
isOrgReady,
]);

// 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 { useIsOrgReady } from "@/hooks/use-is-org-ready";
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 isOrgReady = useIsOrgReady();
useClientFeatureFlagSync(hasPlatformSession && !isSessionInitializing && isOrgReady);
useAssistantLifecycle({
sessionStatus,
isRetired: false,
Expand Down