Skip to main content

Creating Hooks

The AI can scaffold, write, and validate hooks for you — end to end.

Example Prompts

"Create a new hook in finance-bills that calculates late fees on overdue invoices. Use the creating-hooks and flow-query skills."

"Add a hook to confirm a sales order and trigger inventory movements."

"Scaffold a new hook called import-general-ledger-accounts in the data import library."

What the AI Does

1. Scaffolds the files
→ Runs the CLI: npx tsx scripts/logic-bee-create-new-hook.ts --command '{...}'
→ Creates the hook folder with .hook.ts, hook.yml, and context.yaml

2. Looks up pattern examples
→ Searches for similar hooks: npx tsx scripts/logic-bee-search-for-hook-pattern.ts --pattern calculate
→ Reads the top match to understand the implementation pattern

3. Writes the business logic
→ Follows all code style rules automatically:
• Starts with const eventOptions = { ... }
• Uses arrow comments (// -->Get:, // -->Set:)
• Uses naoUtils.mathChain() for math, naoDateTime() for dates
• Throws errors with naoFormatErrorById()
• Ends with ok = true; data = result

4. Validates the code
→ Checks 10 hard rules (ok/data assigned, flowUser passed, etc.)
→ Checks 12 soft conventions (eventOptions first, mathChain for math, etc.)

Result

The AI generates a complete hook at hooks/{library-slug}/{method-slug}/{method-slug}.{library-slug}.hook.ts:

@LogicHook({
name: 'FinanceBillsCalculateLateFees',
path: 'finance-bills/calculate-late-fees',
library: 'finance-bills',
method: 'calculate-late-fees',
legacy: 'app/app.financeBillsHooks.calculateLateFees'
})
export class FinanceBillsCalculateLateFees {
public static execute() {
return async (bob: BobRequest<{ data: { data: any; naoQueryOptions: NaoQueryOptions } }>) => {
let ok = true, error: any = null, data
try {
// -->Get: bills collection
const fc = bob.flowGlobal.getFlowCollection(eventOptions.billNaoQueryOptions)
// -->Get: overdue bills
const bills = await fc.docs.flowQuery()
.user(bob.flowUser).flowOptions(eventOptions.billNaoQueryOptions)
.query({ 'data.status': 'posted', 'data.dueDate': { $lt: naoDateTime().toJSDate() } })
.getMany(undefined, 'FinanceInterface.Bill')
// ... business logic ...
ok = true
data = bills
} catch (err) { error = err; ok = false }
return returnEventResult(bob, { ok, error, data })
}
}
}

Tips for Better Results

  • Be specific about the library — say "finance-bills" or "inventory-management", not just "a hook"
  • Describe the business logic — "calculate late fees based on days overdue" gives better code than "handle late fees"
  • Mention edge cases — "skip bills with zero balance" helps generate guard clauses
  • Reference skills — adding "Use the creating-hooks and flow-query skills" gives the AI precise context
  • Ask for a pattern search first — "find me an example of a calculate hook" before asking it to write one