Files
opencode-manager/frontend/src/components/settings/OAuthAuthorizeDialog.tsx
T
Chris Scott dacde879d6 feat(opencode): migrate OpenCode Manager to OpenCode 2 (#359)
* feat(opencode): migrate OpenCode Manager to OpenCode 2

Port the backend lifecycle, generated plugins, event aggregation,
schedules, worktrees, provider credentials, MCP, config handling, the
frontend chat model, and the ocm CLI to OpenCode 2.

Adopt @opencode/client 2.0.15 as the single typed V2 API contract,
require OpenCode 2.0.0 or newer (pinned 2.0.15), and delete every
V1-only code path.

* refactor(opencode): reload config in place and drop v1 compatibility shims

Route config/skill/AGENTS.md changes through the OpenCode location reload
API instead of a full server restart, stop auto-resuming interrupted
sessions, remove the restart coordinator and v1 MCP/version shims, and pin
support to OpenCode >=2.0.15 <3.0.0. Adds integration-ready retries,
service-mode launch, ocm-cli repo proxy, and new frontend form/MCP helpers.

* refactor(opencode): consolidate v2 migration helpers and harden install, proxy, and session flows

- extract shared semver and model-ref utilities, reuse across version checks
- share the OpenCode release download script across Docker, entrypoint, dev setup
- unify install/upgrade failure recovery and guard externally managed OPENCODE_BIN
- scope the repo proxy location rewrite and surface ocm proxy errors
- rework frontend session projection, permission lookup, and cache invalidation

* fix(schedules): reject unavailable agents before creating a session

* feat(opencode): add ocm install, schedule agent fallback, and session move guards
2026-09-26 13:16:45 -04:00

198 lines
6.9 KiB
TypeScript

import { useState, useMemo, useEffect, useCallback } from 'react'
import { Button } from '@/components/ui/button'
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { Badge } from '@/components/ui/badge'
import { ExternalLink } from 'lucide-react'
import {
oauthApi,
type FormAnswer,
type FormValue,
type IntegrationMethod,
type IntegrationOAuthMethod,
type OAuthAuthorizeResponse,
} from '@/api/oauth'
import { buildAnswer, hasFields, hasMissingAnswers, resolveAnswers, setAnswerValue, visibleFields } from '@/lib/formFields'
import { mapOAuthError } from '@/lib/oauthErrors'
import { ProviderAuthField } from './ProviderAuthField'
interface OAuthAuthorizeDialogProps {
providerId: string
providerName: string
methods: IntegrationMethod[]
open: boolean
onOpenChange: (open: boolean) => void
onSuccess: (response: OAuthAuthorizeResponse, methodID: string) => void
}
function isBrowserLocalMethod(method: IntegrationOAuthMethod): boolean {
return method.label.toLowerCase().includes('browser')
}
export function OAuthAuthorizeDialog({
providerId,
providerName,
methods,
open,
onOpenChange,
onSuccess,
}: OAuthAuthorizeDialogProps) {
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const [selectedMethodID, setSelectedMethodID] = useState<string | null>(null)
const [answers, setAnswers] = useState<Record<string, FormAnswer>>({})
const [autoStarted, setAutoStarted] = useState(false)
const oauthMethods = useMemo(() => {
return methods
.filter((method): method is IntegrationOAuthMethod => method.type === 'oauth')
.filter((method) => !(providerId === 'openai' && isBrowserLocalMethod(method)))
}, [methods, providerId])
const getAnswer = useCallback(
(method: IntegrationOAuthMethod) => resolveAnswers(method.form, answers[method.id]),
[answers],
)
const handleAnswerChange = useCallback((methodID: string, key: string, value: FormValue | undefined) => {
setAnswers((prev) => setAnswerValue(prev, methodID, key, value))
}, [])
const handleAuthorize = useCallback(async (method: IntegrationOAuthMethod) => {
const answer = getAnswer(method)
const fields = visibleFields(method.form ?? [], answer)
if (hasMissingAnswers(method.form ?? [], answer)) {
setError('Please complete all required authentication fields')
setSelectedMethodID(method.id)
return
}
setIsLoading(true)
setError(null)
setSelectedMethodID(method.id)
try {
const response = await oauthApi.authorize(
providerId,
method.id,
fields.length > 0 ? buildAnswer(method.form ?? [], answer) : undefined,
)
onSuccess(response, method.id)
} catch (err) {
setError(mapOAuthError(err, 'authorize'))
} finally {
setIsLoading(false)
}
}, [getAnswer, providerId, onSuccess])
useEffect(() => {
if (open && oauthMethods.length === 1 && !autoStarted && !isLoading) {
const method = oauthMethods[0]
setAutoStarted(true)
setSelectedMethodID(method.id)
if (!hasFields(method.form)) {
void handleAuthorize(method)
}
}
}, [open, oauthMethods, autoStarted, isLoading, handleAuthorize])
const handleMethodSelection = (method: IntegrationOAuthMethod) => {
setError(null)
setSelectedMethodID(method.id)
if (!hasFields(method.form)) {
void handleAuthorize(method)
}
}
const handleClose = () => {
setError(null)
setAnswers({})
setSelectedMethodID(null)
onOpenChange(false)
}
return (
<Dialog open={open} onOpenChange={handleClose}>
<DialogContent className="bg-card border-border w-full sm:max-w-lg">
<DialogHeader>
<DialogTitle>Connect to {providerName}</DialogTitle>
<DialogDescription>
Select an authentication method to connect your {providerName} account.
</DialogDescription>
</DialogHeader>
{error && (
<div className="bg-destructive/10 border border-destructive/20 rounded-md p-3">
<p className="text-sm text-destructive">{error}</p>
</div>
)}
<div className="space-y-4">
{oauthMethods.map((method) => {
const isBrowserLocal = isBrowserLocalMethod(method)
const answer = getAnswer(method)
const fields = visibleFields(method.form ?? [], answer)
const canSubmit = !hasMissingAnswers(method.form ?? [], answer)
return (
<div key={method.id} className="space-y-3">
<div className="flex items-center gap-2 flex-wrap sm:flex-nowrap">
<Button
onClick={() => handleMethodSelection(method)}
disabled={isLoading}
className="flex-1 justify-start min-w-0"
variant={selectedMethodID === method.id ? 'default' : 'outline'}
>
<ExternalLink className="h-4 w-4 mr-2 shrink-0" />
<span className="truncate">
{isLoading && selectedMethodID === method.id ? 'Authorizing...' : method.label}
</span>
</Button>
{isBrowserLocal && (
<Badge variant="secondary" className="text-xs shrink-0">
Localhost only
</Badge>
)}
</div>
{selectedMethodID === method.id && hasFields(method.form) && (
<div className="space-y-3 pl-4 sm:pl-10 pr-1 py-2">
{fields.map((field) => (
<ProviderAuthField
key={field.key}
field={field}
value={answer[field.key]}
disabled={isLoading}
onChange={(value) => handleAnswerChange(method.id, field.key, value)}
/>
))}
<Button
onClick={() => void handleAuthorize(method)}
disabled={isLoading || !canSubmit}
className="w-full"
>
{isLoading && selectedMethodID === method.id ? 'Authorizing...' : 'Continue'}
</Button>
</div>
)}
{isBrowserLocal && (
<p className="text-xs text-muted-foreground pl-1 break-words">
This method relies on a callback server started by OpenCode and may not work when OCM is remote.
</p>
)}
</div>
)
})}
</div>
<div className="text-xs text-muted-foreground">
<p>• Some methods may require completing authorization in your browser</p>
</div>
</DialogContent>
</Dialog>
)
}