waldiez 0.1.6__py3-none-any.whl → 0.1.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of waldiez might be problematic. Click here for more details.
- waldiez/__init__.py +0 -2
- waldiez/_version.py +1 -1
- waldiez/cli.py +10 -6
- waldiez/exporting/agents/rag_user/chroma_utils.py +1 -2
- waldiez/exporting/chats/nested.py +2 -12
- waldiez/exporting/utils/logging_utils.py +1 -0
- waldiez/io/__init__.py +142 -0
- waldiez/models/agents/agent/nested_chat.py +4 -8
- waldiez/models/waldiez.py +18 -11
- waldiez/runner.py +24 -10
- waldiez-0.1.8.dist-info/METADATA +241 -0
- {waldiez-0.1.6.dist-info → waldiez-0.1.8.dist-info}/RECORD +15 -19
- {waldiez-0.1.6.dist-info → waldiez-0.1.8.dist-info}/WHEEL +1 -1
- waldiez/io_stream.py +0 -181
- waldiez/stream/__init__.py +0 -7
- waldiez/stream/consumer.py +0 -139
- waldiez/stream/provider.py +0 -339
- waldiez/stream/server.py +0 -412
- waldiez-0.1.6.dist-info/METADATA +0 -180
- {waldiez-0.1.6.dist-info → waldiez-0.1.8.dist-info}/entry_points.txt +0 -0
- {waldiez-0.1.6.dist-info → waldiez-0.1.8.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
waldiez/__init__.py,sha256=
|
|
1
|
+
waldiez/__init__.py,sha256=AuVJ8ZOPmJoTeKQ_ryezU14tG-DFRb7djvr72a5G63Y,251
|
|
2
2
|
waldiez/__main__.py,sha256=9xR-F2ohZcRPDG6KrM7cJpXciKX-u6WdL221ckyJ04k,112
|
|
3
|
-
waldiez/_version.py,sha256=
|
|
4
|
-
waldiez/cli.py,sha256=
|
|
3
|
+
waldiez/_version.py,sha256=H1WLgLEQD6NHtFBb8DOYUVOZxL9vh2tPJJn9fu19O2k,62
|
|
4
|
+
waldiez/cli.py,sha256=wStRkt056Y-F2CcuD6Zlov-ooUpWndyNesV9s_MjyHU,4798
|
|
5
5
|
waldiez/exporter.py,sha256=iKe-l_Me8NRWsXHIdBcrOrnLT9XIyp4iYi4HLuuj2jA,9342
|
|
6
|
-
waldiez/io_stream.py,sha256=YczhIw0PKEsxCSY6tAqElCqOUh8b-aEkSIGC1JjXxAs,5877
|
|
7
6
|
waldiez/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
waldiez/runner.py,sha256=
|
|
7
|
+
waldiez/runner.py,sha256=YDjEyu9zZF2JX22c6C1g1iYwxRDEdP2pPox6m0kJhfw,10194
|
|
9
8
|
waldiez/exporting/__init__.py,sha256=GMY7qTRpNmc7tpaCFNGLT5wX9eu26NvoNPuYX8MzP50,344
|
|
10
9
|
waldiez/exporting/agents/__init__.py,sha256=v5KA112W_EFYwXE2TSBKYyO8rRKUOUAOpFS5CMSnfRs,110
|
|
11
10
|
waldiez/exporting/agents/agent.py,sha256=otNYclKfi57CUNsM0lVLAbD7YPboe_2TnrdllBwgKzE,7228
|
|
@@ -16,7 +15,7 @@ waldiez/exporting/agents/llm_config.py,sha256=A88e-RKp09r8n9MG11hArpITzxK8nVrTZ6
|
|
|
16
15
|
waldiez/exporting/agents/teachability.py,sha256=ame4hHJCZRBp7hAQGZzv2Cjs6QtcV9vlQ1zheMEMac0,1103
|
|
17
16
|
waldiez/exporting/agents/termination_message.py,sha256=tzI4-tcveYKBVx5PsznQZwAoghSX3mbn_vPu4rX8tuU,1276
|
|
18
17
|
waldiez/exporting/agents/rag_user/__init__.py,sha256=01F4gwgUwtSpZbGXcfieqIuLNT64u9KiqMIB2f0mplI,196
|
|
19
|
-
waldiez/exporting/agents/rag_user/chroma_utils.py,sha256=
|
|
18
|
+
waldiez/exporting/agents/rag_user/chroma_utils.py,sha256=4kZor8wgTaA7Mw9lUEHu594QtbTmiD2iZafhmOo7_9s,4579
|
|
20
19
|
waldiez/exporting/agents/rag_user/mongo_utils.py,sha256=y5IL-Anfktg9cYo2o-ED1A7lwHQWdVMWD_W1AT5_RmE,2664
|
|
21
20
|
waldiez/exporting/agents/rag_user/pgvector_utils.py,sha256=EyGDwvo1Pe8bqoJl3NFpqK6zizN81lPPaSMBMQF79Dc,2722
|
|
22
21
|
waldiez/exporting/agents/rag_user/qdrant_utils.py,sha256=tn1A7pjQjJfGS33ALgnPYTz7xu8rVBidcc5-WHLq8e8,3487
|
|
@@ -25,7 +24,7 @@ waldiez/exporting/agents/rag_user/vector_db.py,sha256=64Gr_y1VTZLXi1pC4KjChsZ6DT
|
|
|
25
24
|
waldiez/exporting/chats/__init__.py,sha256=v5aR1gWqSN5xeDDMIFo-ceC_Z9UgL8qJZofC2sU8HqQ,296
|
|
26
25
|
waldiez/exporting/chats/chats.py,sha256=xI5ZzWpcqYz8Kuu7B9pU6iHN16wUwHxOvYFhH5vxWuA,1259
|
|
27
26
|
waldiez/exporting/chats/helpers.py,sha256=K-IwBAG4-t7NLBP8Qs8_a2AEoKyoLw_B6eHzgXpOshA,13352
|
|
28
|
-
waldiez/exporting/chats/nested.py,sha256=
|
|
27
|
+
waldiez/exporting/chats/nested.py,sha256=nYzxOTHSBpyFdqvX_NtZUvUgTdmoTZcCRaGTFzdnmII,7916
|
|
29
28
|
waldiez/exporting/flow/__init__.py,sha256=WhdPrjXQAcihrS1KUtPNgbx0y1tqD5HtGykzpEAcsBM,98
|
|
30
29
|
waldiez/exporting/flow/def_main.py,sha256=YpdhqO4iFng3r7if69ZPMJAibPVNDqnrOrWvGw7CJq8,1052
|
|
31
30
|
waldiez/exporting/flow/flow.py,sha256=x2AlXr7aJJfrPMeuXQYfT9sG5GycMwf0kRkovZWKGag,6206
|
|
@@ -34,13 +33,14 @@ waldiez/exporting/skills/__init__.py,sha256=oIA9f5ABtYSbS0kMY_4ovU3-m6meVk5blEu_
|
|
|
34
33
|
waldiez/exporting/utils/__init__.py,sha256=tP1V4g9-MyARlfOEL_1YWMJNW7UivUrrukq7DIwdq6k,1018
|
|
35
34
|
waldiez/exporting/utils/comments.py,sha256=X9j8w48rh3DfFDjiMverU9DBSuE9yuMMbbStxBbN1sE,3190
|
|
36
35
|
waldiez/exporting/utils/importing.py,sha256=dA4HCQ-OxmodUjovgXyLI9IwNvLwbY67P41969XoZ7g,8649
|
|
37
|
-
waldiez/exporting/utils/logging_utils.py,sha256=
|
|
36
|
+
waldiez/exporting/utils/logging_utils.py,sha256=DfnC13PqnadG0nRxz_gHA4IIZGnMawpTPlkKYdGT94w,5864
|
|
38
37
|
waldiez/exporting/utils/method_utils.py,sha256=7-RUMTylNM2W0iM1bPX2_Gn3553XZSl2s2VGEijxNp4,891
|
|
39
38
|
waldiez/exporting/utils/naming.py,sha256=VdoVODQduhXIs9hQFWUVEVqTaSyNDt7rkECsuIgXYwI,3196
|
|
40
39
|
waldiez/exporting/utils/object_string.py,sha256=2kdIu4in3iUV92a2KbLWwp9POhvY-fzF_r2AGVnCKls,2166
|
|
41
40
|
waldiez/exporting/utils/path_check.py,sha256=laGSLLiiwIIKxpAp5JIbtEvurbvXGkT4Hx0lemJcA9s,976
|
|
41
|
+
waldiez/io/__init__.py,sha256=-w6ywIjG8ByxGtpSeRt1bjhnP4CZzpHf8WRWckjs4ng,5172
|
|
42
42
|
waldiez/models/__init__.py,sha256=IMq8vzuAgmv92kHSSuZQLF38vVd31ojgOHonuHqWYj0,2888
|
|
43
|
-
waldiez/models/waldiez.py,sha256=
|
|
43
|
+
waldiez/models/waldiez.py,sha256=Oxav9QCYlkfxrNLlcJC89gT0UC4Ls5fTn4kL0PypyGw,9066
|
|
44
44
|
waldiez/models/agents/__init__.py,sha256=3ZyVYBHMFzZjRMIdPrBF6HLg82LPAlEubL6utL6KhfU,1856
|
|
45
45
|
waldiez/models/agents/agents.py,sha256=zIqihnoBjzaQLL_P6FcVoHddcusRNYsWPIFLZD091bE,3641
|
|
46
46
|
waldiez/models/agents/agent/__init__.py,sha256=inA0zV3dnwmcQlcObH_FLaZSURjFG31E_XUampJAnJU,746
|
|
@@ -48,7 +48,7 @@ waldiez/models/agents/agent/agent.py,sha256=DAwreQtIdoM2x_vVccIkALl5whyS07GvfKRU
|
|
|
48
48
|
waldiez/models/agents/agent/agent_data.py,sha256=A3qIvOKyUKBIqqPbsBKVE5JTNQ8JcoDEGMqazNkN_rA,5009
|
|
49
49
|
waldiez/models/agents/agent/code_execution.py,sha256=kgL3pbEEyuMvJid0sIbfe4os7SWKpzL1Bv4O525Biyk,1942
|
|
50
50
|
waldiez/models/agents/agent/linked_skill.py,sha256=8RHWHkHXqFuv7lEe1PuQoK1hTO3kBQ7ILKm9kCEWqNs,667
|
|
51
|
-
waldiez/models/agents/agent/nested_chat.py,sha256=
|
|
51
|
+
waldiez/models/agents/agent/nested_chat.py,sha256=VKzHI38CUCnOlB6oBdO7CJdCk-wmG0aGXIkWpJPa4-Q,1757
|
|
52
52
|
waldiez/models/agents/agent/teachability.py,sha256=IIR4LY9dwx3T7Ok17RYN2g6zGiga2gGizGteaeI3eGs,1703
|
|
53
53
|
waldiez/models/agents/agent/termination_message.py,sha256=aGsIdwGs42BxDktkxoqHGEWM1wzhXr-zh_MlETsbycA,5674
|
|
54
54
|
waldiez/models/agents/assistant/__init__.py,sha256=Zlf-4EI9HXl-LrqGosL7UucoyqIl74GZzohZlRLx2QI,175
|
|
@@ -84,12 +84,8 @@ waldiez/models/model/model_data.py,sha256=pDPKUbltaXWjCuDArgwTOEHw_igfk_DkxzFzdn
|
|
|
84
84
|
waldiez/models/skill/__init__.py,sha256=rU88bajKOGMYoHFcE8MP0jW9H0MswbQmvz5wxS35BYE,169
|
|
85
85
|
waldiez/models/skill/skill.py,sha256=fhsAI413an2_d4DBIkf7dzEuWk6rGs2t4sl97a4dj20,3473
|
|
86
86
|
waldiez/models/skill/skill_data.py,sha256=RTWn8Od6w7g-nRIpsS29sqZ8sPm5dCPiK7-qXmU-KD4,815
|
|
87
|
-
waldiez/
|
|
88
|
-
waldiez/
|
|
89
|
-
waldiez/
|
|
90
|
-
waldiez/
|
|
91
|
-
waldiez-0.1.
|
|
92
|
-
waldiez-0.1.6.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
93
|
-
waldiez-0.1.6.dist-info/entry_points.txt,sha256=5Po4yQXPa_QEdtTevpEBgr3rGoIvDMeQuJR2zqwBLBo,45
|
|
94
|
-
waldiez-0.1.6.dist-info/licenses/LICENSE,sha256=VQEHM6WMQLRu1qaGl3GWsoOknDwro-69eGo4NLIJPIM,1064
|
|
95
|
-
waldiez-0.1.6.dist-info/RECORD,,
|
|
87
|
+
waldiez-0.1.8.dist-info/METADATA,sha256=M3jPlHQhUMCvzgvm8LHzaBIjKl2GX8ARC-mtrsz90hE,8679
|
|
88
|
+
waldiez-0.1.8.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
89
|
+
waldiez-0.1.8.dist-info/entry_points.txt,sha256=5Po4yQXPa_QEdtTevpEBgr3rGoIvDMeQuJR2zqwBLBo,45
|
|
90
|
+
waldiez-0.1.8.dist-info/licenses/LICENSE,sha256=VQEHM6WMQLRu1qaGl3GWsoOknDwro-69eGo4NLIJPIM,1064
|
|
91
|
+
waldiez-0.1.8.dist-info/RECORD,,
|
waldiez/io_stream.py
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
"""Custom IOStream class to use with autogen.
|
|
2
|
-
|
|
3
|
-
It is meant to be used when we want to use custom
|
|
4
|
-
`print` and `input`. For example, when a websocket
|
|
5
|
-
is used to trigger a UI element that requires user input.
|
|
6
|
-
and sends back the user's input to the websocket. In the same
|
|
7
|
-
way, we can use it to forward what is meant to be printed.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
We use:
|
|
11
|
-
|
|
12
|
-
- one tcp server to handle messaging between the clients
|
|
13
|
-
- one tcp client (provider) to set and forward the user's input
|
|
14
|
-
that we got elsewhere (e.g. from a websocket connection)
|
|
15
|
-
- one tcp client (consumer) to ask and get the input from the provider
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
import socket
|
|
19
|
-
from contextlib import closing
|
|
20
|
-
from typing import Any, Callable, Optional
|
|
21
|
-
|
|
22
|
-
from autogen.io import IOStream # type: ignore[import-untyped]
|
|
23
|
-
|
|
24
|
-
from .stream import TCPConsumer, TCPProvider, TCPServer
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class WaldiezIOStream(IOStream):
|
|
28
|
-
"""Custom IOStream class to handle the `print` and `input` functions."""
|
|
29
|
-
|
|
30
|
-
def __init__(
|
|
31
|
-
self,
|
|
32
|
-
port: int = 0,
|
|
33
|
-
input_timeout: float = 60,
|
|
34
|
-
print_function: Optional[Callable[..., None]] = None,
|
|
35
|
-
on_prompt_input: Optional[Callable[[str], None]] = None,
|
|
36
|
-
) -> None:
|
|
37
|
-
"""Initialize the IOStream.
|
|
38
|
-
|
|
39
|
-
Parameters
|
|
40
|
-
----------
|
|
41
|
-
port : int, optional
|
|
42
|
-
The port to use, by default 0 (auto-assign).
|
|
43
|
-
input_timeout : float, optional
|
|
44
|
-
The input timeout, by default 60.
|
|
45
|
-
print_function : Optional[Callable[..., None]], optional
|
|
46
|
-
The print function to use, by default None.
|
|
47
|
-
on_prompt_input : Optional[Callable[[str], None]], optional
|
|
48
|
-
The function to call for getting an input, by default None.
|
|
49
|
-
"""
|
|
50
|
-
self._print_function = print_function
|
|
51
|
-
if port == 0:
|
|
52
|
-
port = get_available_port()
|
|
53
|
-
self._port = port
|
|
54
|
-
self._input_timeout = input_timeout
|
|
55
|
-
self._server = TCPServer(port)
|
|
56
|
-
self._server.start()
|
|
57
|
-
self._provider = TCPProvider("localhost", port, response=None)
|
|
58
|
-
self._on_prompt_input = on_prompt_input
|
|
59
|
-
|
|
60
|
-
@property
|
|
61
|
-
def print_function(self) -> Optional[Callable[..., None]]:
|
|
62
|
-
"""Get the print function."""
|
|
63
|
-
return self._print_function
|
|
64
|
-
|
|
65
|
-
def open(self) -> None:
|
|
66
|
-
"""Start the server."""
|
|
67
|
-
if not self._server.is_running():
|
|
68
|
-
self._server.start()
|
|
69
|
-
|
|
70
|
-
def close(self) -> None:
|
|
71
|
-
"""Stop the server and the provider."""
|
|
72
|
-
# pylint: disable=broad-except
|
|
73
|
-
if self._server.is_running():
|
|
74
|
-
try:
|
|
75
|
-
self._server.stop()
|
|
76
|
-
except BaseException: # pragma: no cover
|
|
77
|
-
pass
|
|
78
|
-
try:
|
|
79
|
-
self._provider.stop()
|
|
80
|
-
except BaseException: # pragma: no cover
|
|
81
|
-
pass
|
|
82
|
-
|
|
83
|
-
def __del__(self) -> None: # pragma: no cover
|
|
84
|
-
"""Delete the instance."""
|
|
85
|
-
self.close()
|
|
86
|
-
|
|
87
|
-
def forward_input(self, input_data: str) -> None:
|
|
88
|
-
"""Forward the user's input to the provider.
|
|
89
|
-
|
|
90
|
-
When we have the input data
|
|
91
|
-
e.g. from 'input(..)' or from a websocket connection,
|
|
92
|
-
we can forward it to the provider (the tcp client)
|
|
93
|
-
to make it available to the consumer (the other tcp client).
|
|
94
|
-
Parameters
|
|
95
|
-
----------
|
|
96
|
-
input_data : str
|
|
97
|
-
The input data to forward.
|
|
98
|
-
"""
|
|
99
|
-
if not self._provider.is_running():
|
|
100
|
-
self._provider.start()
|
|
101
|
-
self._provider.set_response(input_data)
|
|
102
|
-
|
|
103
|
-
def print(
|
|
104
|
-
self,
|
|
105
|
-
*objects: Any,
|
|
106
|
-
sep: str = " ",
|
|
107
|
-
end: str = "\n",
|
|
108
|
-
flush: bool = False,
|
|
109
|
-
) -> None:
|
|
110
|
-
"""Mock the print function.
|
|
111
|
-
|
|
112
|
-
Parameters
|
|
113
|
-
----------
|
|
114
|
-
objects : Any
|
|
115
|
-
The objects to print.
|
|
116
|
-
sep : str, optional
|
|
117
|
-
The separator, by default " ".
|
|
118
|
-
end : str, optional
|
|
119
|
-
The end, by default a new line.
|
|
120
|
-
flush : bool, optional
|
|
121
|
-
Whether to flush, by default False.
|
|
122
|
-
"""
|
|
123
|
-
print_function: Callable[..., None] = self.print_function or print
|
|
124
|
-
print_function(*objects, sep=sep, end=end, flush=flush)
|
|
125
|
-
|
|
126
|
-
def input(self, prompt: str = "", *, password: bool = False) -> str:
|
|
127
|
-
"""Mock the input function.
|
|
128
|
-
|
|
129
|
-
Parameters
|
|
130
|
-
----------
|
|
131
|
-
prompt : str, optional
|
|
132
|
-
The prompt to show, by default "".
|
|
133
|
-
password : bool, optional (not used)
|
|
134
|
-
Whether to show the input as password, by default False.
|
|
135
|
-
|
|
136
|
-
Returns
|
|
137
|
-
-------
|
|
138
|
-
str
|
|
139
|
-
The user's input.
|
|
140
|
-
"""
|
|
141
|
-
_prompt = prompt or "Your input:"
|
|
142
|
-
if _prompt in (">", "> "):
|
|
143
|
-
_prompt = "Your input:"
|
|
144
|
-
if prompt:
|
|
145
|
-
if self._on_prompt_input:
|
|
146
|
-
self._on_prompt_input(_prompt)
|
|
147
|
-
self.print(_prompt, end="")
|
|
148
|
-
if not self._provider.is_running():
|
|
149
|
-
self._provider.start()
|
|
150
|
-
# wait for the provider to start
|
|
151
|
-
self._provider.wait(timeout=self._input_timeout)
|
|
152
|
-
if not self._provider.is_running(): # pragma: no cover
|
|
153
|
-
self.print(
|
|
154
|
-
"WARNING: Provider is not running. Was an input expected?"
|
|
155
|
-
)
|
|
156
|
-
return "\n"
|
|
157
|
-
consumer = TCPConsumer(
|
|
158
|
-
"localhost", self._port, timeout=self._input_timeout
|
|
159
|
-
)
|
|
160
|
-
consumer.start()
|
|
161
|
-
# send the prompt and wait for the response
|
|
162
|
-
consumer.send_prompt(_prompt)
|
|
163
|
-
response = consumer.get_response()
|
|
164
|
-
consumer.stop()
|
|
165
|
-
self._provider.stop()
|
|
166
|
-
# return the response or a line break (i.e. no input)
|
|
167
|
-
return response or "\n"
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
def get_available_port() -> int:
|
|
171
|
-
"""Get an available port.
|
|
172
|
-
|
|
173
|
-
Returns
|
|
174
|
-
-------
|
|
175
|
-
int
|
|
176
|
-
Available port.
|
|
177
|
-
"""
|
|
178
|
-
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as soc:
|
|
179
|
-
soc.bind(("", 0))
|
|
180
|
-
soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
181
|
-
return soc.getsockname()[1]
|
waldiez/stream/__init__.py
DELETED
waldiez/stream/consumer.py
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
"""TCP socket input consumer.
|
|
2
|
-
|
|
3
|
-
It connects to a TCP server,
|
|
4
|
-
listens for `INPUT:` messages to get the user's input,
|
|
5
|
-
and sends `REQUEST:` messages to prompt the user,
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
import socket
|
|
9
|
-
import time
|
|
10
|
-
from types import TracebackType
|
|
11
|
-
from typing import Optional, Type
|
|
12
|
-
|
|
13
|
-
END_OF_MESSAGE = b"\r\n"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class TCPConsumer:
|
|
17
|
-
"""TCP socket input consumer."""
|
|
18
|
-
|
|
19
|
-
def __init__(
|
|
20
|
-
self, host: str, port: int, timeout: Optional[float] = None
|
|
21
|
-
) -> None:
|
|
22
|
-
"""Create a new input consumer.
|
|
23
|
-
|
|
24
|
-
Parameters
|
|
25
|
-
----------
|
|
26
|
-
host : str
|
|
27
|
-
The host to connect to.
|
|
28
|
-
port : int
|
|
29
|
-
The port to connect to.
|
|
30
|
-
timeout : float, optional
|
|
31
|
-
The timeout for the consumer, by default None (no timeout).
|
|
32
|
-
"""
|
|
33
|
-
self.host = host
|
|
34
|
-
self.port = port
|
|
35
|
-
self.timeout = timeout
|
|
36
|
-
self._running = False
|
|
37
|
-
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
|
|
38
|
-
|
|
39
|
-
def __enter__(self) -> "TCPConsumer":
|
|
40
|
-
"""Enter the context."""
|
|
41
|
-
self.start()
|
|
42
|
-
return self
|
|
43
|
-
|
|
44
|
-
def __exit__(
|
|
45
|
-
self,
|
|
46
|
-
exc_type: Optional[Type[BaseException]],
|
|
47
|
-
exc_value: Optional[BaseException],
|
|
48
|
-
traceback: Optional[TracebackType],
|
|
49
|
-
) -> None:
|
|
50
|
-
"""Exit the context."""
|
|
51
|
-
self.stop()
|
|
52
|
-
|
|
53
|
-
def is_running(self) -> bool:
|
|
54
|
-
"""Check if the consumer is running.
|
|
55
|
-
|
|
56
|
-
Returns
|
|
57
|
-
-------
|
|
58
|
-
bool
|
|
59
|
-
True is the consumer is running, else False
|
|
60
|
-
"""
|
|
61
|
-
return self._running
|
|
62
|
-
|
|
63
|
-
def start(self) -> None:
|
|
64
|
-
"""Start the consumer."""
|
|
65
|
-
if self._running:
|
|
66
|
-
return
|
|
67
|
-
self._running = True
|
|
68
|
-
self.socket.connect((self.host, self.port))
|
|
69
|
-
self.socket.sendall("CONSUMER\r\n".encode("utf-8"))
|
|
70
|
-
|
|
71
|
-
def _get_response_no_timeout(self) -> Optional[str]:
|
|
72
|
-
"""Get the response."""
|
|
73
|
-
data = self.socket.recv(1024)
|
|
74
|
-
while not data.endswith(END_OF_MESSAGE):
|
|
75
|
-
data += self.socket.recv(1024)
|
|
76
|
-
if data.startswith(b"INPUT:"):
|
|
77
|
-
response = data[len(b"INPUT:") :]
|
|
78
|
-
if response.endswith(END_OF_MESSAGE):
|
|
79
|
-
response = response[: -len(END_OF_MESSAGE)]
|
|
80
|
-
return response.decode("utf-8")
|
|
81
|
-
return None
|
|
82
|
-
|
|
83
|
-
def _get_response_with_timeout(self, timeout: float) -> Optional[str]:
|
|
84
|
-
"""Get the response using a timeout."""
|
|
85
|
-
start_time = time.monotonic()
|
|
86
|
-
data = b""
|
|
87
|
-
self.socket.settimeout(timeout)
|
|
88
|
-
while time.monotonic() - start_time < timeout:
|
|
89
|
-
try:
|
|
90
|
-
data += self.socket.recv(1024)
|
|
91
|
-
except TimeoutError:
|
|
92
|
-
return None
|
|
93
|
-
if data.endswith(END_OF_MESSAGE):
|
|
94
|
-
break
|
|
95
|
-
if not data:
|
|
96
|
-
return None
|
|
97
|
-
if data.startswith(b"INPUT:"):
|
|
98
|
-
response = data[len(b"INPUT:") :]
|
|
99
|
-
if response.endswith(END_OF_MESSAGE):
|
|
100
|
-
response = response[: -len(END_OF_MESSAGE)]
|
|
101
|
-
return response.decode("utf-8")
|
|
102
|
-
return None
|
|
103
|
-
|
|
104
|
-
def get_response(self) -> Optional[str]:
|
|
105
|
-
"""Get the response.
|
|
106
|
-
|
|
107
|
-
Returns
|
|
108
|
-
-------
|
|
109
|
-
Optional[str]
|
|
110
|
-
The response if available, None otherwise.
|
|
111
|
-
"""
|
|
112
|
-
if not self._running:
|
|
113
|
-
self.start()
|
|
114
|
-
if self.timeout is None or self.timeout < 1:
|
|
115
|
-
return self._get_response_no_timeout()
|
|
116
|
-
return self._get_response_with_timeout(self.timeout)
|
|
117
|
-
|
|
118
|
-
def send_prompt(self, prompt: str) -> None:
|
|
119
|
-
"""Send a prompt.
|
|
120
|
-
|
|
121
|
-
Parameters
|
|
122
|
-
----------
|
|
123
|
-
prompt : str
|
|
124
|
-
The prompt to send.
|
|
125
|
-
"""
|
|
126
|
-
if not self._running:
|
|
127
|
-
self.start()
|
|
128
|
-
message = f"REQUEST:{prompt}" + "\r\n"
|
|
129
|
-
self.socket.sendall(message.encode("utf-8"))
|
|
130
|
-
|
|
131
|
-
def stop(self) -> None:
|
|
132
|
-
"""Close the consumer."""
|
|
133
|
-
try:
|
|
134
|
-
self.socket.close()
|
|
135
|
-
except OSError:
|
|
136
|
-
pass
|
|
137
|
-
del self.socket
|
|
138
|
-
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
|
|
139
|
-
self._running = False
|