// Share a chat with a read-only link. The person who started the chat creates // one link; anyone holding it reads the conversation as it was at that moment // (their messages and the replies, never tool details or files), without // signing in. "Update link" refreshes what the same link shows; "Stop // sharing" ends it. The agent does the same through its in-chat actions. import type { OpenGeniClient, SessionShareLinkState } from "@opengeni/sdk"; import { Loader2Icon } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { apiErrorFacts } from "../../../ui/api-error"; import { Button } from "../../../ui/button"; import { CopyField } from "../../../ui/copy-field"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../../../ui/dialog"; import { ErrorMessage } from "../../../ui/error-message"; import { RelativeTime } from "../../../ui/relative-time"; import { Skeleton } from "../../../ui/skeleton"; type ShareChatClient = Pick< OpenGeniClient, "getSessionShareLink" | "createSessionShareLink" | "revokeSessionShareLink" >; type Busy = "create" | "update" | "stop" | null; type Load = | { kind: "loading" } | { kind: "failed"; error: unknown } | { kind: "ready"; state: SessionShareLinkState }; /** The server's own sentence when it has one (refusals are written for people), else ours. */ function errorTitle(error: unknown, fallback: string): string { const facts = apiErrorFacts(error); return facts.status !== undefined && facts.status < 500 && facts.serverMessage ? facts.serverMessage : fallback; } export function ShareChatDialog({ client, workspaceId, sessionId, open, onOpenChange, }: { client: ShareChatClient; workspaceId: string; sessionId: string; open: boolean; onOpenChange: (open: boolean) => void; }) { const [load, setLoad] = useState({ kind: "loading" }); const [busy, setBusy] = useState(null); const [actionError, setActionError] = useState<{ title: string; error: unknown } | null>(null); const [stopped, setStopped] = useState(false); const refresh = useCallback( async (signal?: AbortSignal) => { setLoad({ kind: "loading" }); try { const state = await client.getSessionShareLink(workspaceId, sessionId); if (!signal?.aborted) setLoad({ kind: "ready", state }); } catch (error) { if (!signal?.aborted) setLoad({ kind: "failed", error }); } }, [client, workspaceId, sessionId], ); useEffect(() => { if (!open) return; setActionError(null); setStopped(false); const controller = new AbortController(); void refresh(controller.signal); return () => controller.abort(); }, [open, refresh]); const run = async (kind: Exclude) => { setBusy(kind); setActionError(null); try { if (kind === "stop") { await client.revokeSessionShareLink(workspaceId, sessionId); setStopped(true); setLoad((current) => current.kind === "ready" ? { kind: "ready", state: { ...current.state, link: null } } : current, ); } else { const link = await client.createSessionShareLink(workspaceId, sessionId); setStopped(false); setLoad({ kind: "ready", state: { enabled: true, link } }); } } catch (error) { setActionError({ title: errorTitle( error, kind === "stop" ? "Couldn't stop sharing. The link still works." : kind === "update" ? "Couldn't update the link. It still shows the earlier conversation." : "Couldn't create a link. Nothing was shared.", ), error, }); } finally { setBusy(null); } }; const link = load.kind === "ready" ? load.state.link : null; const disabled = load.kind === "ready" && !load.state.enabled; const facts = actionError ? apiErrorFacts(actionError.error) : null; return ( { if (!busy) onOpenChange(next); }} > {link ? "This chat is shared" : "Share this chat"} {disabled ? "Share links are turned off on this server." : link ? "Anyone with this link can read the conversation up to when you shared it, without signing in. Tool details and files aren't included." : "Anyone with the link can read the conversation up to now, without signing in: the messages, with who sent them, and the replies. Tool details and files aren't included."} {load.kind === "loading" ? (
) : load.kind === "failed" ? ( void refresh()}> Try again } /> ) : link ? (

Shows the conversation as it was . New messages stay private until you update the link.

) : stopped ? (

Sharing stopped. The link doesn't open anymore.

) : null} {actionError ? ( ) : null} {link ? ( <> ) : ( <> {load.kind === "ready" && load.state.enabled ? ( ) : null} )}
); }