diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac8a8b4875..ba70769fa3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -368,7 +368,8 @@ jobs: # sign-in through Composio held on OpenBot's own step for its countdown before it # leaves, in this tab and in the new one a Bot's card opens, a picture over 8MB fetched # only when it is opened, counted on the wire, and the AI connection changed in Settings, a - # key the provider refuses saying why and changing nothing. The + # key the provider refuses saying why and changing nothing, and on OpenBot Cloud a person + # held at Connect your AI until they connect their own. The # onboarding frame reads the generated app config, which `bun test` alone doesn't write (the # test script's pretest does). - run: bun run generate:app-config @@ -378,7 +379,7 @@ jobs: --volume "$(command -v bun):/usr/local/bin/bun:ro" \ --env OPENBOT_APP_BROWSER=1 \ "$PLAYWRIGHT_IMAGE" \ - bun test --parallel app/tests/tool-line-reduced-motion.test.tsx app/tests/onboarding-window-buttons.test.tsx app/tests/onboarding-orb-room.test.tsx app/tests/transcript-rail-clearance.test.tsx app/tests/a2ui-frame.test.tsx app/tests/update-banner-layout.test.tsx app/tests/composer-dictation-notice-layout.test.tsx app/tests/conversation-intro-halo.test.tsx app/tests/transcript-room.test.tsx app/tests/skins-window-buttons.test.tsx app/tests/opendots-rail-badge.test.tsx app/tests/account-version-layout.test.tsx app/tests/organization-menu-names.test.tsx app/tests/bot-orb-one-color.test.tsx app/tests/compact-avatar-layout.test.tsx app/tests/theme-follows-system.test.tsx app/tests/desktop-window-theme.test.tsx app/tests/bot-screen-record-layout.test.tsx app/tests/teams-screens-layout.test.tsx app/tests/team-grants-screens-layout.test.tsx app/tests/needs-you-board-layout.test.tsx app/tests/checkbox-contrast.test.tsx app/tests/maths-rendering.test.tsx app/tests/task-report-chip.test.tsx app/tests/consent-handoff.test.tsx app/tests/large-picture-tile.test.tsx app/tests/audit-computer-limit-layout.test.tsx app/tests/ai-connection-dialog.test.tsx + bun test --parallel app/tests/tool-line-reduced-motion.test.tsx app/tests/onboarding-window-buttons.test.tsx app/tests/onboarding-orb-room.test.tsx app/tests/transcript-rail-clearance.test.tsx app/tests/a2ui-frame.test.tsx app/tests/update-banner-layout.test.tsx app/tests/composer-dictation-notice-layout.test.tsx app/tests/conversation-intro-halo.test.tsx app/tests/transcript-room.test.tsx app/tests/skins-window-buttons.test.tsx app/tests/opendots-rail-badge.test.tsx app/tests/account-version-layout.test.tsx app/tests/organization-menu-names.test.tsx app/tests/bot-orb-one-color.test.tsx app/tests/compact-avatar-layout.test.tsx app/tests/theme-follows-system.test.tsx app/tests/desktop-window-theme.test.tsx app/tests/bot-screen-record-layout.test.tsx app/tests/teams-screens-layout.test.tsx app/tests/team-grants-screens-layout.test.tsx app/tests/needs-you-board-layout.test.tsx app/tests/checkbox-contrast.test.tsx app/tests/maths-rendering.test.tsx app/tests/task-report-chip.test.tsx app/tests/consent-handoff.test.tsx app/tests/large-picture-tile.test.tsx app/tests/audit-computer-limit-layout.test.tsx app/tests/ai-connection-dialog.test.tsx app/tests/connect-your-ai.test.tsx # Dictation in the Desktop window on a Mac whose recognizer has no English (United Kingdom) on # the device, in Chromium and in WebKit, the engine the Mac window draws with: it listens in the diff --git a/app/src/components/channels/add-model-key-link.tsx b/app/src/components/channels/add-model-key-link.tsx index 1f60bf3e6e..df6966ec77 100644 --- a/app/src/components/channels/add-model-key-link.tsx +++ b/app/src/components/channels/add-model-key-link.tsx @@ -13,6 +13,16 @@ import { queryClient } from "@/query-client"; export const NO_AI_CONNECTION = "This organization doesn't have an AI connection yet. An admin can add a model key in Admin, under Vault."; +/** + * What the server says on OpenBot Cloud when a run has no AI, word for word: the person has none of + * their own and their organization has none for everyone. Anybody can fix it, by connecting theirs. + */ +export const NO_PERSONAL_AI_CONNECTION = + "Connect your AI to use Bots. An admin can also set one up for everyone in your organization."; + +/** Where a person connects their own AI: onboarding's Connect your AI step. */ +export const CONNECT_YOUR_AI_PATH = "/onboarding?step=ai"; + /** Where an admin adds the key: a new credential in Admin, under Vault. */ export const ADD_MODEL_KEY_PATH = "/admin/credentials/new"; @@ -20,12 +30,25 @@ export const ADD_MODEL_KEY_PATH = "/admin/credentials/new"; * Add a model key, beside the sentence that says there is none: where the admin already is when * they find out. Only for an admin of the organization; a member reads the sentence, which tells * them who to ask. Named query client, as everything drawn inside a conversation. + * + * On OpenBot Cloud the sentence is the person's own to fix, so everybody gets Connect your AI, + * which opens the step onboarding has for it. A whole page load, so the step reads the person's AI + * connection afresh. */ export function AddModelKeyLink({ reason }: { reason: string }) { const me = useQuery( { ...currentUserQueryOptions(), enabled: reason === NO_AI_CONNECTION }, queryClient, ).data; + if (reason === NO_PERSONAL_AI_CONNECTION) + return ( + + Connect your AI + + ); if (reason !== NO_AI_CONNECTION || !isOrganizationAdmin(me?.role)) return null; return ( diff --git a/app/src/components/onboarding/ai-step.tsx b/app/src/components/onboarding/ai-step.tsx new file mode 100644 index 0000000000..2a29a00ede --- /dev/null +++ b/app/src/components/onboarding/ai-step.tsx @@ -0,0 +1,211 @@ +import { IconBuilding, IconChevronRight } from "@tabler/icons-react"; +import { useQuery } from "@tanstack/react-query"; +import { useId, useState } from "react"; +import { useSignOut } from "@/components/app-sidebar/use-sign-out"; +import { useSwitchOrganization } from "@/components/organizations/use-switch-organization"; +import { + Item, + ItemActions, + ItemContent, + ItemMedia, + ItemTitle, +} from "@/components/ui/item"; +import { currentUserQueryOptions } from "@/lib/auth/queries"; +import { useOrganizationChoices } from "@/lib/organizations/queries"; +import { + ChooseConnection, + ChooserCard, + ConnectedRow, +} from "@/components/settings/ai-connection-chooser"; +import { Button } from "@/components/ui/button"; +import { + type PersonalAiConnection, + personalAiConnectionQueryOptions, +} from "@/lib/ai-connection/queries"; +import { StepIntro } from "./onboarding-frame"; +import type { OnboardingStep } from "./onboarding-wizard"; + +/** + * Onboarding's Connect your AI step, on an OpenBot that provides no AI of its own (OpenBot Cloud): + * the person connects their own before they use OpenBot, with a key or an address, or by signing in + * to a provider the server can sign them in to. Theirs alone: it follows them into every + * organization they are in and is never shared with any of them. + * + * ONLY WHERE IT IS NEEDED, AND NO WAY PAST IT. The route puts the step in the list only when the + * server says one is required and this person's runs have none (`needsAiConnection`); somebody whose + * organization already has an AI never sees it, and adds their own later in Settings. Until the + * server says a connection is in use, the step's button is disabled and there is no Not now. + * + * THE CHOOSER IS SETTINGS' OWN, ON THE PAGE. The same provider list, ways in and checks as + * Settings' AI connection dialog (ai-connection-chooser.tsx), in its personal mode, drawn in a card + * where the other steps draw their picture: beside the words on a desktop, under them on a phone. + */ +export function useAiStep(): OnboardingStep { + const personal = useQuery(personalAiConnectionQueryOptions()); + const connected = personal.data ? personal.data.uses !== "none" : false; + return { + id: "ai", + title: "Connect your AI", + /* + * The way out sits with the words on a desktop, beside the chooser, and under the chooser on a + * phone, where the column reads top to bottom and connecting is what the step is for. Drawn in + * one place or the other, never both: the hidden one is `display: none`, out of reach. + */ + intro: () => ( + + + + } + > +

+ Bots need an AI model to work. Connect your own by signing in or with + an API key. It's personal to you and never shared with your + organization. +

+

You can change it anytime in Settings.

