๐ค Workflow: Base Agent (Details)
The Base Agent is the most critical workflow, responsible for executing AI tasks. The current version (_Script.json) utilizes a โCode-Firstโ design with logic separated into script files.
Processing Logic (The 6 Phases)
The workflow processes tasks through 6 sequential stages:
Phase 1: Parse Task (phase1_parse_task.js)
- Duty: Receives raw tasks from the n8n node (Supabase), cleaning and normalizing the data.
- Core Logic:
- Parses JSON string fields (
data,extra,ai_settings,hierarchy_path) into objects. - Maps full metadata:
launch_id,user_id,batch_id. - Ensures valid input
status. - Sorts priority (Test mode > Batch Priority > Sequence).
- Parses JSON string fields (
Phase 2: Pre-Process (phase2_pre_process.js)
- Duty: Prepares additional data before calling the AI.
- Core Logic:
- Checks
extra.pre_process.enabled. - If enabled, calls a webhook (
webhook_url) to fetch external data (e.g., retrieving lesson content from a CMS). - Merges returned results into the taskโs
data. - On failure: Can either
abortthe task orcontinuedepending on theon_failureconfiguration.
- Checks
Phase 3: AI Execution & Parsing (phase3_parse_report.js)
- Duty: Invokes the Gemini API, receives results, and reports Key health.
- Core Logic:
- API Call: Uses a key from the Key Rotator. Prioritizes
ai_settingsfrom Phase 2 (if overrides exist) > Phase 1. - Parsing: Filters JSON content from AI Markdown responses (handling
```jsonblocks). - Validation: Verifies if the JSON is valid.
- Reporting: Calls the
set-key-statewebhook to reportsuccessorfailto the Key Rotator. - Status Update: Sets
status: 'generated'. - Data Preservation: Preserves input metadata fields in the output.
- API Call: Uses a key from the Key Rotator. Prioritizes
Phase 4: Post-Process (phase4_post_process.js)
- Duty: Handles post-processing after obtaining AI results.
- Core Logic:
- Checks
extra.post_process.enabled. - Sends results (
result) to a destination webhook (e.g., saving to a vector DB, sending notifications). - Does not modify the primary
resultof the task; performs side-effects only.
- Checks
Phase 5: Prepare Next Tasks (phase5_prepare_next_tasks.js)
- Duty: Determines what needs to be done next (Next Stage).
- Core Logic:
- Reads
next_stage_configsfromextra. - Reads the
grt(Global Routing Table) for mapping. - Cardinality Handling:
one_to_one: 1 parent task produces 1 child task.one_to_many: 1 parent task produces N child tasks (based on an array within theresult).
- Hydration: Generates metadata for child tasks (calculating
sequence,step_number,hierarchy_path).
- Reads
Phase 6: Format Tasks (phase6_format_tasks.js)
- Duty: Normalizes the final format for insertion into the database.
- Core Logic:
- Converts the array of objects from Phase 5 into the standard Supabase format.
- Filters out redundant or erroneous fields.
Error Handling
- Retry Logic: The workflow includes an internal retry mechanism (3 times) for API errors (Network, 5xx).
- Key Rotation: If a Key fails (403, 429), it automatically reports the failure and requests a new key during the subsequent retry.
- Catch All: For logic errors (e.g., JSON Parse fail), the task is marked as
failed, and anerror_messageis logged to the DB for Manual Review.
Last updated on