Chuyển tới nội dung chính

🗄️ Database Schema

The system utilizes PostgreSQL on Supabase. Below is a detailed description of the tables within the orchable module.


1. Core Table: ai_tasks

The central table, serving both as data storage and as a Queue for AI Agents.

ColumnTypeDescription
idUUIDPrimary Key
task_typeVARCHARTask type, used for routing (e.g., core_question_gen)
stage_keyVARCHARStage key within the Orchestrator (e.g., core_question)
statusVARCHARplanprocessinggeneratedcompleted / failed
input_dataJSONBInput data for the prompt
output_dataJSONBJSON result from AI (alias result)
error_messageTEXTError log if failed
prompt_template_idTEXTTemplate ID used to generate content
batch_idUUIDFK → task_batches
parent_task_idUUIDParent task (if a sub-task)
root_task_idUUIDOriginal root task in the chain
hierarchy_pathJSONBArray of ancestor IDs: [root_id, ..., parent_id]
step_numberINTStep order in the flow
next_task_configJSONBNext Stage configuration
extraJSONBExtended config: grt, pre_process, post_process
launch_idUUIDSession ID for a specific run
requires_approvalBOOLEANEnabled when manual approval is needed before proceeding
approved_atTIMESTAMPTZTimestamp of approval
retry_countINTNumber of retries performed

2. Batch Table: task_batches

Manages run batches (e.g., creating course material for an entire grade).

ColumnTypeDescription
idUUIDPrimary Key
nameVARCHARBatch name
orchestrator_idUUIDFK → Orchestrator configuration
statusVARCHARpending, processing, completed, failed, paused
total_tasksINTExpected total root tasks
completed_tasksINTNumber of completed tasks
failed_tasksINTNumber of failed tasks
configJSONBGeneral configuration for the batch
launch_idUUIDRun identifier

3. Prompt Template Table: prompt_templates

Stores all prompts and stage configurations for the Orchestrator.

ColumnTypeDescription
idTEXTPrimary Key (typically orchestratorId_stageKey_stageId)
nameVARCHARTemplate name
descriptionTEXTDescription
templateTEXTPrompt content
versionINTVersion
is_activeBOOLEANWhether it is currently active
default_ai_settingsJSONBModel, temperature, topP, topK, maxOutputTokens
input_schemaJSONBJSON schema for input data
output_schemaJSONBJSON schema for output (used for structured output)
stage_configJSONBStage configuration: cardinality, split_path, merge_path, etc.
next_stage_template_idsTEXT[]Array of template IDs for the next stage
requires_approvalBOOLEANRequires approval before proceeding
organization_codeTEXTID of the Orchestrator owning the template
custom_component_idUUIDFK → custom_components (Custom View component)
view_configJSONBDisplay configuration: hiddenFields, delimiters, etc.

4. Asset Registry Table: custom_components

Repository for custom UI Components written in TSX, used to render task results in specialized ways.

ColumnTypeDescription
idUUIDPrimary Key
nameVARCHARComponent name
descriptionTEXTDescription
codeTEXTTSX source code (executed within a sandbox)
mock_dataJSONBMock data for previewing
is_publicBOOLEANPublicly accessible to all users
created_atTIMESTAMPTZCreation timestamp
created_byUUIDFK → auth.users

5. API Key Table: user_api_keys

Manages the pool of API Keys for rotation to avoid rate limits.

ColumnTypeDescription
idUUIDPrimary Key
key_nameVARCHARMnemonic name
api_key_encryptedTEXTAPI Key content
is_activeBOOLEANAutomatically switches to false if too many errors occur
priorityINTUsage priority
model_preferenceVARCHARDefault model (e.g., gemini-2.0-flash)

6. Key Health Table: api_key_health

ColumnTypeDescription
user_api_key_idUUIDFK → user_api_keys
last_used_atTIMESTAMPTZLast used timestamp
consecutive_failuresINTNumber of consecutive failures
blocked_untilTIMESTAMPTZBlocking expiration timestamp
block_reasonVARCHARReason for blocking

ERD Overview

erDiagram
task_batches ||--|{ ai_tasks : contains
ai_tasks ||--|{ ai_tasks : "parent-child"
ai_tasks }|--|| prompt_templates : uses
prompt_templates }|--o| custom_components : "links to"
user_api_keys ||--|| api_key_health : tracks

task_batches {
uuid id PK
string status
uuid orchestrator_id
jsonb config
}
ai_tasks {
uuid id PK
uuid batch_id FK
uuid parent_task_id FK
string stage_key
string status
jsonb output_data
}
prompt_templates {
text id PK
text template
uuid custom_component_id FK
jsonb stage_config
jsonb view_config
}
custom_components {
uuid id PK
text code
boolean is_public
}

List of Migrations (Chronological)

FileDateDescription
ai_tasks_schema.sqlBaseOriginal schema for ai_tasks
task_batches_schema.sqlBaseOriginal schema for task_batches
prompt_templates_schema.sqlBaseOriginal schema for prompt_templates
20260215_generalize_task_batches.sql2026-02-15Generalization of task_batches
20260216_fix_batch_counter_trigger.sql2026-02-16Fix for task counter trigger
20260216_fix_rls_permissive.sql2026-02-16Relaxed RLS to fix access errors
20260217_fix_launch_id_propagation.sql2026-02-17Fix for lost launch_id during sub-task creation
20260220_add_n1_merge_support.sql2026-02-20Added N:1 merge support
20260221_add_input_mapping_to_configs.sql2026-02-21Added input_mapping
20260221_fix_rpc_and_counters.sql2026-02-21Fix for RPC and counters
20260221_optimize_view_performance.sql2026-02-21Optimized v_runnable_tasks view
20260222_add_isolated_batch_creation.sql2026-02-22Added isolated batch_grouping
20240223_add_view_config_to_prompt_templates.sql2026-02-23Added view_config column
`20260223_add_delete_policies.sql"2026-02-23Added RLS delete + cascade RPC
20260223_add_retry_rpc.sql2026-02-23Added RPC for task/batch retry
20260223_tighten_rls.sql2026-02-23Tightened RLS isolation by user
20260224_add_custom_components_table.sql2026-02-24Created custom_components table + linkage

Last Updated: 2026-02-24