All files / src/utils slash-commands.ts

32.91% Statements 53/161
100% Branches 0/0
0% Functions 0/6
32.91% Lines 53/161

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213            1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                                                                                           1x 1x 1x 1x 1x   1x   1x 1x                                   1x   1x 1x                                                     1x   1x 1x                               1x   1x 1x                                 1x     1x 1x                                                      
export interface SlashCommand {
	name: string
	description?: string
	section?: "default" | "custom"
}
 
export const DEFAULT_SLASH_COMMANDS: SlashCommand[] = [
	{
		name: "newtask",
		description: "Create a new task with context from the current task",
		section: "default",
	},
	{
		name: "smol",
		description: "Condenses your current context window",
		section: "default",
	},
	{
		name: "newrule",
		description: "Create a new AI-Hydro rule based on your conversation",
		section: "default",
	},
	{
		name: "reportbug",
		description: "Create a GitHub issue for AI-Hydro",
		section: "default",
	},
	{
		name: "deep-planning",
		description: "Create a comprehensive implementation plan before coding",
		section: "default",
	},
	{
		name: "subagent",
		description: "Invoke an AI-Hydro CLI subagent for focused research tasks",
		section: "default",
	},
]
 
export function getWorkflowCommands(
	localWorkflowToggles: Record<string, boolean>,
	globalWorkflowToggles: Record<string, boolean>,
): SlashCommand[] {
	const { workflows: localWorkflows, nameSet: localWorkflowNames } = Object.entries(localWorkflowToggles)
		.filter(([_, enabled]) => enabled)
		.reduce(
			(acc, [filePath, _]) => {
				const fileName = filePath.replace(/^.*[/\\]/, "")
 
				// Add to array of workflows
				acc.workflows.push({
					name: fileName,
					section: "custom",
				} as SlashCommand)
 
				// Add to set of names
				acc.nameSet.add(fileName)
 
				return acc
			},
			{ workflows: [] as SlashCommand[], nameSet: new Set<string>() },
		)
 
	const globalWorkflows = Object.entries(globalWorkflowToggles)
		.filter(([_, enabled]) => enabled)
		.flatMap(([filePath, _]) => {
			const fileName = filePath.replace(/^.*[/\\]/, "")
 
			// skip if a local workflow with the same name exists
			if (localWorkflowNames.has(fileName)) {
				return []
			}
 
			return [
				{
					name: fileName,
					section: "custom",
				},
			] as SlashCommand[]
		})
 
	const workflows = [...localWorkflows, ...globalWorkflows]
	return workflows
}
 
// Regex for detecting slash commands in text
// currently doesn't allow whitespace inside of the filename
export const slashCommandRegex = /\/([a-zA-Z0-9_.-]+)(\s|$)/
export const slashCommandRegexGlobal = new RegExp(slashCommandRegex.source, "g")
export const slashCommandDeleteRegex = /^\s*\/([a-zA-Z0-9_.-]+)$/
 
/**
 * Removes a slash command at the cursor position
 */
export function removeSlashCommand(text: string, position: number): { newText: string; newPosition: number } {
	const beforeCursor = text.slice(0, position)
	const afterCursor = text.slice(position)
 
	// Check if we're at the end of a slash command
	const matchEnd = beforeCursor.match(slashCommandDeleteRegex)
 
	if (matchEnd) {
		// If we're at the end of a slash command, remove it
		const newText = text.slice(0, position - matchEnd[0].length) + afterCursor.replace(" ", "") // removes the first space after the command
		const newPosition = position - matchEnd[0].length
		return { newText, newPosition }
	}
 
	// If we're not at the end of a slash command, just return the original text and position
	return { newText: text, newPosition: position }
}
 
/**
 * Determines whether the slash command menu should be displayed based on text input
 */
export function shouldShowSlashCommandsMenu(text: string, cursorPosition: number): boolean {
	const beforeCursor = text.slice(0, cursorPosition)
 
	// first check if there is a slash before the cursor
	const slashIndex = beforeCursor.lastIndexOf("/")
 
	if (slashIndex === -1) {
		return false
	}
 
	// check if slash is at the very beginning (with optional whitespace)
	const textBeforeSlash = beforeCursor.slice(0, slashIndex)
	if (!/^\s*$/.test(textBeforeSlash)) {
		return false
	}
 
	// potential partial or full command
	const textAfterSlash = beforeCursor.slice(slashIndex + 1)
 
	// don't show menu if there's whitespace after the slash but before the cursor
	if (/\s/.test(textAfterSlash)) {
		return false
	}
 
	return true
}
 
/**
 * Gets filtered slash commands that match the current input
 */
export function getMatchingSlashCommands(
	query: string,
	localWorkflowToggles: Record<string, boolean> = {},
	globalWorkflowToggles: Record<string, boolean> = {},
): SlashCommand[] {
	const workflowCommands = getWorkflowCommands(localWorkflowToggles, globalWorkflowToggles)
	const allCommands = [...DEFAULT_SLASH_COMMANDS, ...workflowCommands]
 
	if (!query) {
		return allCommands
	}
 
	// filter commands that start with the query (case sensitive)
	return allCommands.filter((cmd) => cmd.name.startsWith(query))
}
 
/**
 * Insert a slash command at position or replace partial command
 */
export function insertSlashCommand(
	text: string,
	commandName: string,
	partialCommandLength: number,
): { newValue: string; commandIndex: number } {
	const slashIndex = text.indexOf("/")
 
	const beforeSlash = text.substring(0, slashIndex + 1)
	const afterPartialCommand = text.substring(slashIndex + 1 + partialCommandLength)
 
	// replace the partial command with the full command
	const newValue =
		beforeSlash + commandName + (afterPartialCommand.startsWith(" ") ? afterPartialCommand : " " + afterPartialCommand)
 
	return { newValue, commandIndex: slashIndex }
}
 
/**
 * Determines the validation state of a slash command
 * Returns partial if we have a partial match against valid commands, or full for full match
 */
export function validateSlashCommand(
	command: string,
	localWorkflowToggles: Record<string, boolean> = {},
	globalWorkflowToggles: Record<string, boolean> = {},
): "full" | "partial" | null {
	if (!command) {
		return null
	}
 
	const workflowCommands = getWorkflowCommands(localWorkflowToggles, globalWorkflowToggles)
	const allCommands = [...DEFAULT_SLASH_COMMANDS, ...workflowCommands]
 
	// case sensitive matching
	const exactMatch = allCommands.some((cmd) => cmd.name === command)
 
	if (exactMatch) {
		return "full"
	}
 
	const partialMatch = allCommands.some((cmd) => cmd.name.startsWith(command))
 
	if (partialMatch) {
		return "partial"
	}
 
	return null // no match
}