Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions apps/desktop/src/app/settings/custom-endpoints-settings.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import type { CustomEndpoint, CustomEndpointsResponse } from '@/types/hermes'

const activateCustomEndpoint = vi.fn()
const deleteCustomEndpoint = vi.fn()
const getCustomEndpoints = vi.fn()
const saveCustomEndpoint = vi.fn()
const validateCustomEndpoint = vi.fn()

vi.mock('@/hermes', () => ({
activateCustomEndpoint: (id: string) => activateCustomEndpoint(id),
deleteCustomEndpoint: (id: string, source?: string) => deleteCustomEndpoint(id, source),
getCustomEndpoints: () => getCustomEndpoints(),
saveCustomEndpoint: (endpoint: unknown) => saveCustomEndpoint(endpoint),
validateCustomEndpoint: (endpoint: unknown) => validateCustomEndpoint(endpoint)
}))

vi.mock('@/lib/haptics', () => ({ triggerHaptic: vi.fn() }))

vi.mock('@/store/notifications', () => ({
notify: vi.fn(),
notifyError: vi.fn()
}))

function endpoint(id: string, source: string, patch: Partial<CustomEndpoint> = {}): CustomEndpoint {
return {
base_url: `https://${id}.example/v1`,
discover_models: true,
has_api_key: false,
id,
model: `${id}/model`,
models: [`${id}/model`],
name: id,
source,
...patch
}
}

function response(endpoints: CustomEndpoint[]): CustomEndpointsResponse {
return {
current: { base_url: '', model: '', provider: '' },
endpoints
}
}

beforeEach(() => {
activateCustomEndpoint.mockResolvedValue({ model: 'modern/model', ok: true, provider: 'modern' })
deleteCustomEndpoint.mockResolvedValue(response([]))
saveCustomEndpoint.mockResolvedValue(response([]))
validateCustomEndpoint.mockResolvedValue({ message: '', models: [], ok: true, reachable: true })
vi.spyOn(window, 'confirm').mockReturnValue(true)
})

afterEach(() => {
cleanup()
vi.restoreAllMocks()
vi.clearAllMocks()
})

async function renderSettings() {
const { CustomEndpointsSettings } = await import('./custom-endpoints-settings')

return render(<CustomEndpointsSettings />)
}

describe('CustomEndpointsSettings', () => {
it('renders legacy custom_providers entries as delete-only summaries and refreshes after deletion', async () => {
const legacy = endpoint('legacy-0-abcd1234', 'custom_providers', {
api_key_preview: 'sk-...1234',
base_url: 'https://legacy-proxy.example/v1',
has_api_key: true,
is_current: true,
model: 'legacy-proxy/model',
name: 'Legacy Proxy'
})

getCustomEndpoints.mockResolvedValueOnce(response([legacy])).mockResolvedValueOnce(response([]))

await renderSettings()

expect(await screen.findByText('Legacy config')).toBeTruthy()
expect(screen.getByText('https://legacy-proxy.example/v1')).toBeTruthy()
expect(screen.getByText('legacy-proxy/model')).toBeTruthy()
expect(screen.getByText('sk-...1234')).toBeTruthy()
expect(screen.queryByRole('button', { name: /^Legacy Proxy/ })).toBeNull()
expect(screen.queryByRole('button', { name: 'Use' })).toBeNull()
expect((screen.getByLabelText('Name') as HTMLInputElement).value).toBe('')

fireEvent.click(screen.getByRole('button', { name: 'Delete Legacy Proxy' }))

await waitFor(() => expect(deleteCustomEndpoint).toHaveBeenCalledWith('legacy-0-abcd1234', 'custom_providers'))
await waitFor(() => expect(getCustomEndpoints).toHaveBeenCalledTimes(2))
await waitFor(() => expect(screen.queryByText('Legacy config')).toBeNull())
})

it('keeps modern and direct-config endpoints selectable with their existing actions', async () => {
const modern = endpoint('modern', 'providers', { name: 'Modern Proxy' })
const direct = endpoint('custom', 'direct-config', { name: 'Direct Custom' })
getCustomEndpoints.mockResolvedValue(response([modern, direct]))

await renderSettings()
await screen.findByText('Modern Proxy')

fireEvent.click(screen.getByRole('button', { name: 'New endpoint' }))
fireEvent.click(screen.getByRole('button', { name: /^Direct Custom/ }))
expect((screen.getByLabelText('Name') as HTMLInputElement).value).toBe('Direct Custom')

fireEvent.click(screen.getByRole('button', { name: 'New endpoint' }))
fireEvent.click(screen.getByRole('button', { name: /^Modern Proxy/ }))
expect((screen.getByLabelText('Name') as HTMLInputElement).value).toBe('Modern Proxy')

expect(screen.getAllByRole('button', { name: 'Use' })).toHaveLength(2)
expect(screen.getByRole('button', { name: 'Delete Modern Proxy' })).toBeTruthy()
expect(screen.queryByRole('button', { name: 'Delete Direct Custom' })).toBeNull()

fireEvent.click(screen.getAllByRole('button', { name: 'Use' })[0])
await waitFor(() => expect(activateCustomEndpoint).toHaveBeenCalledWith('modern'))
await waitFor(() => expect(getCustomEndpoints).toHaveBeenCalledTimes(2))
})
})
132 changes: 80 additions & 52 deletions apps/desktop/src/app/settings/custom-endpoints-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ function formFromEndpoint(endpoint: CustomEndpoint): EndpointForm {
}
}

