From fc07e9bcad8dedf8ce9ba8c90b0d4288e4158392 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 12 Jul 2026 01:08:52 -0500 Subject: [PATCH] refac --- cptr/models/chats.py | 2 ++ cptr/utils/async_subagents.py | 55 ++++++++++++++++++++++++++--------- cptr/utils/tools.py | 1 - 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/cptr/models/chats.py b/cptr/models/chats.py index a954cd4..2b9b3f2 100644 --- a/cptr/models/chats.py +++ b/cptr/models/chats.py @@ -218,6 +218,8 @@ class Chat(Base): .where( Chat.user_id == user_id, workspace.in_(paths), + Chat.meta["internal"].as_boolean().is_not(True), + Chat.meta["subagent"].as_boolean().is_not(True), Chat.updated_at > func.coalesce(Chat.last_read_at, 0), ) .group_by(workspace) diff --git a/cptr/utils/async_subagents.py b/cptr/utils/async_subagents.py index c1988e2..cb2aa46 100644 --- a/cptr/utils/async_subagents.py +++ b/cptr/utils/async_subagents.py @@ -202,6 +202,7 @@ async def _inject_completion(record: dict[str, Any]) -> None: from cptr.utils.chat_task import get_pending_input_lock, start_task assistant_msg = None + direct_timer_completion = False async with get_pending_input_lock(parent_chat_id): all_msgs = await ChatMessage.get_all_by_chat(parent_chat_id) active = any(m.role == "assistant" and not m.done for m in all_msgs) @@ -216,29 +217,55 @@ async def _inject_completion(record: dict[str, Any]) -> None: if active: meta["async_subagent_pending"] = True - user_msg = await ChatMessage.create( - chat_id=parent_chat_id, - role="user", - content=content, - parent_id=parent_id, - model=model_id, - meta=meta, - created_at=now_ms(), - ) - - if not active: + if record.get("timer_chat_id") and not active: + direct_timer_completion = True + meta["timer_completion"] = True assistant_msg = await ChatMessage.create( chat_id=parent_chat_id, role="assistant", - content="", - parent_id=user_msg.id, + content=record.get("summary") or "Timer completed.", + parent_id=parent_id, model=model_id, - done=False, + done=True, + meta=meta, created_at=now_ms(), ) await Chat.update_current_message(parent_chat_id, assistant_msg.id, now_ms()) + else: + user_msg = await ChatMessage.create( + chat_id=parent_chat_id, + role="user", + content=content, + parent_id=parent_id, + model=model_id, + meta=meta, + created_at=now_ms(), + ) + + if not active: + assistant_msg = await ChatMessage.create( + chat_id=parent_chat_id, + role="assistant", + content="", + parent_id=user_msg.id, + model=model_id, + done=False, + created_at=now_ms(), + ) + await Chat.update_current_message(parent_chat_id, assistant_msg.id, now_ms()) await export_chat_to_file(parent_chat_id) + if direct_timer_completion: + await emit_to_user( + user_id, + { + "chat_id": parent_chat_id, + "message_id": assistant_msg.id, + "pending_inputs_processed": True, + }, + ) + return + if not assistant_msg: await emit_to_user( user_id, diff --git a/cptr/utils/tools.py b/cptr/utils/tools.py index bd8e11f..cb65f09 100644 --- a/cptr/utils/tools.py +++ b/cptr/utils/tools.py @@ -2620,7 +2620,6 @@ GLOBAL_CHAT_DISABLED_TOOLS = { *BUILTIN_TOOL_GROUPS["browser"], *BUILTIN_TOOL_GROUPS["automations"], *BUILTIN_TOOL_GROUPS["images"], - *BUILTIN_TOOL_GROUPS["subagents"], "manage_skill", }