From af25ee4b2e165b7d4abfcb3464ccb9cbfa1cdf1d Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 13 Aug 2026 18:46:15 -0600 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/chat.ts | 4 +-- cptr/routers/chat.py | 45 +++++++++++++----------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/cptr/frontend/src/lib/apis/chat.ts b/cptr/frontend/src/lib/apis/chat.ts index 5ebf89b..2192f86 100644 --- a/cptr/frontend/src/lib/apis/chat.ts +++ b/cptr/frontend/src/lib/apis/chat.ts @@ -200,8 +200,8 @@ export const answerAskUser = ( timedOut = false ) => fetchJSON( - `/api/chats/${chatId}/messages/${messageId}/answer`, - jsonBody({ call_id: callId, answers, timed_out: timedOut }) + `/api/chats/${chatId}/messages/${messageId}/approve`, + jsonBody({ call_id: callId, approved: true, answers, timed_out: timedOut }) ); export const cancelTask = (chatId: string, messageId: string) => diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index 2659309..07cc3f8 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -1210,11 +1210,7 @@ async def compact_chat(request: Request, chat_id: str, body: CompactRequest): class ApproveRequest(BaseModel): call_id: str approved: bool = True - - -class AskUserAnswerRequest(BaseModel): - call_id: str - answers: dict[str, str] + answers: dict[str, str] | None = None timed_out: bool = False @@ -1287,27 +1283,6 @@ async def resolve_ask_user( ) -@router.post("/{chat_id}/messages/{message_id}/answer") -async def answer_ask_user( - chat_id: str, message_id: str, body: AskUserAnswerRequest, request: Request -): - """Resolve a pending Plan-mode ask_user request and resume the chat.""" - user_id = _get_user(request) - chat = await Chat.get_by_id(chat_id) - if not chat or chat.user_id != user_id: - raise HTTPException(404, "chat not found") - - try: - await resolve_ask_user( - request.app, chat_id, message_id, body.call_id, body.answers, body.timed_out - ) - except AskUserNotPendingError as exc: - raise HTTPException(409, str(exc)) from exc - except ValueError as exc: - raise HTTPException(422, str(exc)) from exc - return {"ok": True} - - @router.post("/{chat_id}/messages/{message_id}/approve") async def approve_tool(request: Request, chat_id: str, message_id: str, body: ApproveRequest): """Execute or reject a pending tool call, then continue.""" @@ -1335,6 +1310,24 @@ async def approve_tool(request: Request, chat_id: str, message_id: str, body: Ap if not call: raise HTTPException(400, "no pending tool call with that call_id") + if call.get("name") == "ask_user" and body.approved: + if body.answers is None and not body.timed_out: + raise HTTPException(400, "answers are required for ask_user") + try: + await resolve_ask_user( + request.app, + chat_id, + message_id, + body.call_id, + body.answers, + body.timed_out, + ) + except AskUserNotPendingError as exc: + raise HTTPException(409, str(exc)) from exc + except ValueError as exc: + raise HTTPException(422, str(exc)) from exc + return {"ok": True} + model_id = msg.model or "" workspace = chat.meta.get("workspace", "") if chat.meta else "" from cptr.socket.main import emit_to_user