Edit code for success run
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
from .config import GATEWAY_URL, AGENT, FOLLOWUP_TIMEOUT, log
|
||||
from .audio import record, record_with_timeout
|
||||
from .tts import play_activation_sound, speak, stop_speaking
|
||||
from .config import GATEWAY_URL, AGENT, log
|
||||
from .audio import record
|
||||
from .tts import speak, stop_speaking
|
||||
from .llm import ask_agent_stream, Conversation, is_reset_command
|
||||
|
||||
# Персистентные сессии — одна на день для каждого агента
|
||||
@@ -31,6 +31,29 @@ def _handle_reset(text: str, agent_id: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _conversation_loop(agent_id: str, agent_name: str = "Cosmo"):
|
||||
"""Основной цикл диалога — слушает и отвечает пока пользователь говорит.
|
||||
Выходит когда в течение MAX_DURATION не было речи."""
|
||||
conv = _get_session(agent_id)
|
||||
|
||||
while True:
|
||||
text = record()
|
||||
if not text:
|
||||
print(f"😴 Тишина, жду активации...\n")
|
||||
return
|
||||
|
||||
print(f"📝 Ты → {agent_name}: {text}")
|
||||
|
||||
if _handle_reset(text, agent_id):
|
||||
conv = _get_session(agent_id)
|
||||
continue
|
||||
|
||||
response = ask_agent_stream(text, conv=conv, agent_id=agent_id)
|
||||
print(f"🤖 {agent_name}: {response}\n")
|
||||
# после ответа — следующая итерация с новым record()
|
||||
# record() сам гасит эхо через ECHO_WARMUP
|
||||
|
||||
|
||||
def run_with_enter():
|
||||
print("\n🦞 Cosmo Satellite запущен (режим: Enter для активации)")
|
||||
print(f" Gateway : {GATEWAY_URL}")
|
||||
@@ -40,34 +63,8 @@ def run_with_enter():
|
||||
while True:
|
||||
try:
|
||||
input("⏎ Нажми Enter и говори...")
|
||||
stop_speaking() # barge-in: прервать если ещё говорит
|
||||
play_activation_sound()
|
||||
|
||||
conv = _get_session("cosmo")
|
||||
|
||||
while True:
|
||||
text = record()
|
||||
if not text:
|
||||
print("⚠️ Ничего не распознано")
|
||||
break
|
||||
|
||||
print(f"📝 Ты: {text}")
|
||||
|
||||
if _handle_reset(text, "cosmo"):
|
||||
conv = _get_session("cosmo")
|
||||
break
|
||||
|
||||
response = ask_agent_stream(text, conv=conv)
|
||||
print(f"🤖 Cosmo: {response}\n")
|
||||
|
||||
print(f"👂 Слушаю продолжение ({int(FOLLOWUP_TIMEOUT)} сек)...")
|
||||
followup = record_with_timeout(timeout=FOLLOWUP_TIMEOUT)
|
||||
|
||||
if not followup:
|
||||
print("😴 Нет продолжения, жду активации...\n")
|
||||
break
|
||||
|
||||
text = followup
|
||||
stop_speaking() # barge-in
|
||||
_conversation_loop("cosmo", "Cosmo")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\n👋 Выход")
|
||||
@@ -138,23 +135,13 @@ def run_with_porcupine():
|
||||
if keyword_index >= 0:
|
||||
agent_id = wake_word_map[keyword_index]
|
||||
agent_name = AGENTS[agent_id]["name"]
|
||||
stop_speaking() # barge-in: прервать если ещё говорит
|
||||
stop_speaking() # barge-in
|
||||
print(f"✅ Услышал '{agent_name}'!")
|
||||
play_activation_sound()
|
||||
|
||||
conv = _get_session(agent_id)
|
||||
|
||||
text = record()
|
||||
if not text:
|
||||
continue
|
||||
|
||||
print(f"📝 Ты → {agent_name}: {text}")
|
||||
|
||||
if _handle_reset(text, agent_id):
|
||||
continue
|
||||
|
||||
response = ask_agent_stream(text, conv=conv, agent_id=agent_id)
|
||||
print(f"🤖 {agent_name}: {response}\n")
|
||||
# отпускаем микрофон на время диалога
|
||||
stream.stop_stream()
|
||||
_conversation_loop(agent_id, agent_name)
|
||||
stream.start_stream()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user