Add wait_for_response functionality to ToolResource and related components. Update API models, schemas, and routers to support new parameter. Enhance UI components to manage wait_for_response state, ensuring proper integration across the application.

This commit is contained in:
Xin Wang
2026-02-27 16:54:39 +08:00
parent 95c6e93a9c
commit 229243e832
9 changed files with 303 additions and 44 deletions

View File

@@ -98,6 +98,7 @@ class ToolResource(Base):
http_timeout_ms: Mapped[int] = mapped_column(Integer, default=10000)
parameter_schema: Mapped[dict] = mapped_column(JSON, default=dict)
parameter_defaults: Mapped[dict] = mapped_column(JSON, default=dict)
wait_for_response: Mapped[bool] = mapped_column(default=False)
enabled: Mapped[bool] = mapped_column(default=True)
is_system: Mapped[bool] = mapped_column(default=False)
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

View File

@@ -22,7 +22,13 @@ from ..schemas import (
AssistantOpenerAudioGenerateRequest,
AssistantOpenerAudioOut,
)
from .tools import TOOL_REGISTRY, TOOL_CATEGORY_MAP, TOOL_PARAMETER_DEFAULTS, _ensure_tool_resource_schema
from .tools import (
TOOL_REGISTRY,
TOOL_CATEGORY_MAP,
TOOL_PARAMETER_DEFAULTS,
TOOL_WAIT_FOR_RESPONSE_DEFAULTS,
_ensure_tool_resource_schema,
)
router = APIRouter(prefix="/assistants", tags=["Assistants"])
@@ -142,6 +148,11 @@ def _resolve_runtime_tools(db: Session, selected_tool_ids: List[str], warnings:
)
defaults_raw = resource.parameter_defaults if resource else TOOL_PARAMETER_DEFAULTS.get(tool_id)
defaults = dict(defaults_raw) if isinstance(defaults_raw, dict) else {}
wait_for_response = (
bool(resource.wait_for_response)
if resource
else bool(TOOL_WAIT_FOR_RESPONSE_DEFAULTS.get(tool_id, False))
)
if not resource and tool_id not in TOOL_REGISTRY:
warnings.append(f"Tool resource not found: {tool_id}")
@@ -160,6 +171,7 @@ def _resolve_runtime_tools(db: Session, selected_tool_ids: List[str], warnings:
},
"displayName": display_name or tool_id,
"toolId": tool_id,
"waitForResponse": wait_for_response,
}
if defaults:
runtime_tool["defaultArgs"] = defaults

View File

@@ -98,6 +98,17 @@ TOOL_REGISTRY = {
"required": ["msg"]
}
},
"text_msg_prompt": {
"name": "文本消息提示",
"description": "显示一条文本弹窗提示",
"parameters": {
"type": "object",
"properties": {
"msg": {"type": "string", "description": "提示文本内容"}
},
"required": ["msg"]
}
},
}
TOOL_CATEGORY_MAP = {
@@ -109,6 +120,7 @@ TOOL_CATEGORY_MAP = {
"increase_volume": "system",
"decrease_volume": "system",
"voice_message_prompt": "system",
"text_msg_prompt": "system",
}
TOOL_ICON_MAP = {
@@ -120,6 +132,7 @@ TOOL_ICON_MAP = {
"increase_volume": "Volume2",
"decrease_volume": "Volume2",
"voice_message_prompt": "Volume2",
"text_msg_prompt": "Terminal",
}
TOOL_HTTP_DEFAULTS = {
@@ -130,6 +143,10 @@ TOOL_PARAMETER_DEFAULTS = {
"decrease_volume": {"step": 1},
}
TOOL_WAIT_FOR_RESPONSE_DEFAULTS = {
"text_msg_prompt": True,
}
def _normalize_parameter_schema(value: Any, *, tool_id: Optional[str] = None) -> Dict[str, Any]:
if not isinstance(value, dict):
@@ -177,6 +194,9 @@ def _ensure_tool_resource_schema(db: Session) -> None:
if "parameter_defaults" not in columns:
db.execute(text("ALTER TABLE tool_resources ADD COLUMN parameter_defaults JSON"))
altered = True
if "wait_for_response" not in columns:
db.execute(text("ALTER TABLE tool_resources ADD COLUMN wait_for_response BOOLEAN DEFAULT 0"))
altered = True
if altered:
db.commit()
@@ -222,6 +242,7 @@ def _seed_default_tools_if_empty(db: Session) -> None:
http_timeout_ms=int(http_defaults.get("http_timeout_ms") or 10000),
parameter_schema=_normalize_parameter_schema(payload.get("parameters"), tool_id=tool_id),
parameter_defaults=_normalize_parameter_defaults(TOOL_PARAMETER_DEFAULTS.get(tool_id)),
wait_for_response=bool(TOOL_WAIT_FOR_RESPONSE_DEFAULTS.get(tool_id, False)),
enabled=True,
is_system=True,
))
@@ -311,6 +332,7 @@ def create_tool_resource(data: ToolResourceCreate, db: Session = Depends(get_db)
http_timeout_ms=max(1000, int(data.http_timeout_ms or 10000)),
parameter_schema=parameter_schema,
parameter_defaults=parameter_defaults,
wait_for_response=bool(data.wait_for_response) if data.category == "system" else False,
enabled=data.enabled,
is_system=False,
)
@@ -342,6 +364,8 @@ def update_tool_resource(id: str, data: ToolResourceUpdate, db: Session = Depend
update_data["parameter_schema"] = _normalize_parameter_schema(update_data.get("parameter_schema"), tool_id=id)
if "parameter_defaults" in update_data:
update_data["parameter_defaults"] = _normalize_parameter_defaults(update_data.get("parameter_defaults"))
if new_category != "system":
update_data["wait_for_response"] = False
for field, value in update_data.items():
setattr(item, field, value)

View File

@@ -241,6 +241,7 @@ class ToolResourceBase(BaseModel):
http_timeout_ms: int = 10000
parameter_schema: Dict[str, Any] = Field(default_factory=dict)
parameter_defaults: Dict[str, Any] = Field(default_factory=dict)
wait_for_response: bool = False
enabled: bool = True
@@ -259,6 +260,7 @@ class ToolResourceUpdate(BaseModel):
http_timeout_ms: Optional[int] = None
parameter_schema: Optional[Dict[str, Any]] = None
parameter_defaults: Optional[Dict[str, Any]] = None
wait_for_response: Optional[bool] = None
enabled: Optional[bool] = None