diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index ba85c06..429f867 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -844,6 +844,41 @@ async def update_chat_settings(request: Request, chat_id: str, body: UpdateChatS meta["last_model"] = body.model_id meta["params"] = body.params await Chat.update_meta(chat_id, meta, now_ms()) + params = body.params if isinstance(body.params, dict) else {} + approval_mode = params.get("tool_approval_mode") + if approval_mode not in {"ask", "auto", "full"}: + approval_mode = ( + "full" + if "tool_approval_mode" not in params and params.get("auto_approve_tools") + else "auto" + ) + if approval_mode in {"auto", "full"} and chat.current_message_id: + msg = await ChatMessage.get_by_id(chat.current_message_id) + output = (msg.output or []) if msg else [] + pending_tool_approval = any( + item.get("type") == "function_call" + and item.get("status") == "pending" + and item.get("name") != "ask_user" + for item in output + ) + if msg and msg.role == "assistant" and not msg.done and pending_tool_approval: + from cptr.utils.chat_task import is_running, start_task + + if msg.model and not is_running(msg.id): + try: + from cptr.utils.model_targets import resolve_model_target + + target = await resolve_model_target(msg.model, request.app.state) + start_task( + request, + message_id=msg.id, + chat_id=chat.id, + user_id=chat.user_id, + workspace=meta.get("workspace", ""), + target=target, + ) + except Exception: + log.debug("[chat-settings] pending tool continuation failed", exc_info=True) return {"ok": True} diff --git a/cptr/routers/gateway.py b/cptr/routers/gateway.py index 718352a..027c793 100644 --- a/cptr/routers/gateway.py +++ b/cptr/routers/gateway.py @@ -753,6 +753,18 @@ async def _ensure_chat( meta.get("client_chat_id") == client_chat_id or meta.get("owui_chat_id") == client_chat_id ): + normalized = dict(meta) + normalized["workspace"] = workspace + params = ( + normalized.get("params") + if isinstance(normalized.get("params"), dict) + else {} + ) + normalized["params"] = {**params, "tool_approval_mode": "full"} + normalized["client_chat_id"] = client_chat_id + normalized["owui_chat_id"] = client_chat_id + if normalized != meta: + await Chat.update_meta(chat.id, normalized, now_ms()) return chat.id # Create a new chat diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index 25caba5..fea1828 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -1988,15 +1988,6 @@ async def run_chat_task( } review_builtin_tools = builtin_tools - # Tool approval mode: 'ask' | 'auto' | 'full' - # ask = require approval for ALL tools (including reads) - # auto = run allow tools; review review tools before prompting - # full = auto-approve everything - approval_mode = chat_params.get("tool_approval_mode", "auto") - # Legacy compat: old boolean auto_approve_tools - if "tool_approval_mode" not in chat_params and "auto_approve_tools" in chat_params: - approval_mode = "full" if chat_params["auto_approve_tools"] else "auto" - async def run_queued_tool_calls(tool_ctx: dict) -> str: """Run queued tool calls until approval is needed or the queue is empty. @@ -2022,6 +2013,19 @@ async def run_chat_task( name = item.get("name", "") tool = ALL_TOOLS.get(name) tool_approval = await resolve_builtin_tool_approval(name) if tool else "review" + current_chat = await Chat.get_by_id(chat_id) + current_params = ( + (current_chat.meta or {}).get("params", {}) if current_chat else chat_params + ) + current_params = current_params if isinstance(current_params, dict) else {} + approval_mode = current_params.get("tool_approval_mode") + if approval_mode not in {"ask", "auto", "full"}: + approval_mode = ( + "full" + if "tool_approval_mode" not in current_params + and current_params.get("auto_approve_tools") + else "auto" + ) should_auto = approval_mode == "full" or ( approval_mode == "auto" and tool and tool_approval == "allow" ) @@ -2534,6 +2538,19 @@ async def run_chat_task( loaded_skill_names.add(skill_name) tool = ALL_TOOLS.get(name) tool_approval = await resolve_builtin_tool_approval(name) if tool else "review" + current_chat = await Chat.get_by_id(chat_id) + current_params = ( + (current_chat.meta or {}).get("params", {}) if current_chat else chat_params + ) + current_params = current_params if isinstance(current_params, dict) else {} + approval_mode = current_params.get("tool_approval_mode") + if approval_mode not in {"ask", "auto", "full"}: + approval_mode = ( + "full" + if "tool_approval_mode" not in current_params + and current_params.get("auto_approve_tools") + else "auto" + ) should_auto = approval_mode == "full" or ( approval_mode == "auto" and tool and tool_approval == "allow" )