import { SITE_ID_HEADER, SITE_VERSION_HEADER, siteConversationContext, } from "@opengeni/contracts/site-session-http"; import type { Session, SessionEvent, SessionListResponse } from "@opengeni/sdk"; import { OpenGeniApiError } from "@opengeni/sdk/browser"; import { ArchiveIcon, ArrowLeftIcon, CopyIcon, CopyPlusIcon, InfoIcon, LinkIcon, Maximize2Icon, MoreHorizontalIcon, PanelLeftCloseIcon, PanelLeftIcon, PanelLeftOpenIcon, PanelRightCloseIcon, PanelRightIcon, PanelRightOpenIcon, PinIcon, PlusIcon, } from "lucide-react"; import { lazy, Suspense, useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode, type RefObject, } from "react"; import { SiteView } from "../../artifacts"; import { SiteVersionMenu } from "../components/artifacts/site-version-menu"; import { useArtifactsBackLink } from "../components/artifacts/artifact-page-chrome"; import { CopyArtifactLinkButton } from "../components/artifacts/copy-artifact-link"; import type { CreatedConversation, NewConversationCreateOptions, } from "../../hooks/use-new-conversation"; import { Button } from "../../ui/button"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger, } from "../../ui/context-menu"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "../../ui/dropdown-menu"; import { useSiteActions } from "../components/artifacts/site-actions"; import { useSiteSidebarActions } from "../lib/site-sidebar-bridge"; import { ResizeHandle } from "../../ui/resize-handle"; import { Skeleton } from "../../ui/skeleton"; import { useAppContext, type SessionCreationHandoff } from "../app-context"; import { ChatFileDropCover, ChatFileDropRelay, ChatFileDropRelayProvider, } from "../components/session/chat-viewport-file-drop-target"; import { ConsoleNewConversation } from "../components/session/console-new-conversation"; import { SitePublicationControl, useSitePublicationLink, } from "../components/artifacts/site-public-link"; import { useAppApi } from "../lib/app-api"; import { useConsoleSiteToolBridge, useConsoleSiteViewer } from "../lib/console-site"; import { Link, useNavigate } from "../lib/navigation"; import { hasWorkspacePermission } from "../lib/permissions"; import type { SiteViewArtifactsReturn } from "../lib/routes"; import { sessionDisplayTitle } from "../lib/session-rename"; import { sessionSiteOrigin } from "../lib/session-site-origin"; import { SessionUiFeaturesOff } from "../lib/session-ui-features"; import { SITE_CHAT_DEFAULT_WIDTH, SITE_CHAT_MAX_WIDTH, SITE_CHAT_MIN_WIDTH, SITE_MIN_WIDTH, useSiteChatPanelLayout, type SiteChatPanelLayout, } from "../lib/site-chat-panel"; import { useRecordSiteOpened } from "../lib/site-opened"; import { siteMayHaveChanged, useSiteLiveVersion } from "../lib/site-live-version"; import { useSessionListHintRefresh } from "../lib/use-session-list-hint-refresh"; import { cn } from "../lib/utils"; import { useWindowBarTitle } from "../../ui/window-bar-title"; // Loaded when someone opens it, from the Artifacts route that already holds // the publish form and the Site's management (a separate lazy entry would // regroup the shared code-split chunks). const SitePublishingPage = lazy(() => import("./artifacts").then((module) => ({ default: module.SitePublishingPage, })), ); // The regular chat route, loaded as the chat page loads it: a static import // would make the Site view share the chat's chunks and regroup them. const loadSessionRoute = () => import("./session"); const SessionRoute = lazy(() => loadSessionRoute().then((module) => ({ default: module.SessionRoute })), ); const SITE_ROUTE = "/workspaces/$workspaceId/sites/$siteId"; /** The panel's width while it sits beside the Site, set live while dragging. */ const WIDTH_VARIABLE = "--og-site-chat-width"; type SiteState = | { status: "loading" } | { status: "error" } | { status: "ready"; title: string; versionId: string | null; revision: number | null; archived: boolean; }; /** A conversation this panel just created, for its first open. */ type SiteHandoff = CreatedConversation & { creation: SessionCreationHandoff | null }; /** * A Site with its chat beside it. The Site fills the page; the panel shows * one conversation (the newest when the Site opens, unless one is chosen) as * the regular chat (the same chat as on its own page, without the session * panel), narrower, and starts a new one with the regular composer. The * sidebar lists the Site's conversations, so the panel doesn't repeat them. * The panel can be resized, hidden and moved to the left; all three are * remembered per person, and choosing a conversation brings a hidden panel * back. Files dragged over the Site go to the chat, since the Site's frame * would otherwise take them. * * Edits go live: the Site is watched for new versions (its conversation's * tool calls, and a check every few seconds for anyone else's), the preview * reloads in place when one is published or restored, and the version chip * in its header offers the history and one-click Undo. * * Its header says whether the Site is on the web: "Published" opens or * copies the address, or Publish for people who can. Publishing and its * settings open the Site's own publish form in place of the Site, with the * chat still beside it. */ export function SiteViewRoute({ workspaceId, siteId, conversation, startNew = false, page, artifactsReturn, }: { workspaceId: string; siteId: string; /** The conversation open in the panel; the newest when omitted. */ conversation?: string | undefined; /** Open with a new conversation in the panel, its composer focused (Edit with Opengeni). */ startNew?: boolean | undefined; /** The Site's own address (`?page=/admin`), answered to its `site.location`; its root when omitted. */ page?: string | undefined; /** * The Artifacts page the Site was opened from: the Site's toolbar starts * with a Back to it, and the Site's own moves keep it. */ artifactsReturn?: SiteViewArtifactsReturn | undefined; }) { const { client, accessContext } = useAppContext(); const navigate = useNavigate(); const viewer = useConsoleSiteViewer(); // By value: a host may pass a new object on every render. const backKey = artifactsReturn?.browse ? JSON.stringify(artifactsReturn) : ""; const back = useMemo( () => (backKey ? (JSON.parse(backKey) as SiteViewArtifactsReturn) : undefined), [backKey], ); // The Site's address lives in this page's URL, so Back/Forward, reloads // and copied links keep the Site where it was. const onSiteLocationChange = useCallback( (href: string, options: { replace: boolean }) => void navigate({ to: SITE_ROUTE, params: { workspaceId, siteId }, search: { ...back, ...(conversation ? { conversation } : {}), ...(href === "/" ? {} : { page: href }), }, ...(options.replace ? { replace: true } : {}), }), [back, conversation, navigate, siteId, workspaceId], ); // Anyone who can open the Site sees whether it is published and its // address; Publish and the publish form are for people who can publish. const canPublish = hasWorkspacePermission(accessContext, workspaceId, "artifacts:publish"); const publication = useSitePublicationLink({ client, workspaceId, siteId }); const toolBridge = useConsoleSiteToolBridge(workspaceId); useRecordSiteOpened(client, workspaceId, siteId); const live = useSiteLiveVersion(client, workspaceId, siteId); const artifact = live.detail?.artifact; // The publish form, open for this Site; another Site starts without it. const siteKey = `${workspaceId}/${siteId}`; const [publishingFor, setPublishingFor] = useState(null); const publishingSite = publishingFor === siteKey && artifact?.status === "active" ? artifact : null; const publishing = publishingSite !== null; // In the macOS app the Site's name is the window bar's title, as a page's // is, instead of in its toolbar (which shows it again in full screen, where // the bar's row is hidden). The publish form, opened in front of the Site, // names the bar once it has loaded; the Site's claim mounted first, so it // yields while the form is open. const titleInWindowBar = useWindowBarTitle(artifact?.title ?? null, publishing, { copyable: true, }); // Full screen is the route's so opening the form can leave it first. const [fullScreen, setFullScreen] = useState(false); const openPublishing = useCallback(() => { setFullScreen(false); setPublishingFor(siteKey); }, [siteKey]); // Back from the form returns focus to the control that opened it. const siteColumn = useRef(null); const returnFocus = useRef(false); const closePublishing = useCallback(() => { returnFocus.current = true; setPublishingFor(null); }, []); useEffect(() => { if (publishing || !returnFocus.current) return; returnFocus.current = false; siteColumn.current?.querySelector("[data-site-publication]")?.focus(); }, [publishing]); const site = useMemo( () => artifact ? { status: "ready", title: artifact.title, versionId: artifact.currentVersion?.id ?? null, revision: artifact.currentVersion?.revision ?? null, archived: artifact.status === "archived", } : live.failed ? { status: "error" } : { status: "loading" }, [artifact, live.failed], ); const { check } = live; const backControl = back ? ( ) : undefined; const { layout, update: updateLayout } = useSiteChatPanelLayout(accessContext.subjectId); const [relay] = useState(() => new ChatFileDropRelay()); return (
{/* The Site's column, not its chat: the Site's toolbar is this page's top row. */}
{publishingSite ? ( }> ) : null} {artifact?.status === "archived" ? ( <> {backControl ? (
{backControl}
) : null}

This Site is archived.{" "} {canPublish ? "Restore it from Site details" : "View Site details"}

) : ( // Hidden, not unmounted, behind the form: the Site keeps its page.
( {title} ) : undefined } // The one publication control. The Site view never says "Live": // it stays current, and says so only while Updating or Reconnecting. liveStatus={ <> {artifact ? ( ) : null} } versionId={artifact ? (artifact.currentVersion?.id ?? null) : undefined} watch={live.watch} versionControl={ live.detail ? ( ) : undefined } fullScreen={fullScreen} onFullScreenChange={setFullScreen} archivedMessage={ canPublish ? "This Site is archived. Restore it from Site details." : "This Site is archived." } />
)}
{ // A publish or restore from this conversation shows at once. if (siteMayHaveChanged(event)) check(); }} />
); } /** Right-click the Site's title: copy it or the Site's address. */ function SiteTitleMenu({ workspaceId, origin, children, }: { workspaceId: string; origin: { siteId: string; title: string }; children: ReactNode; }) { const site = useSiteActions(workspaceId, origin); return ( {children} Copy title Copy link ); } /** * The Site's "…", as a chat's header has one: Copy title, the sidebar's Pin * and Archive (while the sidebar lists Sites), Duplicate and Site details. * Copy link and the published address keep their own controls beside it. */ function SiteActionsMenu({ workspaceId, origin, hasVersion, }: { workspaceId: string; origin: { siteId: string; title: string }; hasVersion: boolean; }) { const site = useSiteActions(workspaceId, origin, { hasVersion }); const sidebar = useSiteSidebarActions(workspaceId, origin); return ( {sidebar ? ( ) : null} {site.duplicate ? ( ) : null} {sidebar ? ( <> ) : null} ); } const PAGE_SIZE = 30; /** * "← Artifacts" at the start of the Site's toolbar, when the Site was opened * from the Artifacts page: back to that page with its tab, search and sort * (and its scroll, which the page keeps). A real link, so it also opens in a * new tab. */ function ArtifactsBackLink({ workspaceId, artifactsReturn, }: { workspaceId: string; artifactsReturn: SiteViewArtifactsReturn; }) { const back = useArtifactsBackLink(workspaceId, artifactsReturn.fromSession, artifactsReturn); return ( ); } /** * The Site's newest conversations for this viewer, newest activity first. * The panel needs the newest to open by default and the titles to name the * one open; the sidebar is where all of them are listed. */ function useSiteConversations(workspaceId: string, siteId: string) { const { client } = useAppContext(); const [page, setPage] = useState(null); const [error, setError] = useState(null); const request = useRef(null); const refresh = useCallback(async () => { request.current?.abort(); const abort = new AbortController(); request.current = abort; try { const next = await client.listSessionPage(workspaceId, { originSiteId: siteId, parentSessionId: null, sortBy: "updatedAt", limit: PAGE_SIZE, signal: abort.signal, }); if (abort.signal.aborted) return; setPage(next); setError(null); } catch (failure) { if (!abort.signal.aborted) setError(failure instanceof Error ? failure : new Error(String(failure))); } }, [client, siteId, workspaceId]); useEffect(() => { void refresh(); return () => request.current?.abort(); }, [refresh]); // Only Site conversations matter here, which ordinary hints never name. useSessionListHintRefresh(refresh, error, { siteConversations: true }); const sessions = useMemo( () => page ? [...new Map([...page.pinned, ...page.sessions].map((session) => [session.id, session]))] .map(([, session]) => session) .filter((session) => !session.archived) .sort((a, b) => Date.parse(b.updatedAt) - Date.parse(a.updatedAt)) : [], [page], ); return { sessions, loaded: page !== null, error, refresh }; } /** * How wide the panel may be: its own limit, less what the Site needs beside * it. Measured through the panel's own element, since a parent's ref isn't * attached yet when a child's layout effect first runs. */ function useAvailableWidth(panel: RefObject) { const [max, setMax] = useState(SITE_CHAT_MAX_WIDTH); useLayoutEffect(() => { const element = panel.current?.parentElement; if (!element) return; const measure = () => { const { width } = element.getBoundingClientRect(); // An unmeasured frame (no layout yet) keeps the panel's own limit; a // narrow one still lets the panel be its minimum. setMax( width === 0 ? SITE_CHAT_MAX_WIDTH : Math.max( SITE_CHAT_MIN_WIDTH, Math.min(SITE_CHAT_MAX_WIDTH, Math.floor(width - SITE_MIN_WIDTH)), ), ); }; measure(); if (typeof ResizeObserver === "undefined") return; const observer = new ResizeObserver(measure); observer.observe(element); return () => observer.disconnect(); }, [panel]); return max; } function SiteChatPanel({ workspaceId, siteId, site, retrySite, canPublish, conversation, startNew: startNewRequested, page, artifactsReturn, layout, updateLayout, onEvent, }: { workspaceId: string; siteId: string; site: SiteState; retrySite: () => void; /** Whether this person may publish the Site, and so edit it from here. */ canPublish: boolean; conversation?: string | undefined; startNew: boolean; /** The Site's own address, kept when another conversation is chosen. */ page?: string | undefined; /** The Artifacts page the Site was opened from, kept when another conversation is chosen. */ artifactsReturn?: SiteViewArtifactsReturn | undefined; /** Width, hidden and side, remembered per person; shared with the Site beside it. */ layout: SiteChatPanelLayout; updateLayout: (patch: Partial) => void; /** Each new event in the open conversation, so the Site can follow its edits. */ onEvent: (event: SessionEvent) => void; }) { const context = useAppContext(); const api = useAppApi(); const navigate = useNavigate(); const panelId = useId(); const panel = useRef(null); const hideButtonId = `${panelId}-hide`; const showButtonId = `${panelId}-show`; const list = useSiteConversations(workspaceId, siteId); const left = layout.position === "left"; const maxWidth = useAvailableWidth(panel); const width = Math.min(layout.width, maxWidth); const [composing, setComposing] = useState(false); const [handoff, setHandoff] = useState(null); const [focusRequest, setFocusRequest] = useState(0); // A conversation opened by link that isn't among the newest is named once read. const [linked, setLinked] = useState(null); // The conversation open without a choice is fixed when the Site opens, so // activity elsewhere never swaps it out from under the person reading it. const [defaultId, setDefaultId] = useState(undefined); const newest = list.sessions[0]?.id ?? null; const pinned = defaultId === null || (defaultId !== undefined && list.sessions.some((session) => session.id === defaultId)) ? defaultId : newest; useEffect(() => { if (list.loaded && pinned !== defaultId) setDefaultId(pinned); }, [defaultId, list.loaded, pinned]); // Choosing a conversation anywhere (the rail, a link) ends composing and // shows a hidden panel: the sidebar is where conversations are chosen, so // choosing one must show it. Opening the page keeps the panel as it was. // Returning to the Site itself, as its rail row does, opens its newest. const previousConversation = useRef(conversation); const startingNew = useRef(false); useEffect(() => { const previous = previousConversation.current; previousConversation.current = conversation; if (conversation) { setComposing(false); if (conversation !== previous) updateLayout({ collapsed: false }); } else if (previous && !startingNew.current) setDefaultId(undefined); startingNew.current = false; }, [conversation, updateLayout]); const toSite = useCallback( (search: { conversation?: string }, replace = false) => void navigate({ to: SITE_ROUTE, params: { workspaceId, siteId }, // Choosing a conversation keeps the Site where it is. search: { ...artifactsReturn, ...search, ...(page ? { page } : {}) }, ...(replace ? { replace: true } : {}), }), [artifactsReturn, navigate, page, siteId, workspaceId], ); // A link may name a conversation that isn't this Site's, or one since // archived or deleted; fall back to the Site's own. const listed = !conversation || list.sessions.some((session) => session.id === conversation); // Through a ref: a host's navigate may change identity on every render. const fallBack = useRef(() => {}); useEffect(() => { fallBack.current = () => toSite({}, true); }, [toSite]); useEffect(() => { if (!conversation || !list.loaded || listed) return; const abort = new AbortController(); context.client.getSession(workspaceId, conversation, { signal: abort.signal }).then( (session) => { if (abort.signal.aborted) return; if ( sessionSiteOrigin(session)?.siteId !== siteId || session.parentSessionId !== null || session.archived ) fallBack.current(); else setLinked(session); }, (failure: unknown) => { if ( !abort.signal.aborted && failure instanceof OpenGeniApiError && [403, 404].includes(failure.status) ) fallBack.current(); }, ); return () => abort.abort(); }, [context.client, conversation, list.loaded, listed, siteId, workspaceId]); // Load the chat route with the panel, so a chat created here opens (and // takes focus below) without waiting for it. A failed load shows when the // route renders. useEffect(() => { void loadSessionRoute().catch(() => {}); }, []); // Move focus to the composer New opened, or to the new conversation's. useEffect(() => { if (focusRequest === 0) return; let animation = 0; let tries = 0; const attempt = () => { const input = panel.current?.querySelector("textarea:not([disabled])"); if (input) input.focus(); else if ((tries += 1) < 60) animation = requestAnimationFrame(attempt); }; animation = requestAnimationFrame(attempt); return () => cancelAnimationFrame(animation); }, [focusRequest]); // Hiding and showing keep focus on the control that brings the panel back. const [focusToggle, setFocusToggle] = useState<"show" | "hide" | null>(null); useEffect(() => { if (!focusToggle) return; document.getElementById(focusToggle === "show" ? showButtonId : hideButtonId)?.focus(); setFocusToggle(null); }, [focusToggle, hideButtonId, showButtonId]); const hide = () => { updateLayout({ collapsed: true }); setFocusToggle("show"); }; const show = () => { updateLayout({ collapsed: false }); setFocusToggle("hide"); }; const ready = site.status === "ready" ? site : null; const canCreate = hasWorkspacePermission(context.accessContext, workspaceId, "sessions:create") && ready !== null && ready.versionId !== null && !ready.archived; // The composer mounts once the Site has loaded; focus it then too. useEffect(() => { if (composing && canCreate) setFocusRequest((request) => request + 1); }, [canCreate, composing]); const selectedId = composing ? null : (conversation ?? pinned); const showComposer = composing || (list.loaded && selectedId === null); const startNew = () => { setComposing(true); setFocusRequest((request) => request + 1); // Nothing is selected while composing, so the rail marks the Site. if (conversation) { startingNew.current = true; toSite({}); } }; // Edit with Opengeni opens the Site this way: a new conversation, shown // even if the panel was hidden, its composer focused once it can be used. // The address drops the request so going back or reloading doesn't repeat it. // Once per request, also when the Site is already open: the host's // navigate (and so `toSite`) may change identity on every render. const startNewHandled = useRef(false); useEffect(() => { if (!startNewRequested) { startNewHandled.current = false; return; } if (startNewHandled.current) return; startNewHandled.current = true; setComposing(true); updateLayout({ collapsed: false }); if (conversation) startingNew.current = true; toSite({}, true); }, [conversation, startNewRequested, toSite, updateLayout]); // Created like the Site's own chats: private to this person and listed // under the Site. The API refuses anything else a Site may not set; files // are the person's own, attached here in the console as in any chat. const versionId = ready?.versionId ?? null; // A retry of the same create keeps its first message's identity, so the // chat shows that message at once and recognizes it when it arrives. const clientEventIds = useRef(new Map()); const createdSessions = useRef(new Map()); const createSession = useCallback( async ( initialMessage: string, idempotencyKey: string, options: NewConversationCreateOptions, ): Promise => { if (!versionId) throw new Error("This Site has no published version yet."); const voice = options.startMode === "realtime"; let clientEventId = clientEventIds.current.get(idempotencyKey); if (!voice && !clientEventId) { clientEventId = crypto.randomUUID(); clientEventIds.current.set(idempotencyKey, clientEventId); } const created = await api.request( `/v1/workspaces/${encodeURIComponent(workspaceId)}/sessions`, { method: "POST", headers: { [SITE_ID_HEADER]: siteId, [SITE_VERSION_HEADER]: versionId }, body: JSON.stringify({ ...(voice ? { startMode: "realtime" } : { initialMessage, clientEventId }), idempotencyKey, ...(options.resources?.length ? { resources: options.resources } : {}), ...(options.model ? { model: options.model } : {}), ...(options.reasoningEffort ? { reasoningEffort: options.reasoningEffort } : {}), ...(options.latencyMode ? { latencyMode: options.latencyMode } : {}), }), }, ); if (clientEventId) createdSessions.current.set(created.id, { session: created, clientEventId }); return created.id; }, [api, siteId, versionId, workspaceId], ); const handedOff = handoff !== null && handoff.sessionId === selectedId ? handoff : null; // For someone who may publish the Site, every message says which Site and // version "the Site" is; the API names it on the first one when it creates // the conversation. Others get no editing context, as from the API. const siteContext = useRef(null); siteContext.current = canPublish && ready?.versionId && ready.revision !== null ? siteConversationContext({ siteId, title: ready.title, versionId: ready.versionId, revision: ready.revision, }) : null; const messageContext = useCallback(() => siteContext.current ?? undefined, []); // The handoff is for the conversation's first open only. useEffect(() => { if (handoff && selectedId !== null && selectedId !== handoff.sessionId) setHandoff(null); }, [handoff, selectedId]); // Focus follows a new conversation once it, not the composer, is mounted. const handoffId = handoff?.sessionId ?? null; useEffect(() => { if (handoffId !== null && selectedId === handoffId) setFocusRequest((request) => request + 1); }, [handoffId, selectedId]); const selected = list.sessions.find((session) => session.id === selectedId) ?? (linked?.id === selectedId ? linked : null); // While composing, the composer's own headline names what to do. const title = showComposer ? null : selected ? sessionDisplayTitle(selected) : handedOff?.initialMessage ? handedOff.initialMessage : "Conversation"; return ( <> {layout.collapsed ? (
) : null} ); } function PanelLoading({ label }: { label: string }) { return (
{label}
); } function Retry({ message, onRetry }: { message: string; onRetry: () => void }) { return (

{message}{" "}

); }