Turn Chatbot Misunderstandings Into Grammar Regression Tests
A custom-grammar bot can be reliable right up until someone types a perfectly reasonable sentence that the grammar author did not anticipate. The uncomfortable choice is usually framed as either: keep the grammar deterministic but frustratingly narrow, or let an AI model interpret everything and accept less predictable behavior. That is the wrong boundary. You can keep command execution…
Build a small JavaScript bot that understands specific commands, such as feeding and reminding to feed cats, without allowing AI-generated guesses to execute actions. The bot's parsing process is separated into three outcomes: successfully parsed and domain-validated commands, unrecognized utterances, or commands that violate domain rules. To achieve this, follow these steps:
1. Set up the project by creating a directory, initializing it with npm, installing required packages (Peggy, Zod, Vitest), and updating the package.json file with build and test scripts.
2. Create a grammar file (commands.peggy) that defines the bot's understanding of commands using the Peggy parser. This grammar handles polite prefixes, optional punctuation, and quoted multiword names. It also separates command parsing from domain validation and execution.
3. Implement a time validation function (validTime) to ensure that the specified feeding time is within a valid range (0 to 23 hours and 0 to 59 minutes).
4. Validate the parsed command by checking if it meets domain rules, such as amount of grams for feeding (between 1 and 200 grams) and a valid time for reminders.
5. Parse the user's input using the generated parser and validate the command. If the command is valid, return it; otherwise, return an object indicating the validation failure and the specific reason.
By following this workflow, the bot maintains deterministic command execution while leveraging failed conversations to improve grammar and optionally using AI to propose interpretations for human confirmation.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

