Refactor assistant configuration management and update documentation
- Removed legacy agent profile settings from the .env.example and README, streamlining the configuration process. - Introduced a new local YAML configuration adapter for assistant settings, allowing for easier management of assistant profiles. - Updated backend integration documentation to clarify the behavior of assistant config sourcing based on backend URL settings. - Adjusted various service implementations to directly utilize API keys from the new configuration structure. - Enhanced test coverage for the new local YAML adapter and its integration with backend services.
This commit is contained in:
@@ -2,24 +2,42 @@ import aiohttp
|
||||
import pytest
|
||||
|
||||
from app.backend_adapters import (
|
||||
HistoryDisabledBackendAdapter,
|
||||
HttpBackendAdapter,
|
||||
NullBackendAdapter,
|
||||
AssistantConfigSourceAdapter,
|
||||
LocalYamlAssistantConfigAdapter,
|
||||
build_backend_adapter,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_backend_adapter_without_url_returns_null_adapter():
|
||||
async def test_without_backend_url_uses_local_yaml_for_assistant_config(tmp_path):
|
||||
config_dir = tmp_path / "assistants"
|
||||
config_dir.mkdir(parents=True, exist_ok=True)
|
||||
(config_dir / "dev_local.yaml").write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"assistant:",
|
||||
" assistantId: dev_local",
|
||||
" systemPrompt: local prompt",
|
||||
" greeting: local greeting",
|
||||
]
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
adapter = build_backend_adapter(
|
||||
backend_url=None,
|
||||
backend_mode="auto",
|
||||
history_enabled=True,
|
||||
timeout_sec=3,
|
||||
assistant_local_config_dir=str(config_dir),
|
||||
)
|
||||
assert isinstance(adapter, NullBackendAdapter)
|
||||
assert isinstance(adapter, AssistantConfigSourceAdapter)
|
||||
|
||||
assert await adapter.fetch_assistant_config("assistant_1") is None
|
||||
payload = await adapter.fetch_assistant_config("dev_local")
|
||||
assert isinstance(payload, dict)
|
||||
assert payload.get("__error_code") in (None, "")
|
||||
assert payload["assistant"]["assistantId"] == "dev_local"
|
||||
assert payload["assistant"]["systemPrompt"] == "local prompt"
|
||||
assert (
|
||||
await adapter.create_call_record(
|
||||
user_id=1,
|
||||
@@ -54,7 +72,7 @@ async def test_build_backend_adapter_without_url_returns_null_adapter():
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_http_backend_adapter_create_call_record_posts_expected_payload(monkeypatch):
|
||||
async def test_http_backend_adapter_create_call_record_posts_expected_payload(monkeypatch, tmp_path):
|
||||
captured = {}
|
||||
|
||||
class _FakeResponse:
|
||||
@@ -90,15 +108,31 @@ async def test_http_backend_adapter_create_call_record_posts_expected_payload(mo
|
||||
captured["json"] = json
|
||||
return _FakeResponse(status=200, payload={"id": "call_123"})
|
||||
|
||||
def get(self, url):
|
||||
_ = url
|
||||
return _FakeResponse(
|
||||
status=200,
|
||||
payload={
|
||||
"assistant": {
|
||||
"assistantId": "assistant_9",
|
||||
"systemPrompt": "backend prompt",
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
monkeypatch.setattr("app.backend_adapters.aiohttp.ClientSession", _FakeClientSession)
|
||||
|
||||
config_dir = tmp_path / "assistants"
|
||||
config_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
adapter = build_backend_adapter(
|
||||
backend_url="http://localhost:8100",
|
||||
backend_mode="auto",
|
||||
history_enabled=True,
|
||||
timeout_sec=7,
|
||||
assistant_local_config_dir=str(config_dir),
|
||||
)
|
||||
assert isinstance(adapter, HttpBackendAdapter)
|
||||
assert isinstance(adapter, AssistantConfigSourceAdapter)
|
||||
|
||||
call_id = await adapter.create_call_record(
|
||||
user_id=99,
|
||||
@@ -119,25 +153,115 @@ async def test_http_backend_adapter_create_call_record_posts_expected_payload(mo
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_backend_mode_disabled_forces_null_even_with_url():
|
||||
async def test_with_backend_url_uses_backend_for_assistant_config(monkeypatch, tmp_path):
|
||||
class _FakeResponse:
|
||||
def __init__(self, status=200, payload=None):
|
||||
self.status = status
|
||||
self._payload = payload if payload is not None else {}
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return None
|
||||
|
||||
async def json(self):
|
||||
return self._payload
|
||||
|
||||
def raise_for_status(self):
|
||||
if self.status >= 400:
|
||||
raise RuntimeError("http_error")
|
||||
|
||||
class _FakeClientSession:
|
||||
def __init__(self, timeout=None):
|
||||
self.timeout = timeout
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return None
|
||||
|
||||
def get(self, url):
|
||||
_ = url
|
||||
return _FakeResponse(
|
||||
status=200,
|
||||
payload={
|
||||
"assistant": {
|
||||
"assistantId": "dev_http",
|
||||
"systemPrompt": "backend prompt",
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
def post(self, url, json=None):
|
||||
_ = (url, json)
|
||||
return _FakeResponse(status=200, payload={"id": "call_1"})
|
||||
|
||||
monkeypatch.setattr("app.backend_adapters.aiohttp.ClientSession", _FakeClientSession)
|
||||
|
||||
config_dir = tmp_path / "assistants"
|
||||
config_dir.mkdir(parents=True, exist_ok=True)
|
||||
(config_dir / "dev_http.yaml").write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"assistant:",
|
||||
" assistantId: dev_http",
|
||||
" systemPrompt: local prompt",
|
||||
]
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
adapter = build_backend_adapter(
|
||||
backend_url="http://localhost:8100",
|
||||
backend_mode="auto",
|
||||
history_enabled=True,
|
||||
timeout_sec=3,
|
||||
assistant_local_config_dir=str(config_dir),
|
||||
)
|
||||
assert isinstance(adapter, AssistantConfigSourceAdapter)
|
||||
|
||||
payload = await adapter.fetch_assistant_config("dev_http")
|
||||
assert payload["assistant"]["assistantId"] == "dev_http"
|
||||
assert payload["assistant"]["systemPrompt"] == "backend prompt"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_backend_mode_disabled_uses_local_assistant_config_even_with_url(monkeypatch, tmp_path):
|
||||
class _FailIfCalledClientSession:
|
||||
def __init__(self, timeout=None):
|
||||
_ = timeout
|
||||
raise AssertionError("HTTP client should not be created when backend_mode=disabled")
|
||||
|
||||
monkeypatch.setattr("app.backend_adapters.aiohttp.ClientSession", _FailIfCalledClientSession)
|
||||
|
||||
config_dir = tmp_path / "assistants"
|
||||
config_dir.mkdir(parents=True, exist_ok=True)
|
||||
(config_dir / "dev_disabled.yaml").write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"assistant:",
|
||||
" assistantId: dev_disabled",
|
||||
" systemPrompt: local disabled prompt",
|
||||
]
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
adapter = build_backend_adapter(
|
||||
backend_url="http://localhost:8100",
|
||||
backend_mode="disabled",
|
||||
history_enabled=True,
|
||||
timeout_sec=7,
|
||||
timeout_sec=3,
|
||||
assistant_local_config_dir=str(config_dir),
|
||||
)
|
||||
assert isinstance(adapter, NullBackendAdapter)
|
||||
assert isinstance(adapter, AssistantConfigSourceAdapter)
|
||||
|
||||
payload = await adapter.fetch_assistant_config("dev_disabled")
|
||||
assert payload["assistant"]["assistantId"] == "dev_disabled"
|
||||
assert payload["assistant"]["systemPrompt"] == "local disabled prompt"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_history_disabled_wraps_backend_adapter():
|
||||
adapter = build_backend_adapter(
|
||||
backend_url="http://localhost:8100",
|
||||
backend_mode="auto",
|
||||
history_enabled=False,
|
||||
timeout_sec=7,
|
||||
)
|
||||
assert isinstance(adapter, HistoryDisabledBackendAdapter)
|
||||
assert await adapter.create_call_record(user_id=1, assistant_id="a1", source="debug") is None
|
||||
assert await adapter.add_transcript(
|
||||
call_id="c1",
|
||||
@@ -148,3 +272,53 @@ async def test_history_disabled_wraps_backend_adapter():
|
||||
end_ms=10,
|
||||
duration_ms=10,
|
||||
) is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_local_yaml_adapter_rejects_path_traversal_like_assistant_id(tmp_path):
|
||||
adapter = LocalYamlAssistantConfigAdapter(str(tmp_path))
|
||||
payload = await adapter.fetch_assistant_config("../etc/passwd")
|
||||
assert payload == {"__error_code": "assistant.not_found", "assistantId": "../etc/passwd"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_local_yaml_translates_agent_schema_to_runtime_services(tmp_path):
|
||||
config_dir = tmp_path / "assistants"
|
||||
config_dir.mkdir(parents=True, exist_ok=True)
|
||||
(config_dir / "default.yaml").write_text(
|
||||
"\n".join(
|
||||
[
|
||||
"agent:",
|
||||
" llm:",
|
||||
" provider: openai",
|
||||
" model: gpt-4o-mini",
|
||||
" api_key: sk-llm",
|
||||
" api_url: https://api.example.com/v1",
|
||||
" tts:",
|
||||
" provider: openai_compatible",
|
||||
" model: tts-model",
|
||||
" api_key: sk-tts",
|
||||
" api_url: https://tts.example.com/v1/audio/speech",
|
||||
" voice: anna",
|
||||
" asr:",
|
||||
" provider: openai_compatible",
|
||||
" model: asr-model",
|
||||
" api_key: sk-asr",
|
||||
" api_url: https://asr.example.com/v1/audio/transcriptions",
|
||||
" duplex:",
|
||||
" system_prompt: You are test assistant",
|
||||
]
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
adapter = LocalYamlAssistantConfigAdapter(str(config_dir))
|
||||
payload = await adapter.fetch_assistant_config("default")
|
||||
|
||||
assert isinstance(payload, dict)
|
||||
assistant = payload.get("assistant", {})
|
||||
services = assistant.get("services", {})
|
||||
assert services.get("llm", {}).get("apiKey") == "sk-llm"
|
||||
assert services.get("tts", {}).get("apiKey") == "sk-tts"
|
||||
assert services.get("asr", {}).get("apiKey") == "sk-asr"
|
||||
assert assistant.get("systemPrompt") == "You are test assistant"
|
||||
|
||||
Reference in New Issue
Block a user