Files
ai-video-fullstack/backend/models.py
Xin Wang 4a57b290d3 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.
2026-07-10 14:32:10 +08:00

111 lines
3.4 KiB
Python

"""请求/配置数据模型。
分两层(重要):
- AssistantConfig:**运行时**配置,含真 key,只在后端内部流转,绝不返回前端。
由 config_resolver 从 DB 组装(或信令内联传入)。
- schemas.py 里的 *Request/*Response:面向前端的 DTO(key 打码)。
"""
from __future__ import annotations
from typing import Literal
from pydantic import BaseModel, Field
RuntimeMode = Literal["pipeline", "realtime"]
class RuntimeTool(BaseModel):
"""Tool data resolved from an assistant binding for one runtime session."""
id: str
name: str
function_name: str
type: str
description: str = ""
definition: dict = Field(default_factory=dict)
class AssistantConfig(BaseModel):
"""运行时配置:前端可见部分(name/prompt/...) + 服务端注入部分(*_api_key/*_base_url)。"""
name: str = "未命名助手"
# prompt|workflow|dify|fastgpt|opencode;决定由哪种「大脑」驱动对话
type: str = "prompt"
greeting: str = "您好,我是 AI 视频助手,请问有什么可以帮您?"
prompt: str = "你是一个有帮助的助手。"
runtimeMode: RuntimeMode = "pipeline"
# 模型/音色选项
model: str = "" # LLM
asr: str = "" # STT
tts_model: str = ""
voice: str = "" # TTS 音色
stt_language: str = ""
tts_speed: float = 1.0
realtimeModel: str = ""
realtime_interface_type: str = ""
realtime_values: dict = {}
realtime_secrets: dict = {}
agent_interface_type: str = ""
agent_values: dict = {}
agent_secrets: dict = {}
llm_interface_type: str = "openai-llm"
stt_interface_type: str = "openai-asr"
tts_interface_type: str = "openai-tts"
llm_values: dict = {}
llm_secrets: dict = {}
llm_support_image_input: bool = False
vision_enabled: bool = False
vision_model_resource_id: str | None = None
vision_model: str = ""
vision_llm_interface_type: str = "openai-llm"
vision_llm_values: dict = {}
vision_llm_secrets: dict = {}
vision_llm_support_image_input: bool = False
vision_llm_api_key: str = ""
vision_llm_base_url: str = ""
stt_values: dict = {}
stt_secrets: dict = {}
tts_values: dict = {}
tts_secrets: dict = {}
enableInterrupt: bool = True
# Prompt assistant reusable tools. Execution remains type-specific in the pipeline.
tools: list[RuntimeTool] = Field(default_factory=list)
# workflow 类型:节点图(nodes/edges)。非 workflow 为空,引擎据此决定是否启用。
graph: dict = {}
# 外部托管类型(fastgpt/dify/opencode)的连接信息:context/KB/tools 由对方服务端接管。
fastgpt_api_url: str = ""
fastgpt_api_key: str = ""
fastgpt_app_id: str = ""
# ---- 运行时连接信息(服务端注入,不来自浏览器) ----
# 由模型资源注入;调试 inline_config 也必须显式提供。
llm_api_key: str = ""
llm_base_url: str = ""
stt_api_key: str = ""
stt_base_url: str = ""
tts_api_key: str = ""
tts_base_url: str = ""
realtime_api_key: str = ""
realtime_base_url: str = ""
class SignalingOffer(BaseModel):
"""WS 信令里 offer 消息的 payload。
推荐用 assistant_id(浏览器只传 id,key 在服务端解析);
inline_config 仅用于调试/无库场景。
"""
pc_id: str | None = None
sdp: str
type: str
assistant_id: str | None = None
inline_config: AssistantConfig | None = None
vision_enabled: bool = False