File size: 2,019 Bytes
7d9309d | 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 | /**
* Command Parser for Prix AI
*
* Parses and validates Prix commands from GitHub comments.
* Supports multiple syntaxes and provides structured output.
*/
export interface ParsedCommand {
isPrixCommand: boolean
command: 'plan' | 'fix' | 'review' | 'help' | null
args: string[]
rawCommand: string
}
/**
* Parse a comment body to detect Prix commands
*
* Supported syntaxes:
* - !prix plan
* - /prix plan
* - prix plan
*
* @param commentBody The raw comment body
* @returns Parsed command object
*/
export const parsePrixCommand = (commentBody: string): ParsedCommand => {
const trimmed = commentBody.trim()
// Check if this is a Prix command
const prixCommandRegex = /^[/!]?prix\s+(\w+)(.*)$/i
const match = trimmed.match(prixCommandRegex)
if (!match) {
return {
isPrixCommand: false,
command: null,
args: [],
rawCommand: ''
}
}
const commandStr = match[1].toLowerCase()
const argsStr = match[2].trim()
const args = argsStr ? argsStr.split(/\s+/).filter(Boolean) : []
// Map command string to command type
const commandMap: Record<string, ParsedCommand['command']> = {
plan: 'plan',
fix: 'fix',
review: 'review',
help: 'help'
}
const command = commandMap[commandStr] || null
return {
isPrixCommand: true,
command,
args,
rawCommand: match[0]
}
}
/**
* Check if a comment is from a bot (to prevent loops)
*
* @param username The GitHub username
* @param botUsername The bot's username (default: 'prix-ai[bot]')
* @returns True if the comment is from a bot
*/
export const isBotComment = (
username: string | undefined,
botUsername: string = 'prix-ai[bot]'
): boolean => {
if (!username) return false
const normalizedUsername = username.toLowerCase()
const normalizedBot = botUsername.toLowerCase()
return (
normalizedUsername === normalizedBot || normalizedUsername.endsWith('[bot]')
)
}
|