Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup/record_owner_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def main():
print() # Newline after the countdown
logger.info(f"Voice profile saved to {OWNER_FILE}")
print(f"✅ Voice profile saved to {OWNER_FILE}")
except Exception as e:
except Exception as e: # noqa: BLE001
logger.error(f"Error during recording: {e}")
sys.exit(1)

Expand Down
16 changes: 12 additions & 4 deletions src/audio/ttsplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
logger = get_logger()


def _write_audio_chunk(filename, data):
with open(filename, "ab") as f:
f.write(data)


class TTSPlayer(threading.Thread):
def __init__(self):
super().__init__(daemon=True)
Expand All @@ -37,10 +42,13 @@ def run(self):
async def synthesize_to_file(self, text: str, filename: str):
try:
communicate = edge_tts.Communicate(text=text, voice=config.TTS_VOICE, rate=config.TTS_RATE)
with open(filename, "wb") as f:
async for chunk in communicate.stream():
if chunk["type"] == "audio":
f.write(chunk["data"])
async for chunk in communicate.stream():
if chunk["type"] == "audio":
await asyncio.to_thread(
_write_audio_chunk,
filename,
chunk["data"],
)
except Exception as e:
logger.error(f"Edge TTS error: {e}")
raise
Expand Down
18 changes: 17 additions & 1 deletion src/core/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sqlite3

from langchain_core.messages import HumanMessage
from langchain_core.messages.utils import count_tokens_approximately
from langgraph.checkpoint.sqlite import SqliteSaver
from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.chat_agent_executor import AgentState
Expand All @@ -12,6 +13,7 @@
from src.utils.logger import get_logger

from .generate_prompt import prompt
from .summarizer import summarize_conversation
from .tools import get_all_tools # Import the function to get all tools

logger = get_logger()
Expand Down Expand Up @@ -39,6 +41,21 @@ class State(AgentState):

def call_agent(message):
config_dict = {"configurable": {"thread_id": config.THREAD_ID}}
state = agent.get_state(config_dict)
messages = state.values.get("messages", [])
token_count = count_tokens_approximately(messages)

if token_count > config.MAX_TOKENS_HISTORY:
summary_result = summarize_conversation(
{
"messages": messages,
"summary": state.values.get("summary", ""),
}
)
agent.update_state(
config_dict,
summary_result,
)
response = agent.invoke(
{"messages": [message]},
config_dict,
Expand Down Expand Up @@ -85,7 +102,6 @@ def call_agent(message):
if __name__ == "__main__":
# Run the agent
message = HumanMessage(content="Hey, how are you doing?")
config = {"configurable": {"thread_id": "1"}}

response = call_agent(message)

Expand Down
3 changes: 2 additions & 1 deletion src/core/generate_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def prompt(state: AgentState, config: RunnableConfig) -> list[Any]:
owner_name = app_config.OWNER_NAME
user_profile = _build_user_profile()

previous_conversation = f"\n### 📜 Previous Conversation:\n{summary}" if summary else ""
system_msg = f"""
You are **{assistant_name}**, a witty, intelligent desktop AI assistant running locally on {owner_name}'s Linux machine.

Expand Down Expand Up @@ -74,7 +75,7 @@ def prompt(state: AgentState, config: RunnableConfig) -> list[Any]:

{user_profile}

{f"\n### 📜 Previous Conversation:\n{summary}" if summary else ""}
{previous_conversation}
""".strip()

return [SystemMessage(content=system_msg)] + state["messages"]
Loading