function isLegacyEndpoint(endpoint: CustomEndpoint) {
return endpoint.source === 'custom_providers'
}

function EndpointSummary({ endpoint }: { endpoint: CustomEndpoint }) {
return (
<>
<div className="flex min-w-0 items-center gap-2">
<span className="truncate text-sm font-medium">{endpoint.name}</span>
{endpoint.is_current && (
<Pill tone="primary">
<Check className="size-3" />
Active
</Pill>
)}
{isLegacyEndpoint(endpoint) && <Pill tone="warn">Legacy config</Pill>}
{endpoint.source === 'direct-config' && <Pill>config.yaml</Pill>}
</div>
<div className="mt-1 truncate font-mono text-[0.7rem] text-muted-foreground">{endpoint.base_url}</div>
<div className="mt-1 flex flex-wrap gap-2 text-xs text-muted-foreground">
<span>{endpoint.model}</span>
{endpoint.has_api_key && <span>{endpoint.api_key_preview ?? 'API key set'}</span>}
</div>
</>
)
}

function toPayload(form: EndpointForm, models?: string[]): CustomEndpointUpdate {
const contextLength = Number.parseInt(form.contextLength, 10)

Expand Down Expand Up @@ -101,7 +128,8 @@ export function CustomEndpointsSettings({ onConfigSaved, onMainModelChanged }: C
}

setEndpoints(data.endpoints)
const current = data.endpoints.find(endpoint => endpoint.is_current) ?? data.endpoints[0]
const editableEndpoints = data.endpoints.filter(endpoint => !isLegacyEndpoint(endpoint))
const current = editableEndpoints.find(endpoint => endpoint.is_current) ?? editableEndpoints[0]

if (current) {
setForm(formFromEndpoint(current))
Expand Down Expand Up @@ -201,8 +229,8 @@ export function CustomEndpointsSettings({ onConfigSaved, onMainModelChanged }: C

try {
setDeleting(endpoint.id)
const response = await deleteCustomEndpoint(endpoint.id)
setEndpoints(response.endpoints)
await deleteCustomEndpoint(endpoint.id, endpoint.source)
await refresh()

if (form.id === endpoint.id) {
setForm(EMPTY_FORM)
Expand Down Expand Up @@ -232,59 +260,59 @@ export function CustomEndpointsSettings({ onConfigSaved, onMainModelChanged }: C
<SectionHeading icon={Globe} meta={`${endpoints.length}`} title="Custom Endpoints" />
<div className="divide-y divide-border/40 rounded-md border border-border/50">
{endpoints.length ? (
endpoints.map(endpoint => (
<div className="grid gap-3 p-3 sm:grid-cols-[minmax(0,1fr)_auto] sm:items-center" key={endpoint.id}>
<button
className="min-w-0 text-left"
onClick={() => {
setForm(formFromEndpoint(endpoint))
setDiscoveredModels(endpoint.models)
}}
type="button"
endpoints.map(endpoint => {
const isLegacy = isLegacyEndpoint(endpoint)

return (
<div
className="grid gap-3 p-3 sm:grid-cols-[minmax(0,1fr)_auto] sm:items-center"
key={`${endpoint.source ?? 'unknown'}:${endpoint.id}`}
>
<div className="flex min-w-0 items-center gap-2">
<span className="truncate text-sm font-medium">{endpoint.name}</span>
{endpoint.is_current && (
<Pill tone="primary">
<Check className="size-3" />
Active
</Pill>
)}
{endpoint.source === 'direct-config' && <Pill>config.yaml</Pill>}
</div>
<div className="mt-1 truncate font-mono text-[0.7rem] text-muted-foreground">
{endpoint.base_url}
</div>
<div className="mt-1 flex flex-wrap gap-2 text-xs text-muted-foreground">
<span>{endpoint.model}</span>
{endpoint.has_api_key && <span>{endpoint.api_key_preview ?? 'API key set'}</span>}
</div>
</button>
<div className="flex items-center gap-2 sm:justify-end">
<Button
disabled={endpoint.is_current || activating === endpoint.id}
onClick={() => void handleActivate(endpoint)}
size="sm"
variant="outline"
>
{activating === endpoint.id ? <Loader2 className="animate-spin" /> : <Zap />}
Use
</Button>
{endpoint.source !== 'direct-config' && (
<Button
className="hover:text-destructive"
disabled={deleting === endpoint.id}
onClick={() => void handleDelete(endpoint)}
size="icon-sm"
title="Delete endpoint"
variant="ghost"
{isLegacy ? (
<div className="min-w-0">
<EndpointSummary endpoint={endpoint} />
</div>
) : (
<button
className="min-w-0 text-left"
onClick={() => {
setForm(formFromEndpoint(endpoint))
setDiscoveredModels(endpoint.models)
}}
type="button"
>
{deleting === endpoint.id ? <Loader2 className="animate-spin" /> : <Trash2 />}
</Button>
<EndpointSummary endpoint={endpoint} />
</button>
)}
<div className="flex items-center gap-2 sm:justify-end">
{!isLegacy && (
<Button
disabled={endpoint.is_current || activating === endpoint.id}
onClick={() => void handleActivate(endpoint)}
size="sm"
variant="outline"
>
{activating === endpoint.id ? <Loader2 className="animate-spin" /> : <Zap />}
Use
</Button>
)}
{endpoint.source !== 'direct-config' && (
<Button
aria-label={`Delete ${endpoint.name}`}
className="hover:text-destructive"
disabled={deleting === endpoint.id}
onClick={() => void handleDelete(endpoint)}
size="icon-sm"
title="Delete endpoint"
variant="ghost"
>
{deleting === endpoint.id ? <Loader2 className="animate-spin" /> : <Trash2 />}
</Button>
)}
</div>
</div>
</div>
))
)
})
) : (
<EmptyState description="Add an OpenAI-compatible endpoint below." title="No custom endpoints" />
)}
Expand Down
20 changes: 20 additions & 0 deletions apps/desktop/src/hermes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AUDIO_TRANSCRIBE_MIN_REQUEST_TIMEOUT_MS,
audioSpeakRequestTimeoutMs,
audioTranscribeRequestTimeoutMs,
deleteCustomEndpoint,
getAllSessionMessages,
getCronJobs,
getGlobalModelInfo,
Expand Down Expand Up @@ -310,6 +311,25 @@ describe('Hermes REST helpers', () => {
}
})

it('uses a source-qualified query route only for legacy custom endpoints', async () => {
setApiRequestProfile('xiaoxuxu')
await deleteCustomEndpoint('legacy-2-a/b', 'custom_providers')

expect(api).toHaveBeenLastCalledWith({
method: 'DELETE',
path: '/api/providers/custom-endpoints?endpoint_id=legacy-2-a%2Fb&source=custom_providers',
profile: 'xiaoxuxu'
})

await deleteCustomEndpoint('modern', 'providers')

expect(api).toHaveBeenLastCalledWith({
method: 'DELETE',
path: '/api/providers/custom-endpoints/modern',
profile: 'xiaoxuxu'
})
})

it('keeps the liveness poll on the short default so a dead backend fails fast', async () => {
api.mockResolvedValue({})
api.mockClear()
Expand Down
12 changes: 11 additions & 1 deletion apps/desktop/src/hermes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,17 @@ export function activateCustomEndpoint(id: string): Promise<{ ok: boolean; provi
})
}

export function deleteCustomEndpoint(id: string): Promise<CustomEndpointsResponse> {
export function deleteCustomEndpoint(id: string, source?: string): Promise<CustomEndpointsResponse> {
if (source === 'custom_providers') {
const params = new URLSearchParams({ endpoint_id: id, source })

return window.hermesDesktop.api<CustomEndpointsResponse>({
...profileScoped(),
path: `/api/providers/custom-endpoints?${params.toString()}`,
method: 'DELETE'
})
}

return window.hermesDesktop.api<CustomEndpointsResponse>({
...profileScoped(),
path: `/api/providers/custom-endpoints/${encodeURIComponent(id)}`,
Expand Down
14 changes: 8 additions & 6 deletions hermes_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,21 +1047,23 @@ def clear_model_endpoint_credentials(
clear_api_mode: bool = True,
clear_base_url: bool = False,
) -> Dict[str, Any]:
"""Remove stale inline endpoint credentials from a model config.
"""Remove stale endpoint credentials and transport from a model config.

``model.api_key`` is valid only for explicit custom endpoint assignments.
Built-in providers resolve credentials from env vars, auth.json, or the
credential pool. When switching away from a custom endpoint, leaving these
fields behind keeps secrets in config.yaml and can contaminate later custom
resolution paths.
Custom assignments may use inline keys or environment references, and may
select their API protocol through either the legacy or canonical field.
When switching away, leaving any of those aliases behind can contaminate
later provider resolution.
"""
if not isinstance(model_cfg, dict):
return model_cfg
if clear_api_key:
model_cfg.pop("api_key", None)
model_cfg.pop("key_env", None)
model_cfg.pop("api_key_env", None)
model_cfg.pop("api", None)
if clear_api_mode:
model_cfg.pop("api_mode", None)
model_cfg.pop("transport", None)
if clear_base_url:
model_cfg.pop("base_url", None)
return model_cfg
Expand Down
Loading