mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
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.
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import { useMemo, useEffect } from 'react'
|
|
import { useSession, useAgents } from './useOpenCode'
|
|
import { useSessionAgentStore } from '@/stores/sessionAgentStore'
|
|
import { getPrimaryAgents } from '@/lib/primaryAgents'
|
|
import type { ModelRef } from '@opencode-manager/shared/opencode'
|
|
|
|
interface AgentInfo {
|
|
id: string
|
|
name?: string
|
|
mode?: string
|
|
hidden?: boolean
|
|
}
|
|
|
|
const resolveAvailableAgentId = (
|
|
agentId: string | undefined,
|
|
agents: AgentInfo[] | undefined,
|
|
agentsLoaded: boolean
|
|
): string | undefined => {
|
|
if (!agentId) return undefined
|
|
if (!agentsLoaded) return agentId
|
|
|
|
const normalizedAgentId = agentId.toLowerCase()
|
|
return getPrimaryAgents(agents).find(
|
|
(agent) => agent.id.toLowerCase() === normalizedAgentId
|
|
)?.id
|
|
}
|
|
|
|
export function resolveDefaultSessionAgent(
|
|
agents: AgentInfo[] | undefined,
|
|
agentsLoaded: boolean
|
|
): string {
|
|
const primaryAgents = getPrimaryAgents(agents)
|
|
|
|
if (agentsLoaded && primaryAgents.length > 0) {
|
|
return primaryAgents[0].id
|
|
}
|
|
|
|
return 'build'
|
|
}
|
|
|
|
interface SessionAgentResult {
|
|
agent: string
|
|
model: { providerID: string; modelID: string } | undefined
|
|
variant: string | undefined
|
|
sessionAgentId: string | undefined
|
|
modelRef: ModelRef | undefined
|
|
}
|
|
|
|
export function useSessionAgent(
|
|
sessionID: string | undefined,
|
|
directory?: string
|
|
) {
|
|
const { data: session } = useSession(sessionID, directory)
|
|
const { data: agents, isSuccess: agentsLoaded } = useAgents(directory)
|
|
const storedAgent = useSessionAgentStore((s) => s.agents[sessionID ?? ''] ?? null)
|
|
const setAgent = useSessionAgentStore((s) => s.setAgent)
|
|
|
|
const defaultAgent = useMemo(
|
|
() => resolveDefaultSessionAgent(agents, agentsLoaded),
|
|
[agents, agentsLoaded]
|
|
)
|
|
|
|
const sessionAgent = resolveAvailableAgentId(session?.agent, agents, agentsLoaded)
|
|
const fallbackAgent = resolveAvailableAgentId(storedAgent ?? undefined, agents, agentsLoaded)
|
|
|
|
const result = useMemo<SessionAgentResult>(() => {
|
|
const model = session?.model
|
|
? { providerID: session.model.providerID, modelID: session.model.id }
|
|
: undefined
|
|
|
|
return {
|
|
agent: sessionAgent ?? fallbackAgent ?? defaultAgent,
|
|
model,
|
|
variant: session?.model?.variant,
|
|
sessionAgentId: session?.agent,
|
|
modelRef: session?.model,
|
|
}
|
|
}, [sessionAgent, fallbackAgent, defaultAgent, session?.model, session?.agent])
|
|
|
|
useEffect(() => {
|
|
if (session?.agent && sessionID) {
|
|
setAgent(sessionID, session.agent)
|
|
}
|
|
}, [session?.agent, sessionID, setAgent])
|
|
|
|
return {
|
|
agent: result.agent,
|
|
model: result.model,
|
|
variant: result.variant,
|
|
sessionAgentId: result.sessionAgentId,
|
|
modelRef: result.modelRef,
|
|
}
|
|
}
|