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.
+
+
+
+ >
+ ) : 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 (
+
+ );
+}
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
+ );
+ 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 (
+
+ );
+}
+
+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 (
+
+ 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."}
- 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."}
+
+
+
+
+
+
+
+ >
+ );
+}
+
+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 (
+
+