Add OAuth provider authentication support (#14)

* feat: Implement OAuth support for provider authentication

- Add OAuth routes and API endpoints for authorize/callback flows
- Create OAuthAuthorizeDialog and OAuthCallbackDialog components
- Extend ProviderSettings to support both OAuth and API key auth
- Add OAuth credential support to auth service
- Update shared schemas with OAuth request/response types
- Support both 'auto' and 'code' OAuth flows
- Maintain backward compatibility with existing API key authentication

Backend:
- backend/src/routes/oauth.ts (new)
- backend/src/services/auth.ts (extended)
- backend/src/index.ts (route registration)

Frontend:
- frontend/src/api/oauth.ts (new)
- frontend/src/components/settings/OAuthAuthorizeDialog.tsx (new)
- frontend/src/components/settings/OAuthCallbackDialog.tsx (new)
- frontend/src/components/settings/ProviderSettings.tsx (enhanced)

Shared:
- shared/src/schemas/auth.ts (OAuth schemas)

* feat: Complete OAuth implementation with comprehensive features

- Add comprehensive error handling for OAuth failures with specific error messages
- Implement OAuth token status checking and expiration monitoring
- Add token refresh preparation with 5-minute expiration buffer
- Enhance OAuth API client with detailed error reporting
- Update OAuth routes to include database parameter and token status endpoint
- Add complete OAuth documentation to README with setup guide
- Improve user experience with clear error messages and troubleshooting steps
- Support both auto and code OAuth flows for different providers
- Maintain backward compatibility with existing API key authentication

Error Handling:
- Specific error messages for invalid codes, expiration, access denied
- Graceful fallback for server errors and network issues
- User-friendly error display in OAuth dialogs

Token Management:
- Token status endpoint for checking credential validity
- Expiration monitoring with automatic refresh preparation
- Secure storage of OAuth credentials alongside API keys

Documentation:
- Complete OAuth setup guide in README
- Comparison table for OAuth vs API keys
- Troubleshooting section for common OAuth issues

* fix: Resolve OAuth button opening API key dialog instead

- Add isOAuthMode state to track authentication mode
- Update API key dialog to only open when not in OAuth mode
- Modify OAuth and API key button handlers to set proper mode
- Add proper cleanup handlers to reset OAuth mode on close
- Ensure dialog state management prevents conflicting dialogs

This fixes the issue where clicking 'Add OAuth' would incorrectly
open the API key dialog instead of the OAuth authorization dialog.

* refactor: Address audit findings for OAuth implementation

Critical fixes:
- Remove unused db parameter from createOAuthRoutes()
- Add query invalidation in handleOAuthSuccess to update UI after OAuth
- Remove unused setOAuth and getOAuthWithRefreshCheck methods
- Fix file permission in delete method (add mode: 0o600)

DRY improvements:
- Extract duplicated error handling to shared oauthErrors.ts utility
- Add OAuthMethod constants to replace magic numbers 0/1
- Use centralized OPENCODE_SERVER_URL constant via ENV config
- Remove unused ProviderAuthMethodsResponseSchema import

Code quality:
- Add documentation comment noting types should match shared schemas
- Consolidate error mapping logic into single mapOAuthError function
- Improve code maintainability and reduce duplication

Files changed:
- backend/src/routes/oauth.ts
- backend/src/services/auth.ts
- backend/src/index.ts
- frontend/src/lib/oauthErrors.ts (new)
- frontend/src/api/oauth.ts
- frontend/src/components/settings/OAuthAuthorizeDialog.tsx
- frontend/src/components/settings/OAuthCallbackDialog.tsx
- frontend/src/components/settings/ProviderSettings.tsx

* Store model selection client-side with recent models display

* Limit provider settings to OAuth-capable providers only

* Remove unused code and consolidate model initialization into useModelSelection hook
This commit is contained in:
Chris Scott authored and GitHub committed 2025-12-07 23:01:05 -05:00
1 parent 680fed3313
commit bc99c96021
20 files changed
+848 -423

No files matched your search

+1 -1
View File
@@ -36,6 +36,6 @@
### General
- DRY principles, follow existing patterns
- ./opencode-src/ is reference only, never commit
- ./temp/opencode is reference only, never commit has opencode src
- Use shared types from workspace package
- OpenCode server runs on port 5551, backend API on port 5001
+50 -1
View File
@@ -35,7 +35,8 @@ A full-stack web application for running [OpenCode](https://github.com/sst/openc
### AI Model & Provider Configuration
- **Model Selection** - Browse and select from available AI models with filtering
- **Provider Management** - Configure multiple AI providers with API keys
- **Provider Management** - Configure multiple AI providers with API keys or OAuth
- **OAuth Authentication** - Secure OAuth login for supported providers (Anthropic, GitHub Copilot)
- **Context Usage Indicator** - Visual progress bar showing token usage
- **Agent Configuration** - Create custom agents with system prompts and tool permissions
@@ -138,4 +139,52 @@ cp .env.example .env
npm run dev
```
## OAuth Provider Setup
OpenCode WebUI supports OAuth authentication for select providers, offering a more secure and convenient alternative to API keys.
### Supported Providers
- **Anthropic (Claude)** - OAuth login with Claude Pro/Max accounts
- **GitHub Copilot** - OAuth device flow authentication
### OAuth vs API Keys
| Feature | OAuth | API Keys |
|---------|-------|----------|
| **Security** | High (no manual key handling) | Medium (requires secure storage) |
| **Setup** | One-time authorization flow | Manual key entry |
| **Refresh** | Automatic token refresh | Manual key rotation |
| **Expiration** | Handled automatically | Keys don't expire |
### Setting Up OAuth
1. **Navigate to Settings → Provider Credentials**
2. **Select a provider** that shows the "OAuth" badge
3. **Click "Add OAuth"** to start the authorization flow
4. **Choose authentication method:**
- **"Open Authorization Page"** - Opens browser for sign-in
- **"Use Authorization Code"** - Provides code for manual entry
5. **Complete authorization** in the browser or enter the provided code
6. **Connection status** will show as "Configured" when successful
### OAuth Flow Types
- **Auto Flow** (GitHub Copilot): Opens browser window, automatic completion
- **Code Flow** (Anthropic): Requires manual code entry from authorization page
### Troubleshooting OAuth
- **"Invalid authorization code"**: Start the OAuth flow again
- **"Access denied"**: Check provider permissions and try again
- **"Code expired"**: Authorization codes expire quickly, restart the flow
- **"Server error"**: Check internet connection and try again later
### Token Management
- OAuth tokens are stored securely in your workspace
- Tokens automatically refresh when expired
- Use "Update OAuth" to re-authorize if needed
- API keys can still be used alongside OAuth for the same provider
+2
View File
@@ -9,6 +9,7 @@ import { createHealthRoutes } from './routes/health'
import { createTTSRoutes, cleanupExpiredCache } from './routes/tts'
import { createFileRoutes } from './routes/files'
import { createProvidersRoutes } from './routes/providers'
import { createOAuthRoutes } from './routes/oauth'
import { ensureDirectoryExists, writeFileContent } from './services/file-operations'
import { SettingsService } from './services/settings'
import { opencodeServerManager } from './services/opencode-single-server'
@@ -147,6 +148,7 @@ app.route('/api/settings', createSettingsRoutes(db))
app.route('/api/health', createHealthRoutes(db))
app.route('/api/files', createFileRoutes(db))
app.route('/api/providers', createProvidersRoutes())
app.route('/api/oauth', createOAuthRoutes())
app.route('/api/tts', createTTSRoutes(db))
app.all('/api/opencode/*', async (c) => {
+120
View File
@@ -0,0 +1,120 @@
import { Hono } from 'hono'
import { z } from 'zod'
import { proxyRequest } from '../services/proxy'
import { logger } from '../utils/logger'
import { ENV } from '@opencode-webui/shared'
import {
OAuthAuthorizeRequestSchema,
OAuthAuthorizeResponseSchema,
OAuthCallbackRequestSchema
} from '../../../shared/src/schemas/auth'
const OPENCODE_SERVER_URL = `http://${ENV.OPENCODE.HOST}:${ENV.OPENCODE.PORT}`
export function createOAuthRoutes() {
const app = new Hono()
app.post('/:id/oauth/authorize', async (c) => {
try {
const providerId = c.req.param('id')
const body = await c.req.json()
const validated = OAuthAuthorizeRequestSchema.parse(body)
// Proxy to OpenCode server
const response = await proxyRequest(
new Request(
`${OPENCODE_SERVER_URL}/provider/${providerId}/oauth/authorize`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(validated)
}
)
)
if (!response.ok) {
const error = await response.text()
logger.error(`OAuth authorize failed for ${providerId}:`, error)
return c.json({ error: 'OAuth authorization failed' }, 500)
}
const data = await response.json()
const validatedResponse = OAuthAuthorizeResponseSchema.parse(data)
return c.json(validatedResponse)
} catch (error) {
logger.error('OAuth authorize error:', error)
if (error instanceof z.ZodError) {
return c.json({ error: 'Invalid request data', details: error.issues }, 400)
}
return c.json({ error: 'OAuth authorization failed' }, 500)
}
})
app.post('/:id/oauth/callback', async (c) => {
try {
const providerId = c.req.param('id')
const body = await c.req.json()
const validated = OAuthCallbackRequestSchema.parse(body)
// Proxy to OpenCode server
const response = await proxyRequest(
new Request(
`${OPENCODE_SERVER_URL}/provider/${providerId}/oauth/callback`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(validated)
}
)
)
if (!response.ok) {
const error = await response.text()
logger.error(`OAuth callback failed for ${providerId}:`, error)
return c.json({ error: 'OAuth callback failed' }, 500)
}
const data = await response.json()
return c.json(data)
} catch (error) {
logger.error('OAuth callback error:', error)
if (error instanceof z.ZodError) {
return c.json({ error: 'Invalid request data', details: error.issues }, 400)
}
return c.json({ error: 'OAuth callback failed' }, 500)
}
})
app.get('/auth-methods', async (c) => {
try {
// Proxy to OpenCode server
const response = await proxyRequest(
new Request(`${OPENCODE_SERVER_URL}/provider/auth`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
)
if (!response.ok) {
const error = await response.text()
logger.error('Failed to get provider auth methods:', error)
return c.json({ error: 'Failed to get provider auth methods' }, 500)
}
const data = await response.json()
// The OpenCode server returns the format we need directly
return c.json({ providers: data })
} catch (error) {
logger.error('Provider auth methods error:', error)
if (error instanceof z.ZodError) {
return c.json({ error: 'Invalid response data', details: error.issues }, 500)
}
return c.json({ error: 'Failed to get provider auth methods' }, 500)
}
})
return app
}
+1 -6
View File
@@ -6,7 +6,6 @@ import { AuthCredentialsSchema } from '../../../shared/src/schemas/auth'
import type { z } from 'zod'
type AuthCredentials = z.infer<typeof AuthCredentialsSchema>
type AuthEntry = AuthCredentials[string]
export class AuthService {
private authPath = getAuthPath()
@@ -42,7 +41,7 @@ export class AuthService {
const auth = await this.getAll()
delete auth[providerId]
await fs.writeFile(this.authPath, JSON.stringify(auth, null, 2))
await fs.writeFile(this.authPath, JSON.stringify(auth, null, 2), { mode: 0o600 })
logger.info(`Deleted credentials for provider: ${providerId}`)
}
@@ -56,8 +55,4 @@ export class AuthService {
return !!auth[providerId]
}
async get(providerId: string): Promise<AuthEntry | null> {
const auth = await this.getAll()
return auth[providerId] || null
}
}
+61
View File
@@ -0,0 +1,61 @@
import axios from "axios"
import { API_BASE_URL } from "@/config"
export interface OAuthAuthorizeResponse {
url: string
method: "auto" | "code"
instructions: string
}
export interface OAuthCallbackRequest {
method: number
code?: string
}
export interface ProviderAuthMethod {
type: "oauth" | "api"
label: string
}
export interface ProviderAuthMethods {
[providerId: string]: ProviderAuthMethod[]
}
function handleApiError(error: unknown, context: string): never {
if (axios.isAxiosError(error)) {
const message = error.response?.data?.error || error.message
throw new Error(`${context}: ${message}`)
}
throw error
}
export const oauthApi = {
authorize: async (providerId: string, method: number): Promise<OAuthAuthorizeResponse> => {
try {
const { data } = await axios.post(`${API_BASE_URL}/api/oauth/${providerId}/oauth/authorize`, {
method,
})
return data
} catch (error) {
handleApiError(error, "OAuth authorization failed")
}
},
callback: async (providerId: string, request: OAuthCallbackRequest): Promise<boolean> => {
try {
const { data } = await axios.post(`${API_BASE_URL}/api/oauth/${providerId}/oauth/callback`, request)
return data
} catch (error) {
handleApiError(error, "OAuth callback failed")
}
},
getAuthMethods: async (): Promise<ProviderAuthMethods> => {
try {
const { data } = await axios.get(`${API_BASE_URL}/api/oauth/auth-methods`)
return data.providers || data
} catch (error) {
handleApiError(error, "Failed to get provider auth methods")
}
},
}
@@ -1,9 +1,10 @@
import { useState, useRef, useEffect, useMemo, type KeyboardEvent } from 'react'
import { useSendPrompt, useAbortSession, useMessages, useSendShell, useConfig, useAgents } from '@/hooks/useOpenCode'
import { useSendPrompt, useAbortSession, useMessages, useSendShell, useAgents } from '@/hooks/useOpenCode'
import { useSettings } from '@/hooks/useSettings'
import { useCommands } from '@/hooks/useCommands'
import { useCommandHandler } from '@/hooks/useCommandHandler'
import { useFileSearch } from '@/hooks/useFileSearch'
import { useModelSelection } from '@/hooks/useModelSelection'
import { useUserBash } from '@/stores/userBashStore'
import { ChevronDown } from 'lucide-react'
@@ -11,7 +12,6 @@ import { ChevronDown } from 'lucide-react'
import { CommandSuggestions } from '@/components/command/CommandSuggestions'
import { MentionSuggestions, type MentionItem } from './MentionSuggestions'
import { detectMentionTrigger, parsePromptToParts, getFilename, filterAgentsByQuery } from '@/lib/promptParser'
import { getSessionModel } from '@/lib/model'
import { getModel, formatModelName } from '@/api/providers'
import type { components } from '@/api/opencode-types'
import type { MessageWithParts, FileInfo } from '@/api/types'
@@ -69,7 +69,6 @@ export function PromptInput({
const sendShell = useSendShell(opcodeUrl, directory)
const abortSession = useAbortSession(opcodeUrl, directory, sessionID)
const { data: messages } = useMessages(opcodeUrl, sessionID, directory)
const { data: config } = useConfig(opcodeUrl)
const { preferences, updateSettings } = useSettings()
const { filterCommands } = useCommands(opcodeUrl)
const { executeCommand } = useCommandHandler({
@@ -430,20 +429,16 @@ export function PromptInput({
const modeColor = currentMode === 'plan' ? 'text-yellow-600 dark:text-yellow-500' : 'text-green-600 dark:text-green-500'
const modeBg = currentMode === 'plan' ? 'bg-yellow-500/10 border-yellow-500/30' : 'bg-green-500/10 border-green-500/30'
const currentModel = getSessionModel(messages, config?.model) || ''
const { model: selectedModel, modelString } = useModelSelection(opcodeUrl, directory)
const currentModel = modelString || ''
useEffect(() => {
const loadModelName = async () => {
if (currentModel) {
if (selectedModel) {
try {
const [providerId, modelId] = currentModel.split('/')
if (providerId && modelId) {
const model = await getModel(providerId, modelId)
if (model) {
setModelName(formatModelName(model))
} else {
setModelName(currentModel)
}
const model = await getModel(selectedModel.providerID, selectedModel.modelID)
if (model) {
setModelName(formatModelName(model))
} else {
setModelName(currentModel)
}
@@ -456,7 +451,7 @@ export function PromptInput({
}
loadModelName()
}, [currentModel])
}, [selectedModel, currentModel])
useEffect(() => {
if (textareaRef.current && !disabled && !hasActiveStream) {
@@ -15,9 +15,7 @@ import {
formatModelName,
formatProviderName,
} from "@/api/providers";
import { useSettings } from "@/hooks/useSettings";
import { useOpenCodeClient } from "@/hooks/useOpenCode";
import { useParams } from "react-router-dom";
import { useModelSelection } from "@/hooks/useModelSelection";
import { useQuery } from "@tanstack/react-query";
import type { Model, ProviderWithModels } from "@/api/providers";
@@ -25,7 +23,7 @@ interface ModelSelectDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
opcodeUrl?: string | null;
currentSessionModel?: string | null;
directory?: string;
}
interface FlatModel {
@@ -181,13 +179,17 @@ interface ModelGridProps {
currentModel: string;
onSelect: (providerId: string, modelId: string) => void;
loading: boolean;
recentModels?: FlatModel[];
showRecent?: boolean;
}
const ModelGrid = memo(function ModelGrid({
models,
currentModel,
onSelect,
loading
loading,
recentModels = [],
showRecent = false,
}: ModelGridProps) {
if (loading) {
return (
@@ -197,7 +199,7 @@ const ModelGrid = memo(function ModelGrid({
);
}
if (models.length === 0) {
if (models.length === 0 && recentModels.length === 0) {
return (
<div className="text-center py-12 text-zinc-500">
No models found
@@ -206,17 +208,49 @@ const ModelGrid = memo(function ModelGrid({
}
return (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-3 sm:gap-4">
{models.map(({ model, provider, modelKey }) => (
<ModelCard
key={modelKey}
model={model}
provider={provider}
modelKey={modelKey}
isSelected={currentModel === modelKey}
onSelect={onSelect}
/>
))}
<div className="space-y-6">
{showRecent && recentModels.length > 0 && (
<div>
<h3 className="text-sm font-semibold text-muted-foreground mb-3 flex items-center gap-1.5">
<Star className="h-3.5 w-3.5" />
Recent Models
</h3>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-3 sm:gap-4">
{recentModels.map(({ model, provider, modelKey }) => (
<ModelCard
key={`recent-${modelKey}`}
model={model}
provider={provider}
modelKey={modelKey}
isSelected={currentModel === modelKey}
onSelect={onSelect}
/>
))}
</div>
</div>
)}
{models.length > 0 && (
<div>
{showRecent && recentModels.length > 0 && (
<h3 className="text-sm font-semibold text-muted-foreground mb-3">
All Models
</h3>
)}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-3 sm:gap-4">
{models.map(({ model, provider, modelKey }) => (
<ModelCard
key={modelKey}
model={model}
provider={provider}
modelKey={modelKey}
isSelected={currentModel === modelKey}
onSelect={onSelect}
/>
))}
</div>
</div>
)}
</div>
);
});
@@ -322,15 +356,13 @@ export function ModelSelectDialog({
open,
onOpenChange,
opcodeUrl,
currentSessionModel,
directory,
}: ModelSelectDialogProps) {
const [searchQuery, setSearchQuery] = useState("");
const [selectedProvider, setSelectedProvider] = useState<string>("");
const { preferences, updateSettings } = useSettings();
const client = useOpenCodeClient(opcodeUrl);
const { sessionID } = useParams<{ sessionID: string }>();
const currentModel = currentSessionModel || preferences?.defaultModel || "";
const { modelString, setModel, recentModels } = useModelSelection(opcodeUrl, directory);
const currentModel = modelString || "";
const { data: providers = [], isLoading: loading } = useQuery({
queryKey: ["providers-with-models"],
@@ -341,22 +373,35 @@ export function ModelSelectDialog({
});
useEffect(() => {
if (currentModel && providers.length > 0) {
const [providerId] = currentModel.split("/");
setSelectedProvider(providerId);
if (open) {
setSelectedProvider("");
}
}, [currentModel, providers]);
}, [open]);
const flatModels = useMemo((): FlatModel[] => {
return providers.flatMap((provider) =>
provider.models.map((model) => ({
model,
provider,
modelKey: `${provider.id}/${model.id}`,
}))
);
const sourceOrder = { configured: 0, local: 1, builtin: 2 };
return providers
.slice()
.sort((a, b) => (sourceOrder[a.source] ?? 2) - (sourceOrder[b.source] ?? 2))
.flatMap((provider) =>
provider.models.map((model) => ({
model,
provider,
modelKey: `${provider.id}/${model.id}`,
}))
);
}, [providers]);
const recentFlatModels = useMemo((): FlatModel[] => {
return recentModels
.map((recent) => {
const modelKey = `${recent.providerID}/${recent.modelID}`;
return flatModels.find((fm) => fm.modelKey === modelKey);
})
.filter((fm): fm is FlatModel => fm !== undefined)
.slice(0, 6);
}, [recentModels, flatModels]);
const filteredModels = useMemo(() => {
const search = searchQuery.toLowerCase();
return flatModels.filter((item) => {
@@ -393,24 +438,10 @@ export function ModelSelectDialog({
setSearchQuery(query);
}, []);
const handleModelSelect = useCallback(async (providerId: string, modelId: string) => {
const newModel = `${providerId}/${modelId}`;
updateSettings({ defaultModel: newModel });
if (sessionID && client) {
try {
await client.sendCommand(sessionID, {
command: "model",
arguments: newModel,
model: newModel,
});
} catch {
// Ignore errors
}
}
const handleModelSelect = useCallback((providerId: string, modelId: string) => {
setModel({ providerID: providerId, modelID: modelId });
onOpenChange(false);
}, [sessionID, client, updateSettings, onOpenChange]);
}, [setModel, onOpenChange]);
const searchResetKey = selectedProvider;
@@ -491,6 +522,8 @@ export function ModelSelectDialog({
currentModel={currentModel}
onSelect={handleModelSelect}
loading={loading}
recentModels={recentFlatModels}
showRecent={!selectedProvider && !searchQuery}
/>
</div>
@@ -1,222 +0,0 @@
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog'
import { Badge } from '@/components/ui/badge'
import { Card, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'
import { Loader2, ExternalLink } from 'lucide-react'
import { PROVIDER_TEMPLATES, type ProviderTemplate } from '@/lib/providerTemplates'
import { settingsApi } from '@/api/settings'
import { useMutation, useQueryClient } from '@tanstack/react-query'
interface AddProviderDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
}
export function AddProviderDialog({ open, onOpenChange }: AddProviderDialogProps) {
const [step, setStep] = useState<'select' | 'customize'>('select')
const [selectedTemplate, setSelectedTemplate] = useState<ProviderTemplate | null>(null)
const [providerId, setProviderId] = useState('')
const [providerName, setProviderName] = useState('')
const [baseURL, setBaseURL] = useState('')
const queryClient = useQueryClient()
const addProviderMutation = useMutation({
mutationFn: async () => {
const config = await settingsApi.getDefaultOpenCodeConfig()
const currentProvider = config?.content?.provider || {}
const newProvider = {
npm: selectedTemplate?.npm || '@ai-sdk/openai-compatible',
name: providerName || selectedTemplate?.name,
...(baseURL && {
options: {
baseURL,
},
}),
...(selectedTemplate?.models && {
models: selectedTemplate.models,
}),
}
const updatedConfig = {
...config?.content,
provider: {
...currentProvider,
[providerId]: newProvider,
},
}
await settingsApi.updateOpenCodeConfig(
config?.name || 'default',
{ content: updatedConfig }
)
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['opencode-config'] })
queryClient.invalidateQueries({ queryKey: ['providers'] })
handleClose()
},
})
const handleSelectTemplate = (template: ProviderTemplate) => {
setSelectedTemplate(template)
setProviderId(template.id)
setProviderName(template.name)
setBaseURL(template.options?.baseURL || '')
setStep('customize')
}
const handleAdd = () => {
if (providerId && selectedTemplate) {
addProviderMutation.mutate()
}
}
const handleClose = () => {
setStep('select')
setSelectedTemplate(null)
setProviderId('')
setProviderName('')
setBaseURL('')
onOpenChange(false)
}
return (
<Dialog open={open} onOpenChange={handleClose}>
<DialogContent className="max-w-3xl max-h-[80vh] bg-card border-border overflow-y-auto">
{step === 'select' && (
<>
<DialogHeader>
<DialogTitle>Add Provider</DialogTitle>
<DialogDescription>
Choose a provider template to get started quickly
</DialogDescription>
</DialogHeader>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3 py-4">
{PROVIDER_TEMPLATES.map((template) => (
<Card
key={template.id}
className="cursor-pointer hover:border-blue-500 transition-colors bg-background border-border"
onClick={() => handleSelectTemplate(template)}
>
<CardHeader>
<div className="flex items-start justify-between">
<div className="flex-1">
<CardTitle className="text-base flex items-center gap-2">
{template.name}
{!template.requiresApiKey && (
<Badge variant="secondary" className="text-xs">
Local
</Badge>
)}
</CardTitle>
<CardDescription className="text-sm mt-1">
{template.description}
</CardDescription>
<p className="text-xs text-muted-foreground mt-2">
{template.npm}
</p>
</div>
</div>
</CardHeader>
</Card>
))}
</div>
</>
)}
{step === 'customize' && selectedTemplate && (
<>
<DialogHeader>
<DialogTitle>Configure {selectedTemplate.name}</DialogTitle>
<DialogDescription>
Customize the provider settings before adding
{selectedTemplate.docsUrl && (
<a
href={selectedTemplate.docsUrl}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-blue-500 hover:underline ml-2"
>
View docs <ExternalLink className="h-3 w-3" />
</a>
)}
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<div className="space-y-2">
<Label htmlFor="providerId">Provider ID</Label>
<Input
id="providerId"
value={providerId}
onChange={(e) => setProviderId(e.target.value)}
placeholder="e.g., anthropic, openai, my-provider"
className="bg-background border-border"
/>
<p className="text-xs text-muted-foreground">
Unique identifier for this provider (lowercase, no spaces)
</p>
</div>
<div className="space-y-2">
<Label htmlFor="providerName">Display Name</Label>
<Input
id="providerName"
value={providerName}
onChange={(e) => setProviderName(e.target.value)}
placeholder={selectedTemplate.name}
className="bg-background border-border"
/>
</div>
{selectedTemplate.options?.baseURL && (
<div className="space-y-2">
<Label htmlFor="baseURL">Base URL</Label>
<Input
id="baseURL"
value={baseURL}
onChange={(e) => setBaseURL(e.target.value)}
placeholder={selectedTemplate.options.baseURL}
className="bg-background border-border"
/>
<p className="text-xs text-muted-foreground">
API endpoint for this provider
</p>
</div>
)}
<div className="bg-muted p-3 rounded-md">
<p className="text-xs text-muted-foreground">
<strong>NPM Package:</strong> {selectedTemplate.npm}
</p>
{selectedTemplate.models && (
<p className="text-xs text-muted-foreground mt-1">
<strong>Models:</strong> {Object.keys(selectedTemplate.models).length} pre-configured
</p>
)}
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setStep('select')}>
Back
</Button>
<Button
onClick={handleAdd}
disabled={!providerId || addProviderMutation.isPending}
>
{addProviderMutation.isPending && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
Add Provider
</Button>
</DialogFooter>
</>
)}
</DialogContent>
</Dialog>
)
}
@@ -0,0 +1,91 @@
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { ExternalLink, Copy } from 'lucide-react'
import { oauthApi, type OAuthAuthorizeResponse } from '@/api/oauth'
import { mapOAuthError, OAuthMethod } from '@/lib/oauthErrors'
interface OAuthAuthorizeDialogProps {
providerId: string
providerName: string
open: boolean
onOpenChange: (open: boolean) => void
onSuccess: (response: OAuthAuthorizeResponse) => void
}
export function OAuthAuthorizeDialog({
providerId,
providerName,
open,
onOpenChange,
onSuccess
}: OAuthAuthorizeDialogProps) {
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const handleAuthorize = async (methodIndex: number) => {
setIsLoading(true)
setError(null)
try {
const response = await oauthApi.authorize(providerId, methodIndex)
onSuccess(response)
} catch (err) {
setError(mapOAuthError(err, 'authorize'))
console.error('OAuth authorize error:', err)
} finally {
setIsLoading(false)
}
}
const handleClose = () => {
setError(null)
onOpenChange(false)
}
return (
<Dialog open={open} onOpenChange={handleClose}>
<DialogContent className="bg-card border-border max-w-lg">
<DialogHeader>
<DialogTitle>Connect to {providerName}</DialogTitle>
<DialogDescription>
Choose 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-3">
<Button
onClick={() => handleAuthorize(OAuthMethod.AUTO)}
disabled={isLoading}
className="w-full justify-start"
variant="outline"
>
<ExternalLink className="h-4 w-4 mr-2" />
{isLoading ? 'Authorizing...' : 'Open Authorization Page'}
</Button>
<Button
onClick={() => handleAuthorize(OAuthMethod.CODE)}
disabled={isLoading}
className="w-full justify-start"
variant="outline"
>
<Copy className="h-4 w-4 mr-2" />
{isLoading ? 'Authorizing...' : 'Use Authorization Code'}
</Button>
</div>
<div className="text-xs text-muted-foreground">
<p>• "Open Authorization Page" will open a browser window for you to sign in</p>
<p>• "Use Authorization Code" will give you a code to manually enter</p>
</div>
</DialogContent>
</Dialog>
)
}
@@ -0,0 +1,171 @@
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { Loader2, ExternalLink, CheckCircle } from 'lucide-react'
import { oauthApi, type OAuthAuthorizeResponse } from '@/api/oauth'
import { mapOAuthError, OAuthMethod } from '@/lib/oauthErrors'
interface OAuthCallbackDialogProps {
providerId: string
providerName: string
authResponse: OAuthAuthorizeResponse
open: boolean
onOpenChange: (open: boolean) => void
onSuccess: () => void
}
export function OAuthCallbackDialog({
providerId,
providerName,
authResponse,
open,
onOpenChange,
onSuccess
}: OAuthCallbackDialogProps) {
const [isLoading, setIsLoading] = useState(false)
const [authCode, setAuthCode] = useState('')
const [error, setError] = useState<string | null>(null)
const handleAutoCallback = async () => {
setIsLoading(true)
setError(null)
try {
await oauthApi.callback(providerId, { method: OAuthMethod.AUTO })
onSuccess()
} catch (err) {
setError(mapOAuthError(err, 'callback'))
console.error('OAuth callback error:', err)
} finally {
setIsLoading(false)
}
}
const handleCodeCallback = async () => {
if (!authCode.trim()) {
setError('Please enter the authorization code')
return
}
setIsLoading(true)
setError(null)
try {
await oauthApi.callback(providerId, { method: OAuthMethod.CODE, code: authCode.trim() })
onSuccess()
} catch (err) {
setError(mapOAuthError(err, 'callback'))
console.error('OAuth callback error:', err)
} finally {
setIsLoading(false)
}
}
const handleOpenAuthUrl = () => {
window.open(authResponse.url, '_blank')
}
const handleClose = () => {
setError(null)
setAuthCode('')
onOpenChange(false)
}
return (
<Dialog open={open} onOpenChange={handleClose}>
<DialogContent className="bg-card border-border max-w-lg">
<DialogHeader>
<DialogTitle>Complete {providerName} Authentication</DialogTitle>
<DialogDescription>
{authResponse.method === 'auto'
? 'Follow the instructions to complete authentication.'
: 'Enter the authorization code from the provider.'
}
</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">
{authResponse.method === 'auto' ? (
<div className="space-y-3">
<div className="bg-muted p-3 rounded-md">
<p className="text-sm">{authResponse.instructions}</p>
</div>
<Button
onClick={handleOpenAuthUrl}
className="w-full"
disabled={isLoading}
>
<ExternalLink className="h-4 w-4 mr-2" />
Open Authorization Page
</Button>
<Button
onClick={handleAutoCallback}
variant="outline"
className="w-full"
disabled={isLoading}
>
<CheckCircle className="h-4 w-4 mr-2" />
{isLoading ? 'Completing...' : 'I Completed Authorization'}
</Button>
</div>
) : (
<div className="space-y-3">
<div className="bg-muted p-3 rounded-md">
<p className="text-sm mb-2">{authResponse.instructions}</p>
<Button
onClick={handleOpenAuthUrl}
variant="outline"
size="sm"
className="w-full"
>
<ExternalLink className="h-4 w-4 mr-2" />
Open Authorization Page
</Button>
</div>
<div className="space-y-2">
<Label htmlFor="authCode">Authorization Code</Label>
<Input
id="authCode"
value={authCode}
onChange={(e) => setAuthCode(e.target.value)}
placeholder="Enter the authorization code..."
className="bg-background border-border"
disabled={isLoading}
/>
</div>
<Button
onClick={handleCodeCallback}
className="w-full"
disabled={isLoading || !authCode.trim()}
>
{isLoading ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Completing...
</>
) : (
<>
<CheckCircle className="h-4 w-4 mr-2" />
Complete Authentication
</>
)}
</Button>
</div>
)}
</div>
</DialogContent>
</Dialog>
)
}
@@ -1,20 +1,19 @@
import { useState } from 'react'
import { useState, useMemo, useCallback } from 'react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog'
import { Badge } from '@/components/ui/badge'
import { Loader2, Key, Check, X, Plus } from 'lucide-react'
import { Loader2, Check, X, Shield } from 'lucide-react'
import { providerCredentialsApi, getProviders, type Provider } from '@/api/providers'
import { oauthApi, type OAuthAuthorizeResponse } from '@/api/oauth'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { AddProviderDialog } from './AddProviderDialog'
import { OAuthAuthorizeDialog } from './OAuthAuthorizeDialog'
import { OAuthCallbackDialog } from './OAuthCallbackDialog'
export function ProviderSettings() {
const [selectedProvider, setSelectedProvider] = useState<string | null>(null)
const [apiKey, setApiKey] = useState('')
const [showApiKey, setShowApiKey] = useState(false)
const [addDialogOpen, setAddDialogOpen] = useState(false)
const [oauthDialogOpen, setOauthDialogOpen] = useState(false)
const [oauthCallbackDialogOpen, setOauthCallbackDialogOpen] = useState(false)
const [oauthResponse, setOauthResponse] = useState<OAuthAuthorizeResponse | null>(null)
const queryClient = useQueryClient()
const { data: providers, isLoading: providersLoading } = useQuery<Provider[]>({
@@ -28,14 +27,9 @@ export function ProviderSettings() {
queryFn: () => providerCredentialsApi.list(),
})
const setCredentialMutation = useMutation({
mutationFn: ({ providerId, apiKey }: { providerId: string; apiKey: string }) =>
providerCredentialsApi.set(providerId, apiKey),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['provider-credentials'] })
setSelectedProvider(null)
setApiKey('')
},
const { data: authMethods } = useQuery({
queryKey: ['provider-auth-methods'],
queryFn: () => oauthApi.getAuthMethods(),
})
const deleteCredentialMutation = useMutation({
@@ -45,22 +39,49 @@ export function ProviderSettings() {
},
})
const handleSetCredential = () => {
if (selectedProvider && apiKey) {
setCredentialMutation.mutate({ providerId: selectedProvider, apiKey })
}
}
const handleDeleteCredential = (providerId: string) => {
if (confirm(`Remove credentials for ${providerId}?`)) {
deleteCredentialMutation.mutate(providerId)
}
}
const handleOAuthAuthorize = (response: OAuthAuthorizeResponse) => {
setOauthResponse(response)
setOauthDialogOpen(false)
setOauthCallbackDialogOpen(true)
}
const handleOAuthDialogClose = () => {
setOauthDialogOpen(false)
setSelectedProvider(null)
}
const handleOAuthSuccess = () => {
queryClient.invalidateQueries({ queryKey: ['provider-credentials'] })
setOauthCallbackDialogOpen(false)
setOauthResponse(null)
setSelectedProvider(null)
}
const supportsOAuth = useCallback((providerId: string) => {
const methods = authMethods?.[providerId] || []
return methods.some(method => method.type === 'oauth')
}, [authMethods])
const hasCredentials = (providerId: string) => {
return credentialsList?.includes(providerId) || false
}
const oauthProviders = useMemo(() => {
if (!providers || !authMethods) return []
return providers.filter(provider => supportsOAuth(provider.id))
}, [providers, authMethods, supportsOAuth])
const selectedProviderName = useMemo(() => {
if (!selectedProvider) return ''
return providers?.find(p => p.id === selectedProvider)?.name || selectedProvider
}, [selectedProvider, providers])
if (providersLoading || credentialsLoading) {
return (
<div className="flex items-center justify-center py-12">
@@ -71,30 +92,24 @@ export function ProviderSettings() {
return (
<div className="space-y-4">
<div className="flex items-start justify-between">
<div>
<h2 className="text-lg font-semibold text-foreground mb-2">Provider Credentials</h2>
<p className="text-sm text-muted-foreground">
Manage API keys for AI providers. Keys are stored securely in your workspace.
</p>
</div>
<Button onClick={() => setAddDialogOpen(true)}>
<Plus className="h-4 w-4 mr-2" />
Add Provider
</Button>
<div>
<h2 className="text-lg font-semibold text-foreground mb-2">OAuth Providers</h2>
<p className="text-sm text-muted-foreground">
Connect to AI providers using OAuth. For API keys, configure them in your OpenCode config file.
</p>
</div>
{!providers || providers.length === 0 ? (
{oauthProviders.length === 0 ? (
<Card className="bg-card border-border">
<CardContent className="pt-6">
<p className="text-sm text-muted-foreground text-center">
No providers configured. Add providers in your OpenCode config.
No OAuth-capable providers available.
</p>
</CardContent>
</Card>
) : (
<div className="grid gap-4">
{providers.map((provider) => {
{oauthProviders.map((provider) => {
const hasKey = hasCredentials(provider.id)
const modelCount = Object.keys(provider.models || {}).length
@@ -108,12 +123,12 @@ export function ProviderSettings() {
{hasKey ? (
<Badge variant="default" className="bg-green-600 hover:bg-green-700">
<Check className="h-3 w-3 mr-1" />
Configured
Connected
</Badge>
) : (
<Badge variant="secondary">
<X className="h-3 w-3 mr-1" />
No Key
Not Connected
</Badge>
)}
</CardTitle>
@@ -131,10 +146,13 @@ export function ProviderSettings() {
<Button
size="sm"
variant={hasKey ? 'outline' : 'default'}
onClick={() => setSelectedProvider(provider.id)}
onClick={() => {
setSelectedProvider(provider.id)
setOauthDialogOpen(true)
}}
>
<Key className="h-4 w-4 mr-1" />
{hasKey ? 'Update' : 'Add'} Key
<Shield className="h-4 w-4 mr-1" />
{hasKey ? 'Reconnect' : 'Connect'}
</Button>
{hasKey && (
<Button
@@ -143,7 +161,7 @@ export function ProviderSettings() {
onClick={() => handleDeleteCredential(provider.id)}
disabled={deleteCredentialMutation.isPending}
>
Remove
Disconnect
</Button>
)}
</div>
@@ -155,56 +173,26 @@ export function ProviderSettings() {
</div>
)}
<Dialog open={!!selectedProvider} onOpenChange={(open) => !open && setSelectedProvider(null)}>
<DialogContent className="bg-card border-border">
<DialogHeader>
<DialogTitle>Set API Key for {selectedProvider}</DialogTitle>
<DialogDescription>
Enter your API key. It will be stored securely in your workspace.
</DialogDescription>
</DialogHeader>
{selectedProvider && (
<OAuthAuthorizeDialog
providerId={selectedProvider}
providerName={selectedProviderName}
open={oauthDialogOpen}
onOpenChange={handleOAuthDialogClose}
onSuccess={handleOAuthAuthorize}
/>
)}
<div className="space-y-4 py-4">
<div className="space-y-2">
<Label htmlFor="apiKey">API Key</Label>
<div className="relative">
<Input
id="apiKey"
type={showApiKey ? 'text' : 'password'}
value={apiKey}
onChange={(e) => setApiKey(e.target.value)}
placeholder="sk-..."
className="bg-background border-border pr-20"
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-1 top-1/2 -translate-y-1/2 h-7"
onClick={() => setShowApiKey(!showApiKey)}
>
{showApiKey ? 'Hide' : 'Show'}
</Button>
</div>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setSelectedProvider(null)}>
Cancel
</Button>
<Button
onClick={handleSetCredential}
disabled={!apiKey || setCredentialMutation.isPending}
>
{setCredentialMutation.isPending && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
Save
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<AddProviderDialog open={addDialogOpen} onOpenChange={setAddDialogOpen} />
{selectedProvider && oauthResponse && (
<OAuthCallbackDialog
providerId={selectedProvider}
providerName={selectedProviderName}
authResponse={oauthResponse}
open={oauthCallbackDialogOpen}
onOpenChange={setOauthCallbackDialogOpen}
onSuccess={handleOAuthSuccess}
/>
)}
</div>
)
}
+4 -5
View File
@@ -1,8 +1,7 @@
import { useMemo } from 'react'
import { useMessages } from './useOpenCode'
import { useSettings } from './useSettings'
import { useQuery } from '@tanstack/react-query'
import { getSessionModel } from '@/lib/model'
import { useModelSelection } from './useModelSelection'
interface ContextUsage {
totalTokens: number
@@ -43,7 +42,7 @@ async function fetchProviders(opcodeUrl: string): Promise<ProvidersResponse> {
export const useContextUsage = (opcodeUrl: string | null | undefined, sessionID: string | undefined, directory?: string): ContextUsage => {
const { data: messages, isLoading: messagesLoading } = useMessages(opcodeUrl, sessionID, directory)
const { preferences } = useSettings()
const { modelString } = useModelSelection(opcodeUrl, directory)
const { data: providersData } = useQuery({
queryKey: ['providers', opcodeUrl],
@@ -53,7 +52,7 @@ export const useContextUsage = (opcodeUrl: string | null | undefined, sessionID:
})
return useMemo(() => {
const currentModel = getSessionModel(messages, preferences?.defaultModel)
const currentModel = modelString || null
const assistantMessages = messages?.filter(msg => msg.info.role === 'assistant') || []
let latestAssistantMessage = assistantMessages[assistantMessages.length - 1]
@@ -101,5 +100,5 @@ export const useContextUsage = (opcodeUrl: string | null | undefined, sessionID:
currentModel,
isLoading: false
}
}, [messages, messagesLoading, preferences?.defaultModel, providersData])
}, [messages, messagesLoading, modelString, providersData])
}
+31
View File
@@ -0,0 +1,31 @@
import { useEffect } from 'react'
import { useConfig } from './useOpenCode'
import { useModelStore, type ModelSelection } from '@/stores/modelStore'
interface UseModelSelectionResult {
model: ModelSelection | null
modelString: string | null
recentModels: ModelSelection[]
setModel: (model: ModelSelection) => void
}
export function useModelSelection(
opcodeUrl: string | null | undefined,
directory?: string
): UseModelSelectionResult {
const { data: config } = useConfig(opcodeUrl, directory)
const { model, recentModels, setModel, initializeFromConfig, getModelString } = useModelStore()
useEffect(() => {
if (config?.model) {
initializeFromConfig(config.model)
}
}, [config?.model, initializeFromConfig])
return {
model,
modelString: getModelString(),
recentModels,
setModel,
}
}
+5 -3
View File
@@ -440,13 +440,15 @@ export const useSendShell = (opcodeUrl: string | null | undefined, directory?: s
});
};
export const useConfig = (opcodeUrl: string | null | undefined) => {
const client = useOpenCodeClient(opcodeUrl);
export const useConfig = (opcodeUrl: string | null | undefined, directory?: string) => {
const client = useOpenCodeClient(opcodeUrl, directory);
return useQuery({
queryKey: ["opencode", "config", opcodeUrl],
queryKey: ["opencode", "config", opcodeUrl, directory],
queryFn: () => client!.getConfig(),
enabled: !!client,
staleTime: 0,
refetchOnWindowFocus: true,
});
};
-22
View File
@@ -1,22 +0,0 @@
import type { MessageWithParts } from '@/api/types'
export function getSessionModel(
messages: MessageWithParts[] | undefined,
fallbackModel?: string | null
): string | null {
const assistantMessages = messages?.filter(msg => msg.info.role === 'assistant') || []
let latest = assistantMessages[assistantMessages.length - 1]
if (latest?.info.role === 'assistant') {
const tokens = latest.info.tokens.input + latest.info.tokens.output + latest.info.tokens.reasoning
if (tokens === 0 && assistantMessages.length > 1) {
latest = assistantMessages[assistantMessages.length - 2]
}
}
if (latest?.info.role === 'assistant') {
return `${latest.info.providerID}/${latest.info.modelID}`
}
return fallbackModel || null
}
+31
View File
@@ -0,0 +1,31 @@
export const OAuthMethod = {
AUTO: 0,
CODE: 1,
} as const
export type OAuthMethodType = (typeof OAuthMethod)[keyof typeof OAuthMethod]
const ERROR_MAPPINGS: Record<string, string> = {
'invalid code': 'Invalid authorization code. Please try the OAuth flow again.',
'expired': 'Authorization code has expired. Please try the OAuth flow again.',
'access denied': 'Access was denied. Please check the permissions and try again.',
'server error': 'Server error occurred. Please try again later.',
'provider not found': 'Provider is not available or does not support OAuth.',
'invalid method': 'Invalid authentication method selected.',
}
export function mapOAuthError(err: unknown, context: 'authorize' | 'callback'): string {
const defaultMessage = context === 'authorize'
? 'Failed to initiate OAuth authorization'
: 'Failed to complete OAuth callback'
if (!(err instanceof Error)) return defaultMessage
for (const [key, message] of Object.entries(ERROR_MAPPINGS)) {
if (err.message.toLowerCase().includes(key)) {
return message
}
}
return err.message || defaultMessage
}
+3 -7
View File
@@ -1,4 +1,4 @@
import { useState, useMemo } from "react";
import { useState } from "react";
import { useParams, useNavigate } from "react-router-dom";
import { useQuery } from "@tanstack/react-query";
import { getRepo } from "@/api/repos";
@@ -10,9 +10,8 @@ import { SessionList } from "@/components/session/SessionList";
import { PermissionRequestDialog } from "@/components/session/PermissionRequestDialog";
import { FileBrowserSheet } from "@/components/file-browser/FileBrowserSheet";
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
import { useSession, useAbortSession, useUpdateSession, useOpenCodeClient, useMessages, useConfig } from "@/hooks/useOpenCode";
import { useSession, useAbortSession, useUpdateSession, useOpenCodeClient, useMessages } from "@/hooks/useOpenCode";
import { OPENCODE_API_ENDPOINT } from "@/config";
import { getSessionModel } from "@/lib/model";
import { useSSE } from "@/hooks/useSSE";
import { useSettings } from "@/hooks/useSettings";
import { useKeyboardShortcuts } from "@/hooks/useKeyboardShortcuts";
@@ -65,9 +64,6 @@ export function SessionDetail() {
const repoDirectory = repo?.fullPath;
const { data: messages, isLoading: messagesLoading } = useMessages(opcodeUrl, sessionId, repoDirectory);
const { data: config } = useConfig(opcodeUrl);
const currentSessionModel = useMemo(() => getSessionModel(messages, config?.model), [messages, config?.model]);
const { scrollToBottom } = useAutoScroll({
containerRef: messageContainerRef,
@@ -261,7 +257,7 @@ export function SessionDetail() {
open={modelDialogOpen}
onOpenChange={setModelDialogOpen}
opcodeUrl={opcodeUrl}
currentSessionModel={currentSessionModel}
directory={repoDirectory}
/>
{/* Sessions Dialog */}
+79
View File
@@ -0,0 +1,79 @@
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
export interface ModelSelection {
providerID: string
modelID: string
}
interface ModelStore {
model: ModelSelection | null
recentModels: ModelSelection[]
isInitialized: boolean
setModel: (model: ModelSelection) => void
initializeFromConfig: (configModel: string | undefined) => void
getModelString: () => string | null
}
const MAX_RECENT_MODELS = 10
function parseModelString(model: string): ModelSelection | null {
const [providerID, ...rest] = model.split('/')
const modelID = rest.join('/')
if (!providerID || !modelID) return null
return { providerID, modelID }
}
export const useModelStore = create<ModelStore>()(
persist(
(set, get) => ({
model: null,
recentModels: [],
isInitialized: false,
setModel: (model: ModelSelection) => {
set((state) => {
const newRecent = [
model,
...state.recentModels.filter(
(m) => !(m.providerID === model.providerID && m.modelID === model.modelID)
),
].slice(0, MAX_RECENT_MODELS)
return {
model,
recentModels: newRecent,
}
})
},
initializeFromConfig: (configModel: string | undefined) => {
const state = get()
if (state.isInitialized) return
if (!state.model && configModel) {
const parsed = parseModelString(configModel)
if (parsed) {
set({ model: parsed, isInitialized: true })
return
}
}
set({ isInitialized: true })
},
getModelString: () => {
const { model } = get()
if (!model) return null
return `${model.providerID}/${model.modelID}`
},
}),
{
name: 'opencode-model-selection',
partialize: (state) => ({
model: state.model,
recentModels: state.recentModels,
}),
}
)
)
+26
View File
@@ -21,3 +21,29 @@ export const CredentialStatusResponseSchema = z.object({
export const CredentialListResponseSchema = z.object({
providers: z.array(z.string()),
});
export const ProviderAuthMethodSchema = z.object({
type: z.enum(["oauth", "api"]),
label: z.string(),
});
export const ProviderAuthMethodsSchema = z.record(z.array(ProviderAuthMethodSchema));
export const OAuthAuthorizeRequestSchema = z.object({
method: z.number(),
});
export const OAuthAuthorizeResponseSchema = z.object({
url: z.string(),
method: z.enum(["auto", "code"]),
instructions: z.string(),
});
export const OAuthCallbackRequestSchema = z.object({
method: z.number(),
code: z.string().optional(),
});
export const ProviderAuthMethodsResponseSchema = z.object({
providers: z.record(z.array(ProviderAuthMethodSchema)),
});