2026-06-08 13:51:28 +08:00
|
|
|
"""异步数据库引擎 + 会话。
|
|
|
|
|
|
|
|
|
|
- engine / SessionLocal:全局单例
|
|
|
|
|
- get_session:FastAPI 依赖,按请求注入一个会话
|
2026-07-09 16:29:10 +08:00
|
|
|
- sync_interface_definitions:启动时同步接口定义;表结构由 Alembic 管理
|
2026-06-08 13:51:28 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from collections.abc import AsyncGenerator
|
2026-06-14 19:36:12 +08:00
|
|
|
import json
|
2026-06-08 13:51:28 +08:00
|
|
|
|
2026-07-09 21:40:29 +08:00
|
|
|
import settings
|
2026-06-14 19:36:12 +08:00
|
|
|
from services.interface_catalog import INTERFACE_DEFINITIONS
|
2026-06-09 14:42:25 +08:00
|
|
|
from sqlalchemy import text
|
2026-06-08 13:51:28 +08:00
|
|
|
from sqlalchemy.ext.asyncio import (
|
|
|
|
|
AsyncSession,
|
|
|
|
|
async_sessionmaker,
|
|
|
|
|
create_async_engine,
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-09 21:40:29 +08:00
|
|
|
engine = create_async_engine(settings.DATABASE_URL, echo=False, pool_pre_ping=True)
|
2026-06-08 13:51:28 +08:00
|
|
|
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
|
|
|
|
async with SessionLocal() as session:
|
|
|
|
|
yield session
|
|
|
|
|
|
|
|
|
|
|
2026-07-09 16:29:10 +08:00
|
|
|
async def sync_interface_definitions() -> None:
|
2026-06-08 13:51:28 +08:00
|
|
|
async with engine.begin() as conn:
|
2026-06-14 19:36:12 +08:00
|
|
|
for definition in INTERFACE_DEFINITIONS:
|
|
|
|
|
await conn.execute(
|
|
|
|
|
text(
|
|
|
|
|
"INSERT INTO interface_definitions "
|
|
|
|
|
"(interface_type, name, capability, field_schema, enabled, version) "
|
|
|
|
|
"VALUES (:interface_type, :name, :capability, CAST(:field_schema AS jsonb), TRUE, 1) "
|
|
|
|
|
"ON CONFLICT (interface_type) DO UPDATE SET "
|
|
|
|
|
"name = EXCLUDED.name, capability = EXCLUDED.capability, "
|
|
|
|
|
"field_schema = EXCLUDED.field_schema, enabled = TRUE, updated_at = now()"
|
|
|
|
|
),
|
|
|
|
|
{
|
|
|
|
|
"interface_type": definition["interface_type"],
|
|
|
|
|
"name": definition["name"],
|
|
|
|
|
"capability": definition["capability"],
|
|
|
|
|
"field_schema": json.dumps({"fields": definition["fields"]}),
|
|
|
|
|
},
|
2026-06-09 14:42:25 +08:00
|
|
|
)
|
2026-07-10 13:49:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def sync_default_tools() -> None:
|
|
|
|
|
"""Ensure system-provided reusable tools exist without overwriting edits."""
|
|
|
|
|
async with engine.begin() as conn:
|
|
|
|
|
await conn.execute(
|
|
|
|
|
text(
|
|
|
|
|
"INSERT INTO tools "
|
|
|
|
|
"(id, name, function_name, type, description, definition, secrets, status) "
|
|
|
|
|
"VALUES ("
|
|
|
|
|
":id, :name, :function_name, :type, :description, "
|
|
|
|
|
"CAST(:definition AS jsonb), CAST(:secrets AS jsonb), :status"
|
|
|
|
|
") "
|
|
|
|
|
"ON CONFLICT (function_name) DO NOTHING"
|
|
|
|
|
),
|
|
|
|
|
{
|
|
|
|
|
"id": "tool_end_call_default",
|
|
|
|
|
"name": "结束对话",
|
|
|
|
|
"function_name": "end_call",
|
|
|
|
|
"type": "end_call",
|
|
|
|
|
"description": "当用户明确要求结束对话,或任务已完成时调用。",
|
|
|
|
|
"definition": json.dumps(
|
|
|
|
|
{
|
|
|
|
|
"schema_version": 1,
|
|
|
|
|
"type": "end_call",
|
|
|
|
|
"config": {
|
|
|
|
|
"message_type": "none",
|
|
|
|
|
"custom_message": "",
|
|
|
|
|
"capture_reason": True,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
"secrets": "{}",
|
|
|
|
|
"status": "active",
|
|
|
|
|
},
|
|
|
|
|
)
|