Files
opencode-manager/frontend/src/components/settings/ServerEnvVarsSettings.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

146 lines
4.5 KiB
TypeScript

import { useState, useEffect, useMemo } from 'react'
import { useSettings } from '@/hooks/useSettings'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Plus, Trash2, RotateCcw } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { showToast } from '@/lib/toast'
import { BLOCKED_SERVER_ENV_KEYS } from '@/api/types/settings'
import { SettingsDisclosure } from './SettingsDisclosure'
interface EnvVar {
key: string
value: string
}
export function ServerEnvVarsSettings() {
const { preferences, updateSettingsAsync, isUpdating } = useSettings()
const [isOpen, setIsOpen] = useState(false)
const [envVars, setEnvVars] = useState<EnvVar[]>([])
const [needsRestart, setNeedsRestart] = useState(false)
useEffect(() => {
setEnvVars(preferences?.serverEnvVars ?? [])
}, [preferences?.serverEnvVars])
const blockedSet = useMemo(() => new Set<string>(BLOCKED_SERVER_ENV_KEYS), [])
const blockedKeys = useMemo(
() => envVars
.map((envVar) => envVar.key.trim())
.filter((key) => key.length > 0 && blockedSet.has(key)),
[envVars, blockedSet],
)
const handleAdd = () => {
setEnvVars((prev) => [...prev, { key: '', value: '' }])
}
const handleRemove = (index: number) => {
setEnvVars((prev) => prev.filter((_, i) => i !== index))
}
const handleChange = (index: number, field: keyof EnvVar, value: string) => {
setEnvVars((prev) => prev.map((envVar, i) => (i === index ? { ...envVar, [field]: value } : envVar)))
}
const handleSave = async () => {
if (blockedKeys.length > 0) return
const filtered = envVars.filter((envVar) => envVar.key.trim() !== '')
try {
await updateSettingsAsync({
serverEnvVars: filtered,
})
setNeedsRestart(true)
showToast.success('Environment variables saved')
} catch {
showToast.error('Failed to save environment variables')
}
}
return (
<SettingsDisclosure
title="Server Environment Variables"
isOpen={isOpen}
onToggle={() => setIsOpen((value) => !value)}
contentClassName="space-y-3"
meta={
<Badge variant="outline" className="text-xs">
{(preferences?.serverEnvVars ?? []).length}
</Badge>
}
>
{needsRestart && (
<Alert>
<RotateCcw className="h-4 w-4" />
<AlertDescription>
Restart the OpenCode server to apply environment variable changes.
</AlertDescription>
</Alert>
)}
<div className="space-y-2">
{envVars.map((envVar, index) => {
const isBlocked = blockedSet.has(envVar.key.trim())
return (
<div key={index} className="flex gap-2 items-center">
<div className="flex-1 flex gap-2">
<Input
value={envVar.key}
onChange={(event) => handleChange(index, 'key', event.target.value)}
placeholder="VARIABLE_NAME"
className={`font-mono ${isBlocked ? 'border-destructive' : ''}`}
/>
<Input
value={envVar.value}
onChange={(event) => handleChange(index, 'value', event.target.value)}
placeholder="value"
className="font-mono"
/>
</div>
<Button
type="button"
variant="ghost"
size="icon"
onClick={() => handleRemove(index)}
className="shrink-0"
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
)
})}
{blockedKeys.length > 0 && (
<p className="text-xs text-destructive">
Reserved keys cannot be overridden: {blockedKeys.join(', ')}
</p>
)}
</div>
<div className="flex gap-2 pt-1">
<Button type="button" variant="outline" size="sm" onClick={handleAdd}>
<Plus className="h-3 w-3 mr-1" />
Add variable
</Button>
<Button
type="button"
size="sm"
onClick={handleSave}
disabled={isUpdating || blockedKeys.length > 0}
>
{isUpdating ? 'Saving...' : 'Save'}
</Button>
</div>
<p className="text-xs text-muted-foreground">
Variables are injected into the OpenCode server process at startup.
Changes require a server restart.
</p>
</SettingsDisclosure>
)
}