Files
opencode-manager/frontend/src/api/fetchWrapper.ts
T
Chris Scott 03bd3dac95 feat(frontend): improve model selection experience (#237)
* chore: remove dead code and unused exports

* fix: preserve model form during config refresh

Signed-off-by: mehmet turac <mehmetturac@gmail.com>

* loop: model-select-fix completed after 11 iterations

* fix(frontend): show provider prefix for duplicate models in quick select dropdown

When the same model exists under multiple providers, show the provider name
for both entries so users can distinguish them. Detects duplicates by modelID
across all favorites and recents.

* refactor(frontend): replace useTallScrollContent with scroll visibility hook

* fix: harden mirror push rollback

* Remove Load button from skill cards, clicking card loads skill

* feat(backend): add plugin installation support and schema updates

* refactor(frontend): remove unused useHeaderScrollVisibility hook

* refactor(frontend): replace model dropdown with bottom sheet selector

Replace DropdownMenu-based ModelQuickSelect with a bottom sheet
implementation featuring favorites, recents, provider browsing, and
search. Remove onShowModelsDialog prop from PromptInput and
SessionDetail, using data attribute trigger instead. Fix ZodIssueCode
deprecation, input text color, and portal rendering for bottom sheet.

* fix(frontend): simplify session model restore and fix mobile swipe reset

* loop: single-source-models completed after 10 iterations

* refactor(frontend): add desktop sidebar layout to model quick select

* refactor(frontend): improve model selector with connected providers, favorites, and recent management

* fix(frontend): constrain model name width to prevent overflow in prompt input

* fix(frontend): adjust padding and styling in model quick select

* fix(frontend): add optimistic removal with rollback for recent models

---------
2026-05-25 09:47:59 -04:00

121 lines
3.3 KiB
TypeScript

import { FetchError } from '@opencode-manager/shared'
import type { ApiErrorResponse } from '@opencode-manager/shared'
export { FetchError }
interface FetchWrapperOptions extends RequestInit {
timeout?: number
params?: Record<string, string | number | boolean | undefined>
}
function formatDetails(details: unknown): string | undefined {
if (Array.isArray(details)) {
return details
.map((d) => {
if (typeof d !== 'object' || d === null) return null
const path = Array.isArray((d as Record<string, unknown>).path)
? ((d as Record<string, unknown>).path as string[])
: undefined
const message = typeof (d as Record<string, unknown>).message === 'string'
? (d as Record<string, unknown>).message as string
: undefined
return path?.length ? `${path.join('.')}: ${message}` : message
})
.filter(Boolean)
.join('; ')
}
if (typeof details === 'string') return details
return undefined
}
async function handleResponse(response: Response): Promise<never> {
const data: ApiErrorResponse = await response.json().catch(() => ({ error: 'An error occurred' }))
const detail = data.detail || formatDetails(data.details)
throw new FetchError(
data.error || 'Request failed',
response.status,
data.code,
detail,
{
details: data.details,
validationIssues: data.validationIssues,
removedFields: data.removedFields,
}
)
}
function buildUrl(url: string, params?: Record<string, string | number | boolean | undefined>): URL {
const urlObj = new URL(url, window.location.origin)
if (params) {
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined) {
urlObj.searchParams.append(key, String(value))
}
})
}
return urlObj
}
async function fetchWithTimeout(
url: string,
options: FetchWrapperOptions = {}
): Promise<Response> {
const { timeout = 30000, params, ...fetchOptions } = options
const urlObj = buildUrl(url, params)
const controller = new AbortController()
const timeoutId = timeout > 0 ? setTimeout(() => controller.abort(), timeout) : null
try {
const response = await fetch(urlObj.toString(), {
credentials: 'include',
...fetchOptions,
signal: controller.signal,
})
if (timeoutId) clearTimeout(timeoutId)
if (!response.ok) {
await handleResponse(response)
}
return response
} catch (error) {
if (timeoutId) clearTimeout(timeoutId)
if (error instanceof Error && error.name === 'AbortError') {
throw new FetchError('Request timeout', 408, 'TIMEOUT')
}
throw error
}
}
async function fetchWrapper<T = unknown>(
url: string,
options: FetchWrapperOptions = {}
): Promise<T> {
const response = await fetchWithTimeout(url, options)
try {
return await response.json()
} catch {
throw new FetchError('Invalid JSON response', response.status, 'INVALID_JSON')
}
}
async function fetchWrapperVoid(
url: string,
options: FetchWrapperOptions = {}
): Promise<void> {
await fetchWithTimeout(url, options)
}
async function fetchWrapperBlob(
url: string,
options: FetchWrapperOptions = {}
): Promise<Blob> {
const response = await fetchWithTimeout(url, options)
return response.blob()
}
export { fetchWrapper, fetchWrapperVoid, fetchWrapperBlob }