+
+ ), + demo: () => + personal.data ? ( +
+ + {/* + * Room below it for the fade above the phone's pinned buttons (`before:h-8` in the frame's + * footer) and the frame's 16px gutter past it, so scrolled to the end, Sign out reads + * clear of it. + */} +
+ +
+
+ ) : null, + primaryDisabled: !connected, + }; +} + +/** + * THE WAY OUT THAT ISN'T A SKIP. Somebody in more than one organization may be here because the + * one they are working in has no AI, while another of theirs has one for everyone: they can work + * there instead, by the same switch as the account menu's. And anybody can sign out. Both sit with + * the words, under the sentence, as Welcome's one setting does. + */ +function WayOut() { + const headingId = useId(); + const choices = useOrganizationChoices(); + const switching = useSwitchOrganization(); + const signOut = useSignOut(); + const email = useQuery(currentUserQueryOptions()).data?.email; + const others = + choices?.organizations.filter((one) => one.id !== choices.currentId) ?? []; + return ( +
+ {others.length > 0 ? ( + <> +
+

+ Or work in another organization +

+

+ If one of them has an AI for everyone, Bots can use it there. +

+
+
+ {others.map((organization) => ( + switching.run(organization.id)} + type="button" + /> + } + size="sm" + variant="outline" + > + + + + + + {organization.name} + + + + + + + ))} +
+ + ) : null} +
+ + {email ? `Signed in as ${email}` : null} + + +
+
+ ); +} + +/** The chooser, or once connected, what is connected and a way to change it. */ +function AiChooserPane({ personal }: { personal: PersonalAiConnection }) { + const [changing, setChanging] = useState(false); + const connected = personal.uses !== "none"; + return ( +
+ + {connected && !changing ? ( +
+ {personal.uses === "personal" ? ( + + ) : ( +

+ Bots use the AI your organization set up. +

+ )} +

+ Bots use it when they work for you. +

+
+ +
+
+ ) : ( + setChanging(false) : undefined} + onConnected={() => setChanging(false)} + /> + )} +
+
+ ); +} diff --git a/app/src/components/organizations/organization-boundary.tsx b/app/src/components/organizations/organization-boundary.tsx index 7f13232b6b..124eed7856 100644 --- a/app/src/components/organizations/organization-boundary.tsx +++ b/app/src/components/organizations/organization-boundary.tsx @@ -1,6 +1,9 @@ -import { useNavigate } from "@tanstack/react-router"; +import { useNavigate, useRouter } from "@tanstack/react-router"; import { Fragment, type ReactNode, useEffect } from "react"; -import { useCurrentOrganizationId } from "@/lib/organizations/current"; +import { + onOrganizationChange, + useCurrentOrganizationId, +} from "@/lib/organizations/current"; import { onSwitchedElsewhere } from "@/lib/organizations/queries"; /** @@ -11,9 +14,16 @@ import { onSwitchedElsewhere } from "@/lib/organizations/queries"; * conversation, a draft, a list it had drawn) is carried from one organization into another. * * It also says how a switch made in another window is followed here: from home. + * + * AND THE GATES DECIDE AGAIN, IN THE NEW ONE. A switch takes the person home while the app still + * works in the organization they are leaving, so the signed-in gate (`_authed`) and onboarding's + * decided on that one's answers. Once the app has changed over (and dropped what it held for the + * old one, query-client.ts), the routes on screen load again, and their gates ask the new one: + * somebody who has to connect their own AI there is sent to do it at once, not on their next click. */ export function OrganizationBoundary({ children }: { children: ReactNode }) { const navigate = useNavigate(); + const router = useRouter(); // A switch made in another window is followed from home, as a switch made here is. useEffect( () => @@ -22,5 +32,13 @@ export function OrganizationBoundary({ children }: { children: ReactNode }) { }), [navigate], ); + useEffect( + () => + onOrganizationChange((next, previous) => { + if (next === undefined || previous === undefined) return; + void router.invalidate(); + }), + [router], + ); return {children}; } diff --git a/app/src/components/settings/ai-connection-chooser.tsx b/app/src/components/settings/ai-connection-chooser.tsx new file mode 100644 index 0000000000..1f6cfde398 --- /dev/null +++ b/app/src/components/settings/ai-connection-chooser.tsx @@ -0,0 +1,1014 @@ +import { + IconChevronRight, + IconCircleCheckFilled, + IconCloud, + IconExternalLink, + IconSparkles, +} from "@tabler/icons-react"; +import { useMutation } from "@tanstack/react-query"; +import { + createContext, + type ReactNode, + useContext, + useEffect, + useRef, + useState, +} from "react"; +import { HarnessMark } from "@/components/agents/harness-mark"; +import { Button } from "@/components/ui/button"; +import { Checkbox } from "@/components/ui/checkbox"; +import { + DialogBody, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { + Field, + FieldDescription, + FieldGroup, + FieldLabel, +} from "@/components/ui/field"; +import { Input } from "@/components/ui/input"; +import { + Item, + ItemActions, + ItemContent, + ItemDescription, + ItemGroup, + ItemMedia, + ItemTitle, +} from "@/components/ui/item"; +import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { + type AiConnectionOwner, + describeConnection, +} from "@/lib/ai-connection/describe"; +import { + beginPlanSignIn, + beginProviderSignIn, + cancelPlanSignIn, + cancelProviderSignIn, + finishPlanSignIn, + finishProviderSignIn, + type Plan, +} from "@/lib/ai-connection/desktop"; +import { + type ConnectPhase, + type Connected as ConnectedResult, + connectAiMutationOptions, + connectPersonalAiMutationOptions, + readPersonalSignIn, + startPersonalSignIn, +} from "@/lib/ai-connection/mutations"; +import { + AI_PROVIDERS, + type AiProvider, + ENDPOINT_PRESETS, + hostOf, + isEndpointAddress, + loginsHere, + personalLogins, + providerById, +} from "@/lib/ai-connection/providers"; +import type { + AiConnection, + AiConnectionInput, + AiConnectionLogin, + AiConnectionProvider, +} from "@/lib/api/generated/model"; +import { markFor } from "@/lib/marks"; +import { queryClient } from "@/query-client"; + +/** + * Choosing an AI connection: the provider list, the way in (a key, an address, or a sign-in), and + * Connect, which the server checks with the provider before it saves anything. The app's port of + * OpenBot Desktop's setup window (desktop/src/ProviderPicker.tsx), shared by every place a person + * connects an AI. + * + * TWO KINDS OF CONNECTION. `organization` is the AI every Bot in the organization runs on, which an + * admin changes in Settings; inside OpenBot Desktop it may be a plan or a provider sign-in Desktop + * holds. `personal` is a person's own AI on OpenBot Cloud: a key or an address for every provider, + * and a provider's sign-in only where the server can run it itself (`signIns`). Never a plan or a + * Desktop-held sign-in there, even inside Desktop: the server runs the person's Bots. + * + * TWO SURFACES. In a dialog (Settings) it draws the dialog's header, body and footer. On a page + * (onboarding) it draws the same body and buttons inside a card, under the page's own heading. + */ +export type ChooserMode = + | { + kind: "organization"; + inDesktop: boolean; + /** + * Beside each person's own AI (OpenBot Cloud): this is the organization's, for everyone in + * it who hasn't connected their own, and the chooser says so. + */ + organizationWide?: boolean; + } + | { kind: "personal"; signIns: readonly AiConnectionProvider[] }; + +export type ChooserSurface = "dialog" | "card"; + +const SurfaceContext = createContext("dialog"); + +/** The dialog's header, or nothing on a card: the page's own heading says it there. */ +function ChooserHeader({ + title, + description, +}: { + title: string; + description: string; +}) { + if (useContext(SurfaceContext) === "card") return null; + return ( + + {title} + {description} + + ); +} + +function ChooserBody({ + className, + children, +}: { + className: string; + children: ReactNode; +}) { + if (useContext(SurfaceContext) === "card") + return
{children}
; + return ( + + {children} + + ); +} + +function ChooserFooter({ children }: { children: ReactNode }) { + if (useContext(SurfaceContext) === "card") + return ( +
+ {children} +
+ ); + return {children}; +} + +/** + * A card on a page that holds the chooser: the booking calendar's shape (a real control, so the + * card's fill, never the illustrations' muted panel), with the dialog's padding. + */ +export function ChooserCard({ children }: { children: ReactNode }) { + return ( + +
+ {children} +
+
+ ); +} + +/** A provider's mark on the row's tile, or a cloud for the row that stands for every provider. */ +export function ProviderTile({ + provider, +}: { + provider: AiProvider | undefined; +}) { + return ( + + {provider?.mark && markFor(provider.mark) ? ( + + ) : provider ? ( + + ) : ( + + )} + + ); +} + +export function Details({ + details, +}: { + details: { label: string; value: string }[]; +}) { + return ( + + {details.map((detail) => ( + + + + {detail.label} + + + + {detail.value} + + + ))} + + ); +} + +/** What each way in is called on its tab, in the setup window's words. */ +function loginLabel(login: AiConnectionLogin): string { + switch (login) { + case "plan": + return "Sign in with my plan"; + case "oauth": + return "Sign in"; + default: + return "Use an API key"; + } +} + +/** The ways in this chooser offers for a provider. */ +function loginsFor(provider: AiProvider, mode: ChooserMode) { + return mode.kind === "personal" + ? personalLogins(provider, mode.signIns) + : loginsHere(provider, mode.inDesktop); +} + +/** The provider row's second line: the setup window's, where its sign-in is offered here. */ +function providerSummary(provider: AiProvider, mode: ChooserMode) { + if (mode.kind === "organization") + return mode.inDesktop ? provider.summary : provider.keySummary; + return personalLogins(provider, mode.signIns).includes("oauth") + ? provider.summary + : provider.keySummary; +} + +/** A sign-in in the browser, while it waits for the person to approve it there. */ +type Waiting = { url: string; code?: string | null; attemptId?: string }; + +const wait = (seconds: number) => + new Promise((resolve) => setTimeout(resolve, seconds * 1000)); + +export function ChooseConnection({ + connection, + mode, + onBack, + onConnected, +}: { + connection: AiConnection; + mode: ChooserMode; + /** Back from a chosen provider, or Cancel from the list. Absent: the list has no way back. */ + onBack?: () => void; + onConnected: (connected: ConnectedResult) => void; +}) { + const personal = mode.kind === "personal"; + const inDesktop = mode.kind === "organization" && mode.inDesktop; + const organizationWide = + mode.kind === "organization" && mode.organizationWide === true; + const [providerId, setProviderId] = useState(null); + const provider = providerById(providerId); + const logins = provider ? loginsFor(provider, mode) : []; + const [login, setLogin] = useState(null); + const preset = provider ? ENDPOINT_PRESETS[provider.id] : undefined; + const [apiKey, setApiKey] = useState(""); + const [baseUrl, setBaseUrl] = useState(""); + const [model, setModel] = useState(""); + const [useSaved, setUseSaved] = useState(true); + const [phase, setPhase] = useState("checking"); + // A sign-in in the browser: waiting for it, done, or failed with its sentence. + const [waiting, setWaiting] = useState(null); + const [signInCode, setSignInCode] = useState(""); + const [signedIn, setSignedIn] = useState(false); + const [signInFailure, setSignInFailure] = useState(null); + const [signingIn, setSigningIn] = useState(false); + // The key an Unbiased AI sign-in in Desktop mints: held for the one request that sends it, never drawn. + const minted = useRef(null); + const attempt = useRef<{ plan?: Plan; attemptId?: string } | null>(null); + const run = useRef(0); + const connect = useMutation( + personal + ? connectPersonalAiMutationOptions(queryClient) + : connectAiMutationOptions(queryClient), + ); + + /** Stop a sign-in still waiting in the browser, so its tab can't finish into a closed chooser. */ + function stopSignIn() { + run.current += 1; + const pending = attempt.current; + attempt.current = null; + if (pending?.plan) void cancelPlanSignIn(pending.plan).catch(() => {}); + if (pending?.attemptId) + void cancelProviderSignIn(pending.attemptId).catch(() => {}); + setWaiting(null); + setSigningIn(false); + } + const stopLatest = useRef(stopSignIn); + stopLatest.current = stopSignIn; + useEffect(() => () => stopLatest.current(), []); + + function choose(next: AiProvider | null) { + stopSignIn(); + connect.reset(); + setProviderId(next?.id ?? null); + const nextLogins = next ? loginsFor(next, mode) : []; + setLogin(nextLogins[0] ?? null); + const nextPreset = next ? ENDPOINT_PRESETS[next.id] : undefined; + setApiKey(""); + setBaseUrl(nextPreset?.baseUrl ?? ""); + setModel(nextPreset?.model ?? ""); + setUseSaved(true); + setSignedIn(false); + setSignInFailure(null); + setSignInCode(""); + minted.current = null; + } + + function chooseLogin(next: AiConnectionLogin) { + if (next === login) return; + stopSignIn(); + connect.reset(); + setSignedIn(false); + setSignInFailure(null); + setLogin(next); + } + + const address = preset ? preset.baseUrl : baseUrl; + const host = hostOf(address); + const savedKey = + provider && + (login === "api-key" || login === "endpoint") && + connection.saved.some( + (saved) => + saved.provider === provider.id && + saved.login === login && + (login === "api-key" || saved.endpointHost === host), + ); + const usingSaved = Boolean(savedKey && useSaved); + const typedKey = apiKey.trim(); + const typedModel = model.trim(); + + const ready = + !!provider && + !!login && + (login === "api-key" + ? usingSaved || typedKey.length > 0 + : login === "endpoint" + ? isEndpointAddress(address) && + typedModel.length > 0 && + (!preset || usingSaved || typedKey.length > 0) + : login === "oauth" + ? signedIn && typedModel.length > 0 + : signedIn); + + function input(): AiConnectionInput | null { + if (!provider || !login) return null; + if (login === "api-key") + return usingSaved + ? { provider: provider.id, login, useSavedKey: true } + : { provider: provider.id, login, apiKey: typedKey }; + if (login === "endpoint") + return { + provider: provider.id, + login, + baseUrl: address.trim(), + model: typedModel, + ...(usingSaved + ? { useSavedKey: true } + : typedKey + ? { apiKey: typedKey } + : {}), + }; + // A sign-in the server ran: its key is kept for the person, for the provider's own address. + if (login === "oauth" && personal && preset) + return { + provider: provider.id, + login: "endpoint", + baseUrl: preset.baseUrl, + model: typedModel, + useSavedKey: true, + }; + // Unbiased AI's sign-in ends in a key for its own address, sent exactly as a typed one is. + if (login === "oauth" && provider.id === "unbiased" && preset) { + if (!minted.current) return null; + return { + provider: provider.id, + login: "endpoint", + apiKey: minted.current, + baseUrl: preset.baseUrl, + model: typedModel, + }; + } + if (login === "oauth") + return { provider: provider.id, login, model: typedModel }; + return { provider: provider.id, login }; + } + + /** + * The server's own sign-in: start it, open the provider's page, and read where it stands every + * `interval` seconds until it is approved, failed or expired. Its key stays on the server. + */ + async function serverSignIn(chosen: AiProvider, current: () => boolean) { + const begun = await startPersonalSignIn(chosen.id); + if (!current()) return false; + setWaiting({ url: begun.url, code: begun.userCode }); + window.open(begun.url, "_blank", "noopener"); + let interval = begun.interval; + for (;;) { + await wait(interval); + if (!current()) return false; + const status = await readPersonalSignIn(begun.id); + if (!current()) return false; + if (status.status === "approved") return true; + if (status.status === "failed" || status.status === "expired") + throw new Error( + status.detail ?? "The sign-in didn't finish. Try again.", + ); + interval = status.interval; + } + } + + async function signIn() { + if (!provider || !login) return; + stopSignIn(); + const mine = run.current; + const current = () => run.current === mine; + setSigningIn(true); + setSignInFailure(null); + setSignedIn(false); + try { + if (personal) { + if (!(await serverSignIn(provider, current))) return; + } else if (login === "plan") { + const plan: Plan = provider.id === "anthropic" ? "claude" : "chatgpt"; + attempt.current = { plan }; + const { url } = await beginPlanSignIn(plan); + if (!current()) return; + setWaiting({ url }); + window.open(url, "_blank", "noopener"); + // Claude's sign-in hands back a code to paste here; ChatGPT's finishes in the browser. + if (plan === "claude") { + setSigningIn(false); + return; + } + await finishPlanSignIn(plan); + } else { + const begun = await beginProviderSignIn( + provider.id as "google" | "xai" | "unbiased", + ); + if (!current()) { + void cancelProviderSignIn(begun.attemptId).catch(() => {}); + return; + } + attempt.current = { attemptId: begun.attemptId }; + setWaiting({ + url: begun.url, + code: begun.userCode, + attemptId: begun.attemptId, + }); + window.open(begun.url, "_blank", "noopener"); + const finished = await finishProviderSignIn(begun.attemptId); + if (!current()) return; + minted.current = finished?.apiKey ?? null; + } + if (!current()) return; + attempt.current = null; + setWaiting(null); + setSignedIn(true); + setSigningIn(false); + } catch (error) { + if (!current()) return; + attempt.current = null; + setWaiting(null); + setSigningIn(false); + setSignInFailure( + error instanceof Error ? error.message : "Couldn't sign in. Try again.", + ); + } + } + + async function finishClaude() { + const mine = run.current; + setSigningIn(true); + setSignInFailure(null); + try { + await finishPlanSignIn("claude", signInCode.trim()); + if (run.current !== mine) return; + attempt.current = null; + setWaiting(null); + setSignInCode(""); + setSignedIn(true); + } catch (error) { + if (run.current !== mine) return; + // A refused code ends the attempt: the sign-in starts again. + attempt.current = null; + setWaiting(null); + setSignInFailure( + error instanceof Error ? error.message : "Couldn't sign in. Try again.", + ); + } finally { + if (run.current === mine) setSigningIn(false); + } + } + + function submit() { + const body = input(); + if (!ready || !body || connect.isPending) return; + connect.mutate( + { input: body, desktop: inDesktop, onPhase: setPhase }, + { + onSuccess: (connected) => { + minted.current = null; + setApiKey(""); + onConnected(connected); + }, + }, + ); + } + + const busy = connect.isPending; + const pendingLabel = + phase === "starting" + ? `Starting ${provider?.id === "anthropic" ? "Claude" : "ChatGPT"}…` + : phase === "finishing" + ? "Finishing…" + : `Checking with ${provider?.name ?? "the provider"}…`; + const resetFailure = () => { + if (connect.error) connect.reset(); + }; + /* + * Back from a chosen provider: to the dialog's previous screen for the organization's AI, as it + * always was; to the list for a person's own, which has no screen before it on a page. + */ + const back = personal && provider ? () => choose(null) : onBack; + + return ( +
{ + event.preventDefault(); + submit(); + }} + > + + +
+ {(provider ? [provider] : AI_PROVIDERS).map((row) => ( + choose(provider ? null : row)} + type="button" + /> + } + // A button centres its text; a row reads from the left like every other. + className="text-left" + size="sm" + variant="muted" + > + + + {row.name} + {providerSummary(row, mode)} + + + {provider ? ( + Change + ) : ( + + )} + + + ))} +
+ + {provider && login ? ( + + {logins.length > 1 ? ( + + chooseLogin(value as AiConnectionLogin) + } + value={login} + > + + {logins.map((option) => ( + + {loginLabel(option)} + + ))} + + + ) : null} + + {login === "plan" || login === "oauth" ? ( + + ) : null} + + {login === "endpoint" && !preset ? ( + + Address + 0 && !isEndpointAddress(baseUrl) + } + autoComplete="off" + disabled={busy} + id="ai-connection-address" + onChange={(event) => { + resetFailure(); + setBaseUrl(event.target.value); + }} + placeholder="https://api.example.com/v1" + spellCheck={false} + value={baseUrl} + /> + + The address of a server that speaks OpenAI's API. + + + ) : null} + + {login === "endpoint" || login === "oauth" ? ( + + + Model name + + { + resetFailure(); + setModel(event.target.value); + }} + placeholder={ + preset?.model ?? "The name the server knows it by" + } + spellCheck={false} + value={model} + /> + + ) : null} + + {login === "api-key" || login === "endpoint" ? ( + { + resetFailure(); + setApiKey(value); + }} + onUseSaved={(value) => { + resetFailure(); + setUseSaved(value); + }} + savedKey={Boolean(savedKey)} + useSaved={usingSaved} + /> + ) : null} + + {provider.id === "anthropic" && login === "plan" ? ( +

+ A Claude plan carries a separate monthly pool of Agent SDK + credits. When that pool is empty, Bots stop until it renews or + you add API credits. +

+ ) : null} +
+ ) : null} + + {connect.error ? ( +

+ {connect.error.message} +

+ ) : null} +
+ {back || provider ? ( + + {back ? ( + + ) : null} + {provider ? ( + + ) : null} + + ) : null} + + ); +} + +function KeyFields({ + apiKey, + busy, + keyLink, + label, + onApiKey, + onUseSaved, + savedKey, + useSaved, +}: { + apiKey: string; + busy: boolean; + keyLink: { keyUrl: string; keyLink: string } | undefined; + label: string; + onApiKey: (value: string) => void; + onUseSaved: (value: boolean) => void; + savedKey: boolean; + useSaved: boolean; +}) { + return ( + <> + {savedKey ? ( + + onUseSaved(checked === true)} + /> + + Use the saved key + + + ) : null} + {useSaved ? null : ( + + {label} + onApiKey(event.target.value)} + spellCheck={false} + type="password" + value={apiKey} + /> + {keyLink ? ( + + + {keyLink.keyLink} + + + ) : null} + + )} + + ); +} + +/** What approving a sign-in in the browser does, for each provider that signs in that way. */ +const SIGN_IN_SAYS: Record = { + anthropic: + "Anthropic opens in your browser. Approve the request there, then paste the code it shows you.", + openai: + "OpenAI opens in your browser. It finishes on its own once you approve.", + google: + "Authorize Gemini API access using the configured Google Cloud project and its API quota.", + xai: "Authorize OpenBot to use models available to your xAI account.", + unbiased: + "Sign in or create an Unbiased AI account in your browser and approve the code shown here.", +}; + +function SignInFields({ + busy, + code, + failure, + login, + onCancel, + onCode, + onFinishCode, + onSignIn, + provider, + signedIn, + waiting, +}: { + busy: boolean; + code: string; + failure: string | null; + login: "plan" | "oauth"; + onCancel: () => void; + onCode: (code: string) => void; + onFinishCode: () => void; + onSignIn: () => void; + provider: AiProvider; + signedIn: boolean; + waiting: Waiting | null; +}) { + const claude = login === "plan" && provider.id === "anthropic"; + const signInName = provider.id === "google" ? "Google" : provider.name; + return ( +
+

+ {SIGN_IN_SAYS[provider.id]} +

+ {signedIn ? ( +

+ + Signed in to {provider.name}. +

+ ) : waiting ? ( +
+ {waiting.code ? ( +

+ Verification code:{" "} + {waiting.code} +

+ ) : null} +

+ Didn't open?{" "} + + Open the sign-in page + + +

+ {claude ? ( + + + Code from your browser + +
+ onCode(event.target.value)} + spellCheck={false} + value={code} + /> + +
+
+ ) : ( +

+ Waiting for you to approve it in your browser… +

+ )} +
+ +
+
+ ) : null} + {signedIn || waiting ? null : ( +
+ +
+ )} + {failure ? ( +

+ {failure} +

+ ) : null} +
+ ); +} + +/** The connection now in use, as a row with a tick. */ +export function ConnectedRow({ + connection, + owner = "you", +}: { + connection: AiConnection; + owner?: AiConnectionOwner; +}) { + const described = describeConnection(connection, owner); + return ( + + + + {described?.provider} + {described?.summary} + + + + + + ); +} + +/** A dialog's last step: what is connected now, and Done. */ +export function Connected({ + connection, + desktopProblem, + onClose, + owner = "you", +}: { + connection: AiConnection; + desktopProblem: string | null; + onClose: () => void; + owner?: AiConnectionOwner; +}) { + const described = describeConnection(connection); + return ( + <> + + + Connected to {described?.provider ?? "your AI"} + + + {owner === "organization" + ? "Bots use it for anyone in your organization who hasn't connected their own AI." + : "Your next message runs on it. Anything already running finishes on the connection it started with."} + + + + + {desktopProblem ? ( +

+ {desktopProblem} +

+ ) : null} +
+ + + + + ); +} diff --git a/app/src/components/settings/ai-connection.tsx b/app/src/components/settings/ai-connection.tsx index 6243cd9618..392b135c6c 100644 --- a/app/src/components/settings/ai-connection.tsx +++ b/app/src/components/settings/ai-connection.tsx @@ -1,16 +1,8 @@ -import { - IconChevronRight, - IconCircleCheckFilled, - IconCloud, - IconExternalLink, - IconSparkles, -} from "@tabler/icons-react"; +import { IconChevronRight, IconSparkles } from "@tabler/icons-react"; import { useMutation, useQuery } from "@tanstack/react-query"; -import { useEffect, useRef, useState } from "react"; -import { HarnessMark } from "@/components/agents/harness-mark"; +import { useState } from "react"; import { PageRows, PageSection } from "@/components/layout/page-shell"; import { Button } from "@/components/ui/button"; -import { Checkbox } from "@/components/ui/checkbox"; import { Dialog, DialogBody, @@ -20,55 +12,21 @@ import { DialogHeader, DialogTitle, } from "@/components/ui/dialog"; -import { - Field, - FieldDescription, - FieldGroup, - FieldLabel, -} from "@/components/ui/field"; -import { Input } from "@/components/ui/input"; import { Item, ItemActions, ItemContent, ItemDescription, - ItemGroup, ItemMedia, ItemTitle, } from "@/components/ui/item"; -import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { describeConnection } from "@/lib/ai-connection/describe"; -import { - beginPlanSignIn, - beginProviderSignIn, - cancelPlanSignIn, - cancelProviderSignIn, - finishPlanSignIn, - finishProviderSignIn, - type Plan, -} from "@/lib/ai-connection/desktop"; -import { - type ConnectPhase, - connectAiMutationOptions, -} from "@/lib/ai-connection/mutations"; -import { - AI_PROVIDERS, - type AiProvider, - ENDPOINT_PRESETS, - hostOf, - isEndpointAddress, - loginsHere, - providerById, -} from "@/lib/ai-connection/providers"; +import { providerById } from "@/lib/ai-connection/providers"; import { aiConnectionQueryOptions, desktopAiSupportQueryOptions, } from "@/lib/ai-connection/queries"; -import type { - AiConnection, - AiConnectionInput, - AiConnectionLogin, -} from "@/lib/api/generated/model"; +import type { AiConnection } from "@/lib/api/generated/model"; import { describeAiConnection } from "@/lib/desktop/ai-connection"; import { changeAiConnectionMutationOptions } from "@/lib/desktop/mutations"; import { @@ -76,8 +34,12 @@ import { desktopAiConnectionQueryOptions, } from "@/lib/desktop/queries"; import { desktopShell } from "@/lib/desktop/shell"; -import { markFor } from "@/lib/marks"; -import { queryClient } from "@/query-client"; +import { + ChooseConnection, + Connected, + Details, + ProviderTile, +} from "./ai-connection-chooser"; /** * Settings → General's AI model: the AI provider OpenBot's Bots run on and how OpenBot signs in to @@ -88,8 +50,16 @@ import { queryClient } from "@/query-client"; * holds and renews what they sign in with. An OpenBot older than this operation, or a Desktop older * than changing it in place, keeps the row from before: Change there stops OpenBot and opens the * setup window. + * + * On OpenBot Cloud (a person can set their own AI there, `available`) it is the organization's AI, + * for everyone in it, beside each person's own in "Your AI" (your-ai.tsx). */ -export function AiModelSection() { +export function AiModelSection({ + organizationWide = false, +}: { + /** Beside "Your AI": this one is the organization's, for everyone in it. */ + organizationWide?: boolean; +}) { const connection = useQuery(aiConnectionQueryOptions()); const support = useQuery(desktopAiSupportQueryOptions()); if (connection.isPending || support.isPending) return null; @@ -103,45 +73,40 @@ export function AiModelSection() { if (!connection.data.editable) return null; return ( ); } -/** A provider's mark on the row's tile, or a cloud for the row that stands for every provider. */ -function ProviderTile({ provider }: { provider: AiProvider | undefined }) { - return ( - - {provider?.mark && markFor(provider.mark) ? ( - - ) : provider ? ( - - ) : ( - - )} - - ); -} - function AiConnectionRow({ connection, inDesktop, + organizationWide, }: { connection: AiConnection; inDesktop: boolean; + organizationWide: boolean; }) { const [open, setOpen] = useState(false); // A new dialog each time it opens, so nothing typed into the last one is kept. const [opened, setOpened] = useState(0); - const described = describeConnection(connection); + const described = describeConnection( + connection, + organizationWide ? "organization" : "you", + ); return ( <> {described?.provider ?? "Not chosen yet"} - {described?.summary ?? "Choose the AI your Bots use."} + {described?.summary ?? + (organizationWide + ? "Choose an AI for everyone in your organization." + : "Choose the AI your Bots use.")} @@ -173,6 +141,7 @@ function AiConnectionRow({ inDesktop={inDesktop} key={opened} onClose={() => setOpen(false)} + organizationWide={organizationWide} /> @@ -188,12 +157,15 @@ function AiConnectionDialog({ connection, inDesktop, onClose, + organizationWide, }: { connection: AiConnection; inDesktop: boolean; onClose: () => void; + organizationWide: boolean; }) { - const described = describeConnection(connection); + const owner = organizationWide ? "organization" : "you"; + const described = describeConnection(connection, owner); const [step, setStep] = useState( described ? { at: "current" } : { at: "choose" }, ); @@ -204,6 +176,7 @@ function AiConnectionDialog({ connection={step.connection} desktopProblem={step.desktopProblem} onClose={onClose} + owner={owner} /> ); @@ -212,7 +185,7 @@ function AiConnectionDialog({ setStep({ at: "current" }) : onClose} onConnected={(next) => setStep({ at: "done", ...next })} /> @@ -221,18 +194,23 @@ function AiConnectionDialog({ return ( - AI connection + + {organizationWide ? "Organization's AI" : "AI connection"} + {described - ? `Your Bots use ${described.provider}. OpenBot keeps the key or sign-in and never shows it.` + ? organizationWide + ? `Bots use ${described.provider} for anyone in your organization who hasn't connected their own AI. OpenBot keeps the key and never shows it.` + : `Your Bots use ${described.provider}. OpenBot keeps the key or sign-in and never shows it.` : "Your Bots don't have an AI provider yet."} {described &&
}

- You can switch to another provider or key while OpenBot keeps running. - Your next message uses the new connection. + {organizationWide + ? "You can switch to another provider or key at any time. Runs that start after that use it." + : "You can switch to another provider or key while OpenBot keeps running. Your next message uses the new connection."}

@@ -247,728 +225,6 @@ function AiConnectionDialog({ ); } -function Details({ details }: { details: { label: string; value: string }[] }) { - return ( - - {details.map((detail) => ( - - - - {detail.label} - - - - {detail.value} - - - ))} - - ); -} - -/** What each way in is called on its tab, in the setup window's words. */ -function loginLabel(login: AiConnectionLogin): string { - switch (login) { - case "plan": - return "Sign in with my plan"; - case "oauth": - return "Sign in"; - default: - return "Use an API key"; - } -} - -/** A sign-in in the browser, while it waits for the person to approve it there. */ -type Waiting = { url: string; code?: string | null; attemptId?: string }; - -function ChooseConnection({ - connection, - inDesktop, - onBack, - onConnected, -}: { - connection: AiConnection; - inDesktop: boolean; - onBack: () => void; - onConnected: (connected: { - connection: AiConnection; - desktopProblem: string | null; - }) => void; -}) { - const [providerId, setProviderId] = useState(null); - const provider = providerById(providerId); - const logins = provider ? loginsHere(provider, inDesktop) : []; - const [login, setLogin] = useState(null); - const preset = provider ? ENDPOINT_PRESETS[provider.id] : undefined; - const [apiKey, setApiKey] = useState(""); - const [baseUrl, setBaseUrl] = useState(""); - const [model, setModel] = useState(""); - const [useSaved, setUseSaved] = useState(true); - const [phase, setPhase] = useState("checking"); - // A sign-in in the browser: waiting for it, done, or failed with Desktop's sentence. - const [waiting, setWaiting] = useState(null); - const [signInCode, setSignInCode] = useState(""); - const [signedIn, setSignedIn] = useState(false); - const [signInFailure, setSignInFailure] = useState(null); - const [signingIn, setSigningIn] = useState(false); - // The key an Unbiased AI sign-in mints: held for the one request that sends it, never drawn. - const minted = useRef(null); - const attempt = useRef<{ plan?: Plan; attemptId?: string } | null>(null); - const run = useRef(0); - const connect = useMutation(connectAiMutationOptions(queryClient)); - - /** Stop a sign-in still waiting in the browser, so its tab can't finish into a closed dialog. */ - function stopSignIn() { - run.current += 1; - const pending = attempt.current; - attempt.current = null; - if (pending?.plan) void cancelPlanSignIn(pending.plan).catch(() => {}); - if (pending?.attemptId) - void cancelProviderSignIn(pending.attemptId).catch(() => {}); - setWaiting(null); - setSigningIn(false); - } - const stopLatest = useRef(stopSignIn); - stopLatest.current = stopSignIn; - useEffect(() => () => stopLatest.current(), []); - - function choose(next: AiProvider | null) { - stopSignIn(); - connect.reset(); - setProviderId(next?.id ?? null); - const nextLogins = next ? loginsHere(next, inDesktop) : []; - setLogin(nextLogins[0] ?? null); - const nextPreset = next ? ENDPOINT_PRESETS[next.id] : undefined; - setApiKey(""); - setBaseUrl(nextPreset?.baseUrl ?? ""); - setModel(nextPreset?.model ?? ""); - setUseSaved(true); - setSignedIn(false); - setSignInFailure(null); - setSignInCode(""); - minted.current = null; - } - - function chooseLogin(next: AiConnectionLogin) { - if (next === login) return; - stopSignIn(); - connect.reset(); - setSignedIn(false); - setSignInFailure(null); - setLogin(next); - } - - const address = preset ? preset.baseUrl : baseUrl; - const host = hostOf(address); - const savedKey = - provider && - (login === "api-key" || login === "endpoint") && - connection.saved.some( - (saved) => - saved.provider === provider.id && - saved.login === login && - (login === "api-key" || saved.endpointHost === host), - ); - const usingSaved = Boolean(savedKey && useSaved); - const typedKey = apiKey.trim(); - const typedModel = model.trim(); - - const ready = - !!provider && - !!login && - (login === "api-key" - ? usingSaved || typedKey.length > 0 - : login === "endpoint" - ? isEndpointAddress(address) && - typedModel.length > 0 && - (!preset || usingSaved || typedKey.length > 0) - : login === "oauth" - ? signedIn && typedModel.length > 0 - : signedIn); - - function input(): AiConnectionInput | null { - if (!provider || !login) return null; - if (login === "api-key") - return usingSaved - ? { provider: provider.id, login, useSavedKey: true } - : { provider: provider.id, login, apiKey: typedKey }; - if (login === "endpoint") - return { - provider: provider.id, - login, - baseUrl: address.trim(), - model: typedModel, - ...(usingSaved - ? { useSavedKey: true } - : typedKey - ? { apiKey: typedKey } - : {}), - }; - // Unbiased AI's sign-in ends in a key for its own address, sent exactly as a typed one is. - if (login === "oauth" && provider.id === "unbiased" && preset) { - if (!minted.current) return null; - return { - provider: provider.id, - login: "endpoint", - apiKey: minted.current, - baseUrl: preset.baseUrl, - model: typedModel, - }; - } - if (login === "oauth") - return { provider: provider.id, login, model: typedModel }; - return { provider: provider.id, login }; - } - - async function signIn() { - if (!provider || !login) return; - stopSignIn(); - const mine = run.current; - const current = () => run.current === mine; - setSigningIn(true); - setSignInFailure(null); - setSignedIn(false); - try { - if (login === "plan") { - const plan: Plan = provider.id === "anthropic" ? "claude" : "chatgpt"; - attempt.current = { plan }; - const { url } = await beginPlanSignIn(plan); - if (!current()) return; - setWaiting({ url }); - window.open(url, "_blank", "noopener"); - // Claude's sign-in hands back a code to paste here; ChatGPT's finishes in the browser. - if (plan === "claude") { - setSigningIn(false); - return; - } - await finishPlanSignIn(plan); - } else { - const begun = await beginProviderSignIn( - provider.id as "google" | "xai" | "unbiased", - ); - if (!current()) { - void cancelProviderSignIn(begun.attemptId).catch(() => {}); - return; - } - attempt.current = { attemptId: begun.attemptId }; - setWaiting({ - url: begun.url, - code: begun.userCode, - attemptId: begun.attemptId, - }); - window.open(begun.url, "_blank", "noopener"); - const finished = await finishProviderSignIn(begun.attemptId); - if (!current()) return; - minted.current = finished?.apiKey ?? null; - } - if (!current()) return; - attempt.current = null; - setWaiting(null); - setSignedIn(true); - setSigningIn(false); - } catch (error) { - if (!current()) return; - attempt.current = null; - setWaiting(null); - setSigningIn(false); - setSignInFailure( - error instanceof Error ? error.message : "Couldn't sign in. Try again.", - ); - } - } - - async function finishClaude() { - const mine = run.current; - setSigningIn(true); - setSignInFailure(null); - try { - await finishPlanSignIn("claude", signInCode.trim()); - if (run.current !== mine) return; - attempt.current = null; - setWaiting(null); - setSignInCode(""); - setSignedIn(true); - } catch (error) { - if (run.current !== mine) return; - // A refused code ends the attempt: the sign-in starts again. - attempt.current = null; - setWaiting(null); - setSignInFailure( - error instanceof Error ? error.message : "Couldn't sign in. Try again.", - ); - } finally { - if (run.current === mine) setSigningIn(false); - } - } - - function submit() { - const body = input(); - if (!ready || !body || connect.isPending) return; - connect.mutate( - { input: body, desktop: inDesktop, onPhase: setPhase }, - { - onSuccess: (connected) => { - minted.current = null; - setApiKey(""); - onConnected(connected); - }, - }, - ); - } - - const busy = connect.isPending; - const pendingLabel = - phase === "starting" - ? `Starting ${provider?.id === "anthropic" ? "Claude" : "ChatGPT"}…` - : phase === "finishing" - ? "Finishing…" - : `Checking with ${provider?.name ?? "the provider"}…`; - const resetFailure = () => { - if (connect.error) connect.reset(); - }; - - return ( -
{ - event.preventDefault(); - submit(); - }} - > - - Change AI connection - - {provider - ? "OpenBot checks the connection before it switches. Nothing changes if the check fails." - : "Choose the AI your Bots use. OpenBot keeps running while it switches."} - - - -
- {(provider ? [provider] : AI_PROVIDERS).map((row) => ( - choose(provider ? null : row)} - type="button" - /> - } - // A button centres its text; a row reads from the left like every other. - className="text-left" - size="sm" - variant="muted" - > - - - {row.name} - - {inDesktop ? row.summary : row.keySummary} - - - - {provider ? ( - Change - ) : ( - - )} - - - ))} -
- - {provider && login ? ( - - {logins.length > 1 ? ( - - chooseLogin(value as AiConnectionLogin) - } - value={login} - > - - {logins.map((option) => ( - - {loginLabel(option)} - - ))} - - - ) : null} - - {login === "plan" || login === "oauth" ? ( - - ) : null} - - {login === "endpoint" && !preset ? ( - - Address - 0 && !isEndpointAddress(baseUrl) - } - autoComplete="off" - disabled={busy} - id="ai-connection-address" - onChange={(event) => { - resetFailure(); - setBaseUrl(event.target.value); - }} - placeholder="https://api.example.com/v1" - spellCheck={false} - value={baseUrl} - /> - - The address of a server that speaks OpenAI's API. - - - ) : null} - - {login === "endpoint" || login === "oauth" ? ( - - - Model name - - { - resetFailure(); - setModel(event.target.value); - }} - placeholder={ - preset?.model ?? "The name the server knows it by" - } - spellCheck={false} - value={model} - /> - - ) : null} - - {login === "api-key" || login === "endpoint" ? ( - { - resetFailure(); - setApiKey(value); - }} - onUseSaved={(value) => { - resetFailure(); - setUseSaved(value); - }} - savedKey={Boolean(savedKey)} - useSaved={usingSaved} - /> - ) : null} - - {provider.id === "anthropic" && login === "plan" ? ( -

- A Claude plan carries a separate monthly pool of Agent SDK - credits. When that pool is empty, Bots stop until it renews or - you add API credits. -

- ) : null} -
- ) : null} - - {connect.error ? ( -

- {connect.error.message} -

- ) : null} -
- - - {provider ? ( - - ) : null} - -
- ); -} - -function KeyFields({ - apiKey, - busy, - keyLink, - label, - onApiKey, - onUseSaved, - savedKey, - useSaved, -}: { - apiKey: string; - busy: boolean; - keyLink: { keyUrl: string; keyLink: string } | undefined; - label: string; - onApiKey: (value: string) => void; - onUseSaved: (value: boolean) => void; - savedKey: boolean; - useSaved: boolean; -}) { - return ( - <> - {savedKey ? ( - - onUseSaved(checked === true)} - /> - - Use the saved key - - - ) : null} - {useSaved ? null : ( - - {label} - onApiKey(event.target.value)} - spellCheck={false} - type="password" - value={apiKey} - /> - {keyLink ? ( - - - {keyLink.keyLink} - - - ) : null} - - )} - - ); -} - -/** What approving a sign-in in the browser does, for each provider that signs in that way. */ -const SIGN_IN_SAYS: Record = { - anthropic: - "Anthropic opens in your browser. Approve the request there, then paste the code it shows you.", - openai: - "OpenAI opens in your browser. It finishes on its own once you approve.", - google: - "Authorize Gemini API access using the configured Google Cloud project and its API quota.", - xai: "Authorize OpenBot to use models available to your xAI account.", - unbiased: - "Sign in or create an Unbiased AI account in your browser and approve the code shown here.", -}; - -function SignInFields({ - busy, - code, - failure, - login, - onCancel, - onCode, - onFinishCode, - onSignIn, - provider, - signedIn, - waiting, -}: { - busy: boolean; - code: string; - failure: string | null; - login: "plan" | "oauth"; - onCancel: () => void; - onCode: (code: string) => void; - onFinishCode: () => void; - onSignIn: () => void; - provider: AiProvider; - signedIn: boolean; - waiting: Waiting | null; -}) { - const claude = login === "plan" && provider.id === "anthropic"; - const signInName = provider.id === "google" ? "Google" : provider.name; - return ( -
-

- {SIGN_IN_SAYS[provider.id]} -

- {signedIn ? ( -

- - Signed in to {provider.name}. -

- ) : waiting ? ( -
- {waiting.code ? ( -

- Verification code:{" "} - {waiting.code} -

- ) : null} -

- Didn't open?{" "} - - Open the sign-in page - - -

- {claude ? ( - - - Code from your browser - -
- onCode(event.target.value)} - spellCheck={false} - value={code} - /> - -
-
- ) : ( -

- Waiting for you to approve it in your browser… -

- )} -
- -
-
- ) : null} - {signedIn || waiting ? null : ( -
- -
- )} - {failure ? ( -

- {failure} -

- ) : null} -
- ); -} - -function Connected({ - connection, - desktopProblem, - onClose, -}: { - connection: AiConnection; - desktopProblem: string | null; - onClose: () => void; -}) { - const described = describeConnection(connection); - const provider = providerById(connection.provider); - return ( - <> - - - Connected to {described?.provider ?? "your AI"} - - - Your next message runs on it. Anything already running finishes on the - connection it started with. - - - - - - - {described?.provider} - {described?.summary} - - - - - - {desktopProblem ? ( -

- {desktopProblem} -

- ) : null} -
- - - - - ); -} - /** * The row from before changing the connection in place: in an OpenBot Desktop older than that, or * against an OpenBot server that doesn't offer it. Change there stops OpenBot and opens Desktop's diff --git a/app/src/components/settings/your-ai.tsx b/app/src/components/settings/your-ai.tsx new file mode 100644 index 0000000000..e9f309e6fc --- /dev/null +++ b/app/src/components/settings/your-ai.tsx @@ -0,0 +1,235 @@ +import { + IconBuilding, + IconChevronRight, + IconSparkles, +} from "@tabler/icons-react"; +import { useMutation, useQuery } from "@tanstack/react-query"; +import { useNavigate } from "@tanstack/react-router"; +import { useState } from "react"; +import { PageRows, PageSection } from "@/components/layout/page-shell"; +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogBody, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { + Item, + ItemActions, + ItemContent, + ItemDescription, + ItemMedia, + ItemTitle, +} from "@/components/ui/item"; +import { describeConnection } from "@/lib/ai-connection/describe"; +import { removePersonalAiMutationOptions } from "@/lib/ai-connection/mutations"; +import { providerById } from "@/lib/ai-connection/providers"; +import { + needsAiConnection, + type PersonalAiConnection, + personalAiConnectionQueryOptions, +} from "@/lib/ai-connection/queries"; +import type { AiConnection } from "@/lib/api/generated/model"; +import { queryClient } from "@/query-client"; +import { + ChooseConnection, + Connected, + Details, + ProviderTile, +} from "./ai-connection-chooser"; + +/** + * Settings → General → Your AI: the AI a person's Bots use when they work for them, on an OpenBot + * where each person can bring their own (getPersonalAiConnection's `available`, OpenBot Cloud). + * Theirs alone: it follows them into every organization they are in, and nobody else's runs use it. + * + * One row, saying which AI their runs use now: their own, the organization's, or none yet. It opens + * the same chooser as onboarding's Connect your AI step and the organization's AI connection, in + * its personal mode. Their own can be removed, and their runs go back to the organization's; where + * the organization has none and one is required, they are sent to connect another. + */ +export function YourAiSection() { + const personal = useQuery(personalAiConnectionQueryOptions()); + // Pending, failed, or an OpenBot without this: the section isn't there. + if (!personal.data?.available) return null; + return ( + + + + + + ); +} + +function YourAiRow({ personal }: { personal: PersonalAiConnection }) { + const [open, setOpen] = useState(false); + // A new dialog each time it opens, so nothing typed into the last one is kept. + const [opened, setOpened] = useState(0); + const described = + personal.uses === "personal" + ? describeConnection(personal.connection) + : null; + return ( + <> + { + setOpened((count) => count + 1); + setOpen(true); + }} + type="button" + /> + } + size="sm" + > + {described ? ( + + ) : ( + + {personal.uses === "organization" ? ( + + ) : ( + + )} + + )} + + + {described + ? described.provider + : personal.uses === "organization" + ? "Your organization's AI" + : "Not connected"} + + + {described + ? described.summary + : personal.uses === "organization" + ? "Bots use the AI your organization set up. Connect your own to use it instead." + : "Connect your AI so Bots can work for you."} + + + + + + + + setOpen(false)} + personal={personal} + /> + + + ); +} + +type Step = + | { at: "current" } + | { at: "choose" } + | { at: "done"; connection: AiConnection }; + +function YourAiDialog({ + personal, + onClose, +}: { + personal: PersonalAiConnection; + onClose: () => void; +}) { + const navigate = useNavigate(); + const own = personal.uses === "personal"; + const described = own ? describeConnection(personal.connection) : null; + const [step, setStep] = useState( + described ? { at: "current" } : { at: "choose" }, + ); + const remove = useMutation(removePersonalAiMutationOptions(queryClient)); + if (step.at === "done") + return ( + + + + ); + if (step.at === "choose") + return ( + + setStep({ at: "current" }) : onClose} + onConnected={(next) => + setStep({ at: "done", connection: next.connection }) + } + /> + + ); + const afterRemoving = personal.organization.ready + ? "Remove it and Bots use your organization's AI for you." + : personal.required + ? "Remove it and Bots can't work for you until you connect another." + : "Remove it and Bots stop using it for you."; + return ( + + + Your AI + + {described + ? `Bots use ${described.provider} when they work for you. It's yours alone, and OpenBot never shows the key.` + : "You haven't connected your own AI."} + + + + {described &&
} +

