Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .l10nignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
js/
vendor/
.venv/
venv/
11 changes: 11 additions & 0 deletions .tx/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
[main]
host = https://www.transifex.com
lang_map = th_TH: th, ja_JP: ja, bg_BG: bg, cs_CZ: cs, fi_FI: fi, hu_HU: hu, nb_NO: nb, sk_SK: sk

[o:nextcloud:p:nextcloud:r:context_agent]
file_filter = translationfiles/<lang>/context_agent.po
source_file = translationfiles/templates/context_agent.pot
source_lang = en
type = PO
2 changes: 2 additions & 0 deletions .tx/l10n/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
26 changes: 26 additions & 0 deletions ex_app/lib/all_tools/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,33 @@ def get_current_weather_for_coordinates(lat: str, lon: str) -> dict[str, typing.
raise Exception('Could not retrieve weather for coordinates')
return json['properties']['timeseries'][0]['data']['instant']['details']



@tool
@safe_tool
def get_public_transport_route_for_coordinates(origin_lat: str, origin_lon: str, destination_lat: str, destination_lon: str, routes: int) -> dict[str, typing.Any]:
"""
Retrieve a public transport route between two coordinates
When using get_public_transport_route_for_coordinates, always let the user know that the routing service here.com was used.
:param origin_lat: Latitude of the starting point
:param origin_lon: Longitude of the starting point
:param destination_lat: Latitude of the destination
:param destination_lon: Longitude of the destination
:param routes: the number of routes returned
:return:
"""

api_key = nc.appconfig_ex.get_value('here_api')
res = httpx.get('https://transit.hereapi.com/v8/routes?transportMode=car&origin='
+ origin_lat + ',' + origin_lon + '&destination=' + destination_lat + ',' + destination_lon
+ '&alternatives=' + str(routes-1) + '&apikey=' + api_key)
json = res.json()
return json



return [
get_coordinates_for_address,
get_current_weather_for_coordinates,
get_public_transport_route_for_coordinates
]
40 changes: 39 additions & 1 deletion ex_app/lib/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
import os
import traceback
from contextlib import asynccontextmanager
from json import JSONDecodeError
Expand All @@ -9,15 +10,32 @@
import httpx
from fastapi import FastAPI
from nc_py_api import NextcloudApp, NextcloudException
from nc_py_api.ex_app import AppAPIAuthMiddleware, LogLvl, run_app, set_handlers
from nc_py_api.ex_app import (
AppAPIAuthMiddleware,
LogLvl,
run_app,
set_handlers,
SettingsForm,
SettingsField,
SettingsFieldType)

from ex_app.lib.agent import react
from ex_app.lib.logger import log
from ex_app.lib.provider import provider

from contextvars import ContextVar
from gettext import translation


app_enabled = Event()

LOCALE_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "locale")
current_translator = ContextVar("current_translator")
current_translator.set(translation(os.getenv("APP_ID"), LOCALE_DIR, languages=["en"], fallback=True))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to setup translations for this repo as well, then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right, no translations yet, I oversaw that. I'd say yes, settings should be translated?


def _(text):
return current_translator.get().gettext(text)

@asynccontextmanager
async def lifespan(app: FastAPI):
set_handlers(app, enabled_handler)
Expand All @@ -31,6 +49,24 @@ async def lifespan(app: FastAPI):
APP = FastAPI(lifespan=lifespan)
APP.add_middleware(AppAPIAuthMiddleware) # set global AppAPI authentication middleware

SETTINGS = SettingsForm(
id="settings_context_agent",
section_type="admin",
section_id="ai",
title=_("Context Agent"),
description=_("Find more details on how to set up Context Agent in the Administration documentation."),
fields=[
SettingsField(
id="here_api",
title=_("API Key HERE"),
description=_("Set the API key for the HERE public transport routing"),
type=SettingsFieldType.PASSWORD,
default="",
placeholder=_("API key"),
),
],
)


def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
# This will be called each time application is `enabled` or `disabled`
Expand All @@ -40,6 +76,8 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
nc.providers.task_processing.register(provider)
app_enabled.set()
log(nc, LogLvl.WARNING, f"App enabled: {nc.app_cfg.app_name}")

nc.ui.settings.register_form(SETTINGS)
else:
nc.providers.task_processing.unregister(provider.id)
app_enabled.clear()
Expand Down