Skip to content

An open-source, faster Devin alternative

TypeScript

dashboard: a connect flow for Twilio, since installing an app is not one

Brian Reardonbd9e0e583cc4
diff --git a/web/src/managed-agents/Detail.tsx b/web/src/managed-agents/Detail.tsx
index 5d6a39a4..9238c343 100644
--- a/web/src/managed-agents/Detail.tsx
+++ b/web/src/managed-agents/Detail.tsx
@@ -72,6 +72,7 @@ import {
} from './session-history'
import { ManagedProjectSecrets } from './Secrets'
import { ManagedSlackWizard } from './SlackWizard'
+import { ManagedTwilioWizard } from './TwilioWizard'
import { ManagedAgentOutboxes } from './Outboxes'
import { ManagedAgentSchedules } from './Schedules'
import { ManagedAgentWebhooks } from './Webhooks'
@@ -1064,22 +1065,38 @@ export default function ManagedAgentDetail({
</PanelHeader>
{agent && activeDeployment.data ? (
declaredChannels.length ? (
- declaredChannels.map((declaredChannel) => (
- <ManagedSlackWizard
- key={declaredChannel.id}
- agentId={agent.id}
- alias={activeDeployment.data.alias}
- agentName={displayManagedAgentName(agent)}
- channelName={
- declaredChannel.displayName ?? declaredChannel.id
- }
- connection={activeAliasChannels.find(
- (channel) => channel.channelId === declaredChannel.id,
- )}
- channelId={declaredChannel.id}
- destinations={Object.keys(declaredChannel.destinations)}
- />
- ))
+ // The wizard follows the channel's declared provider: Slack has
+ // an app to install, Twilio has credentials to paste and a
+ // number to point here. Nothing about connecting one resembles
+ // connecting the other.
+ declaredChannels.map((declaredChannel) =>
+ declaredChannel.type === 'twilio' ? (
+ <ManagedTwilioWizard
+ key={declaredChannel.id}
+ agentId={agent.id}
+ alias={activeDeployment.data.alias}
+ connection={activeAliasChannels.find(
+ (channel) => channel.channelId === declaredChannel.id,
+ )}
+ channelId={declaredChannel.id}
+ />
+ ) : (
+ <ManagedSlackWizard
+ key={declaredChannel.id}
+ agentId={agent.id}
+ alias={activeDeployment.data.alias}
+ agentName={displayManagedAgentName(agent)}
+ channelName={
+ declaredChannel.displayName ?? declaredChannel.id
+ }
+ connection={activeAliasChannels.find(
+ (channel) => channel.channelId === declaredChannel.id,
+ )}
+ channelId={declaredChannel.id}
+ destinations={Object.keys(declaredChannel.destinations)}
+ />
+ ),
+ )
) : (
<ManagedSlackWizard
agentId={agent.id}
diff --git a/web/src/managed-agents/TwilioWizard.test.ts b/web/src/managed-agents/TwilioWizard.test.ts
new file mode 100644
index 00000000..ae876895
--- /dev/null
+++ b/web/src/managed-agents/TwilioWizard.test.ts
@@ -0,0 +1,50 @@
+import { describe, expect, it } from 'vitest'
+import { z } from 'zod'
+
+/**
+ * The declared-channel schema is what decides which wizard a customer sees.
+ * Slack has an app to install; Twilio has credentials to paste and a number to
+ * point here. Getting the discriminator wrong shows the wrong form entirely.
+ */
+const declaredChannel = z.object({
+ id: z.string(),
+ type: z.enum(['slack', 'twilio', 'email']),
+ displayName: z.string().optional(),
+ destinations: z.record(
+ z.string(),
+ z.object({
+ type: z.enum(['conversation', 'reply']),
+ visibility: z.enum(['public', 'private']).optional(),
+ }),
+ ),
+})
+
+describe('declared channels drive the connect flow', () => {
+ it('accepts a Twilio channel, which has no scopes and no visibility', () => {
+ const parsed = declaredChannel.parse({
+ id: 'shop-sms',
+ type: 'twilio',
+ destinations: { reply: { type: 'reply' } },
+ })
+ expect(parsed.type).toBe('twilio')
+ expect(parsed.destinations.reply?.visibility).toBeUndefined()
+ })
+
+ it('still accepts a Slack channel with a public conversation', () => {
+ const parsed = declaredChannel.parse({
+ id: 'team-slack',
+ type: 'slack',
+ displayName: 'Engineering',
+ destinations: {
+ 'pull-request-reviews': { type: 'conversation', visibility: 'public' },
+ },
+ })
+ expect(parsed.destinations['pull-request-reviews']?.visibility).toBe('public')
+ })
+
+ it('refuses a provider the dashboard has no flow for', () => {
+ expect(() =>
+ declaredChannel.parse({ id: 'x', type: 'carrier-pigeon', destinations: {} }),
+ ).toThrow()
+ })
+})
diff --git a/web/src/managed-agents/TwilioWizard.tsx b/web/src/managed-agents/TwilioWizard.tsx
new file mode 100644
index 00000000..0abe5b0b
--- /dev/null
+++ b/web/src/managed-agents/TwilioWizard.tsx
@@ -0,0 +1,333 @@
+import { useState } from 'react'
+import { useMutation, useQueryClient } from '@tanstack/react-query'
+import { Check, LoaderCircle, Phone, TriangleAlert } from 'lucide-react'
+import { ConfirmDialog } from '@/components/confirm-dialog'
+import { Field, Input } from '@/components/form'
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from '@/components/ui/dialog'
+import { Button } from '@/components/ui/button'
+import { StatusBadge } from '@/components/status-badge'
+import { notifyError, notifySuccess } from '@/lib/errors'
+import { cn } from '@/lib/utils'
+import {
+ completeManagedAgentTwilio,
+ disconnectManagedAgentTwilio,
+ startManagedAgentTwilio,
+ type ManagedAgentChannel,
+ type ManagedTwilioSetup,
+} from './api'
+
+type Step = 'credentials' | 'number' | 'forwarding'
+const STEPS = ['Credentials', 'Choose a number', 'Forward your line']
+
+function WizardSteps({ current }: { current: number }) {
+ return (
+ <ol className="flex items-center gap-2 pt-2">
+ {STEPS.map((label, index) => (
+ <li key={label} className="flex min-w-0 items-center gap-2">
+ <span
+ className={cn(
+ 'flex size-5 shrink-0 items-center justify-center rounded-full text-[11px] font-semibold',
+ index <= current
+ ? 'bg-foreground text-background'
+ : 'bg-secondary text-muted-foreground',
+ )}
+ >
+ {index < current ? <Check className="size-3" /> : index + 1}
+ </span>
+ <span
+ className={cn(
+ 'truncate text-xs',
+ index === current
+ ? 'text-foreground font-medium'
+ : 'text-muted-foreground',
+ )}
+ >
+ {label}
+ </span>
+ {index < STEPS.length - 1 ? (
+ <span className="bg-border h-px w-4 shrink-0" />
+ ) : null}
+ </li>
+ ))}
+ </ol>
+ )
+}
+
+export function ManagedTwilioWizard({
+ agentId,
+ alias,
+ connection,
+ channelId,
+}: {
+ agentId: string
+ alias: string
+ connection?: ManagedAgentChannel
+ channelId?: string
+}) {
+ const queryClient = useQueryClient()
+ const [open, setOpen] = useState(false)
+ const [step, setStep] = useState<Step>('credentials')
+ const [accountSid, setAccountSid] = useState('')
+ const [authToken, setAuthToken] = useState('')
+ const [setup, setSetup] = useState<ManagedTwilioSetup>()
+ const [numberSid, setNumberSid] = useState('')
+ const [connectedNumber, setConnectedNumber] = useState('')
+ const [confirmDisconnect, setConfirmDisconnect] = useState(false)
+
+ const invalidate = () =>
+ queryClient.invalidateQueries({ queryKey: ['managed-agent-channels'] })
+
+ const start = useMutation({
+ mutationFn: () =>
+ startManagedAgentTwilio(`${agentId}@${alias}`, {
+ accountSid: accountSid.trim(),
+ authToken: authToken.trim(),
+ ...(channelId ? { channelId } : {}),
+ }),
+ onSuccess: (result) => {
+ setSetup(result)
+ // Clear the token as soon as it has been exchanged; the browser has no
+ // further use for it.
+ setAuthToken('')
+ setNumberSid(result.numbers[0]?.sid ?? '')
+ setStep('number')
+ void invalidate()
+ },
+ onError: (error) =>
+ notifyError('Twilio did not accept those credentials.', error),
+ })
+
+ const complete = useMutation({
+ mutationFn: () => {
+ const chosen = setup?.numbers.find((number) => number.sid === numberSid)
+ if (!setup || !chosen) throw new Error('Choose a number to continue')
+ return completeManagedAgentTwilio(setup.connection.id, {
+ numberSid: chosen.sid,
+ phoneNumber: chosen.phoneNumber,
+ webhookToken: setup.webhookToken,
+ })
+ },
+ onSuccess: (result) => {
+ setConnectedNumber(result.connection.phoneNumber ?? '')
+ setStep('forwarding')
+ notifySuccess('Twilio is connected.')
+ void invalidate()
+ },
+ onError: (error) =>
+ notifyError("Twilio wouldn't accept the webhook setup.", error),
+ })
+
+ const disconnect = useMutation({
+ mutationFn: () => disconnectManagedAgentTwilio(connection!.id),
+ onSuccess: () => {
+ setConfirmDisconnect(false)
+ void invalidate()
+ },
+ onError: (error) => notifyError("Couldn't disconnect Twilio.", error),
+ })
+
+ const begin = () => {
+ setStep('credentials')
+ setAccountSid('')
+ setAuthToken('')
+ setSetup(undefined)
+ setNumberSid('')
+ setConnectedNumber('')
+ setOpen(true)
+ }
+
+ const isConnected = connection?.status === 'connected'
+
+ return (
+ <>
+ <div className="flex items-center gap-2">
+ {isConnected ? (
+ <>
+ <StatusBadge status="active" label="Connected" />
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => setConfirmDisconnect(true)}
+ >
+ Disconnect
+ </Button>
+ </>
+ ) : (
+ <Button size="sm" onClick={begin}>
+ <Phone className="size-4" />
+ Connect Twilio
+ </Button>
+ )}
+ </div>
+
+ <Dialog open={open} onOpenChange={setOpen}>
+ <DialogContent className="sm:max-w-lg">
+ <DialogHeader>
+ <DialogTitle>Connect Twilio</DialogTitle>
+ <DialogDescription>
+ Calls and texts to a Twilio number reach this agent.
+ </DialogDescription>
+ <WizardSteps current={STEPS.indexOf(
+ step === 'credentials'
+ ? 'Credentials'
+ : step === 'number'
+ ? 'Choose a number'
+ : 'Forward your line',
+ )} />
+ </DialogHeader>
+
+ {step === 'credentials' ? (
+ <div className="space-y-4">
+ <p className="text-muted-foreground text-sm">
+ Both values are on your Twilio console dashboard. We check them
+ with Twilio before storing anything.
+ </p>
+ <Field label="Account SID">
+ <Input
+ value={accountSid}
+ onChange={(event) => setAccountSid(event.target.value)}
+ placeholder="AC…"
+ autoComplete="off"
+ />
+ </Field>
+ <Field label="Auth token">
+ <Input
+ type="password"
+ value={authToken}
+ onChange={(event) => setAuthToken(event.target.value)}
+ placeholder="Your Twilio auth token"
+ autoComplete="off"
+ />
+ </Field>
+ <DialogFooter>
+ <Button
+ onClick={() => start.mutate()}
+ disabled={
+ start.isPending || !accountSid.trim() || !authToken.trim()
+ }
+ >
+ {start.isPending ? (
+ <LoaderCircle className="size-4 animate-spin" />
+ ) : null}
+ Continue
+ </Button>
+ </DialogFooter>
+ </div>
+ ) : null}
+
+ {step === 'number' && setup ? (
+ <div className="space-y-4">
+ <p className="text-muted-foreground text-sm">
+ Connected to{' '}
+ <span className="text-foreground font-medium">
+ {setup.account.friendlyName}
+ </span>
+ . Choose the number this agent should answer. We'll point it at
+ OpenComputer for you.
+ </p>
+ {setup.numbers.length === 0 ? (
+ <div className="flex items-start gap-2 rounded-md border p-3 text-sm">
+ <TriangleAlert className="text-muted-foreground mt-0.5 size-4 shrink-0" />
+ <p>
+ This account has no number that can take both calls and
+ texts. Buy one in Twilio, then come back — a number without
+ voice can't catch a missed call.
+ </p>
+ </div>
+ ) : (
+ <div className="space-y-2">
+ {setup.numbers.map((number) => (
+ <label
+ key={number.sid}
+ className={cn(
+ 'flex cursor-pointer items-center gap-3 rounded-md border p-3 text-sm',
+ numberSid === number.sid
+ ? 'border-foreground'
+ : 'border-border',
+ )}
+ >
+ <input
+ type="radio"
+ name="twilio-number"
+ value={number.sid}
+ checked={numberSid === number.sid}
+ onChange={() => setNumberSid(number.sid)}
+ />
+ <span className="min-w-0">
+ <span className="block font-medium">
+ {number.phoneNumber}
+ </span>
+ <span className="text-muted-foreground block truncate text-xs">
+ {number.friendlyName}
+ </span>
+ </span>
+ </label>
+ ))}
+ </div>
+ )}
+ <DialogFooter>
+ <Button
+ onClick={() => complete.mutate()}
+ disabled={complete.isPending || !numberSid}
+ >
+ {complete.isPending ? (
+ <LoaderCircle className="size-4 animate-spin" />
+ ) : null}
+ Connect this number
+ </Button>
+ </DialogFooter>
+ </div>
+ ) : null}
+
+ {step === 'forwarding' ? (
+ <div className="space-y-4">
+ <div className="flex items-start gap-2 rounded-md border p-3 text-sm">
+ <Check className="mt-0.5 size-4 shrink-0" />
+ <p>
+ <span className="font-medium">{connectedNumber}</span> now
+ reaches this agent. Texts to it start a conversation, and
+ calls take a voicemail the agent reads.
+ </p>
+ </div>
+ <div className="space-y-2 text-sm">
+ <p className="font-medium">
+ One last step, on your existing business line
+ </p>
+ <p className="text-muted-foreground">
+ Set busy and no-answer forwarding to {connectedNumber}. On
+ most carriers that is a code you dial once from the phone
+ itself, or a single setting if the line is VoIP. Calls you
+ answer are unaffected — only the ones you miss come here.
+ </p>
+ <p className="text-muted-foreground">
+ This replaces your carrier's voicemail with one the agent can
+ act on, so keep your greeting in mind before you switch it.
+ </p>
+ </div>
+ <DialogFooter>
+ <Button onClick={() => setOpen(false)}>Done</Button>
+ </DialogFooter>
+ </div>
+ ) : null}
+ </DialogContent>
+ </Dialog>
+
+ <ConfirmDialog
+ open={confirmDisconnect}
+ onOpenChange={setConfirmDisconnect}
+ title="Disconnect Twilio?"
+ description="Calls and texts to this number stop reaching the agent. Your Twilio number and its history are untouched, and the forwarding on your business line stays until you remove it."
+ confirmLabel="Disconnect"
+ onConfirm={() => disconnect.mutate()}
+ pending={disconnect.isPending}
+ />
+ </>
+ )
+}
diff --git a/web/src/managed-agents/api.ts b/web/src/managed-agents/api.ts
index c8385ad5..7d12765b 100644
--- a/web/src/managed-agents/api.ts
+++ b/web/src/managed-agents/api.ts
@@ -27,13 +27,15 @@ const deploymentSchema = z.object({
channels: z.array(
z.object({
id: z.string(),
- type: z.literal('slack'),
+ type: z.enum(['slack', 'twilio', 'email']),
displayName: z.string().optional(),
+ // Slack addresses a named conversation; the others reply on the
+ // one the message arrived on, so visibility does not apply.
destinations: z.record(
z.string(),
z.object({
- type: z.literal('conversation'),
- visibility: z.enum(['public', 'private']),
+ type: z.enum(['conversation', 'reply']),
+ visibility: z.enum(['public', 'private']).optional(),
}),
),
}),
@@ -1016,6 +1018,74 @@ export async function completeManagedAgentSlack(
)
}
+const twilioNumberSchema = z.object({
+ sid: z.string(),
+ phoneNumber: z.string(),
+ friendlyName: z.string(),
+ capabilities: z.object({ sms: z.boolean(), voice: z.boolean() }),
+})
+
+const twilioSetupSchema = z.object({
+ connection: z.object({
+ id: z.string(),
+ provider: z.string(),
+ status: z.string(),
+ }),
+ account: z.object({ sid: z.string(), friendlyName: z.string() }),
+ numbers: z.array(twilioNumberSchema),
+ // Shown once, held only in the browser between the two steps, and never
+ // stored: it is half of what authenticates the webhook.
+ webhookToken: z.string(),
+})
+
+const twilioConnectionSchema = z.object({
+ connection: z.object({
+ id: z.string(),
+ provider: z.string().optional(),
+ status: z.string(),
+ phoneNumber: z.string().optional(),
+ }),
+})
+
+export type ManagedTwilioNumber = z.infer<typeof twilioNumberSchema>
+export type ManagedTwilioSetup = z.infer<typeof twilioSetupSchema>
+
+/**
+ * Step one. The credentials are checked against Twilio before anything is
+ * stored, so a mistyped SID comes back as a message rather than a broken
+ * connection nobody can see is broken.
+ */
+export async function startManagedAgentTwilio(
+ agentId: string,
+ input: { accountSid: string; authToken: string; channelId?: string },
+) {
+ return apiFetch(
+ '/managed-agents/channels/twilio/connections',
+ { method: 'POST', body: JSON.stringify({ agentId, ...input }) },
+ twilioSetupSchema,
+ )
+}
+
+/** Step two. Points the chosen number at OpenComputer and opens the line. */
+export async function completeManagedAgentTwilio(
+ connectionId: string,
+ input: { numberSid: string; phoneNumber: string; webhookToken: string },
+) {
+ return apiFetch(
+ `/managed-agents/channels/twilio/connections/${encodeURIComponent(connectionId)}`,
+ { method: 'PUT', body: JSON.stringify(input) },
+ twilioConnectionSchema,
+ )
+}
+
+export async function disconnectManagedAgentTwilio(connectionId: string) {
+ return apiFetch(
+ `/managed-agents/channels/twilio/connections/${encodeURIComponent(connectionId)}`,
+ { method: 'DELETE' },
+ twilioConnectionSchema,
+ )
+}
+
export async function disconnectManagedAgentSlack(connectionId: string) {
return apiFetch(
`/managed-agents/channels/slack/connections/${encodeURIComponent(connectionId)}`,