11# SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
22# SPDX-License-Identifier: AGPL-3.0-or-later
3+ import os
34import traceback
45from contextlib import asynccontextmanager
56from json import JSONDecodeError
910import httpx
1011from fastapi import FastAPI
1112from nc_py_api import NextcloudApp , NextcloudException
12- from nc_py_api .ex_app import AppAPIAuthMiddleware , LogLvl , run_app , set_handlers
13+ from nc_py_api .ex_app import (
14+ AppAPIAuthMiddleware ,
15+ LogLvl ,
16+ run_app ,
17+ set_handlers ,
18+ SettingsForm ,
19+ SettingsField ,
20+ SettingsFieldType )
1321
1422from ex_app .lib .agent import react
1523from ex_app .lib .logger import log
1624from ex_app .lib .provider import provider
1725
26+ from contextvars import ContextVar
27+ from gettext import translation
28+
1829
1930app_enabled = Event ()
2031
32+ LOCALE_DIR = os .path .join (os .path .dirname (os .path .dirname (__file__ )), "locale" )
33+ current_translator = ContextVar ("current_translator" )
34+ current_translator .set (translation (os .getenv ("APP_ID" ), LOCALE_DIR , languages = ["en" ], fallback = True ))
35+
36+ def _ (text ):
37+ return current_translator .get ().gettext (text )
38+
2139@asynccontextmanager
2240async def lifespan (app : FastAPI ):
2341 set_handlers (app , enabled_handler )
@@ -31,6 +49,24 @@ async def lifespan(app: FastAPI):
3149APP = FastAPI (lifespan = lifespan )
3250APP .add_middleware (AppAPIAuthMiddleware ) # set global AppAPI authentication middleware
3351
52+ SETTINGS = SettingsForm (
53+ id = "settings_context_agent" ,
54+ section_type = "admin" ,
55+ section_id = "ai" ,
56+ title = _ ("Context Agent" ),
57+ description = _ ("Find more details on how to set up Context Agent in the Administration documentation." ),
58+ fields = [
59+ SettingsField (
60+ id = "here_api" ,
61+ title = _ ("API Key HERE" ),
62+ description = _ ("Set the API key for the HERE public transport routing" ),
63+ type = SettingsFieldType .PASSWORD ,
64+ default = "" ,
65+ placeholder = _ ("API key" ),
66+ ),
67+ ],
68+ )
69+
3470
3571def enabled_handler (enabled : bool , nc : NextcloudApp ) -> str :
3672 # This will be called each time application is `enabled` or `disabled`
@@ -40,6 +76,8 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
4076 nc .providers .task_processing .register (provider )
4177 app_enabled .set ()
4278 log (nc , LogLvl .WARNING , f"App enabled: { nc .app_cfg .app_name } " )
79+
80+ nc .ui .settings .register_form (SETTINGS )
4381 else :
4482 nc .providers .task_processing .unregister (provider .id )
4583 app_enabled .clear ()
0 commit comments