Skip to content

Commit 946657d

Browse files
authored
Merge pull request #110 from nextcloud/chore/niquests
Chore: switch from httpx to niquests and retry task scheduling
2 parents 5a7057c + fc5bc78 commit 946657d

File tree

12 files changed

+66
-47
lines changed

12 files changed

+66
-47
lines changed

.github/workflows/integration_test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
matrix:
2929
php-versions: [ '8.2' ]
3030
databases: [ 'sqlite' ]
31-
server-versions: [ 'master', 'stable32', 'stable31' ]
31+
server-versions: [ 'master', 'stable33', 'stable32', 'stable31' ]
3232

3333
name: Integration test on ☁️${{ matrix.server-versions }} 🐘${{ matrix.php-versions }}
3434

@@ -198,7 +198,7 @@ jobs:
198198
CREDS: "alice:alice"
199199
run: |
200200
sleep 300
201-
TASK=$(curl -X POST -u "$CREDS" -H "oCS-APIRequest: true" -H "Content-type: application/json" http://localhost:8080/ocs/v2.php/taskprocessing/schedule?format=json --data-raw '{"input": {"input": "Search duckduckgo for Nextcloud", "confirmation":1, "conversation_token": ""},"type":"core:contextagent:interaction", "appId": "test", "customId": ""}')
201+
TASK=$(curl -X POST -u "$CREDS" -H "oCS-APIRequest: true" -H "Content-type: application/json" http://localhost:8080/ocs/v2.php/taskprocessing/schedule?format=json --data-raw '{"input": {"input": "Search youtube for videos about Nextcloud", "confirmation":1, "conversation_token": ""},"type":"core:contextagent:interaction", "appId": "test", "customId": ""}')
202202
echo $TASK
203203
TASK_ID=$(echo $TASK | jq '.ocs.data.task.id')
204204
NEXT_WAIT_TIME=0
@@ -215,7 +215,7 @@ jobs:
215215
[ "$TASK_STATUS" == '"STATUS_SUCCESSFUL"' ]
216216
echo $TASK | jq '.ocs.data.task.output.output'
217217
echo $TASK | jq '.ocs.data.task.output.sources'
218-
echo $TASK | jq '.ocs.data.task.output.sources' | grep -q 'duckduckgo_results_json'
218+
echo $TASK | jq '.ocs.data.task.output.sources' | grep -q 'youtube_search'
219219
echo $TASK | jq '.ocs.data.task.output.output' | grep -q 'Nextcloud'
220220
221221
- name: Show nextcloud logs

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Positive:
3939
<screenshot>https://github.com/nextcloud/context_agent/blob/main/img/screenshot.png?raw=true</screenshot>
4040
<repository type="git">https://github.com/nextcloud/context_agent</repository>
4141
<dependencies>
42-
<nextcloud min-version="31.0.8" max-version="33"/>
42+
<nextcloud min-version="31.0.8" max-version="34"/>
4343
</dependencies>
4444
<external-app>
4545
<docker-install>

ex_app/lib/all_tools/calendar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from time import sleep
55
from typing import Optional
66

7-
import httpx
7+
from niquests import ConnectionError, Timeout
88
import pytz
99
from ics import Calendar, Event, Attendee, Organizer, Todo
1010
from langchain_core.tools import tool
@@ -91,10 +91,8 @@ def schedule_event(calendar_name: str, title: str, description: str, start_date:
9191
json = nc.ocs('GET', '/ocs/v2.php/cloud/user')
9292
break
9393
except (
94-
httpx.RemoteProtocolError,
95-
httpx.ReadError,
96-
httpx.LocalProtocolError,
97-
httpx.PoolTimeout,
94+
ConnectionError,
95+
Timeout
9896
) as e:
9997
log(nc, LogLvl.DEBUG, "Ignored error during task polling")
10098
i += 1

ex_app/lib/all_tools/deck.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from time import sleep
55
from typing import Optional
66

7-
import httpx
87
import pytz
98
from langchain_core.tools import tool
109
from nc_py_api import Nextcloud

ex_app/lib/all_tools/here.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import datetime
55
import urllib.parse
66

7-
import httpx
7+
import niquests
88
from langchain_core.tools import tool
99
from nc_py_api import Nextcloud
1010

@@ -30,7 +30,7 @@ def get_public_transport_route_for_coordinates(origin_lat: str, origin_lon: str,
3030
if departure_time is None:
3131
departure_time = urllib.parse.quote_plus(datetime.datetime.now(datetime.UTC).isoformat())
3232
api_key = nc.appconfig_ex.get_value('here_api')
33-
res = httpx.get('https://transit.hereapi.com/v8/routes?transportMode=car&origin='
33+
res = niquests.get('https://transit.hereapi.com/v8/routes?transportMode=car&origin='
3434
+ origin_lat + ',' + origin_lon + '&destination=' + destination_lat + ',' + destination_lon
3535
+ '&alternatives=' + str(routes-1) + '&departureTime=' + departure_time + '&apikey=' + api_key)
3636
json = res.json()

ex_app/lib/all_tools/lib/task_processing.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44
import typing
55

6-
import httpx
6+
from niquests import ConnectionError, Timeout
77
from nc_py_api import NextcloudException
88
from nc_py_api.ex_app import LogLvl
99
from pydantic import BaseModel, ValidationError
@@ -20,11 +20,24 @@ class Response(BaseModel):
2020
task: Task
2121

2222
def run_task(nc, type, task_input):
23-
response = nc.ocs(
24-
"POST",
25-
"/ocs/v1.php/taskprocessing/schedule",
26-
json={"type": type, "appId": "context_agent", "input": task_input},
27-
)
23+
i = 0
24+
while i < 20:
25+
try:
26+
response = nc.ocs(
27+
"POST",
28+
"/ocs/v1.php/taskprocessing/schedule",
29+
json={"type": type, "appId": "context_agent", "input": task_input},
30+
)
31+
break
32+
except (
33+
ConnectionError,
34+
Timeout
35+
36+
) as e:
37+
log(nc, LogLvl.DEBUG, "Ignored error during task scheduling")
38+
i += 1
39+
sleep(1)
40+
continue
2841

2942
try:
3043
task = Response.model_validate(response).task
@@ -38,10 +51,8 @@ def run_task(nc, type, task_input):
3851
try:
3952
response = nc.ocs("GET", f"/ocs/v1.php/taskprocessing/task/{task.id}")
4053
except (
41-
httpx.RemoteProtocolError,
42-
httpx.ReadError,
43-
httpx.LocalProtocolError,
44-
httpx.PoolTimeout,
54+
ConnectionError,
55+
Timeout
4556
) as e:
4657
log(nc, LogLvl.DEBUG, "Ignored error during task polling")
4758
time.sleep(5)

ex_app/lib/all_tools/mail.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: AGPL-3.0-or-later
33
from time import sleep
44

5-
import httpx
5+
from niquests import ConnectionError, Timeout
66
from langchain_core.tools import tool
77
from nc_py_api import Nextcloud
88
from nc_py_api.ex_app import LogLvl
@@ -35,10 +35,8 @@ def send_email(subject: str, body: str, account_id: int, from_email: str, to_ema
3535
'to': [{'label': '', 'email': email} for email in to_emails],
3636
})
3737
except (
38-
httpx.RemoteProtocolError,
39-
httpx.ReadError,
40-
httpx.LocalProtocolError,
41-
httpx.PoolTimeout,
38+
ConnectionError,
39+
Timeout
4240
) as e:
4341
log(nc, LogLvl.DEBUG, "Ignored error during task polling")
4442
i += 1

ex_app/lib/all_tools/openstreetmap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import urllib.parse
77
from time import sleep
88

9-
import httpx
9+
import niquests
1010
from langchain_core.tools import tool
1111
from nc_py_api import Nextcloud
1212

@@ -22,7 +22,7 @@ def get_coordinates_for_address(address: str) -> (str, str):
2222
:param address: the address to calculate the coordinates for
2323
:return: a tuple of latitude and longitude
2424
"""
25-
res = httpx.get('https://nominatim.openstreetmap.org/search', params={'q': address, 'format': 'json', 'addressdetails': '1', 'extratags': '1', 'namedetails': '1', 'limit': '1'})
25+
res = niquests.get('https://nominatim.openstreetmap.org/search', params={'q': address, 'format': 'json', 'addressdetails': '1', 'extratags': '1', 'namedetails': '1', 'limit': '1'})
2626
json = res.json()
2727
if 'error' in json:
2828
raise Exception(json['error'])
@@ -56,7 +56,7 @@ def get_osm_route(profile: str, origin_lat: str, origin_lon: str, destination_la
5656
profile_num = "2"
5757
url = f'https://routing.openstreetmap.de/{profile}/route/v1/driving/{origin_lon},{origin_lat};{destination_lon},{destination_lat}?overview=false&steps=true'
5858
map_url = f' https://routing.openstreetmap.de/?loc={origin_lat}%2C{origin_lon}&loc={destination_lat}%2C{destination_lon}&srv={profile_num}'
59-
res = httpx.get(url)
59+
res = niquests.get(url)
6060
json = res.json()
6161
return {'route_json_description': json, 'map_url': map_url}
6262

@@ -70,7 +70,7 @@ def get_osm_link(location: str):
7070
:return: URL
7171
"""
7272

73-
res = httpx.get('https://nominatim.openstreetmap.org/search', params={'q': location, 'format': 'json','limit': '1'})
73+
res = niquests.get('https://nominatim.openstreetmap.org/search', params={'q': location, 'format': 'json','limit': '1'})
7474
json = res.json()
7575
if 'error' in json:
7676
raise Exception(json['error'])

ex_app/lib/all_tools/weather.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import typing
44
import urllib.parse
55

6-
import httpx
6+
import niquests
77
from langchain_core.tools import tool
88
from nc_py_api import Nextcloud
99

@@ -20,7 +20,7 @@ def get_current_weather_for_coordinates(lat: str, lon: str) -> dict[str, typing.
2020
:param lon: Longitude
2121
:return:
2222
"""
23-
res = httpx.get('https://api.met.no/weatherapi/locationforecast/2.0/compact', params={
23+
res = niquests.get('https://api.met.no/weatherapi/locationforecast/2.0/compact', params={
2424
'lat': lat,
2525
'lon': lon,
2626
},

ex_app/lib/nc_model.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import typing
66
from typing import Optional, Any, Sequence, Union, Callable
77

8-
import httpx
8+
from niquests import ConnectionError, Timeout
99
from langchain_core.callbacks import CallbackManagerForLLMRun
1010
from langchain_core.language_models import LanguageModelInput
1111
from langchain_core.messages import BaseMessage, AIMessage
@@ -90,11 +90,24 @@ def _generate(
9090

9191
log(nc, LogLvl.DEBUG, task_input)
9292

93-
response = nc.ocs(
94-
"POST",
95-
"/ocs/v1.php/taskprocessing/schedule",
96-
json={"type": "core:text2text:chatwithtools", "appId": "context_agent", "input": task_input},
97-
)
93+
i = 0
94+
while i < 20:
95+
try:
96+
response = nc.ocs(
97+
"POST",
98+
"/ocs/v1.php/taskprocessing/schedule",
99+
json={"type": "core:text2text:chatwithtools", "appId": "context_agent", "input": task_input},
100+
)
101+
break
102+
except (
103+
ConnectionError,
104+
Timeout
105+
106+
) as e:
107+
log(nc, LogLvl.DEBUG, "Ignored error during task scheduling")
108+
i += 1
109+
sleep(1)
110+
continue
98111

99112
try:
100113
task = Response.model_validate(response).task
@@ -109,10 +122,9 @@ def _generate(
109122
try:
110123
response = nc.ocs("GET", f"/ocs/v1.php/taskprocessing/task/{task.id}")
111124
except (
112-
httpx.RemoteProtocolError,
113-
httpx.ReadError,
114-
httpx.LocalProtocolError,
115-
httpx.PoolTimeout,
125+
ConnectionError,
126+
Timeout
127+
116128
) as e:
117129
log(nc, LogLvl.DEBUG, "Ignored error during task polling")
118130
time.sleep(5)

0 commit comments

Comments
 (0)