PRININIT / src /command-parser.ts
Rachit-Tw's picture
Upload 50 files
7d9309d verified
Raw
History Blame Contribute Delete
2.02 kB
/**
* 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]')
)
}