An open-source, faster Devin alternative
TypeScript
Link card for a dashboard user not linked to Slack (work 083)
Igor Zalutski via Computer43febe4c1e23
diff --git a/web/src/api/mock.ts b/web/src/api/mock.tsindex 37b3f544..1b98e496 100644--- a/web/src/api/mock.ts+++ b/web/src/api/mock.ts@@ -1297,6 +1297,7 @@ let previewBootSince: number | null = null// that sends no agent block). The paste form answers by the token: one// containing "rejected" is refused, "taken" is someone else's, "unchecked"// is saved without a check; any other well-formed one is verified.+// Unlinked with Slack installed, the card mints a link code (work 083).// `?agent=codex` is a connected ChatGPT plan. The one-command connect (work// 082) mints a placeholder code; with `?connect=auto` the preview plays the// person running it: about six seconds later the card's subscription (Codex@@ -1400,15 +1401,32 @@ function previewAgent(): PreviewAgent | null {function previewAgentRequest(path: string, method: string, body: unknown) {const agent = previewAgent()if (!agent) throw new ApiError('Not found', 404, 'not_found')- const { token, choice } = (+ const { token, choice, purpose } = (typeof body === 'string' ? JSON.parse(body) : {}- ) as { token?: string; choice?: PreviewAgent['choice'] }+ ) as { token?: string; choice?: PreviewAgent['choice']; purpose?: string }if (path.endsWith('/choice')) {agent.choice = choice ?? nullreturn { agent: structuredClone(agent) }}if (path.endsWith('/connect-code')) {const connect = new URLSearchParams(window.location.search).get('connect')+ // A link code (work 083): with `?connect=auto` the preview plays the+ // person sending `!link` in Slack.+ if (purpose === 'link') {+ if (connect === 'auto')+ setTimeout(() => {+ agent.linked = true+ agent.linkedSlackUser = {+ id: 'U0PREVIEW',+ name: 'maya',+ teamName: 'Acme',+ }+ }, 6_000)+ return {+ code: 'oc_PREVIEW00000000',+ expiresAt: new Date(Date.now() + 10 * 60_000).toISOString(),+ }+ }if (connect === 'unavailable' && agent.choice === 'codex')throw new ApiError("Codex isn't available on this computer yet",diff --git a/web/src/managed-agents/AgentStep.test.tsx b/web/src/managed-agents/AgentStep.test.tsxindex 81ba13c7..70119286 100644--- a/web/src/managed-agents/AgentStep.test.tsx+++ b/web/src/managed-agents/AgentStep.test.tsx@@ -895,13 +895,33 @@ describe('choosing the coding agent', () => {expect(button('Resume')).toBeUndefined()})- it('sends someone not linked to Slack there to connect', async () => {+ it('links someone not linked to Slack with a code', async () => {await show({ ...onboarded, agent: { ...fresh, choice: 'credits' } },'Your agent',)- expect(text()).toContain('Connect from Slack: !claude connect')+ await settle(() => text().includes(`!link ${CODE}`), 'link command')+ expect(computerApi.createConnectCode).toHaveBeenCalledWith('link')+ expect(text()).toContain('Or connect from Slack: !claude connect')expect(container.querySelector('input[type="password"]')).toBeNull()++ computerApi.getComputer.mockResolvedValue({+ ...onboarded,+ agent: {+ ...fresh,+ choice: 'credits',+ linked: true,+ linkedSlackUser: { id: 'U1', name: 'maya', teamName: 'Acme' },+ },+ })+ refocus()+ await settle(() => text().includes('Connect Claude Code'), 'connect card')+ expect(notify.notifySuccess).toHaveBeenCalledWith(+ 'Linked to your Slack account.',+ )+ expect(computerApi.createConnectCode).toHaveBeenCalledTimes(1)+ // A used code is never offered again.+ expect(Object.keys(window.localStorage)).toEqual([])})it('is not there for an older backend', async () => {diff --git a/web/src/managed-agents/AgentStep.tsx b/web/src/managed-agents/AgentStep.tsxindex c90aa8be..b6925335 100644--- a/web/src/managed-agents/AgentStep.tsx+++ b/web/src/managed-agents/AgentStep.tsx@@ -52,6 +52,7 @@ import {type AgentChoice,type ComputerAgent,type ComputerConnectCode,+ type ConnectCodePurpose,type ComputerStatus,type SubscriptionProvider,} from './computer-api'@@ -365,8 +366,9 @@ const computerQueryHash = hashKey(computerQueryKey)// A stored code is reused only with this long left to run it.const CODE_REUSE_MIN_MS = 60_000-function connectCodeStorageKey(scope: string) {- return `opencomputer.computer.connect-code:${scope}`+function connectCodeStorageKey(scope: string, purpose: ConnectCodePurpose) {+ const base = `opencomputer.computer.connect-code:${scope}`+ return purpose === 'link' ? `${base}:link` : base}/**@@ -411,10 +413,15 @@ function storedConnectCode(* only until `renewUntil`: after that a tab left open mints nothing, not* even when it is focused again.*/-function useConnectCode(renewUntil: number) {+function useConnectCode(+ renewUntil: number,+ purpose: ConnectCodePurpose = 'connect',+ enabled = true,+) {const auth = useContext(AuthContext)const key = connectCodeStorageKey(`${auth?.user?.orgId ?? ''}:${auth?.user?.id ?? ''}`,+ purpose,)const queryClient = useQueryClient()const query = useQuery({@@ -424,7 +431,7 @@ function useConnectCode(renewUntil: number) {const stored = storedConnectCode(key, status?.agent?.subscription?.setAt)if (stored) return storedconst mintedAt = Date.now()- const minted = await createConnectCode()+ const minted = await createConnectCode(purpose)try {window.localStorage.setItem(key,@@ -435,6 +442,7 @@ function useConnectCode(renewUntil: number) {}return minted},+ enabled,retry: false,staleTime: (q) => msUntil(q.state.data?.expiresAt),refetchInterval: (q) =>@@ -694,6 +702,116 @@ function CommandConnect({)}+/**+ * Linking this person to their Slack user (work 083): `!link <code>` sent to+ * the app in Slack. The page re-reads the status until it says linked; then+ * the card becomes the usual connect card, with this Slack user's+ * subscriptions.+ */+function LinkSlack() {+ const [actedAt, setActedAt] = useState(() => Date.now())+ // Set with the link, before this unmounts: the forgotten code's query is+ // still observed until then, and must not mint again.+ const [linked, setLinked] = useState(false)+ const {+ query: code,+ renew: renewCode,+ forget,+ } = useConnectCode(actedAt + CODE_AUTO_RENEW_FOR_MS, 'link', !linked)+ const renew = () => {+ setActedAt(Date.now())+ renewCode()+ }+ useQuery({+ queryKey: computerQueryKey,+ queryFn: getComputer,+ refetchInterval: () => {+ const waited = Date.now() - actedAt+ if (waited >= WAIT_FOR_MS) return false+ return waited < WAIT_FAST_FOR_MS ? WAIT_FAST_MS : WAIT_SLOW_MS+ },+ })+ // Announced once, from the cache: the card swaps in the render that+ // brings the link, and this unmounts.+ const queryClient = useQueryClient()+ const announced = useRef(false)+ useEffect(() => {+ const check = (next: ComputerStatus | undefined) => {+ if (announced.current || !next?.agent?.linked) return+ announced.current = true+ setLinked(true)+ forget()+ notifySuccess('Linked to your Slack account.')+ }+ check(queryClient.getQueryData<ComputerStatus>(computerQueryKey))+ return queryClient.getQueryCache().subscribe((event) => {+ if (+ event.type === 'updated' &&+ event.query.queryHash === computerQueryHash+ )+ check(event.query.state.data as ComputerStatus | undefined)+ })+ }, [queryClient, forget])++ const codeValue = code.data?.code+ const lead = <>Link your Slack account: send this to the app in Slack.</>+ return (+ <div className="space-y-3">+ {code.isError && !code.data ? (+ <div className="bg-muted/60 text-foreground space-y-3 rounded-lg p-4">+ <p role="alert" className="text-destructive">+ {connectErrorCopy(code.error)}+ </p>+ <Button size="sm" variant="outline" onClick={renew}>+ Try again+ </Button>+ </div>+ ) : (+ <div className="bg-muted/60 text-foreground space-y-3 rounded-lg p-4">+ {codeValue ? (+ <CopyCommand+ value={`!link ${codeValue}`}+ lead={lead}+ onCopy={() => setActedAt(Date.now())}+ />+ ) : (+ <>+ <p>{lead}</p>+ <div+ className="bg-panel-2 text-muted-foreground flex items-center gap-2 rounded-md border px-3 py-2 text-[13px]"+ aria-busy+ >+ <Loader2 className="size-3.5 animate-spin" aria-hidden />+ Getting your code…+ </div>+ </>+ )}+ <div className="text-muted-foreground flex flex-wrap items-center gap-x-3 gap-y-1 text-xs">+ <span className="flex items-center gap-1.5" aria-live="polite">+ <Loader2 className="size-3 animate-spin" aria-hidden />+ Waiting for it in Slack…+ </span>+ <span>Works once, for 10 minutes.</span>+ <Button+ variant="link"+ size="xs"+ className="text-muted-foreground h-auto px-0 text-xs underline"+ disabled={code.isFetching}+ onClick={renew}+ >+ Get a new code+ </Button>+ </div>+ </div>+ )}+ <p>+ Or connect from Slack: <Cmd>!claude connect</Cmd> or{' '}+ <Cmd>!codex connect</Cmd>+ </p>+ </div>+ )+}+/*** The paste form. The token lives in the field only until it is sent, and* then in a ref only until the server answers: the field is a password@@ -922,13 +1040,13 @@ export function AgentManage({const linkedLine = linked ? <p>{linked}</p> : nullif (!subscription) {- // After Slack, a person not linked to a Slack user connects there: the- // token belongs to whoever asks in Slack.+ // After Slack, subscriptions belong to Slack users: a person not linked+ // to one links first (work 083), or connects from Slack.if (slackInstalled && !agent.linked) {return (- <div className="text-muted-foreground space-y-2 text-sm">+ <div className="text-muted-foreground space-y-4 text-sm"><p className="text-foreground font-medium">{agentSummary(agent)}</p>- <p>{SLACK_INSTRUCTION}</p>+ <LinkSlack /></div>)}diff --git a/web/src/managed-agents/computer-api.ts b/web/src/managed-agents/computer-api.tsindex a3198591..4a316e23 100644--- a/web/src/managed-agents/computer-api.ts+++ b/web/src/managed-agents/computer-api.ts@@ -141,6 +141,7 @@ export type ComputerAgent = z.infer<typeof computerAgentSchema>export type AgentChoice = z.infer<typeof agentChoiceSchema>export type SubscriptionProvider = z.infer<typeof subscriptionProviderSchema>export type ComputerConnectCode = z.infer<typeof computerConnectCodeSchema>+export type ConnectCodePurpose = 'connect' | 'link'export type AgentSubscriptionHealth = z.infer<typeof agentSubscriptionHealthSchema>@@ -233,11 +234,17 @@ export async function setAgentChoice(choice: AgentChoice) {).agent}-/** Mints this person's connect code for the CLI (work 082). */-export function createConnectCode() {+/**+ * Mints this person's code: `connect` for the CLI (work 082), `link` for+ * `!link` in Slack, which links them to the Slack user who sends it (083).+ */+export function createConnectCode(purpose: ConnectCodePurpose = 'connect') {return apiFetch('/managed-agents/computer/agent/connect-code',- { method: 'POST', body: '{}' },+ {+ method: 'POST',+ body: JSON.stringify(purpose === 'link' ? { purpose } : {}),+ },computerConnectCodeSchema,)}