Skip to content

Commit

Permalink
0.1.7 HA compatibility fix (#9)
Browse files Browse the repository at this point in the history
default conversation agent instantiation method was changed
  • Loading branch information
gritaro committed May 16, 2024
1 parent 2f23407 commit c6a2ea1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
21 changes: 9 additions & 12 deletions custom_components/gigachain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from home_assistant_intents import get_languages
from homeassistant.components import conversation
from homeassistant.components.conversation import agent
from homeassistant.components.conversation import agent_manager
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers import intent, template
Expand Down Expand Up @@ -53,9 +53,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = _client
_agent = GigaChatAI(hass, entry)
await _agent.async_initialize(
hass.data.get("conversation_config")
)
conversation.async_set_agent(hass, entry, _agent)
return True

Expand All @@ -67,22 +64,22 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True


class GigaChatAI(conversation.DefaultAgent):
class GigaChatAI(conversation.AbstractConversationAgent):
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Initialize the agent."""
super().__init__(hass)
self.hass = hass
self.entry = entry
self.history: dict[str, list[BaseMessage]] = {}
self.default_agent = agent_manager.async_get_agent(hass, None)

@property
def supported_languages(self) -> list[str] | Literal["*"]:
"""Return a list of supported languages."""
return get_languages()

async def async_process(
self, user_input: agent.ConversationInput
) -> agent.ConversationResult:
self, user_input: conversation.ConversationInput
) -> conversation.ConversationResult:
"""Process a sentence."""
raw_prompt = self.entry.options.get(CONF_PROMPT, DEFAULT_PROMPT)
chat_history_enabled = self.entry.options.get(CONF_CHAT_HISTORY, DEFAULT_CHAT_HISTORY)
Expand All @@ -100,12 +97,12 @@ async def async_process(
use_builtin_sentences = self.entry.options.get(CONF_PROCESS_BUILTIN_SENTENCES,
DEFAULT_PROCESS_BUILTIN_SENTENCES)
if use_builtin_sentences:
default_agent_response = await super(GigaChatAI, self).async_process(user_input)
default_agent_response = await self.default_agent.async_process(user_input)

if default_agent_response.response.intent:
messages.append(AIMessage(content=default_agent_response.response.speech.get("plain").get("speech")))
self.history[conversation_id] = messages
return agent.ConversationResult(
return conversation.ConversationResult(
conversation_id=conversation_id, response=default_agent_response.response
)

Expand All @@ -120,7 +117,7 @@ async def async_process(
intent.IntentResponseErrorCode.UNKNOWN,
f"Houston we have a problem: {err}",
)
return agent.ConversationResult(
return conversation.ConversationResult(
conversation_id=conversation_id, response=response
)

Expand All @@ -131,7 +128,7 @@ async def async_process(
response = intent.IntentResponse(language=user_input.language)
response.async_set_speech(res.content)
LOGGER.debug(response)
return agent.ConversationResult(
return conversation.ConversationResult(
conversation_id=conversation_id, response=response
)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/gigachain/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"gigachain==0.1.7.1",
"yandexcloud==0.260.0"
],
"version": "0.1.6"
"version": "0.1.7"
}

0 comments on commit c6a2ea1

Please sign in to comment.