diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index b9346bd..2659309 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -1269,8 +1269,16 @@ async def resolve_ask_user( chat.user_id, {"chat_id": chat_id, "message_id": message_id, "output": output[-1]} ) target = await resolve_model_target(msg.model or "", getattr(app, "state", None)) + task_request = None + if app is not None: + try: + from cptr.utils.identity import internal_request_for_user + + task_request = await internal_request_for_user(app, chat.user_id) + except Exception: + log.debug("[ask_user] internal request creation failed", exc_info=True) start_task( - None, + task_request, message_id=message_id, chat_id=chat_id, user_id=chat.user_id, diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index eecda2e..d8fca22 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -1403,6 +1403,16 @@ async def run_chat_task( output_queue: asyncio.Queue | None = None, ): """Plain async function. Makes raw API calls in a loop.""" + if request is None: + try: + from cptr.app import app as cptr_app + from cptr.utils.identity import internal_request_for_user + + request = await internal_request_for_user(cptr_app, user_id) + except Exception: + logger.debug( + "[task %s] internal request creation failed", message_id[:8], exc_info=True + ) async def emit(**data): """Stream an output delta to the user.""" diff --git a/cptr/utils/identity.py b/cptr/utils/identity.py index baa8ec9..47eb93e 100644 --- a/cptr/utils/identity.py +++ b/cptr/utils/identity.py @@ -95,6 +95,8 @@ def _identity_from_auth(auth: AuthResult | None) -> ExecutionIdentity: async def identity_for_request(request) -> ExecutionIdentity: + if request is None: + return _identity_from_auth(None) return _identity_from_auth(getattr(request.state, "auth", None))