{afterRemoving}

+ {remove.error ? ( +

+ {remove.error.message} +

+ ) : null} + + + + + + + + ); +} diff --git a/app/src/lib/ai-connection/describe.ts b/app/src/lib/ai-connection/describe.ts index 1906b159c5..42f5a05579 100644 --- a/app/src/lib/ai-connection/describe.ts +++ b/app/src/lib/ai-connection/describe.ts @@ -12,40 +12,49 @@ export type AiConnectionWords = { details: AiConnectionDetail[]; }; +/** + * Whose key or sign-in it is, in the row's words. `organization` beside a person's own AI (OpenBot + * Cloud), where the organization's AI is not the reader's: "Uses your organization's OpenAI API + * key." Everywhere else the reader's: "Uses your OpenAI API key." + */ +export type AiConnectionOwner = "you" | "organization"; + /** What Settings says about the AI connection; null when none has been chosen yet. */ export function describeConnection( connection: Pick< AiConnection, "provider" | "login" | "model" | "endpointHost" >, + owner: AiConnectionOwner = "you", ): AiConnectionWords | null { const { provider: id, login, model, endpointHost } = connection; if (!id || !login) return null; const provider = providerById(id)?.name ?? id; + const your = owner === "organization" ? "your organization's" : "your"; let signIn: string; let said: string; switch (login) { case "plan": signIn = id === "anthropic" ? "Claude plan" : "ChatGPT plan"; - said = `Signed in with your ${signIn}.`; + said = `Signed in with ${your} ${signIn}.`; break; case "oauth": signIn = id === "google" ? "Google account" : `${provider} account`; - said = `Signed in with your ${signIn}.`; + said = `Signed in with ${your} ${signIn}.`; break; case "api-key": signIn = "API key"; - said = `Uses your ${provider} API key.`; + said = `Uses ${your} ${provider} API key.`; break; case "endpoint": if (id === "openai-compatible") { signIn = endpointHost ?? "Your own address"; said = endpointHost ? `Connected to ${endpointHost}.` - : "Connected to your own address."; + : `Connected to ${your} own address.`; } else { signIn = "API key"; - said = `Uses your ${provider} API key.`; + said = `Uses ${your} ${provider} API key.`; } break; } diff --git a/app/src/lib/ai-connection/mutations.ts b/app/src/lib/ai-connection/mutations.ts index 2c701e98b2..62776183e3 100644 --- a/app/src/lib/ai-connection/mutations.ts +++ b/app/src/lib/ai-connection/mutations.ts @@ -2,10 +2,18 @@ import { mutationOptions, type QueryClient } from "@tanstack/react-query"; import type { AiConnection, AiConnectionInput, + AiConnectionProvider, + AiSignIn, + AiSignInStatus, + PersonalAiConnection, } from "@/lib/api/generated/model"; import { + deletePersonalAiConnection, getAiConnection, + getPersonalAiSignIn, replaceAiConnection, + replacePersonalAiConnection, + startPersonalAiSignIn, } from "@/lib/api/generated/models/models"; import { idempotencyKey, organizationId } from "@/lib/api/request"; import { desktopKeys } from "@/lib/desktop/queries"; @@ -105,11 +113,87 @@ async function settleBack() { export function connectAiMutationOptions(queryClient: QueryClient) { return mutationOptions({ mutationFn: (variables: ConnectVariables) => connectAi(variables), - onSuccess: () => - Promise.all([ - queryClient.invalidateQueries({ queryKey: aiConnectionKeys.all }), - queryClient.invalidateQueries({ queryKey: modelKeys.all }), - queryClient.invalidateQueries({ queryKey: desktopKeys.aiConnection }), - ]), + onSuccess: () => invalidateAiConnection(queryClient), + }); +} + +/** + * Every AI connection this page has read, and what follows from it. The person's own with + * `refetchType: "all"`: the `_authed` gate reads it through `ensureQueryData`, which takes the + * cache as it finds it, and nothing may be observing it while a person connects in onboarding. + */ +function invalidateAiConnection(queryClient: QueryClient) { + return Promise.all([ + queryClient.invalidateQueries({ + queryKey: aiConnectionKeys.personal, + refetchType: "all", + }), + queryClient.invalidateQueries({ queryKey: aiConnectionKeys.all }), + queryClient.invalidateQueries({ queryKey: modelKeys.all }), + queryClient.invalidateQueries({ queryKey: desktopKeys.aiConnection }), + ]); +} + +const PERSONAL_FALLBACK = "Couldn't connect your AI. Try again."; + +/** + * Connect the person's own AI: a key, an address, or the key a sign-in through OpenBot kept for + * them (`useSavedKey`). The server checks it with the provider before it saves anything; a refusal + * throws its one sentence and nothing changes. Never Desktop's engines: the server runs this + * person's Bots, so `desktop` and its phases have nothing to do here. + */ +export function connectPersonalAiMutationOptions(queryClient: QueryClient) { + return mutationOptions({ + mutationFn: async ({ input }: ConnectVariables): Promise => { + const personal = await replacePersonalAiConnection( + organizationId(), + "me", + input, + { "Idempotency-Key": idempotencyKey() }, + { fallback: PERSONAL_FALLBACK }, + ); + return { connection: personal.connection, desktopProblem: null }; + }, + onSuccess: () => invalidateAiConnection(queryClient), + }); +} + +/** + * Remove the person's own AI. Their runs use the organization's where it has one; where it has none + * and one is required, the answer says so (`uses` none) and the app sends them to connect again. + */ +export function removePersonalAiMutationOptions(queryClient: QueryClient) { + return mutationOptions({ + mutationFn: (): Promise => + deletePersonalAiConnection( + organizationId(), + "me", + { "Idempotency-Key": idempotencyKey() }, + { fallback: "Couldn't remove your AI. Try again." }, + ), + onSuccess: () => invalidateAiConnection(queryClient), + }); +} + +/** + * A sign-in to an AI provider that the server runs itself, for the person's own AI. Not a cached + * read nor a write somebody asked to keep: a step on the way to connecting, so plain functions. + * The provider's key never comes back; once approved, connecting with `useSavedKey` uses it. + */ +export function startPersonalSignIn( + provider: AiConnectionProvider, +): Promise { + return startPersonalAiSignIn( + organizationId(), + "me", + { provider }, + { "Idempotency-Key": idempotencyKey() }, + { fallback: "Couldn't start the sign-in. Try again." }, + ); +} + +export function readPersonalSignIn(signInId: string): Promise { + return getPersonalAiSignIn(organizationId(), "me", signInId, { + fallback: "Couldn't check the sign-in. Try again.", }); } diff --git a/app/src/lib/ai-connection/providers.ts b/app/src/lib/ai-connection/providers.ts index 9102f5084a..91cc1e3151 100644 --- a/app/src/lib/ai-connection/providers.ts +++ b/app/src/lib/ai-connection/providers.ts @@ -119,6 +119,26 @@ export function loginsHere( ); } +/** + * The ways in for a person's own AI: the provider's sign-in first where the server can run it + * itself (`signIns`, and a preset address its key is for), then the provider's key or address. + * Never a plan or a sign-in OpenBot Desktop holds, even inside Desktop: the server runs the + * person's Bots, and can't use a credential kept on one computer. + */ +export function personalLogins( + provider: AiProvider, + signIns: readonly AiConnectionProvider[], +): AiConnectionLogin[] { + const signIn = + signIns.includes(provider.id) && + provider.logins.includes("oauth") && + ENDPOINT_PRESETS[provider.id] !== undefined; + return [ + ...(signIn ? (["oauth"] as const) : []), + ...loginsHere(provider, false), + ]; +} + export function providerById( id: AiConnectionProvider | null | undefined, ): AiProvider | undefined { diff --git a/app/src/lib/ai-connection/queries.ts b/app/src/lib/ai-connection/queries.ts index 392f7d9597..9927a5f74d 100644 --- a/app/src/lib/ai-connection/queries.ts +++ b/app/src/lib/ai-connection/queries.ts @@ -1,6 +1,12 @@ -import { queryOptions } from "@tanstack/react-query"; -import type { AiConnection } from "@/lib/api/generated/model"; -import { getAiConnection } from "@/lib/api/generated/models/models"; +import { type QueryClient, queryOptions } from "@tanstack/react-query"; +import type { + AiConnection, + PersonalAiConnection, +} from "@/lib/api/generated/model"; +import { + getAiConnection, + getPersonalAiConnection, +} from "@/lib/api/generated/models/models"; import { organizationId } from "@/lib/api/request"; import { ClientError } from "@/lib/client"; import { invokeDesktop } from "@/lib/desktop/queries"; @@ -8,13 +14,15 @@ import { desktopShell } from "@/lib/desktop/shell"; /** * How OpenBot reaches its AI provider (getAiConnection), and whether the Desktop window this page - * may be in can change it in place. + * may be in can change it in place. And a person's own AI connection (getPersonalAiConnection), + * which on OpenBot Cloud each person brings for themselves. */ -export type { AiConnection }; +export type { AiConnection, PersonalAiConnection }; export const aiConnectionKeys = { all: ["ai-connection"] as const, current: ["ai-connection", "current"] as const, + personal: ["ai-connection", "personal"] as const, // Not under `all`: what Desktop can do does not change when the connection does. desktop: ["ai-connection-desktop"] as const, }; @@ -40,6 +48,56 @@ export function aiConnectionQueryOptions() { }); } +/** + * The person's own AI connection, and which AI their runs use. Never a key. Null from an OpenBot + * older than this operation (404), which reads as "nothing to connect": only OpenBot Cloud requires + * one, and it answers. + */ +export function personalAiConnectionQueryOptions() { + return queryOptions({ + queryKey: aiConnectionKeys.personal, + queryFn: async ({ signal }): Promise => { + try { + return await getPersonalAiConnection(organizationId(), "me", { + signal, + fallback: "Couldn't load your AI connection.", + }); + } catch (error) { + if (error instanceof ClientError && error.status === 404) return null; + throw error; + } + }, + }); +} + +/** + * Whether the person has to connect their own AI before they use OpenBot: the server requires one + * (OpenBot Cloud) and their runs have none, neither theirs nor the organization's. The server's two + * verdicts, never recomputed from the connection itself. + */ +export function needsAiConnection( + personal: PersonalAiConnection | null | undefined, +): boolean { + return personal?.required === true && personal.uses === "none"; +} + +/** + * The same question, for a route's gate. A server that can't answer is read as "not required": a + * self-hosted OpenBot and an older one never require it, and OpenBot Cloud refuses the run itself + * with a sentence the chat turns into a way to connect. + */ +export async function aiConnectionRequired( + queryClient: QueryClient, +): Promise { + try { + return needsAiConnection( + await queryClient.ensureQueryData(personalAiConnectionQueryOptions()), + ); + } catch { + return false; + } +} + /** * Whether this page is in an OpenBot Desktop that changes the AI connection in place: one that * answers `ai_providers`, as every Desktop with `prepare_ai_connection` does. False in a plain diff --git a/app/src/routes/_authed.tsx b/app/src/routes/_authed.tsx index 6d0c4f4a9a..a0160d5e24 100644 --- a/app/src/routes/_authed.tsx +++ b/app/src/routes/_authed.tsx @@ -7,6 +7,7 @@ import { TeamFeaturesDialog } from "../components/edition/team-features-dialog"; import { CommandMenu } from "../components/navigation/command-menu"; import { NewOrganizationDialog } from "../components/organizations/new-organization-dialog"; import { OrganizationBoundary } from "../components/organizations/organization-boundary"; +import { aiConnectionRequired } from "../lib/ai-connection/queries"; import { connectedThroughDesktop, signOutThroughDesktop, @@ -50,6 +51,19 @@ export const Route = createFileRoute("/_authed")({ rememberInvitationLink(location.pathname); throw redirect({ to: "/onboarding" }); } + /* + * Where OpenBot provides no AI (OpenBot Cloud), somebody whose runs have none, neither their own + * nor their organization's, connects their own before anything else: from anywhere, and + * somebody who finished onboarding before that step existed too. The onboarding route admits + * them while it holds. + */ + if ( + location.pathname !== "/onboarding" && + (await aiConnectionRequired(context.queryClient)) + ) { + rememberInvitationLink(location.pathname); + throw redirect({ to: "/onboarding", search: { step: "ai" } }); + } // Before any conversation is drawn, so a long-named action's step never shows its cut name. await learnShortenedToolNames(context.queryClient); }, diff --git a/app/src/routes/_authed/onboarding.tsx b/app/src/routes/_authed/onboarding.tsx index c11bd0a568..dfaa61f824 100644 --- a/app/src/routes/_authed/onboarding.tsx +++ b/app/src/routes/_authed/onboarding.tsx @@ -20,6 +20,12 @@ import { OnboardingWizard, } from "@/components/onboarding/onboarding-wizard"; import { Button } from "@/components/ui/button"; +import { useAiStep } from "@/components/onboarding/ai-step"; +import { + aiConnectionRequired, + needsAiConnection, + personalAiConnectionQueryOptions, +} from "@/lib/ai-connection/queries"; import { currentUserQueryOptions, needsOnboarding } from "@/lib/auth/queries"; import { deploymentCapabilitiesQueryOptions } from "@/lib/deployment/queries"; import { appConfig } from "@/lib/generated/application-config"; @@ -45,9 +51,13 @@ const STEP_NUMBER = { welcome: 1, selfhost: 2, name: 3, + ai: 4, } as const; type StepId = keyof typeof STEP_NUMBER; +/** The order the steps are shown in, whichever of them a person has. */ +const ORDER: StepId[] = ["welcome", "ai", "selfhost", "name"]; + const isStepId = (value: unknown): value is StepId => typeof value === "string" && Object.hasOwn(STEP_NUMBER, value); @@ -77,8 +87,15 @@ export const Route = createFileRoute("/_authed/onboarding")({ const user = await context.queryClient.ensureQueryData( currentUserQueryOptions(), ); - // Somebody who has finished, or whose deployment tracks no onboarding, has no business here. - if (!user || !needsOnboarding(user)) { + /* + * Somebody who has finished, or whose deployment tracks no onboarding, has no business here, + * unless they still have to connect their own AI: then Connect your AI is their one step, even + * for somebody who finished onboarding before it existed. + */ + if (!user) throw redirect({ to: "/" }); + // Asked every time, not only for somebody who finished: the step list reads it from the cache. + const aiRequired = await aiConnectionRequired(context.queryClient); + if (!needsOnboarding(user) && !aiRequired) { throw redirect({ to: "/" }); } }, @@ -261,26 +278,57 @@ function RouteComponent() { Boolean(me.data?.name) && me.data?.name !== me.data?.email, ); - const ids: StepId[] = [ - "welcome", - ...(capabilities?.localEvaluation ? (["selfhost"] as const) : []), - ...(nameKnown ? [] : (["name"] as const)), - ]; + /* + * CONNECT YOUR AI, WHERE ONE IS REQUIRED AND THIS PERSON'S RUNS HAVE NONE (OpenBot Cloud, with no + * AI of their own or their organization's). Right after Welcome. Decided once, on arrival, like + * the name: connecting makes it unneeded, and deciding again would drop the step from under the + * person while they look at what they connected. `beforeLoad` has the answer in the cache. + * + * Somebody who finished onboarding before this step existed, or whose organization's AI went + * away, has it as their only step, and its button takes them into the app. + */ + const personal = useQuery(personalAiConnectionQueryOptions()); + const [aiNeeded] = React.useState(() => needsAiConnection(personal.data)); + const [onboarded] = React.useState( + () => + me.data !== undefined && me.data !== null && !needsOnboarding(me.data), + ); + const aiConnected = personal.data ? personal.data.uses !== "none" : false; + const ids: StepId[] = onboarded + ? ["ai"] + : [ + "welcome", + ...(aiNeeded ? (["ai"] as const) : []), + ...(capabilities?.localEvaluation ? (["selfhost"] as const) : []), + ...(nameKnown ? [] : (["name"] as const)), + ]; /* * The step on screen: the address's, else the one the server saved, else the first. A step not in - * this person's list (OpenBot for Business for an organization's person, the name step once it is known) - * reads as the last one they have, once the answer that decides it is in. Arriving without a step + * this person's list (OpenBot for Business for an organization's person, the name step once it is + * known, Connect your AI once their runs have one) reads as the next one they have, or the last. + * No step after Connect your AI is reachable until it is connected. Arriving without a step * writes it into the address in place, so the entry the person lands on already names its step. */ const reachable = (step: StepId | null | undefined): StepId | undefined => { if (!step) return undefined; if (step === "selfhost" && !capabilities) return step; - return ids.includes(step) ? step : ids[ids.length - 1]; + const found = ids.includes(step) + ? step + : (ids.find((id) => ORDER.indexOf(id) > ORDER.indexOf(step)) ?? + ids[ids.length - 1]); + if ( + found && + ids.includes("ai") && + !aiConnected && + ids.indexOf(found) > ids.indexOf("ai") + ) + return "ai"; + return found; }; const saved = reachable(stepForNumber(me.data?.onboarding?.step)); const asked = reachable(isStepId(search.step) ? search.step : undefined); - const current: StepId = asked ?? saved ?? "welcome"; + const current: StepId = asked ?? saved ?? ids[0] ?? "welcome"; React.useEffect(() => { if (asked !== undefined && asked === search.step) return; navigate({ to: "/onboarding", search: { step: current }, replace: true }); @@ -290,6 +338,8 @@ function RouteComponent() { const savedStep = me.data?.onboarding?.step; const { mutate: save } = advance; React.useEffect(() => { + // Somebody who finished onboarding has no place in it to keep. + if (onboarded) return; if (savedStep === undefined || savedStep === STEP_NUMBER[current]) return; // OpenBot for Business waits for the answer that says whether this person has one. if (current === "selfhost" && !capabilities) return; @@ -302,7 +352,7 @@ function RouteComponent() { onError: (error) => console.warn("Your place in onboarding was not saved.", error), }); - }, [current, savedStep, save, capabilities]); + }, [current, savedStep, save, capabilities, onboarded]); /* * CONTINUE PUSHES, AND BACK RETURNS THROUGH HISTORY WHEN IT CAN. Continue adds an entry carrying @@ -336,7 +386,8 @@ function RouteComponent() { const [finished, setFinished] = React.useState(false); const [leaving, setLeaving] = React.useState(false); const finish = async () => { - await complete.mutateAsync(undefined); + // Somebody who finished onboarding before only had their AI to connect. + if (!onboarded) await complete.mutateAsync(undefined); setFinished(true); await new Promise((resolve) => setTimeout(resolve, reduced ? 150 : FINISH_MS), @@ -352,6 +403,7 @@ function RouteComponent() { const name = useNameStep({ finish, finished }); const selfhost = useBookDemoStep(); + const ai = useAiStep(); /* * The checklist says whether there is a Bot computer, and the intro shows one only where there * is. Until it answers, the usual case: a computer that is there. @@ -395,6 +447,7 @@ function RouteComponent() { primaryDisabled: !capabilities, notice: capabilitiesFailed, }, + ai, selfhost, name, }; diff --git a/app/src/routes/_authed/settings/index.tsx b/app/src/routes/_authed/settings/index.tsx index ba5ddeb86b..d88b9ee4fd 100644 --- a/app/src/routes/_authed/settings/index.tsx +++ b/app/src/routes/_authed/settings/index.tsx @@ -1,3 +1,4 @@ +import { useQuery } from "@tanstack/react-query"; import { createFileRoute } from "@tanstack/react-router"; import React from "react"; import { @@ -13,6 +14,7 @@ import { StandingInstructionsRow } from "@/components/settings/standing-instruct import { StarOnGitHubSection } from "@/components/settings/star-on-github"; import { ThemeRow } from "@/components/settings/theme-row"; import { UsageDataSection } from "@/components/settings/usage-data"; +import { YourAiSection } from "@/components/settings/your-ai"; import { Item, ItemActions, @@ -21,6 +23,7 @@ import { ItemTitle, } from "@/components/ui/item"; import { Separator } from "@/components/ui/separator"; +import { personalAiConnectionQueryOptions } from "@/lib/ai-connection/queries"; import { formatHotkey, HOTKEYS } from "@/lib/hotkeys/hotkeys"; import { SkinPicker } from "@/skins/skin-picker"; @@ -37,6 +40,7 @@ function RouteComponent() { * Connected accounts used to be a section below. It is its own screen now: a connector can need * more from a person than one switch, and a section cannot grow a page's worth of that. */ + const personal = useQuery(personalAiConnectionQueryOptions()); return ( {/* Only where the organization has identity providers: the person's company account, and the way to link it. */} - {/* Only in OpenBot Desktop, which holds the AI connection and changes it in its own window. */} - + {/* Where each person brings their own AI (OpenBot Cloud): theirs, or that Bots use the organization's. */} + + {/* + * The organization's AI, for an admin who may change it. Beside "Your AI" it says it is the + * organization's, for everyone in it. + */} + {/* Only in OpenBot Desktop, which sends usage data and keeps this choice. */} diff --git a/app/tests/add-model-key-link.test.tsx b/app/tests/add-model-key-link.test.tsx index ecec32b371..ac72b73c90 100644 --- a/app/tests/add-model-key-link.test.tsx +++ b/app/tests/add-model-key-link.test.tsx @@ -7,6 +7,7 @@ import { ADD_MODEL_KEY_PATH, AddModelKeyLink, NO_AI_CONNECTION, + NO_PERSONAL_AI_CONNECTION, } from "@/components/channels/add-model-key-link"; import { getGetPersonMockHandler, @@ -73,6 +74,21 @@ test("a member gets the sentence alone, with nothing to click", async () => { expect(view.queryByRole("link")).toBeNull(); }); +test("on OpenBot Cloud anybody, a member too, gets Connect your AI, which opens the step", async () => { + for (const role of ["member", "admin"] as const) { + signedInAs(role); + const view = render(); + const link = await waitFor(() => + view.getByRole("link", { name: "Connect your AI" }), + ); + expect(link.getAttribute("href")).toBe("/onboarding?step=ai"); + // Not the admin's way to the organization's key: their own AI is the fix here. + expect(view.queryByRole("link", { name: "Add a model key" })).toBeNull(); + cleanup(); + queryClient.clear(); + } +}); + test("any other reason a turn ended draws nothing and asks the server nothing", async () => { const view = render( , diff --git a/app/tests/connect-your-ai.fixture.tsx b/app/tests/connect-your-ai.fixture.tsx new file mode 100644 index 0000000000..03451c23a2 --- /dev/null +++ b/app/tests/connect-your-ai.fixture.tsx @@ -0,0 +1,135 @@ +import { QueryClientProvider } from "@tanstack/react-query"; +import { + createBrowserHistory, + createRootRouteWithContext, + createRoute, + createRouter, + Outlet, + RouterProvider, +} from "@tanstack/react-router"; +import type { ReactNode } from "react"; +import { createRoot } from "react-dom/client"; +import { SidebarShell } from "@/components/layout/sidebar-shell"; +import { OrganizationBoundary } from "@/components/organizations/organization-boundary"; +import { useSwitchOrganization } from "@/components/organizations/use-switch-organization"; +import { SettingsSidebar } from "@/components/settings/settings-sidebar"; +import { ThemeProvider } from "@/components/theme-provider"; +import { SidebarInset } from "@/components/ui/sidebar"; +import { setCurrentOrganization } from "@/lib/organizations/current"; +import { queryClient } from "@/query-client"; +import type { RouterContext } from "@/router-context"; +import { Route as AuthedRoute } from "@/routes/_authed"; +import { Route as OnboardingRoute } from "@/routes/_authed/onboarding"; +import { Route as GeneralRoute } from "@/routes/_authed/settings/index"; + +/** + * The signed-in gate, onboarding and Settings → General, for connect-your-ai.test.tsx to bundle and + * load in a browser. The gate is `_authed`'s own `beforeLoad`, and onboarding is its own route, so + * where a person is sent is what the app does. The app behind the gate is a page that says Home: + * this is about getting there. What the server answers comes from the test's routes. + * + * Started in `default`, the organization those routes answer for, as every test does. + */ +setCurrentOrganization("default"); + +const component = (route: { options: { component?: unknown } }) => + route.options.component as () => ReactNode; + +const rootRoute = createRootRouteWithContext()({ + component: () => , +}); + +const authedRoute = createRoute({ + id: "/_authed", + getParentRoute: () => rootRoute, + // The app's own gate, as it runs in front of every signed-in page. + beforeLoad: async (options) => { + await ( + AuthedRoute.options.beforeLoad as unknown as ( + given: typeof options, + ) => Promise + )(options); + }, + // The app's own boundary: built again for each organization, and the gates decide again in it. + component: () => ( + + + + ), +}); + +/** + * Home, with the account menu's switch to another organization: the same hook the menu runs, so a + * switch goes the way it does in the app (home first, then the app changes over). + */ +function Home() { + const switching = useSwitchOrganization(); + return ( +
+

Home

+ +
+ ); +} + +const onboardingRoute = ( + OnboardingRoute as unknown as { + update: (options: unknown) => typeof OnboardingRoute; + } +).update({ + id: "/onboarding", + path: "/onboarding", + getParentRoute: () => authedRoute, +}); + +const settingsRoute = createRoute({ + getParentRoute: () => authedRoute, + id: "settings-frame", + component: () => ( + + + + + + + ), +}); + +const router = createRouter({ + history: createBrowserHistory(), + context: { queryClient }, + routeTree: rootRoute.addChildren([ + authedRoute.addChildren([ + onboardingRoute as never, + settingsRoute.addChildren([ + createRoute({ + getParentRoute: () => settingsRoute, + path: "/settings", + component: component(GeneralRoute), + }), + ]), + createRoute({ + getParentRoute: () => authedRoute, + path: "/", + component: Home, + }), + ]), + createRoute({ + getParentRoute: () => rootRoute, + path: "/sign", + component: () =>

Sign in

, + }), + ]), +}); + +const root = document.getElementById("root"); +if (root) + createRoot(root).render( + + + + + , + ); diff --git a/app/tests/connect-your-ai.test.tsx b/app/tests/connect-your-ai.test.tsx new file mode 100644 index 0000000000..82231aff63 --- /dev/null +++ b/app/tests/connect-your-ai.test.tsx @@ -0,0 +1,1071 @@ +import { afterAll, beforeAll, describe, expect, test } from "bun:test"; +import { existsSync, mkdirSync, readdirSync, readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import type { Browser, BrowserType, Page, Request } from "playwright-core"; +import { compile } from "tailwindcss"; +import { parse } from "yaml"; +import type { + AiConnection, + PersonalAiConnection, +} from "@/lib/api/generated/model"; +import { + GetAiConnectionResponse, + GetPersonalAiConnectionResponse, + GetPersonalAiSignInResponse, + ReplacePersonalAiConnectionBody, + StartPersonalAiSignInBody, + StartPersonalAiSignInResponse, +} from "@/lib/api/generated/models/models.zod"; +import { ListOrganizationsResponse } from "@/lib/api/generated/organizations/organizations.zod"; +import { personBody } from "./accounts-api"; + +/** + * Connect your AI, on an OpenBot that provides no AI of its own (OpenBot Cloud). + * + * In a real browser, through the app's own signed-in gate (`_authed`), its onboarding route and + * Settings → General: + * + * - A new person whose organization has no AI is held at Connect your AI: its button is disabled, + * there is no way past it, and the gate sends them back to it from anywhere in the app. A key the + * provider refuses says why; a good one connects, and the button takes them in. + * - A new person whose organization has an AI never sees the step. + * - Somebody who finished onboarding before the step existed is sent to it as their only step, and + * its button takes them in without finishing onboarding again. + * - Inside OpenBot Desktop the chooser still offers no plan, and Unbiased AI signs in through the + * server: the code, Open the sign-in page, then Connect with the key the server kept. + * - Settings shows Your AI: theirs, which can be removed, back to the organization's. + * + * The server is served as routes at the API's address, answering as api/openapi.yaml says: the + * spec's own examples for the person's AI connection, every body the page sends checked against the + * generated request schema, every answer against the response schema. + * + * OPENBOT_APP_BROWSER=1 bun test app/tests/connect-your-ai.test.tsx + * + * Chromium by default, as CI has it. With OPENBOT_APP_SHOTS= each screen is saved as + * connect-ai----.png, beside onboarding's Welcome and Settings' AI + * connection dialog from before. + */ +const asked = process.env.OPENBOT_APP_BROWSER === "1"; +const shots = process.env.OPENBOT_APP_SHOTS; +const engines = (process.env.OPENBOT_APP_ENGINES ?? "chromium") + .split(",") + .map((engine) => engine.trim()) + .filter((engine) => engine === "chromium" || engine === "webkit") as ( + | "chromium" + | "webkit" +)[]; + +const APP = join(import.meta.dir, ".."); +const ORIGIN = "http://openbot.test"; +const ORGANIZATION = "/api/v2/organizations/default"; +const ME = `${ORGANIZATION}/people/me`; +const PERSONAL = `${ME}/ai-connection`; +const SIGN_INS = `${PERSONAL}/sign-ins`; + +const WIDE = { width: 1440, height: 900 }; +const PHONE = { width: 390, height: 844 }; +const THEMES = ["light", "dark"] as const; +type Theme = (typeof THEMES)[number]; +type Window = { width: number; height: number }; + +type Example = { value: Record }; +const spec = parse( + readFileSync(join(APP, "..", "api", "openapi.yaml"), "utf8"), +) as { + components: { + examples: Record; + responses: Record< + string, + { + content: { + "application/problem+json": { examples: Record }; + }; + } + >; + }; +}; +/** One of the spec's examples, as the server would send it. */ +function example(name: string): Record { + const found = spec.components.examples[name]?.value; + if (!found) throw new Error(`the spec has no ${name} example`); + return structuredClone(found); +} +const REFUSED = + spec.components.responses.AiConnectionRefused?.content[ + "application/problem+json" + ].examples.keyRefused?.value; +if (!REFUSED) throw new Error("the spec has no keyRefused example"); + +const GOOD_KEY = "sk-ant-api03-good"; +const BAD_KEY = "sk-ant-api03-refused"; + +type Start = "none" | "organization"; + +/** The person's AI connection as the spec's example for `start` says it. */ +function personalFor(start: Start): PersonalAiConnection { + return GetPersonalAiConnectionResponse.parse( + example( + start === "none" + ? "PersonalAiConnectionNoneExample" + : "PersonalAiConnectionOrganizationExample", + ), + ) as PersonalAiConnection; +} + +/** + * The organization's own AI, as an admin reads it, agreeing with what the person's answer says of + * it: none on OpenBot Cloud until one is set. + */ +function organizationAi(personal: PersonalAiConnection) { + const organization: AiConnection = { + provider: personal.organization.provider, + login: personal.organization.provider ? "api-key" : null, + model: null, + endpointHost: null, + keySet: personal.organization.ready, + ready: personal.organization.ready, + source: personal.organization.ready ? "stored" : "none", + editable: true, + saved: [], + changedAt: null, + }; + return GetAiConnectionResponse.parse(organization); +} + +/** A second organization the person is in, beside `default`. */ +const ACME = "/api/v2/organizations/acme"; + +/** One organization, as the person's list of their own answers it. */ +function organizationRow(id: string, name: string, active: boolean) { + return { personal: false, id, slug: id, name, role: "member", active }; +} + +/** One OpenBot Cloud's server, as far as these screens ask it. */ +class FakeServer { + personal: PersonalAiConnection; + /** + * The person's answer in Acme, a second organization they are in, or null when they are in only + * `default` (the list of their organizations is then not served, as OpenBot with one answers). + */ + acme: PersonalAiConnection | null = null; + /** The organization the session works in. */ + active = "default"; + onboarding: { step: number; completedAt: string | null }; + replaces: unknown[] = []; + removes = 0; + completes = 0; + signIns: unknown[] = []; + signInReads = 0; + signOuts = 0; + + constructor(start: Start, onboarded = false) { + this.personal = personalFor(start); + this.onboarding = onboarded + ? { step: 1, completedAt: "2026-11-01T09:00:00.000Z" } + : { step: 0, completedAt: null }; + } + + answer(request: Request): { status: number; json: unknown } { + const url = new URL(request.url()); + const method = request.method(); + // Acme answers as `default` does, but for the person's AI there. + const inAcme = url.pathname.startsWith(`${ACME}/`) && this.acme !== null; + const path = inAcme + ? `${ORGANIZATION}${url.pathname.slice(ACME.length)}` + : url.pathname; + if (inAcme && this.acme && path === PERSONAL && method === "GET") + return { status: 200, json: this.acme }; + if (inAcme && this.acme && path === `${ORGANIZATION}/ai-connection`) + return { status: 200, json: organizationAi(this.acme) }; + if (url.pathname === "/api/v2/people/me/organizations" && this.acme) + return { + status: 200, + json: ListOrganizationsResponse.parse({ + organizations: [ + organizationRow("default", "Default", this.active === "default"), + organizationRow("acme", "Acme", this.active === "acme"), + ], + canCreate: false, + nextCursor: null, + }), + }; + if (url.pathname === "/api/auth/organization/set-active") { + this.active = ( + request.postDataJSON() as { organizationId: string } + ).organizationId; + return { status: 200, json: {} }; + } + if (url.pathname === "/api/auth/sign-out") { + this.signOuts += 1; + return { status: 200, json: { success: true } }; + } + if (path === PERSONAL && method === "GET") + return { status: 200, json: this.personal }; + if (path === PERSONAL && method === "PUT") { + expect(request.headers()["idempotency-key"]?.length ?? 0).toBeGreaterThan( + 0, + ); + const body = request.postDataJSON() as Record; + this.replaces.push(body); + const input = ReplacePersonalAiConnectionBody.parse(body); + for (const key of Object.keys(body)) + expect(Object.keys(input)).toContain(key); + if (input.apiKey === BAD_KEY) return { status: 422, json: REFUSED }; + const host = input.baseUrl ? new URL(input.baseUrl).host : null; + this.personal = GetPersonalAiConnectionResponse.parse({ + ...this.personal, + connection: { + provider: input.provider, + login: input.login, + model: input.model ?? null, + endpointHost: host, + keySet: true, + ready: true, + source: "stored", + editable: true, + saved: [ + { + provider: input.provider, + login: input.login, + endpointHost: host, + }, + ], + changedAt: "2026-11-21T10:00:00.000Z", + }, + uses: "personal", + }) as PersonalAiConnection; + return { status: 200, json: this.personal }; + } + if (path === PERSONAL && method === "DELETE") { + this.removes += 1; + const none = example("PersonalAiConnectionNoneExample") as { + connection: unknown; + }; + this.personal = GetPersonalAiConnectionResponse.parse({ + ...this.personal, + connection: none.connection, + uses: this.personal.organization.ready ? "organization" : "none", + }) as PersonalAiConnection; + return { status: 200, json: this.personal }; + } + if (path === SIGN_INS && method === "POST") { + const body = StartPersonalAiSignInBody.parse(request.postDataJSON()); + this.signIns.push(body); + // The spec's example, read again every second rather than five so the test is quick. + return { + status: 201, + json: StartPersonalAiSignInResponse.parse({ + ...example("AiSignInExample"), + interval: 1, + }), + }; + } + if (path.startsWith(`${SIGN_INS}/`) && method === "GET") { + this.signInReads += 1; + return { + status: 200, + json: GetPersonalAiSignInResponse.parse({ + ...example( + this.signInReads < 2 + ? "AiSignInPendingExample" + : "AiSignInApprovedExample", + ), + interval: 1, + }), + }; + } + if (path === ME && method === "GET") + return { + status: 200, + json: { + person: personBody({ + id: "dana", + name: "Dana Whitfield", + email: "dana@example.com", + role: "admin", + onboarding: this.onboarding, + }), + openbotVersion: "v1.1.3", + }, + }; + if (path === `${ME}/onboarding/advance` && method === "POST") { + const { step } = request.postDataJSON() as { step: number }; + this.onboarding = { ...this.onboarding, step }; + return { status: 200, json: { onboarding: this.onboarding } }; + } + if (path === `${ME}/onboarding/complete` && method === "POST") { + this.completes += 1; + this.onboarding = { + ...this.onboarding, + completedAt: "2026-11-21T10:05:00.000Z", + }; + return { status: 200, json: { onboarding: this.onboarding } }; + } + if (path === `${ORGANIZATION}/ai-connection` && method === "GET") + return { status: 200, json: organizationAi(this.personal) }; + if (path === `${ORGANIZATION}/deployment`) + return { + status: 200, + json: { + generativeUi: true, + selfHostBanner: false, + localEvaluation: false, + edition: "team", + authProviders: [], + ssoConfigured: false, + ssoRequired: false, + }, + }; + if (path === `${ME}/approval-preferences`) + return { + status: 200, + json: { + enabled: false, + autoReview: false, + hostCommands: "ask", + effectiveHostCommands: "ask", + }, + }; + if (path === `${ME}/preferences`) + return { + status: 200, + json: { + messageListEmphasis: "thread", + selfHostBannerDismissed: false, + narration: null, + skin: "openbot", + proactive: "on", + }, + }; + return { status: 404, json: { detail: "Not found." } }; + } +} + +/** The vendored marks as data URIs, as Vite's `import.meta.glob` hands them to `@/lib/marks`. */ +function marksModule(): string { + const dir = join(APP, "..", "shared", "marks"); + const marks = Object.fromEntries( + readdirSync(dir) + .filter((file) => file.endsWith(".svg")) + .map((file) => [ + file.replace(".svg", ""), + `data:image/svg+xml;base64,${readFileSync(join(dir, file)).toString("base64")}`, + ]), + ); + return `const marks = ${JSON.stringify(marks)}; export const markFor = (id) => (id && marks[id]) || null;`; +} + +async function bundle(): Promise { + const built = await Bun.build({ + entrypoints: [join(import.meta.dir, "connect-your-ai.fixture.tsx")], + target: "browser", + define: { "process.env.NODE_ENV": JSON.stringify("production") }, + // The gate's module brings the app behind it, whose chat loads a map library only when a Bot + // draws a map. Nothing here does. + external: ["maplibre-gl"], + plugins: [ + { + name: "marks", + setup(build) { + build.onResolve({ filter: /^@\/lib\/marks$/ }, () => ({ + path: "marks", + namespace: "marks", + })); + build.onLoad({ filter: /.*/, namespace: "marks" }, () => ({ + contents: marksModule(), + loader: "js", + })); + }, + }, + ], + }); + if (!built.success) throw new AggregateError(built.logs, "bundle failed"); + const script = built.outputs.find((output) => output.kind === "entry-point"); + if (!script) throw new Error("bundle has no entry point"); + return script.text(); +} + +/** The directory of an installed package, from the app or the repository root. */ +function packageDir(name: string): string { + const dir = [ + join(APP, "node_modules", name), + join(APP, "..", "node_modules", name), + ].find(existsSync); + if (!dir) throw new Error(`no package ${name}`); + return dir; +} + +/** Where `@import "[/]"` lands, by the package's `style` export as Vite reads it. */ +function packageStylesheet(id: string): string { + const parts = id.split("/"); + const name = id.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0]; + const dir = packageDir(name); + const pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); + const subpath = `.${id.slice(name.length)}`; + const entry = pkg.exports?.[subpath]; + const file = + typeof entry === "string" + ? entry + : (entry?.style ?? entry?.default ?? pkg.main ?? "index.css"); + return join(dir, file); +} + +/** The app's stylesheet, built by Tailwind for these classes, as the app's build does. */ +async function stylesheet(classes: string[]): Promise { + const tailwind = dirname(Bun.resolveSync("tailwindcss/package.json", APP)); + const entry = join(APP, "src", "styles.css"); + const compiler = await compile(readFileSync(entry, "utf8"), { + base: dirname(entry), + loadStylesheet: async (id, base) => { + const path = + id === "tailwindcss" + ? join(tailwind, "index.css") + : id.startsWith(".") + ? join(base, id) + : packageStylesheet(id); + return { path, base: dirname(path), content: readFileSync(path, "utf8") }; + }, + }); + return compiler.build(classes); +} + +const browsers = new Map(); +let script: string; + +beforeAll(async () => { + if (!asked) return; + script = await bundle(); + const playwright = await import("playwright-core"); + for (const engine of engines) { + const type: BrowserType = playwright[engine]; + browsers.set( + engine, + // The CI job installs full Chromium without the headless shell. + await type.launch(engine === "chromium" ? { channel: "chromium" } : {}), + ); + } +}, 120_000); + +afterAll(async () => { + for (const browser of browsers.values()) await browser.close(); +}); + +/** The page at `path`, its server answering from `server`; `desktop` puts it in a Desktop window. */ +async function open( + path: string, + server: FakeServer, + engine: string, + theme: Theme, + window: Window, + desktop = false, +): Promise { + const browser = browsers.get(engine); + if (!browser) throw new Error(`no ${engine}`); + const page = await browser.newPage({ + viewport: window, + colorScheme: theme, + reducedMotion: "reduce", + }); + page.on("pageerror", (error) => console.error(error)); + if (desktop) + // OpenBot Desktop's window, as the page sees it: Tauri's IPC, answering nothing it is asked. + await page.addInitScript(() => { + (window as unknown as Record).__TAURI_INTERNALS__ = { + transformCallback: () => 1, + invoke: async () => null, + }; + }); + const inter = join(packageDir("@fontsource-variable/inter"), "files"); + await page.route(`${ORIGIN}/**`, (route) => { + const where = new URL(route.request().url()).pathname; + if (where === "/screens.js") + return route.fulfill({ contentType: "text/javascript", body: script }); + const font = join(inter, where.split("/").pop() ?? ""); + if (where.startsWith("/files/") && existsSync(font)) + return route.fulfill({ path: font }); + if (where.startsWith("/api/")) { + const { status, json } = server.answer(route.request()); + return route.fulfill({ + status, + json, + headers: + status >= 400 ? { "content-type": "application/problem+json" } : {}, + }); + } + return route.fulfill({ + contentType: "text/html", + body: `
`, + }); + }); + await page.goto(`${ORIGIN}${path}`); + return page; +} + +/** Style the page for every class it draws now: what just opened brings classes of its own. */ +async function style(page: Page) { + const classes = await page.evaluate(() => + [...document.querySelectorAll("[class]")].flatMap((element) => [ + ...element.classList, + ]), + ); + await page.addStyleTag({ content: await stylesheet([...new Set(classes)]) }); + await page.evaluate(() => document.fonts.ready); + await page.waitForTimeout(250); +} + +async function shoot( + page: Page, + screen: string, + engine: string, + theme: Theme, + window: Window, +) { + if (!shots) return; + await style(page); + mkdirSync(shots, { recursive: true }); + await page.screenshot({ + path: join( + shots, + `connect-ai-${screen}-${engine}-${theme}-${window.width}.png`, + ), + fullPage: true, + }); +} + +/** What a person would see wrong: sideways scroll, or a control cut off at the side. */ +async function measured(page: Page) { + await style(page); + return page.evaluate(() => { + const inside = (rect: DOMRect) => + rect.left >= -0.5 && rect.right <= innerWidth + 0.5; + const cut = [...document.querySelectorAll("button, a, input")] + .filter((control) => { + const rect = control.getBoundingClientRect(); + // A switch's or a radio's own input is a 1px box out of sight; its control is what shows. + return rect.width > 1 && rect.height > 1; + }) + .filter((control) => !control.matches("[data-sidebar=rail]")) + .filter((control) => !inside(control.getBoundingClientRect())) + .map( + (control) => + control.textContent?.trim() || + control.getAttribute("aria-label") || + control.tagName, + ); + return { sideways: document.documentElement.scrollWidth > innerWidth, cut }; + }); +} + +const FINE = { sideways: false, cut: [] }; +const primary = (page: Page) => page.locator("[data-onboarding-primary]"); +const heading = (page: Page, name: string) => + page.getByRole("heading", { level: 1, name }).waitFor(); + +describe.skipIf(!asked)("Connect your AI on OpenBot Cloud", () => { + for (const engine of engines) + test(`a new person is held at Connect your AI until they connect, then goes in, in ${engine}`, async () => { + const server = new FakeServer("none"); + const page = await open("/", server, engine, "light", WIDE); + try { + // The gate sends a new person to onboarding; Welcome first, then Connect your AI. + await heading(page, "Welcome to OpenBot"); + await page.waitForFunction( + () => + !document + .querySelector("[data-onboarding-primary]") + ?.hasAttribute("disabled"), + ); + await primary(page).click(); + await heading(page, "Connect your AI"); + expect(new URL(page.url()).searchParams.get("step")).toBe("ai"); + expect(await primary(page).isDisabled()).toBe(true); + // No way past it. + expect(await page.locator("[data-onboarding-secondary]").count()).toBe( + 0, + ); + + // Anywhere else in the app sends them back to it. + await page.goto(`${ORIGIN}/settings`); + await heading(page, "Connect your AI"); + expect(new URL(page.url()).pathname).toBe("/onboarding"); + + const chooser = page.locator("[data-ai-chooser]"); + // A key for every provider, and no plan: the server runs this person's Bots. + await chooser.getByRole("button", { name: /^OpenAI/ }).click(); + await chooser.getByLabel("OpenAI API key").waitFor(); + expect(await chooser.getByText("Sign in with my plan").count()).toBe(0); + await chooser.getByRole("button", { name: "Back" }).click(); + + await chooser.getByRole("button", { name: /^Anthropic/ }).click(); + const key = chooser.getByLabel("Anthropic API key"); + await key.fill(BAD_KEY); + await chooser.getByRole("button", { name: "Connect" }).click(); + const alert = chooser.getByRole("alert"); + await alert.waitFor(); + expect(await alert.textContent()).toBe( + "Anthropic didn't accept that key. Check it and paste it again.", + ); + expect(server.personal.uses).toBe("none"); + expect(await primary(page).isDisabled()).toBe(true); + + await key.fill(GOOD_KEY); + await chooser.getByRole("button", { name: "Connect" }).click(); + await chooser.getByRole("status").waitFor(); + expect(await chooser.textContent()).not.toContain(GOOD_KEY); + expect(server.replaces).toEqual([ + { provider: "anthropic", login: "api-key", apiKey: BAD_KEY }, + { provider: "anthropic", login: "api-key", apiKey: GOOD_KEY }, + ]); + expect(await measured(page)).toEqual(FINE); + + // Connected: the step's button is the way in, and it finishes onboarding. + expect(await primary(page).textContent()).toContain("Get started"); + await primary(page).click(); + await page.locator("[data-app-home]").waitFor(); + expect(server.completes).toBe(1); + expect(new URL(page.url()).pathname).toBe("/"); + } finally { + await page.close(); + } + }, 90_000); + + for (const engine of engines) + test(`a person whose organization has an AI goes straight in, in ${engine}`, async () => { + const server = new FakeServer("organization"); + const page = await open("/", server, engine, "light", WIDE); + try { + await heading(page, "Welcome to OpenBot"); + // One step: no "Step 1 of 2", no Connect your AI. + expect(await page.locator("[data-onboarding-steps]").count()).toBe(0); + await page.waitForFunction( + () => + !document + .querySelector("[data-onboarding-primary]") + ?.hasAttribute("disabled"), + ); + await primary(page).click(); + await page.locator("[data-app-home]").waitFor(); + // And once in, nothing sends them back. + await page.goto(`${ORIGIN}/settings`); + await page.getByRole("heading", { name: "Your AI" }).waitFor(); + await page + .getByRole("button", { name: /Your organization's AI/ }) + .waitFor(); + expect(new URL(page.url()).pathname).toBe("/settings"); + } finally { + await page.close(); + } + }, 60_000); + + for (const engine of engines) + test(`somebody who finished onboarding before has the step alone, in ${engine}`, async () => { + const server = new FakeServer("none", true); + const page = await open("/settings", server, engine, "light", WIDE); + try { + await heading(page, "Connect your AI"); + expect(await page.locator("[data-onboarding-steps]").count()).toBe(0); + const chooser = page.locator("[data-ai-chooser]"); + await chooser.getByRole("button", { name: /^Anthropic/ }).click(); + await chooser.getByLabel("Anthropic API key").fill(GOOD_KEY); + await chooser.getByRole("button", { name: "Connect" }).click(); + await chooser.getByRole("status").waitFor(); + await primary(page).click(); + await page.locator("[data-app-home]").waitFor(); + // Already finished: not finished again, and no place in it saved. + expect(server.completes).toBe(0); + expect(server.onboarding.step).toBe(1); + } finally { + await page.close(); + } + }, 60_000); + + for (const engine of engines) + test(`inside OpenBot Desktop: no plan, and Unbiased AI signs in through the server, in ${engine}`, async () => { + const server = new FakeServer("none", true); + const page = await open( + "/onboarding?step=ai", + server, + engine, + "light", + WIDE, + true, + ); + try { + await heading(page, "Connect your AI"); + const chooser = page.locator("[data-ai-chooser]"); + await chooser.getByRole("button", { name: /^OpenAI/ }).click(); + await chooser.getByLabel("OpenAI API key").waitFor(); + expect(await chooser.getByRole("tab").count()).toBe(0); + expect(await chooser.getByText("Sign in with my plan").count()).toBe(0); + await chooser.getByRole("button", { name: "Back" }).click(); + + await chooser.getByRole("button", { name: /^Unbiased AI/ }).click(); + // Sign-in first, a key as the alternative. + expect(await chooser.getByRole("tab").allTextContents()).toEqual([ + "Sign in", + "Use an API key", + ]); + await chooser + .getByRole("button", { + name: "Sign in with Unbiased AI", + exact: true, + }) + .click(); + await chooser.getByText("BCDF-GHJK").waitFor(); + await chooser + .getByRole("link", { name: "Open the sign-in page" }) + .waitFor(); + await shoot(page, "sign-in-waiting", engine, "light", WIDE); + await chooser.getByText("Signed in to Unbiased AI.").waitFor(); + expect(server.signIns).toEqual([{ provider: "unbiased" }]); + await chooser.getByRole("button", { name: "Connect" }).click(); + await chooser.getByRole("status").waitFor(); + expect(server.replaces).toEqual([ + { + provider: "unbiased", + login: "endpoint", + baseUrl: "https://api.unbiased.ai/v1", + model: "pareto", + useSavedKey: true, + }, + ]); + expect(await primary(page).isDisabled()).toBe(false); + } finally { + await page.close(); + } + }, 60_000); + + for (const engine of engines) + test(`Settings shows Your AI, and removing it goes back to the organization's, in ${engine}`, async () => { + const server = new FakeServer("organization", true); + server.personal = GetPersonalAiConnectionResponse.parse( + example("PersonalAiConnectionExample"), + ) as PersonalAiConnection; + const page = await open("/settings", server, engine, "light", WIDE); + try { + await page.getByRole("heading", { name: "Your AI" }).waitFor(); + // The admin's section for the organization's AI says it is the organization's. + await page + .getByRole("heading", { name: "Organization's AI" }) + .waitFor(); + await page + .getByRole("button", { + name: /Anthropic.*Uses your Anthropic API key\./, + }) + .click(); + const dialog = page.getByRole("dialog"); + await dialog.getByText("Your AI", { exact: true }).waitFor(); + await style(page); + await dialog.getByRole("button", { name: "Remove" }).click(); + await page + .getByRole("button", { name: /Your organization's AI/ }) + .waitFor(); + expect(server.removes).toBe(1); + } finally { + await page.close(); + } + }, 60_000); + + for (const engine of engines) + test(`switching into an organization with no AI sends them to the step at once, in ${engine}`, async () => { + // Default has an AI for everyone; Acme has none, and the person has none of their own. + const server = new FakeServer("organization", true); + server.acme = personalFor("none"); + const page = await open("/", server, engine, "light", WIDE); + try { + await page.locator("[data-app-home]").waitFor(); + await page.getByRole("button", { name: "Work in Acme" }).click(); + // No second click: the gate decides again in Acme as soon as the app works there. + await heading(page, "Connect your AI"); + expect(server.active).toBe("acme"); + expect(new URL(page.url()).searchParams.get("step")).toBe("ai"); + expect(await page.locator("[data-app-home]").count()).toBe(0); + } finally { + await page.close(); + } + }, 60_000); + + for (const engine of engines) + test(`the step lets them work in another organization of theirs, or sign out, in ${engine}`, async () => { + // Default has no AI and neither do they; Acme has one for everyone. + const server = new FakeServer("none", true); + server.acme = personalFor("organization"); + const page = await open("/", server, engine, "light", WIDE); + try { + await heading(page, "Connect your AI"); + // Styled: the way out is drawn beside the words on a desktop and under the chooser on a + // phone, and the stylesheet is what shows one of them. + await page.getByText("Signed in as dana@example.com").first().waitFor(); + await style(page); + await page + .getByRole("heading", { name: "Or work in another organization" }) + .waitFor(); + // Only the others: Default is where they are. + const others = page.getByRole("group", { + name: "Or work in another organization", + }); + expect( + await others.locator("[data-slot=item-title]").allTextContents(), + ).toEqual(["Acme"]); + await others.getByRole("button", { name: /Acme/ }).click(); + await page.locator("[data-app-home]").waitFor(); + expect(server.active).toBe("acme"); + } finally { + await page.close(); + } + + // In one organization there is no list, and Sign out is always there. + const alone = new FakeServer("none", true); + const single = await open("/", alone, engine, "light", WIDE); + try { + await heading(single, "Connect your AI"); + await single + .getByText("Signed in as dana@example.com") + .first() + .waitFor(); + await style(single); + // One of the two is shown: beside the words here, at a desktop's width. + expect( + await single + .getByText("Signed in as dana@example.com") + .filter({ visible: true }) + .count(), + ).toBe(1); + expect( + await single + .getByRole("heading", { name: "Or work in another organization" }) + .count(), + ).toBe(0); + await single.getByRole("button", { name: "Sign out" }).click(); + await single.locator("[data-sign-in]").waitFor(); + expect(alone.signOuts).toBe(1); + } finally { + await single.close(); + } + }, 90_000); + + for (const engine of engines) + for (const theme of THEMES) + test(`at a phone's width, scrolled to the end, Sign out clears the buttons' fade, in ${engine}, ${theme}`, async () => { + const server = new FakeServer("none", true); + server.acme = personalFor("organization"); + const page = await open("/", server, engine, theme, PHONE); + try { + await heading(page, "Connect your AI"); + await page.getByText("Acme").first().waitFor(); + await style(page); + await page.evaluate(() => + window.scrollTo(0, document.documentElement.scrollHeight), + ); + await page.waitForTimeout(100); + const room = await page.evaluate(() => { + const footer = document.querySelector("footer"); + const signOut = [...document.querySelectorAll("button")].find( + (button) => + button.textContent?.trim() === "Sign out" && + button.getBoundingClientRect().height > 0, + ); + if (!footer || !signOut) return null; + // The fade is drawn 32px above the footer (`before:-top-8 before:h-8`). + return ( + footer.getBoundingClientRect().top - + 32 - + signOut.getBoundingClientRect().bottom + ); + }); + expect(room).not.toBeNull(); + expect(room ?? -1).toBeGreaterThanOrEqual(0); + if (shots) { + mkdirSync(shots, { recursive: true }); + await page.screenshot({ + path: join( + shots, + `connect-ai-step-bottom-${engine}-${theme}-${PHONE.width}.png`, + ), + }); + } + } finally { + await page.close(); + } + }, 60_000); + + for (const engine of engines) + test(`on Cloud the organization's AI is said to be the organization's, in ${engine}`, async () => { + const server = new FakeServer("organization", true); + server.personal = GetPersonalAiConnectionResponse.parse( + example("PersonalAiConnectionExample"), + ) as PersonalAiConnection; + const page = await open("/settings", server, engine, "light", WIDE); + try { + // Its key is the organization's, not the admin's own. + await page + .getByRole("button", { + name: /OpenAI.*Uses your organization's OpenAI API key\./, + }) + .waitFor(); + } finally { + await page.close(); + } + + // With none set yet, the dialog sets one up rather than changing it. + const unset = new FakeServer("organization", true); + unset.personal = GetPersonalAiConnectionResponse.parse({ + ...example("PersonalAiConnectionExample"), + organization: { ready: false, provider: null }, + }) as PersonalAiConnection; + const second = await open("/settings", unset, engine, "light", WIDE); + try { + await second + .getByRole("button", { + name: /Not chosen yet.*Choose an AI for everyone in your organization\./, + }) + .click(); + const dialog = second.getByRole("dialog"); + await dialog + .getByText("Set up your organization's AI", { exact: true }) + .waitFor(); + expect(await dialog.getByText("Change AI connection").count()).toBe(0); + } finally { + await second.close(); + } + }, 60_000); + + // Every screen this adds, light and dark, wide and at a phone's width, beside the screens from + // before they should match: onboarding's Welcome and Settings' AI connection dialog. + test.skipIf(!shots)( + "screenshots of the step and Settings, beside Welcome and the AI connection dialog", + async () => { + for (const engine of engines) + for (const theme of THEMES) + for (const window of [WIDE, PHONE]) { + const fresh = new FakeServer("none"); + const page = await open("/", fresh, engine, theme, window); + try { + await heading(page, "Welcome to OpenBot"); + await shoot(page, "compare-welcome", engine, theme, window); + await page.waitForFunction( + () => + !document + .querySelector("[data-onboarding-primary]") + ?.hasAttribute("disabled"), + ); + await primary(page).click(); + await heading(page, "Connect your AI"); + expect(await measured(page)).toEqual(FINE); + await shoot(page, "step", engine, theme, window); + const chooser = page.locator("[data-ai-chooser]"); + await chooser + .getByRole("button", { name: /^Unbiased AI/ }) + .click(); + expect(await measured(page)).toEqual(FINE); + await shoot(page, "step-unbiased", engine, theme, window); + await chooser + .getByRole("tab", { name: "Use an API key" }) + .click(); + await chooser.getByLabel("Unbiased AI API key").fill("ub-key"); + await chooser.getByRole("button", { name: "Connect" }).click(); + await chooser.getByRole("status").waitFor(); + expect(await measured(page)).toEqual(FINE); + await shoot(page, "step-connected", engine, theme, window); + } finally { + await page.close(); + } + + const own = new FakeServer("organization", true); + own.personal = GetPersonalAiConnectionResponse.parse( + example("PersonalAiConnectionExample"), + ) as PersonalAiConnection; + const settings = await open( + "/settings", + own, + engine, + theme, + window, + ); + try { + await settings + .getByRole("heading", { name: "Your AI" }) + .waitFor(); + await settings + .getByRole("heading", { name: "Organization's AI" }) + .waitFor(); + expect(await measured(settings)).toEqual(FINE); + await shoot(settings, "settings", engine, theme, window); + await settings + .getByRole("button", { name: /Anthropic.*Uses your Anthropic/ }) + .click(); + await settings.getByRole("dialog").waitFor(); + await shoot(settings, "settings-dialog", engine, theme, window); + await settings + .getByRole("dialog") + .getByRole("button", { name: "Change", exact: true }) + .click(); + await settings + .getByRole("dialog") + .getByRole("button", { name: /^Unbiased AI/ }) + .waitFor(); + await shoot(settings, "settings-chooser", engine, theme, window); + await settings.keyboard.press("Escape"); + await settings.getByRole("dialog").waitFor({ state: "detached" }); + // The organization's AI connection dialog from before, to compare. + await settings + .getByRole("button", { name: /Not chosen yet|OpenAI/ }) + .first() + .click(); + await settings.getByRole("dialog").waitFor(); + await shoot( + settings, + "compare-org-dialog", + engine, + theme, + window, + ); + } finally { + await settings.close(); + } + + // The step for somebody in two organizations: the other one to work in instead. + const two = new FakeServer("none", true); + two.acme = personalFor("organization"); + const both = await open("/", two, engine, theme, window); + try { + await heading(both, "Connect your AI"); + await both + .getByRole("heading", { + name: "Or work in another organization", + }) + .first() + .waitFor(); + expect(await measured(both)).toEqual(FINE); + await shoot( + both, + "step-other-organizations", + engine, + theme, + window, + ); + } finally { + await both.close(); + } + + // The organization's AI with none set yet: the dialog sets one up. + const unset = new FakeServer("organization", true); + unset.personal = GetPersonalAiConnectionResponse.parse({ + ...example("PersonalAiConnectionExample"), + organization: { ready: false, provider: null }, + }) as PersonalAiConnection; + const setup = await open("/settings", unset, engine, theme, window); + try { + await setup + .getByRole("button", { name: /Not chosen yet/ }) + .click(); + await setup.getByRole("dialog").waitFor(); + await shoot(setup, "org-setup-dialog", engine, theme, window); + } finally { + await setup.close(); + } + } + }, + 600_000, + ); +});