Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix (audio tools): fix hot reloading issues for audio tools based on configs

m056838700a4716ac3d684
diff --git a/jiuwenswarm/agents/harness/common/tools/multimodal_config.py b/jiuwenswarm/agents/harness/common/tools/multimodal_config.py
index dcf3d4d6a0..c9146e3d31 100644
--- a/jiuwenswarm/agents/harness/common/tools/multimodal_config.py
+++ b/jiuwenswarm/agents/harness/common/tools/multimodal_config.py
@@ -88,6 +88,21 @@ def dedicated_multimodal_model_configured(
return bool(api_key)
+def complete_multimodal_model_configured(
+ config_base: dict[str, Any] | None, model_type: str
+) -> bool:
+ """Whether `models.{model_type}` has api key, base URL, and model name."""
+ if model_type not in ("audio", "vision", "video"):
+ return False
+ if not isinstance(config_base, dict):
+ return False
+ mc = _get_model_config(config_base, model_type)
+ api_key = str(mc.get("api_key") or "").strip()
+ api_base = str(mc.get("api_base") or mc.get("base_url") or "").strip()
+ model_name = str(mc.get("model_name") or mc.get("model") or "").strip()
+ return bool(api_key and api_base and model_name)
+
+
def _get_embed_model_name(embed_cfg: dict[str, Any], model_type: str) -> str:
"""
从 embed 配置中获取指定类型的模型名称
diff --git a/jiuwenswarm/agents/swarm/providers/tools.py b/jiuwenswarm/agents/swarm/providers/tools.py
index 795821e821..9ff7836293 100644
--- a/jiuwenswarm/agents/swarm/providers/tools.py
+++ b/jiuwenswarm/agents/swarm/providers/tools.py
@@ -45,6 +45,7 @@ from jiuwenswarm.agents.harness.common.tools.multimodal_config import (
apply_video_model_config_from_yaml,
apply_vision_model_config_from_yaml,
dedicated_multimodal_model_configured,
+ complete_multimodal_model_configured,
)
from jiuwenswarm.agents.harness.common.tools.skill_retrieval_toolkits import (
SkillRetrievalToolkit,
@@ -270,17 +271,13 @@ def vision_model_config_params(config: dict[str, Any]) -> dict[str, Any]:
def audio_dedicated_configured(config: dict[str, Any]) -> bool:
- """Return whether a dedicated audio model is configured."""
- return dedicated_multimodal_model_configured(config, "audio")
+ """Return whether a complete dedicated audio model is configured."""
+ return complete_multimodal_model_configured(config, "audio")
def audio_model_config_params(config: dict[str, Any]) -> dict[str, Any]:
- """Return ``AudioModelConfig`` constructor kwargs, or {} when incomplete.
-
- Empty result with a dedicated key present means the core ``audio`` element
- keeps only ``audio_metadata`` (the degraded fallback).
- """
- if not dedicated_multimodal_model_configured(config, "audio"):
+ """Return ``AudioModelConfig`` constructor kwargs, or {} when incomplete."""
+ if not complete_multimodal_model_configured(config, "audio"):
return {}
apply_audio_model_config_from_yaml(config)
api_key = str(os.getenv("AUDIO_API_KEY", "")).strip()
@@ -370,12 +367,15 @@ def _build_user_todo_tools() -> list[Any]:
def _build_video_tools(ctx: SwarmBuildContext) -> list[Any]:
- """Build the video understanding tool when ``models.video`` is configured."""
+ """Build the video understanding tool when ``models.video`` is complete."""
config = ctx.config or {}
apply_video_model_config_from_yaml(config)
- if not dedicated_multimodal_model_configured(config, "video"):
+ if not complete_multimodal_model_configured(config, "video"):
return []
- if not os.getenv("VIDEO_API_KEY"):
+ video_api_key = str(os.getenv("VIDEO_API_KEY", "")).strip()
+ video_api_base = str(os.getenv("VIDEO_API_BASE", "")).strip()
+ video_model_name = str(os.getenv("VIDEO_MODEL_NAME", "")).strip()
+ if not video_api_key or not video_api_base or not video_model_name:
return []
return _mark_stateless([video_understanding])
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
index a3685f5fa7..a70847a7fc 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
@@ -191,6 +191,7 @@ from jiuwenswarm.agents.harness.common.tools.multimodal_config import (
apply_video_model_config_from_yaml,
apply_vision_model_config_from_yaml,
dedicated_multimodal_model_configured,
+ complete_multimodal_model_configured,
)
from jiuwenswarm.agents.harness.common.tools.video_tools import video_understanding
from jiuwenswarm.agents.harness.common.tools.image_tools import generate_image
@@ -1471,10 +1472,10 @@ class JiuWenSwarmDeepAdapter:
config_base: dict[str, Any],
) -> AudioModelConfig | None:
"""Build DeepAgent audio config from service config/env mapping."""
- if not dedicated_multimodal_model_configured(config_base, "audio"):
+ if not complete_multimodal_model_configured(config_base, "audio"):
logger.info(
- "[JiuWenSwarmDeepAdapter] skip full audio LLM config: models.audio has no "
- "dedicated api_key in config.yaml"
+ "[JiuWenSwarmDeepAdapter] audio tools skipped: models.audio requires "
+ "api_key, api_base, and model_name in config.yaml"
)
return None
apply_audio_model_config_from_yaml(config_base)
@@ -1520,13 +1521,16 @@ class JiuWenSwarmDeepAdapter:
) -> bool:
"""Build DeepAgent video config from service config/env mapping."""
apply_video_model_config_from_yaml(config_base)
- if not dedicated_multimodal_model_configured(config_base, "video"):
+ if not complete_multimodal_model_configured(config_base, "video"):
logger.info(
- "[JiuWenSwarmDeepAdapter] skip video_understanding: models.video has no "
- "dedicated api_key in config.yaml"
+ "[JiuWenSwarmDeepAdapter] skip video_understanding: models.video requires "
+ "api_key, api_base, and model_name in config.yaml"
)
return False
- if not os.getenv("VIDEO_API_KEY"):
+ video_api_key = str(os.getenv("VIDEO_API_KEY", "")).strip()
+ video_api_base = str(os.getenv("VIDEO_API_BASE", "")).strip()
+ video_model_name = str(os.getenv("VIDEO_MODEL_NAME", "")).strip()
+ if not video_api_key or not video_api_base or not video_model_name:
logger.info("[JiuWenSwarmDeepAdapter] video tools skipped: incomplete config")
return False
return True
@@ -1543,37 +1547,18 @@ class JiuWenSwarmDeepAdapter:
return True
def _iter_runtime_audio_tools(self, agent_id: str | None) -> list[Any]:
- """可注册的音频工具:须先在 config 中为 ``models.audio`` 配置独立 ``api_key``。
-
- 与 vision / video 一致,无该 key 时不挂载任何音频工具(含 ``audio_metadata``)。
- 已配置 key 且 ``_audio_model_config`` 完整时注册全部 harness 音频工具;否则仅保留
- ``audio_metadata``(ACRCloud,仍依赖 ``ACR_*`` 环境变量在运行时识别曲库)。
- """
- config_base = get_config()
- if not dedicated_multimodal_model_configured(config_base, "audio"):
- logger.info(
- "[JiuWenSwarmDeepAdapter] skip all audio tools (incl. audio_metadata): "
- "models.audio 未配置独立 api_key"
- )
- return []
- lang = self._resolve_runtime_language()
- cfg = self._audio_model_config if self._audio_model_config else None
+ """Return metadata-only audio tools unless a complete audio model is configured."""
+ cfg = self._audio_model_config or AudioModelConfig()
tools = list(
create_audio_tools(
- language=lang,
+ language=self._resolve_runtime_language(),
audio_model_config=cfg,
agent_id=agent_id,
)
)
- if self._audio_model_config:
+ if self._audio_model_config is not None:
return tools
- filtered = [t for t in tools if t.card.name == "audio_metadata"]
- if len(tools) > len(filtered):
- logger.info(
- "[JiuWenSwarmDeepAdapter] skip audio_transcription & audio_question_answering: "
- "incomplete audio LLM config (metadata only)"
- )
- return filtered
+ return [tool for tool in tools if tool.card.name == "audio_metadata"]
def _refresh_multimodal_configs(
self,
@@ -1588,7 +1573,7 @@ class JiuWenSwarmDeepAdapter:
for tool in self._vision_tools:
tool.vision_model_config = self._vision_model_config
for tool in self._audio_tools:
- tool.audio_model_config = self._audio_model_config
+ tool.audio_model_config = self._audio_model_config or AudioModelConfig()
def _sync_tool_group(
self,
@@ -1821,11 +1806,19 @@ class JiuWenSwarmDeepAdapter:
warn_label="vision tools",
)
+ desired_audio_tools = self._iter_runtime_audio_tools(agent_id)
+ if self._audio_tools_registered:
+ current_names = {tool.card.name for tool in self._audio_tools}
+ desired_names = {tool.card.name for tool in desired_audio_tools}
+ if current_names != desired_names:
+ self._remove_registered_tools(self._audio_tools)
+ self._audio_tools = []
+ self._audio_tools_registered = False
self._audio_tools, self._audio_tools_registered = self._sync_tool_group(
current_tools=self._audio_tools,
registered=self._audio_tools_registered,
- enabled=True,
- create_fn=lambda: self._iter_runtime_audio_tools(agent_id),
+ enabled=bool(desired_audio_tools),
+ create_fn=lambda: desired_audio_tools,
warn_label="audio tools",
)
diff --git a/tests/agents/swarm/test_swarm_assembly.py b/tests/agents/swarm/test_swarm_assembly.py
index 56fdc60265..bccfd10647 100644
--- a/tests/agents/swarm/test_swarm_assembly.py
+++ b/tests/agents/swarm/test_swarm_assembly.py
@@ -766,12 +766,14 @@ def test_video_tool_gated_by_config(monkeypatch: pytest.MonkeyPatch) -> None:
# Gate closed: empty config has no dedicated video model.
assert tools._build_video_tools(ctx) == []
- # Gate open: dedicated key reported + VIDEO_API_KEY present.
+ # Gate open: complete config reported + VIDEO_API_KEY present.
monkeypatch.setattr(tools, "apply_video_model_config_from_yaml", lambda cfg: None)
monkeypatch.setattr(
- tools, "dedicated_multimodal_model_configured", lambda cfg, kind: True
+ tools, "complete_multimodal_model_configured", lambda cfg, kind: True
)
monkeypatch.setenv("VIDEO_API_KEY", "k")
+ monkeypatch.setenv("VIDEO_API_BASE", "https://video.example/v1")
+ monkeypatch.setenv("VIDEO_MODEL_NAME", "video-model")
built = tools._build_video_tools(ctx)
assert [tool.card.name for tool in built] == ["video_understanding"]