AI Workflows
The guided workflow builder and natural-language authoring — how a user actually creates a workflow.
Why a guided wizard, not a drag-and-drop canvas
This app is entirely server-rendered (Next.js Server Components + Server Actions) with no client-side canvas/graph library anywhere in the codebase. A literal node-graph editor would have been the first heavy client-side SPA surface in this project — a multi-times-larger scope than everything else in this milestone combined, for the same functional capability. The guided wizard (src/components/automation/ workflow-wizard.tsx) delivers the same real capability — pick a trigger, add conditions, add actions, set a schedule, set safety options, see a live human-readable summary — as a step-by-step form instead.
The backend doesn't know or care which UI built a workflow. Every piece — WorkflowEngine, trigger-engine.ts, condition-engine.ts, action-engine.ts, scheduler.ts, the Execution Log, the Template Library — operates on the same Workflow/WorkflowCondition/ WorkflowAction/ScheduleConfig JSON shape (src/lib/automation/ types.ts). A future drag-and-drop visual editor would produce and consume that exact same shape through the exact same queries.ts CRUD functions and createWorkflowAction/updateWorkflowAction Server Actions — no backend change required.
The wizard's 6 sections
1. Natural language (optional) — a free-text box + "Generate Draft" button. Calls the Workflow Parser (below); the resulting draft pre-fills every field below it, but nothing is saved yet. 2. Name & Trigger — pick from the 18 trigger types; a small set of common config fields (threshold %, threshold count, target %) covers every trigger that needs a number. 3. Conditions — add/remove condition rows (type, operator, value), with AND/OR logic for the whole list. 4. Actions — add/remove action rows (type + a small set of relevant config fields per action type), each with an optional delay. 5. Scheduling — none (event-driven only) / once / daily / weekly / monthly / market session. 6. Safety — dry-run, confirmation mode, cooldown seconds (see docs/EXECUTION_SAFETY.md).
A live human-readable summary ("When daily drawdown fires AND symbol equals EURUSD → reduce risk, send notification") updates as the form changes. Saving always creates the workflow as draft — a user must explicitly activate it from the Workflows tab.
Natural-language authoring
WorkflowParser (src/lib/automation/workflow-parser.ts):
1. The user's free text is sanitized (sanitizeUserInput(), same prompt-injection defense-in-depth as the AI Platform's chat/prop-firm inputs) and rate-limited (checkAIRateLimit('automation', userId), 50/hour). 2. Sent to the AI provider with the workflow_parse prompt (src/lib/ai/prompts/automation.ts, registered in the existing PROMPT_DEFAULTS — reuses AIPromptManager exactly as-is), which lists the real 18/17/14-type vocabulary inline and asks for a single JSON object. 3. validateWorkflowDraft() parses the response and rejects any trigger/condition/action type outside the real vocabulary — the model cannot invent a type the rest of the engine can't execute. A malformed response throws a clear WorkflowParseError, never silently produces a broken draft. 4. The validated draft, including the model's own self-reported confidence score, is returned to the wizard for the user to review and edit. It is never saved automatically, regardless of confidence.
Example requests this handles
| Request | Produces |
|---|---|
| "Pause copying after three consecutive losses." | trigger: consecutive_losses (threshold 3), action: pause_copying |
| "Reduce risk to 0.25% after a 2% daily drawdown." | trigger: daily_drawdown (thresholdPercent 2), action: reduce_risk |
| "Only copy EURUSD during the London session." | condition: symbol equals EURUSD, condition: session equals london |
| "Disable trading during high-impact news." | trigger: news_event, action: disable_strategy |
Strategy templates
9 system-seeded templates (workflow_templates, is_system = true, readable by every user, never deletable outside a migration) — FTMO Safe Mode, Apex Evaluation Protection, Conservative Risk, Aggressive Growth, News Filter, Weekend Protection, Daily Loss Lock, Profit Lock, Multi-Account Scaling. Each has parameterized variables (e.g. FTMO Safe Mode's implicit drawdown warning threshold) a user can override before instantiating — instantiateTemplate() resolves every {{var}} placeholder, preserving numeric types (a "{{lossStreak}}" placeholder becomes the number 3, not the string "3", since trigger-engine.ts's comparisons need real numbers). A user can also save any of their own workflows as a personal template (is_system: false, visible only to them alongside the system library).