Skip to content

Commit a0f2922

Browse files
Merge branch 'main' into develop
2 parents 9010ea4 + cff1d4a commit a0f2922

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

cheshire_cat_api/cat_client.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ def url(self):
4646
return f"{secure}{self.settings.base_url}{port}"
4747

4848
def _connect(self):
49+
connection_url = f"ws{self.url}/{self.settings.ws.path}"
50+
if self.settings.ws.user_id is not None:
51+
connection_url += f"/{self.settings.ws.user_id}"
4952
""""Connect to the WebSocket in a separate thread"""
5053
self._ws = WebSocketApp(
51-
f"ws{self.url}/{self.settings.ws.path}",
54+
connection_url,
5255
on_message=self.on_ws_message,
5356
on_error=self.on_ws_error,
5457
on_close=self.on_ws_close,
@@ -71,6 +74,7 @@ def run(self):
7174
header_name='access_token',
7275
header_value=self.settings.auth_key
7376
))
77+
time.sleep(2)
7478
self.is_started = True
7579
time.sleep(2)
7680

@@ -125,12 +129,16 @@ def on_ws_close(self, ws, close_status_code: int, msg: str):
125129

126130
print(f"Connection closed: {msg}")
127131

128-
def send(self, message: str, prompt_settings: Dict = {}):
132+
def send(self, message: str, prompt_settings=None, user_id="user", **kwargs):
129133
"""Send a message to WebSocket server using a separate thread"""
134+
if prompt_settings is None:
135+
prompt_settings = {}
130136
if not self.is_closed:
131137
self._ws.send(json.dumps({
132138
"text": message,
133-
"prompt_settings": prompt_settings
139+
"prompt_settings": prompt_settings,
140+
"user_id": user_id,
141+
**kwargs
134142
}))
135143

136144
def close(self):

cheshire_cat_api/utils.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,44 @@ class WebSocketSettings:
2323
Class containing the WebSocket configuration options and variables used by ccat-api package
2424
"""
2525

26-
def __init__(self):
26+
def __init__(self, path='ws', retries=3, delay=5000, on_failed=None, user_id=None):
2727
# Websocket path to use to communicate with the CCat
28-
self.path = 'ws'
28+
self.path = path
2929
# The maximum number of retries before calling on_failed
30-
self.retries = 3
30+
self.retries = retries
3131
# The delay for reconnect, in milliseconds
32-
self.delay = 5000
32+
self.delay = delay
3333
# The function to call after failing to reconnect
34-
self.on_failed = None
34+
self.on_failed = on_failed
35+
# User to connect with
36+
self.user_id = user_id
3537

3638

3739
class Settings:
3840
"""
3941
Class containing all the configuration options and variables used by ccat-api package
4042
"""
4143

42-
def __init__(self):
44+
def __init__(self,
45+
base_url='localhost',
46+
auth_key='',
47+
port=1865,
48+
secure=False,
49+
timeout=10000,
50+
instant=True,
51+
ws=WebSocketSettings(),
52+
):
4353
# The URL to which connect to the Cat
44-
self.base_url = 'localhost'
54+
self.base_url = base_url
4555
# The key to authenticate the Cat endpoints
46-
self.auth_key = ''
56+
self.auth_key = auth_key
4757
# The port to which connect to the Cat
48-
self.port = 1865
58+
self.port = port
4959
# Choose to either use the secure protocol or not
50-
self.secure = False
60+
self.secure = secure
5161
# Timeout for the endpoints, in milliseconds
52-
self.timeout = 10000
62+
self.timeout = timeout
5363
# Choose to either instantly initialize websocket and api client or not
54-
self.instant = True
64+
self.instant = instant
5565
# WebSocket Settings
56-
self.ws = WebSocketSettings()
66+
self.ws = ws

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cheshire_cat_api"
3-
version = "1.0.1"
3+
version = "1.0.2"
44
description = "😸 Cheshire-Cat API Client"
55
authors = ["Chesire Cat AI"]
66
maintainers = [

0 commit comments

Comments
 (0)