Build lightweight AI agent admin

This commit is contained in:
Codex
2026-06-08 18:14:59 +08:00
commit e164840f43
2530 changed files with 435693 additions and 0 deletions
+101
View File
@@ -0,0 +1,101 @@
import asyncio
import importlib
import pkgutil
from logging.config import fileConfig
from pathlib import Path
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
from app.database import Base
from app.config import settings
def auto_import_models():
"""
自动导入所有模块下的*model.py文件
扫描项目根目录下所有以model结尾的py文件(如model.py、screen_model.py、material_model.py
"""
project_root = Path(__file__).parent.parent
# 需要扫描的目录(可以添加更多)
scan_dirs = ["zq_demo", "core", "scheduler", "online_dev", "ai_platform", "zq_smart_table"]
for scan_dir in scan_dirs:
scan_path = project_root / scan_dir
if not scan_path.exists():
continue
# 递归查找所有以model结尾的py文件(如model.py、screen_model.py、material_model.py
for model_file in scan_path.rglob("*model.py"):
# 计算模块路径,如 zq_demo.demo.model 或 core.screen_design.screen_model
relative_path = model_file.relative_to(project_root)
module_path = str(relative_path.with_suffix("")).replace("/", ".").replace("\\", ".")
try:
importlib.import_module(module_path)
except ImportError as e:
print(f"Warning: Failed to import {module_path}: {e}")
# 自动导入所有模型
auto_import_models()
config = context.config
# 设置数据库URL
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL)
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode."""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
"""Run migrations in 'online' mode with async engine."""
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+26
View File
@@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,55 @@
"""add stream event log table
Revision ID: a1b2c3d4e5f6
Revises: 9c178034aad7
Create Date: 2026-05-12 17:35:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'a1b2c3d4e5f6'
down_revision: Union[str, None] = '9c178034aad7'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
'core_dingtalk_stream_event_log',
sa.Column('event_type', sa.String(length=50), nullable=False, comment='事件类型'),
sa.Column('target_type', sa.String(length=20), nullable=False, comment='目标类型: dept/user'),
sa.Column('target_name', sa.String(length=100), nullable=True, comment='目标名称'),
sa.Column('dingtalk_dept_id', sa.String(length=50), nullable=True, comment='钉钉部门ID'),
sa.Column('dingtalk_userid', sa.String(length=100), nullable=True, comment='钉钉用户ID'),
sa.Column('status', sa.String(length=20), nullable=True, comment='状态: success/failed'),
sa.Column('error_detail', sa.Text(), nullable=True, comment='失败详情'),
sa.Column('event_time', sa.DateTime(), nullable=True, comment='事件时间'),
# BaseModel columns
sa.Column('id', sa.String(length=21), nullable=False, comment='主键ID(NanoId)'),
sa.Column('sort', sa.Integer(), nullable=True, comment='排序'),
sa.Column('is_deleted', sa.Boolean(), nullable=True, comment='是否删除'),
sa.Column('sys_create_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
sa.Column('sys_update_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
sa.Column('sys_creator_id', sa.String(length=21), nullable=True, comment='创建人ID'),
sa.Column('sys_modifier_id', sa.String(length=21), nullable=True, comment='修改人ID'),
sa.Column('sys_dept_id', sa.String(length=21), nullable=True, comment='部门ID'),
sa.PrimaryKeyConstraint('id'),
)
op.create_index('ix_core_dingtalk_stream_event_log_event_type', 'core_dingtalk_stream_event_log', ['event_type'])
op.create_index('ix_core_dingtalk_stream_event_log_target_type', 'core_dingtalk_stream_event_log', ['target_type'])
op.create_index('ix_core_dingtalk_stream_event_log_status', 'core_dingtalk_stream_event_log', ['status'])
op.create_index('ix_core_dingtalk_stream_event_log_is_deleted', 'core_dingtalk_stream_event_log', ['is_deleted'])
op.create_index('ix_core_dingtalk_stream_event_log_sys_create_datetime', 'core_dingtalk_stream_event_log', ['sys_create_datetime'])
def downgrade() -> None:
op.drop_index('ix_core_dingtalk_stream_event_log_sys_create_datetime', table_name='core_dingtalk_stream_event_log')
op.drop_index('ix_core_dingtalk_stream_event_log_is_deleted', table_name='core_dingtalk_stream_event_log')
op.drop_index('ix_core_dingtalk_stream_event_log_status', table_name='core_dingtalk_stream_event_log')
op.drop_index('ix_core_dingtalk_stream_event_log_target_type', table_name='core_dingtalk_stream_event_log')
op.drop_index('ix_core_dingtalk_stream_event_log_event_type', table_name='core_dingtalk_stream_event_log')
op.drop_table('core_dingtalk_stream_event_log')
@@ -0,0 +1,55 @@
"""add dingtalk todo record table
Revision ID: b2c3d4e5f6g7
Revises: a1b2c3d4e5f6
Create Date: 2026-05-13 19:50:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'b2c3d4e5f6g7'
down_revision: Union[str, None] = 'a1b2c3d4e5f6'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
'core_dingtalk_todo_record',
sa.Column('source_type', sa.String(length=50), nullable=False, comment='来源类型: workflow_task/workflow_instance/message'),
sa.Column('source_id', sa.String(length=50), nullable=False, comment='业务ID'),
sa.Column('user_id', sa.String(length=50), nullable=False, comment='系统用户ID'),
sa.Column('dingtalk_union_id', sa.String(length=100), nullable=False, comment='钉钉unionId'),
sa.Column('dingtalk_task_id', sa.String(length=100), nullable=False, comment='钉钉待办taskId'),
sa.Column('status', sa.String(length=20), nullable=True, server_default='pending', comment='状态: pending/done/deleted'),
# BaseModel columns
sa.Column('id', sa.String(length=21), nullable=False, comment='主键ID(NanoId)'),
sa.Column('sort', sa.Integer(), nullable=True, comment='排序'),
sa.Column('is_deleted', sa.Boolean(), nullable=True, comment='是否删除'),
sa.Column('sys_create_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
sa.Column('sys_update_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
sa.Column('sys_creator_id', sa.String(length=21), nullable=True, comment='创建人ID'),
sa.Column('sys_modifier_id', sa.String(length=21), nullable=True, comment='修改人ID'),
sa.Column('sys_dept_id', sa.String(length=21), nullable=True, comment='部门ID'),
sa.PrimaryKeyConstraint('id'),
)
op.create_index('ix_dingtalk_todo_source', 'core_dingtalk_todo_record', ['source_type', 'source_id'])
op.create_index('ix_dingtalk_todo_source_type', 'core_dingtalk_todo_record', ['source_type'])
op.create_index('ix_dingtalk_todo_source_id', 'core_dingtalk_todo_record', ['source_id'])
op.create_index('ix_dingtalk_todo_user_id', 'core_dingtalk_todo_record', ['user_id'])
op.create_index('ix_dingtalk_todo_status', 'core_dingtalk_todo_record', ['status'])
op.create_index('ix_dingtalk_todo_is_deleted', 'core_dingtalk_todo_record', ['is_deleted'])
def downgrade() -> None:
op.drop_index('ix_dingtalk_todo_is_deleted', table_name='core_dingtalk_todo_record')
op.drop_index('ix_dingtalk_todo_status', table_name='core_dingtalk_todo_record')
op.drop_index('ix_dingtalk_todo_user_id', table_name='core_dingtalk_todo_record')
op.drop_index('ix_dingtalk_todo_source_id', table_name='core_dingtalk_todo_record')
op.drop_index('ix_dingtalk_todo_source_type', table_name='core_dingtalk_todo_record')
op.drop_index('ix_dingtalk_todo_source', table_name='core_dingtalk_todo_record')
op.drop_table('core_dingtalk_todo_record')
@@ -0,0 +1,95 @@
"""add report tables
Revision ID: c3d4e5f6g7h8
Revises: b2c3d4e5f6g7
Create Date: 2026-05-22 10:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = 'c3d4e5f6g7h8'
down_revision: Union[str, None] = 'b2c3d4e5f6g7'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
_BASE_COLS = [
sa.Column('id', sa.String(length=21), nullable=False, comment='主键ID(NanoId)'),
sa.Column('sort', sa.Integer(), nullable=True, comment='排序'),
sa.Column('is_deleted', sa.Boolean(), nullable=True, comment='是否删除'),
sa.Column('sys_create_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
sa.Column('sys_update_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
sa.Column('sys_creator_id', sa.String(length=21), nullable=True, comment='创建人ID'),
sa.Column('sys_modifier_id', sa.String(length=21), nullable=True, comment='修改人ID'),
sa.Column('sys_dept_id', sa.String(length=21), nullable=True, comment='部门ID'),
]
def upgrade() -> None:
op.create_table(
'report_template',
sa.Column('application_id', sa.String(length=21), nullable=True, comment='所属应用ID'),
sa.Column('name', sa.String(length=100), nullable=False, comment='报表名称'),
sa.Column('code', sa.String(length=100), nullable=False, comment='报表编码'),
sa.Column('category', sa.String(length=50), nullable=True, comment='分类'),
sa.Column('description', sa.Text(), nullable=True, comment='描述'),
sa.Column('status', sa.String(length=20), nullable=True, server_default='draft', comment='状态'),
sa.Column('allow_export', sa.Boolean(), nullable=True, server_default='true', comment='允许导出'),
sa.Column('allow_print', sa.Boolean(), nullable=True, server_default='true', comment='允许打印'),
sa.Column('allow_watermark', sa.Boolean(), nullable=True, server_default='false', comment='允许水印'),
sa.Column('watermark_config', sa.JSON(), nullable=True, comment='水印配置'),
*_BASE_COLS,
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('code'),
)
op.create_index('ix_report_template_application_id', 'report_template', ['application_id'])
op.create_index('ix_report_template_status', 'report_template', ['status'])
op.create_index('ix_report_template_is_deleted', 'report_template', ['is_deleted'])
op.create_table(
'report_version',
sa.Column('template_id', sa.String(length=21), nullable=False, comment='模板ID'),
sa.Column('version', sa.Integer(), nullable=True, server_default='1', comment='版本号'),
sa.Column('state', sa.SmallInteger(), nullable=True, server_default='0', comment='版本状态'),
sa.Column('snapshot', sa.JSON(), nullable=True, comment='Univer snapshot'),
sa.Column('cells', sa.JSON(), nullable=True, comment='单元格绑定'),
sa.Column('query_list', sa.JSON(), nullable=True, comment='查询条件'),
sa.Column('sort_list', sa.JSON(), nullable=True, comment='排序'),
sa.Column('column_list', sa.JSON(), nullable=True, comment='分栏'),
sa.Column('fence_list', sa.JSON(), nullable=True, comment='围栏'),
sa.Column('convert_config', sa.JSON(), nullable=True, comment='转换配置'),
*_BASE_COLS,
sa.PrimaryKeyConstraint('id'),
)
op.create_index('ix_report_version_template_id', 'report_version', ['template_id'])
op.create_index('ix_report_version_state', 'report_version', ['state'])
op.create_index('ix_report_version_is_deleted', 'report_version', ['is_deleted'])
op.create_table(
'report_dataset',
sa.Column('version_id', sa.String(length=21), nullable=False, comment='版本ID'),
sa.Column('data_source_id', sa.String(length=21), nullable=False, comment='数据源ID'),
sa.Column('alias', sa.String(length=100), nullable=False, comment='别名'),
sa.Column('field_mapping', sa.JSON(), nullable=True, comment='字段映射'),
sa.Column('convert_config', sa.JSON(), nullable=True, comment='转换配置'),
*_BASE_COLS,
sa.PrimaryKeyConstraint('id'),
)
op.create_index('ix_report_dataset_version_id', 'report_dataset', ['version_id'])
op.create_index('ix_report_dataset_data_source_id', 'report_dataset', ['data_source_id'])
def downgrade() -> None:
op.drop_index('ix_report_dataset_data_source_id', table_name='report_dataset')
op.drop_index('ix_report_dataset_version_id', table_name='report_dataset')
op.drop_table('report_dataset')
op.drop_index('ix_report_version_is_deleted', table_name='report_version')
op.drop_index('ix_report_version_state', table_name='report_version')
op.drop_index('ix_report_version_template_id', table_name='report_version')
op.drop_table('report_version')
op.drop_index('ix_report_template_is_deleted', table_name='report_template')
op.drop_index('ix_report_template_status', table_name='report_template')
op.drop_index('ix_report_template_application_id', table_name='report_template')
op.drop_table('report_template')
@@ -0,0 +1,67 @@
"""add core_database_connection table
Revision ID: d4e5f6g7h8i9
Revises: c3d4e5f6g7h8
Create Date: 2026-05-29 10:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = 'd4e5f6g7h8i9'
down_revision: Union[str, None] = 'c3d4e5f6g7h8'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
_BASE_COLS = [
sa.Column('id', sa.String(length=21), nullable=False, comment='主键ID(NanoId)'),
sa.Column('sort', sa.Integer(), nullable=True, comment='排序'),
sa.Column('is_deleted', sa.Boolean(), nullable=True, comment='是否删除'),
sa.Column('sys_create_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
sa.Column('sys_update_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
sa.Column('sys_creator_id', sa.String(length=21), nullable=True, comment='创建人ID'),
sa.Column('sys_modifier_id', sa.String(length=21), nullable=True, comment='修改人ID'),
sa.Column('sys_dept_id', sa.String(length=21), nullable=True, comment='部门ID'),
]
def upgrade() -> None:
op.create_table(
'core_database_connection',
sa.Column('application_id', sa.String(length=21), nullable=True, comment='所属应用ID'),
sa.Column('code', sa.String(length=50), nullable=False, comment='连接编码'),
sa.Column('name', sa.String(length=100), nullable=False, comment='连接名称'),
sa.Column('db_type', sa.String(length=20), nullable=False, comment='数据库类型'),
sa.Column('host', sa.String(length=255), nullable=False, comment='主机'),
sa.Column('port', sa.Integer(), nullable=False, comment='端口'),
sa.Column('user', sa.String(length=100), nullable=True, comment='用户名'),
sa.Column('password_enc', sa.Text(), nullable=True, comment='加密密码'),
sa.Column('default_database', sa.String(length=100), nullable=True, comment='默认数据库名'),
sa.Column('description', sa.Text(), nullable=True, comment='描述'),
sa.Column('status', sa.Boolean(), nullable=True, server_default='true', comment='是否启用'),
sa.Column('is_system', sa.Boolean(), nullable=True, server_default='false', comment='是否系统内置'),
sa.Column('extra_options', sa.JSON(), nullable=True, comment='扩展选项'),
*_BASE_COLS,
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('code'),
)
op.create_index('ix_core_database_connection_application_id', 'core_database_connection', ['application_id'])
op.create_index('ix_core_database_connection_code', 'core_database_connection', ['code'])
op.create_index('ix_core_database_connection_status', 'core_database_connection', ['status'])
op.create_index('ix_core_database_connection_is_deleted', 'core_database_connection', ['is_deleted'])
op.create_index(
'ix_core_database_connection_app_status',
'core_database_connection',
['application_id', 'status'],
)
def downgrade() -> None:
op.drop_index('ix_core_database_connection_app_status', table_name='core_database_connection')
op.drop_index('ix_core_database_connection_is_deleted', table_name='core_database_connection')
op.drop_index('ix_core_database_connection_status', table_name='core_database_connection')
op.drop_index('ix_core_database_connection_code', table_name='core_database_connection')
op.drop_index('ix_core_database_connection_application_id', table_name='core_database_connection')
op.drop_table('core_database_connection')
@@ -0,0 +1,56 @@
"""add workflow run metadata columns
Revision ID: e5f6g7h8i9j0
Revises: d4e5f6g7h8i9
Create Date: 2026-06-01 12:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = 'e5f6g7h8i9j0'
down_revision: Union[str, None] = 'd4e5f6g7h8i9'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
existing_columns = {
column['name']
for column in inspector.get_columns('ai_workflow_run')
}
if 'trigger_type' not in existing_columns:
op.add_column(
'ai_workflow_run',
sa.Column('trigger_type', sa.String(length=30), nullable=True, comment='触发来源'),
)
if 'use_draft' not in existing_columns:
op.add_column(
'ai_workflow_run',
sa.Column('use_draft', sa.Boolean(), nullable=True, comment='是否使用草稿定义执行'),
)
if 'workflow_version' not in existing_columns:
op.add_column(
'ai_workflow_run',
sa.Column('workflow_version', sa.Integer(), nullable=True, comment='执行时发布版本号'),
)
if 'definition_snapshot' not in existing_columns:
op.add_column(
'ai_workflow_run',
sa.Column('definition_snapshot', sa.JSON(), nullable=True, comment='运行开始时的工作流定义快照'),
)
op.execute("UPDATE ai_workflow_run SET trigger_type = 'api' WHERE trigger_type IS NULL")
op.execute("UPDATE ai_workflow_run SET use_draft = false WHERE use_draft IS NULL")
op.execute("UPDATE ai_workflow_run SET definition_snapshot = '{}' WHERE definition_snapshot IS NULL")
def downgrade() -> None:
op.drop_column('ai_workflow_run', 'definition_snapshot')
op.drop_column('ai_workflow_run', 'workflow_version')
op.drop_column('ai_workflow_run', 'use_draft')
op.drop_column('ai_workflow_run', 'trigger_type')