Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

One way to start switch-buddy, for Meet switch-buddy and the provider card

Simon Flackb74264fee4fa
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/meet-buddy-page.tsx b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/meet-buddy-page.tsx
index fa48d6a73..f83639037 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/meet-buddy-page.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/meet-buddy-page.tsx
@@ -4,7 +4,6 @@ import { useEffect, useMemo, useRef, useState } from 'react';
import { useManagedAgents } from '@renderer/features/managed-agents/use-managed-agents';
import { onboardingStore } from '@renderer/features/onboarding/onboarding-store';
import { AgentIcon } from '@renderer/lib/components/agent-icon';
-import { failureText } from '@renderer/lib/errors/describe-failure';
import { report } from '@renderer/lib/telemetry/report';
import { Button } from '@renderer/lib/ui/button';
import { Spinner } from '@renderer/lib/ui/spinner';
@@ -15,9 +14,9 @@ import { findBuddy, firstNameOf } from './buddy-identity';
import { buddyPerson } from './buddy-person';
import { buddyProviders, startButtons } from './buddy-providers';
import { reportBuddyStep } from './buddy-telemetry';
-import { createBuddy } from './create-buddy';
import { MeetBuddyIntro } from './meet-buddy-intro';
import { ProviderSetup } from './provider-setup';
+import { useStartBuddy } from './use-start-buddy';
import { useThisComputer } from './use-this-computer';
/**
@@ -87,10 +86,14 @@ export const MeetBuddyPage = observer(function MeetBuddyPage({
[firstDecided, providers]
);
const existing = findBuddy(useManagedAgents(serverId).data);
- const [starting, setStarting] = useState<string | null>(null);
- const [error, setError] = useState<string | null>(null);
- const person = buddyPerson(serverId);
- const firstName = firstNameOf(person.name);
+ const { starting, error, start } = useStartBuddy({
+ serverId,
+ workspaceId,
+ computer,
+ entryPoint: 'onboarding',
+ onCreated,
+ });
+ const firstName = firstNameOf(buddyPerson(serverId).name);
// The opening plays once: back from setting up a provider, it is at rest.
const introPlayed = useRef(false);
@@ -105,48 +108,6 @@ export const MeetBuddyPage = observer(function MeetBuddyPage({
setSettingUp(true);
};
- // A provider chosen before this computer's controller is up: started once it is.
- const [waitingToStart, setWaitingToStart] = useState<string | null>(null);
- useEffect(() => {
- if (waitingToStart === null) return;
- if (computer.kind === 'failed') {
- setWaitingToStart(null);
- setStarting(null);
- return;
- }
- if (computer.kind !== 'ready') return;
- const providerId = waitingToStart;
- setWaitingToStart(null);
- void startWithRef.current(providerId);
- }, [computer, waitingToStart]);
-
- const startWith = async (providerId: string) => {
- setStarting(providerId);
- if (computer.kind !== 'ready') {
- setWaitingToStart(providerId);
- return;
- }
- reportBuddyStep('start', providerId);
- setError(null);
- try {
- onCreated(
- await createBuddy({
- serverId,
- workspaceId,
- machineId: computer.machine.id,
- provider: providerId,
- person,
- entryPoint: 'onboarding',
- })
- );
- } catch (cause) {
- setError(failureText(cause, 'Could not create switch-buddy.'));
- setStarting(null);
- }
- };
- const startWithRef = useRef(startWith);
- startWithRef.current = startWith;
-
const header = (
<div className="flex flex-col items-center gap-2 text-center">
<BuddyAvatar size={48} shape="tile" />
@@ -201,7 +162,7 @@ export const MeetBuddyPage = observer(function MeetBuddyPage({
checking={computer.kind !== 'failed' && !checked}
ready={ready}
starting={starting}
- onStart={(providerId) => void startWith(providerId)}
+ onStart={start}
onSetUp={openSetup}
onSkip={skip}
/>
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/provider-setup.tsx b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/provider-setup.tsx
index d4272e8cd..2f45e89b5 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/provider-setup.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/provider-setup.tsx
@@ -14,11 +14,10 @@ import { agentProviders, requireProvider } from '@shared/core/providers/agent-pr
import { linksIn } from '@shared/core/providers/provider-login';
import type { UiEntryPoint } from '@shared/core/telemetry/reporting';
import { providerLoginExitChannel, providerLoginLineChannel } from '@shared/events/appEvents';
-import { buddyPerson } from './buddy-person';
import { type BuddyProvider, buddyProviders } from './buddy-providers';
import { reportBuddyStep } from './buddy-telemetry';
-import { createBuddy } from './create-buddy';
import { installOutputSummary } from './install-output-summary';
+import { useStartBuddy } from './use-start-buddy';
import { type ThisComputer, useThisComputer } from './use-this-computer';
/** How many lines of a sign-in command's output are kept on screen. */
@@ -66,15 +65,28 @@ export const ProviderSetup = observer(function ProviderSetup({
providers.find((p) => p.state === 'sign-in') ??
null;
- const [busy, setBusy] = useState<'install' | 'check' | 'create' | null>(null);
- const [error, setError] = useState<string | null>(null);
+ const [working, setWorking] = useState<'install' | 'check' | null>(null);
+ const [actionError, setActionError] = useState<string | null>(null);
+ const started = useStartBuddy({ serverId, workspaceId, computer, entryPoint, onCreated });
+ const busy = working ?? (started.starting !== null ? 'create' : null);
+ // One error at a time, as each action clears what the last one said.
+ const error = actionError ?? started.error;
+ const { clearError } = started;
+ const setError = useCallback(
+ (message: string | null) => {
+ setActionError(message);
+ clearError();
+ },
+ [clearError]
+ );
+
const [login, setLogin] = useState<Login | null>(null);
/** What the last Check again found, when it found nothing new: said, not left blank. */
const [checked, setChecked] = useState<string | null>(null);
const checkAgain = useCallback(async () => {
reportBuddyStep('check_again', null);
- setBusy('check');
+ setWorking('check');
setError(null);
setChecked(null);
try {
@@ -82,9 +94,9 @@ export const ProviderSetup = observer(function ProviderSetup({
} catch (cause) {
setError(failureText(cause, 'Could not check this computer again.'));
} finally {
- setBusy(null);
+ setWorking(null);
}
- }, [recheck]);
+ }, [recheck, setError]);
useEffect(() => {
const offLine = events.on(providerLoginLineChannel, ({ attemptId, line }) =>
@@ -124,7 +136,7 @@ export const ProviderSetup = observer(function ProviderSetup({
const install = async (provider: BuddyProvider) => {
reportBuddyStep('install', provider.id);
- setBusy('install');
+ setWorking('install');
setError(null);
try {
const result = await rpc.providers.install(provider.id);
@@ -133,30 +145,14 @@ export const ProviderSetup = observer(function ProviderSetup({
} catch (cause) {
setError(failureText(cause, `Could not install ${provider.name}.`));
} finally {
- setBusy(null);
+ setWorking(null);
}
};
- const start = async (provider: BuddyProvider) => {
+ const start = (provider: BuddyProvider) => {
if (computer.kind !== 'ready') return;
- reportBuddyStep('start', provider.id);
- setBusy('create');
- setError(null);
- try {
- onCreated(
- await createBuddy({
- serverId,
- workspaceId,
- machineId: computer.machine.id,
- provider: provider.id,
- person: buddyPerson(serverId),
- entryPoint,
- })
- );
- } catch (cause) {
- setError(failureText(cause, 'Could not create switch-buddy.'));
- setBusy(null);
- }
+ setActionError(null);
+ started.start(provider.id);
};
return (
@@ -181,7 +177,7 @@ export const ProviderSetup = observer(function ProviderSetup({
provider={selected}
login={login?.providerId === selected.id ? login : null}
busy={busy}
- onStart={() => void start(selected)}
+ onStart={() => start(selected)}
onSignIn={() => void signIn(selected)}
onCancelSignIn={() => {
void rpc.providers.cancelLogin();
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/use-start-buddy.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/use-start-buddy.ts
new file mode 100644
index 000000000..df950bd1b
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/use-start-buddy.ts
@@ -0,0 +1,79 @@
+import { useCallback, useEffect, useRef, useState } from 'react';
+import { failureText } from '@renderer/lib/errors/describe-failure';
+import type { UiEntryPoint } from '@shared/core/telemetry/reporting';
+import { buddyPerson } from './buddy-person';
+import { reportBuddyStep } from './buddy-telemetry';
+import { createBuddy } from './create-buddy';
+import type { ThisComputer } from './use-this-computer';
+
+/**
+ * Starting switch-buddy with a provider: the one way Meet switch-buddy's
+ * buttons and the provider card both do it.
+ *
+ * `starting` is the provider being started, from the click until the buddy
+ * exists (or the start fails, when it is null again and `error` says why). A
+ * provider chosen while this computer is still getting ready is started once
+ * it is, and dropped if setting it up fails.
+ */
+export function useStartBuddy({
+ serverId,
+ workspaceId,
+ computer,
+ entryPoint,
+ onCreated,
+}: {
+ serverId: string;
+ workspaceId: string;
+ computer: ThisComputer;
+ /** Where the person set the buddy up: the first run, or the buddy's chat later. */
+ entryPoint: UiEntryPoint;
+ onCreated: (agent: { agentId: string; name: string }) => void;
+}) {
+ const [starting, setStarting] = useState<string | null>(null);
+ const [error, setError] = useState<string | null>(null);
+ const [waitingToStart, setWaitingToStart] = useState<string | null>(null);
+
+ const start = async (providerId: string) => {
+ setStarting(providerId);
+ if (computer.kind !== 'ready') {
+ setWaitingToStart(providerId);
+ return;
+ }
+ reportBuddyStep('start', providerId);
+ setError(null);
+ try {
+ onCreated(
+ await createBuddy({
+ serverId,
+ workspaceId,
+ machineId: computer.machine.id,
+ provider: providerId,
+ person: buddyPerson(serverId),
+ entryPoint,
+ })
+ );
+ } catch (cause) {
+ setError(failureText(cause, 'Could not create switch-buddy.'));
+ setStarting(null);
+ }
+ };
+ // The effect below starts with the latest closure, which reads the computer as it is now.
+ const startRef = useRef(start);
+ startRef.current = start;
+
+ useEffect(() => {
+ if (waitingToStart === null) return;
+ if (computer.kind === 'failed') {
+ setWaitingToStart(null);
+ setStarting(null);
+ return;
+ }
+ if (computer.kind !== 'ready') return;
+ const providerId = waitingToStart;
+ setWaitingToStart(null);
+ void startRef.current(providerId);
+ }, [computer, waitingToStart]);
+
+ const clearError = useCallback(() => setError(null), []);
+ return { starting, error, start: (providerId: string) => void start(providerId), clearError };
+}