mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-22 07:03:58 +08:00
feat: Implement response variables support and comprehensive production audit (v2.1.8)
🚀 Major Features: - Add response variables support to ask_question service - Eliminate 255-character limitation for AI responses - Enable direct data access in automations without sensors - Prevent race conditions in parallel automations 🔧 Production Code Audit & Fixes: - Enhanced resource management with context managers in api_client.py - Fixed critical race conditions with asyncio.Semaphore implementation - Improved file operations with atomic writes and corruption handling - Enhanced error handling and logging security (removed sensitive data) - Fixed _check_memory_available method placement in coordinator.py 🌐 Translation Updates (8 languages): - Updated all translation files with response variables information - Enhanced service descriptions in: en, ru, de, es, it, hi, sr, zh - Added information about direct response capability - Maintained consistency across all language files 📚 Documentation Enhancements: - Added comprehensive Response Variables section to README - Created advanced automation examples with response_variable usage - Added migration guide from sensors to response variables - Enhanced service documentation with response data structure - Added practical examples for multi-step AI workflows 🔄 Service Improvements: - Enhanced ask_question service to return structured response data - Added comprehensive response schema in services.yaml - Improved error handling with success/failure indicators - Added metadata support (tokens, model, timestamp) �� Version & Manifest: - Bumped version to 2.1.8 - Maintained compatibility with existing integrations - Updated service documentation This release addresses GitHub issue #2 and significantly improves the integration's production readiness while adding powerful new response variable functionality.
This commit is contained in:
@@ -112,11 +112,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
# Initialize domain data storage
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
|
||||
async def async_ask_question(call: ServiceCall) -> None:
|
||||
"""Handle ask_question service."""
|
||||
async def async_ask_question(call: ServiceCall) -> dict:
|
||||
"""Handle ask_question service with response data."""
|
||||
try:
|
||||
coordinator = get_coordinator_by_instance(hass, call.data["instance"])
|
||||
await coordinator.async_ask_question(
|
||||
response = await coordinator.async_ask_question(
|
||||
question=call.data["question"],
|
||||
model=call.data.get("model"),
|
||||
temperature=call.data.get("temperature"),
|
||||
@@ -124,9 +124,34 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
system_prompt=call.data.get("system_prompt"),
|
||||
context_messages=call.data.get("context_messages"),
|
||||
)
|
||||
|
||||
# Return structured response data
|
||||
return {
|
||||
"response_text": response.get("content", ""),
|
||||
"tokens_used": response.get("tokens", {}).get("total", 0),
|
||||
"prompt_tokens": response.get("tokens", {}).get("prompt", 0),
|
||||
"completion_tokens": response.get("tokens", {}).get("completion", 0),
|
||||
"model_used": response.get("model", call.data.get("model", coordinator.model)),
|
||||
"instance": call.data["instance"],
|
||||
"question": call.data["question"],
|
||||
"timestamp": response.get("timestamp"),
|
||||
"success": True
|
||||
}
|
||||
except Exception as err:
|
||||
_LOGGER.error("Error asking question: %s", str(err))
|
||||
raise HomeAssistantError(f"Failed to process question: {str(err)}")
|
||||
# Return error response
|
||||
return {
|
||||
"response_text": "",
|
||||
"tokens_used": 0,
|
||||
"prompt_tokens": 0,
|
||||
"completion_tokens": 0,
|
||||
"model_used": call.data.get("model", ""),
|
||||
"instance": call.data["instance"],
|
||||
"question": call.data["question"],
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"success": False,
|
||||
"error": str(err)
|
||||
}
|
||||
|
||||
async def async_clear_history(call: ServiceCall) -> None:
|
||||
"""Handle clear_history service."""
|
||||
|
||||
Reference in New Issue
Block a user