Add RuntimeTool model and enhance AssistantConfig for tool management

- Introduce a new `RuntimeTool` model to encapsulate tool data for runtime sessions, including attributes like `id`, `name`, `function_name`, `type`, and `description`.
- Update the `AssistantConfig` model to include a list of reusable tools, allowing for better management of tools within assistant configurations.
- Modify the `config_resolver` service to fetch and resolve tools associated with assistants, ensuring they are available during runtime.
- Refactor tool-related CRUD operations in the `tools` route to support the new runtime execution model, enhancing the overall tool management system.
- Update documentation and comments to reflect changes in tool execution and configuration handling, improving clarity for future development.
This commit is contained in:
Xin Wang
2026-07-10 14:32:10 +08:00
parent 5fca14865f
commit 4a57b290d3
5 changed files with 202 additions and 25 deletions

View File

@@ -4,8 +4,14 @@
助手按 capability binding 引用资源;取不到则回退该能力默认资源。
"""
from db.models import Assistant, AssistantModelBinding, ModelResource
from models import AssistantConfig
from db.models import (
Assistant,
AssistantModelBinding,
AssistantToolBinding,
ModelResource,
Tool,
)
from models import AssistantConfig, RuntimeTool
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
@@ -75,6 +81,33 @@ def _secret(resource: ModelResource | None, key: str, default: str = "") -> str:
return str((resource.secrets or {}).get(key) or default)
async def _tools_for(session: AsyncSession, assistant: Assistant) -> list[RuntimeTool]:
if assistant.type != "prompt":
return []
tools = (
await session.execute(
select(Tool)
.join(AssistantToolBinding, AssistantToolBinding.tool_id == Tool.id)
.where(
AssistantToolBinding.assistant_id == assistant.id,
Tool.status == "active",
)
.order_by(AssistantToolBinding.created_at, Tool.id)
)
).scalars().all()
return [
RuntimeTool(
id=tool.id,
name=tool.name,
function_name=tool.function_name,
type=tool.type,
description=tool.description,
definition=tool.definition or {},
)
for tool in tools
]
async def resolve_runtime_config(
session: AsyncSession, assistant_id: str
) -> AssistantConfig:
@@ -102,6 +135,7 @@ async def resolve_runtime_config(
prompt=assistant.prompt or "你是一个有帮助的助手。",
runtimeMode=assistant.runtime_mode, # type: ignore[arg-type]
enableInterrupt=assistant.enable_interrupt,
tools=await _tools_for(session, assistant),
# workflow 图:仅 workflow 类型非空,引擎据此启用图驱动对话
graph=(assistant.graph or {}) if assistant.type == "workflow" else {},
# 外部托管类型连接信息(DB 存真 key,直接注入)