Files
opencode-manager/frontend/src/hooks/useSessionAgent.ts
T
Chris Scott 7e93eb4c34 feat(commands): align built-in slash commands with OpenCode v2
Replace the split built-in command name list and switch with one typed
registry, so a registered command without a handler is a compile error.
Remove the broken themes/share/unshare/editor entries and the hardcoded
init; server and custom commands now win on a name clash and built-ins
dispatch by registry identity.

Add /copy, /btw, /rename, /fork (message picker), /timeline, /model,
/agent, /variants, /mcp, /skills, /settings and /connect. Open the model
picker through state so /model and the select-model shortcut work while a
response is running, load full session history in the fork picker, and
keep attachments when a built-in command is submitted.

Extract shared clipboard and primary-agent helpers, reuse CopyButton in
the manager token settings, and update the Quick Start and chat docs.
2026-10-02 09:53:57 -04:00

83 lines
2.3 KiB
TypeScript

import { useMemo, useEffect } from 'react'
import { useSession, useAgents } from './useOpenCode'
import { useSessionAgentStore } from '@/stores/sessionAgentStore'
import { getPrimaryAgents } from '@/lib/primaryAgents'
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
}
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,
}
}, [sessionAgent, fallbackAgent, defaultAgent, session?.model])
useEffect(() => {
if (session?.agent && sessionID) {
setAgent(sessionID, session.agent)
}
}, [session?.agent, sessionID, setAgent])
return { agent: result.agent, model: result.model, variant: result.variant }
}