Add sync_default_tools function to manage system-provided tools

- Introduce a new `sync_default_tools` function in `session.py` to ensure essential reusable tools are created without overwriting existing edits.
- Update the `lifespan` context manager in `app.py` to call `sync_default_tools`, enhancing the initialization process for the application.
- This change improves the management of default tools within the system, ensuring they are available for use while preserving user modifications.
This commit is contained in:
Xin Wang
2026-07-10 13:49:35 +08:00
parent dc2a7484bb
commit 5fca14865f
2 changed files with 38 additions and 1 deletions

View File

@@ -16,7 +16,7 @@ from contextlib import asynccontextmanager
import settings
import uvicorn
from db.session import sync_interface_definitions
from db.session import sync_default_tools, sync_interface_definitions
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
@@ -36,6 +36,7 @@ from routes import (
@asynccontextmanager
async def lifespan(_app: FastAPI):
await sync_interface_definitions()
await sync_default_tools()
yield

View File

@@ -45,3 +45,39 @@ async def sync_interface_definitions() -> None:
"field_schema": json.dumps({"fields": definition["fields"]}),
},
)
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",
},
)