Files
opencode-manager/shared/src/opencode/mcp.ts
T
Chris Scott 65b72e55ae refactor: unify model selection with the TUI
Align the manager's model selection with the OpenCode TUI: one
model.json-backed preference store, one provider catalog, one ordering
and default-ownership rule, and one set of availability, ref-formatting
and variant helpers shared across chat, schedules, agents and the goal
auditor.

- Add a shared TUI-compatible model-preference module and make the
  backend model.json store match its format.
- Consolidate the frontend provider catalog, availability check, model
  ref formatter and variant persistence into single owners.
- Drive chat, schedule, agent and goal-auditor model lists from a shared
  useModelSections hook and buildModelSections, following TUI ordering.
- Extract a shared ModelCombobox with a taller list, and list models the
  TUI way in ModelQuickSelect.
- Add a shared isRecord util and model-selection comparison helpers.
2026-10-06 20:44:27 -04:00

81 lines
2.6 KiB
TypeScript

import type { ConfigEntry, McpServer } from '@opencode/client'
import { isRecord } from '../utils/record'
export const MCP_OAUTH_CALLBACK_PATH = '/api/mcp-oauth-proxy/callback'
type ConfigDocumentInfo = Extract<ConfigEntry, { type: 'document' }>['info']
type McpConfig = NonNullable<ConfigDocumentInfo['mcp']>
export type McpServerConfig = NonNullable<McpConfig['servers']>[string]
export type McpTimeoutConfig = NonNullable<McpServerConfig['timeout']>
export type McpStatus = McpServer['status'] & Pick<McpServer, 'integrationID'>
export type McpStatusMap = Record<string, McpStatus>
export function mcpOAuthRedirectUri(origin: string): string {
return new URL(MCP_OAUTH_CALLBACK_PATH, origin).toString()
}
function isMcpServerConfig(value: unknown): value is McpServerConfig {
return isRecord(value) && (value.type === 'local' || value.type === 'remote')
}
export function mcpServersFromConfig(mcp: unknown): Record<string, McpServerConfig> {
if (!isRecord(mcp) || !isRecord(mcp.servers)) return {}
return Object.fromEntries(Object.entries(mcp.servers).filter((entry): entry is [string, McpServerConfig] => isMcpServerConfig(entry[1])))
}
export type McpServerShape = 'servers' | 'legacy'
export interface McpServerView {
name: string
type: 'local' | 'remote'
command?: string[]
url?: string
enabled: boolean
shape: McpServerShape
}
function toMcpServerView(
name: string,
config: McpServerConfig,
enabled: boolean,
shape: McpServerShape,
): McpServerView {
return {
name,
type: config.type,
...(config.type === 'local' ? { command: config.command } : { url: config.url }),
enabled,
shape,
}
}
export function mcpServerViewsFromConfig(mcp: unknown): McpServerView[] {
if (!isRecord(mcp)) return []
const nativeViews = Object.entries(mcpServersFromConfig(mcp)).map(([name, config]) =>
toMcpServerView(name, config, config.disabled !== true, 'servers'),
)
const legacyViews = Object.entries(mcp).flatMap(([name, value]) => {
if (name === 'servers' || name === 'timeout' || !isMcpServerConfig(value)) return []
const enabled = (value as { enabled?: unknown }).enabled !== false
return [toMcpServerView(name, value, enabled, 'legacy')]
})
return [...nativeViews, ...legacyViews].sort((left, right) => left.name.localeCompare(right.name))
}
export function mcpStatusByName(servers: McpServer[]): McpStatusMap {
return Object.fromEntries(
servers.map((server) => [
server.name,
{
...server.status,
...(server.integrationID ? { integrationID: server.integrationID } : {}),
},
]),
)
}