Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP 408 when registering TaskProcessing provider from ex_app #494

Closed
marcelklehr opened this issue Jan 13, 2025 · 1 comment
Closed

HTTP 408 when registering TaskProcessing provider from ex_app #494

marcelklehr opened this issue Jan 13, 2025 · 1 comment

Comments

@marcelklehr
Copy link
Member

Describe the bug

Traceback (most recent call last):
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/uvicorn/middleware/message_logger.py", line 84, in __call__
    raise exc from None
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/uvicorn/middleware/message_logger.py", line 80, in __call__
    await self.app(scope, inner_receive, inner_send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 187, in __call__
    raise exc
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/nc_py_api/ex_app/integration_fastapi.py", line 258, in __call__
    await self.app(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/routing.py", line 288, in handle
    await self.app(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/routing.py", line 76, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/routing.py", line 73, in app
    response = await f(request)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 301, in app
    raw_response = await run_endpoint_function(
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 214, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/starlette/concurrency.py", line 39, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 2441, in run_sync_in_worker_thread
    return await future
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 943, in run
    result = context.run(func, *args)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/nc_py_api/ex_app/integration_fastapi.py", line 94, in enabled_callback
    return JSONResponse(content={"error": enabled_handler(enabled, nc)})
  File "/home/marcel/code/context_agent/ex_app/lib/main.py", line 38, in enabled_handler
    nc.providers.task_processing.register(provider)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/nc_py_api/ex_app/providers/task_processing.py", line 123, in register
    self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)
  File "/home/marcel/code/context_agent/.venv/lib/python3.10/site-packages/nc_py_api/_session.py", line 211, in ocs
    raise NextcloudException(408, info=info) from None
nc_py_api._exceptions.NextcloudException: [408] <request: POST /ocs/v1.php/apps/app_api/api/v1/ai_provider/task_processing>

Steps/Code to Reproduce

@asynccontextmanager
async def lifespan(app: FastAPI):
    set_handlers(app, enabled_handler)
    start_bg_task()
    nc = NextcloudApp()
    if nc.enabled_state:
        app_enabled.set()
    yield


APP = FastAPI(lifespan=lifespan)
APP.add_middleware(AppAPIAuthMiddleware)  # set global AppAPI authentication middleware


provider = TaskProcessingProvider(
	id='context_agent:agent',
	name='ContextAgent Provider',
	task_type='core:contextagent:interaction',
	expected_runtime=60,
)

def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
    # This will be called each time application is `enabled` or `disabled`
    # NOTE: `user` is unavailable on this step, so all NC API calls that require it will fail as unauthorized.
    log(nc, LogLvl.INFO, f"enabled={enabled}")
    if enabled:
        nc.providers.task_processing.register(provider)
        app_enabled.set()
        log(nc, LogLvl.WARNING, f"App enabled: {nc.app_cfg.app_name}")
    else:
        nc.providers.task_processing.unregister(provider.id)
        app_enabled.clear()
        log(nc, LogLvl.WARNING, f"App disabled: {nc.app_cfg.app_name}")
    # In case of an error, a non-empty short string should be returned, which will be shown to the NC administrator.
    return ""

Expected Results

No error

Actual Results

nc_py_api._exceptions.NextcloudException: [408] <request: POST /ocs/v1.php/apps/app_api/api/v1/ai_provider/task_processing>

Setup configuration

nextcloud = 31.0.0 beta 2
app_api = 32.0.0-dev.1
nc-py-api = 0.18.0

@marcelklehr
Copy link
Member Author

The reason was brute force protection kicking in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant