Files
opencode-manager/frontend/src/hooks/useCommandHandler.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

68 lines
2.1 KiB
TypeScript

import { useCallback } from 'react'
import { runCommand } from '@/api/opencode'
import type { PromptAgentInput, PromptFileInput, PromptSkillInput } from '@/api/opencode'
import { useSyncSessionSelection } from '@/hooks/useOpenCode'
import { showToast } from '@/lib/toast'
import type { CommandInfo, ModelRef } from '@opencode-manager/shared/opencode'
import { useSessionStatus } from '@/stores/sessionStatusStore'
import { isBuiltinCommand, type CommandActions } from '@/lib/builtinCommands'
interface CommandSubmission {
text: string
files?: PromptFileInput[]
agents?: PromptAgentInput[]
skills?: PromptSkillInput[]
}
interface CommandHandlerProps {
sessionID: string
directory?: string
model?: ModelRef
currentAgent?: string
actions: CommandActions
}
export function useCommandHandler({
sessionID,
directory,
model,
currentAgent,
actions,
}: CommandHandlerProps) {
const syncSelection = useSyncSessionSelection(directory)
const setSessionStatus = useSessionStatus((state) => state.setStatus)
const executeCommand = useCallback(async (command: CommandInfo, submission: CommandSubmission = { text: '' }): Promise<boolean> => {
if (isBuiltinCommand(command)) {
try {
await actions[command.action](submission.text)
return !command.keepsPrompt
} catch (error) {
showToast.error(`Command failed: ${error instanceof Error ? error.message : 'Unknown error'}`)
return false
}
}
try {
await syncSelection({ sessionID, model, agent: currentAgent })
await runCommand({
sessionID,
name: command.name,
text: submission.text,
...(submission.files ? { files: submission.files } : {}),
...(submission.agents ? { agents: submission.agents } : {}),
...(submission.skills ? { skills: submission.skills } : {}),
})
return true
} catch (error) {
showToast.error(`Command failed: ${error instanceof Error ? error.message : 'Unknown error'}`)
setSessionStatus(sessionID, { type: 'idle' })
return false
}
}, [actions, sessionID, model, syncSelection, currentAgent, setSessionStatus])
return {
executeCommand
}
}