mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
feat(exit-intent): send PUT request to update memory in Java on exit
- Add HTTP PUT call to /agent/{agent_id} with summaryMemory on exit intent
- Translate exit messages and logs into Portuguese
This commit is contained in:
parent
0bba2992c9
commit
7dfb16e484
1 changed files with 48 additions and 8 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
import requests
|
||||||
|
from config.settings import XIAOZHI_BASE_URL, XIAOZHI_USER_TOKEN, DEFAULT_AGENT_ID
|
||||||
from plugins_func.register import register_function, ToolType, ActionResponse, Action
|
from plugins_func.register import register_function, ToolType, ActionResponse, Action
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
@ -31,18 +33,56 @@ handle_exit_intent_function_desc = {
|
||||||
"handle_exit_intent", handle_exit_intent_function_desc, ToolType.SYSTEM_CTL
|
"handle_exit_intent", handle_exit_intent_function_desc, ToolType.SYSTEM_CTL
|
||||||
)
|
)
|
||||||
def handle_exit_intent(conn: "ConnectionHandler", say_goodbye: str | None = None):
|
def handle_exit_intent(conn: "ConnectionHandler", say_goodbye: str | None = None):
|
||||||
# 处理退出意图
|
# Processar intenção de encerramento da conversa
|
||||||
try:
|
try:
|
||||||
if say_goodbye is None:
|
if say_goodbye is None:
|
||||||
say_goodbye = "再见,祝您生活愉快!"
|
say_goodbye = "Até logo! Tenha um ótimo dia!"
|
||||||
if not conn.close_after_chat:
|
if not conn.close_after_chat:
|
||||||
conn.close_after_chat = True
|
conn.close_after_chat = True
|
||||||
logger.bind(tag=TAG).info(f"退出意图已处理:{say_goodbye}")
|
|
||||||
return ActionResponse(
|
logger.bind(tag=TAG).info(
|
||||||
action=Action.RESPONSE, result="退出意图已处理", response=say_goodbye
|
f"Intenção de encerramento processada: {say_goodbye}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# SUCCESS
|
||||||
|
# Vamos montar uma requisição para o servidor JAVA
|
||||||
|
# PUT para o /agent/:agent_id/
|
||||||
|
# Body
|
||||||
|
# {
|
||||||
|
# "summaryMemory": "eai "
|
||||||
|
# }
|
||||||
|
# --- REQUISIÇÃO PUT PARA O SERVIDOR JAVA ---
|
||||||
|
agent_id = DEFAULT_AGENT_ID
|
||||||
|
user_token = XIAOZHI_USER_TOKEN
|
||||||
|
base_url = XIAOZHI_BASE_URL
|
||||||
|
|
||||||
|
headers = {"Content-Type": "application/json"}
|
||||||
|
if user_token:
|
||||||
|
headers["Authorization"] = f"Bearer {user_token}" if not user_token.startswith("Bearer ") else user_token
|
||||||
|
|
||||||
|
update_agent_url = f"{base_url}/agent/{agent_id}"
|
||||||
|
payload = {
|
||||||
|
"summaryMemory": "" # Adicione/substitua pelo resumo real caso tenha essa variável
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.bind(tag=TAG).info(f"[ExitIntent] Enviando PUT para {update_agent_url} com payload: {payload}")
|
||||||
|
|
||||||
|
# Requisição PUT
|
||||||
|
res = requests.put(update_agent_url, json=payload, headers=headers, timeout=5)
|
||||||
|
logger.bind(tag=TAG).info(f"[ExitIntent] Resposta do Java ({res.status_code}): {res.text}")
|
||||||
|
|
||||||
|
return ActionResponse(
|
||||||
|
action=Action.RESPONSE, result="Intenção de encerramento processada", response=say_goodbye,
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).error(f"处理退出意图错误: {e}")
|
logger.bind(tag=TAG).error(
|
||||||
return ActionResponse(
|
f"Erro ao processar intenção de encerramento: {e}"
|
||||||
action=Action.NONE, result="退出意图处理失败", response=""
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return ActionResponse(
|
||||||
|
action=Action.NONE, result="Falha ao processar intenção de encerramento", response="",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue