Skip to content

Commit 8278a6b

Browse files
committed
fix: LMAO web api
1 parent ac59904 commit 8278a6b

File tree

2 files changed

+82
-80
lines changed

2 files changed

+82
-80
lines changed

lmao_process_loop_web.py

+79-77
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,18 @@ def _request_wrapper(
104104
if token:
105105
payload["token"] = token
106106

107-
# Set lock
108-
request_lock.acquire()
109-
110107
try:
108+
# Set lock
109+
request_lock.acquire()
111110

112111
# Wait if needed
113112
with cooldown_timer.get_lock():
114113
cooldown_timer_ = cooldown_timer.value
115-
while time.time() - cooldown_timer_ < REQUEST_COOLDOWN:
116-
time.sleep(0.1)
114+
if time.time() - cooldown_timer_ < REQUEST_COOLDOWN:
115+
logging.info("Waiting for cooldown before sending request")
116+
while time.time() - cooldown_timer_ < REQUEST_COOLDOWN:
117+
time.sleep(0.1)
118+
logging.info("Cooldown passed")
117119

118120
with cooldown_timer.get_lock():
119121
cooldown_timer.value = time.time()
@@ -372,7 +374,6 @@ def _lmao_stop_stream_loop() -> None:
372374
logging.warning(f"Exit from {name} loop requested")
373375
break
374376

375-
release_lock = False
376377
request_response = None
377378

378379
try:
@@ -438,79 +439,83 @@ def _lmao_stop_stream_loop() -> None:
438439
users_handler_.set_key(request_response.user_id, "suggestions", [])
439440

440441
# Ask and read stream
441-
release_lock = True
442-
for line in _request_wrapper(
443-
api_url,
444-
"ask",
445-
{name_lmao: module_request},
446-
cooldown_timer,
447-
request_lock,
448-
proxy,
449-
token,
450-
stream=True,
451-
).iter_lines():
452-
if not line:
453-
continue
454-
try:
455-
response = json.loads(line.decode("utf-8"))
456-
except Exception as e:
457-
logging.warning(f"Unable to parse response line as JSON: {e}")
458-
continue
459-
460-
finished = response.get("finished")
461-
conversation_id = response.get("conversation_id")
462-
request_response.response_text = response.get("response")
463-
464-
images = response.get("images")
465-
if images is not None:
466-
request_response.response_images = images[:]
467-
468-
# Format and add attributions
469-
attributions = response.get("attributions")
470-
if attributions is not None and len(attributions) != 0:
471-
response_link_format = messages_.get_message(
472-
"response_link_format", user_id=request_response.user_id
473-
)
474-
request_response.response_text += "\n"
475-
for i, attribution in enumerate(attributions):
476-
request_response.response_text += response_link_format.format(
477-
source_name=str(i + 1), link=attribution.get("url", "")
442+
try:
443+
for line in _request_wrapper(
444+
api_url,
445+
"ask",
446+
{name_lmao: module_request},
447+
cooldown_timer,
448+
request_lock,
449+
proxy,
450+
token,
451+
stream=True,
452+
).iter_lines():
453+
if not line:
454+
continue
455+
try:
456+
response = json.loads(line.decode("utf-8"))
457+
except Exception as e:
458+
logging.warning(f"Unable to parse response line as JSON: {e}")
459+
continue
460+
461+
finished = response.get("finished")
462+
conversation_id = response.get("conversation_id")
463+
request_response.response_text = response.get("response")
464+
465+
images = response.get("images")
466+
if images is not None:
467+
request_response.response_images = images[:]
468+
469+
# Format and add attributions
470+
attributions = response.get("attributions")
471+
if attributions is not None and len(attributions) != 0:
472+
response_link_format = messages_.get_message(
473+
"response_link_format", user_id=request_response.user_id
478474
)
475+
request_response.response_text += "\n"
476+
for i, attribution in enumerate(attributions):
477+
request_response.response_text += response_link_format.format(
478+
source_name=str(i + 1), link=attribution.get("url", "")
479+
)
479480

480-
# Suggestions must be stored as tuples with unique ID for reply-markup
481-
if finished:
482-
suggestions = response.get("suggestions")
483-
if suggestions is not None:
484-
request_response.response_suggestions = []
485-
for suggestion in suggestions:
486-
if not suggestion or len(suggestion) < 1:
487-
continue
488-
id_ = "".join(
489-
random.choices(
490-
string.ascii_uppercase + string.ascii_lowercase + string.digits, k=8
481+
# Suggestions must be stored as tuples with unique ID for reply-markup
482+
if finished:
483+
suggestions = response.get("suggestions")
484+
if suggestions is not None:
485+
request_response.response_suggestions = []
486+
for suggestion in suggestions:
487+
if not suggestion or len(suggestion) < 1:
488+
continue
489+
id_ = "".join(
490+
random.choices(
491+
string.ascii_uppercase + string.ascii_lowercase + string.digits, k=8
492+
)
491493
)
494+
request_response.response_suggestions.append((id_, suggestion))
495+
users_handler_.set_key(
496+
request_response.user_id,
497+
"suggestions",
498+
request_response.response_suggestions,
492499
)
493-
request_response.response_suggestions.append((id_, suggestion))
494-
users_handler_.set_key(
495-
request_response.user_id,
496-
"suggestions",
497-
request_response.response_suggestions,
498-
)
499500

500-
# Check if exit was requested
501-
with lmao_process_running.get_lock():
502-
lmao_process_running_value = lmao_process_running.value
503-
if not lmao_process_running_value:
504-
finished = True
501+
# Check if exit was requested
502+
with lmao_process_running.get_lock():
503+
lmao_process_running_value = lmao_process_running.value
504+
if not lmao_process_running_value:
505+
finished = True
506+
507+
# Send response to the user
508+
async_helper(
509+
send_message_async(config.get("telegram"), messages_, request_response, end=finished)
510+
)
505511

506-
# Send response to the user
507-
async_helper(
508-
send_message_async(config.get("telegram"), messages_, request_response, end=finished)
509-
)
512+
# Exit from stream reader
513+
if not lmao_process_running_value:
514+
break
510515

511-
# Exit from stream reader
512-
if not lmao_process_running_value:
513-
break
516+
# Release lock here, because _request_wrapper will not do it for stream response
517+
finally:
518+
request_lock.release()
514519

515520
# Save conversation ID
516521
logging.info(f"Saving user {request_response.user_id} conversation ID as: name_{conversation_id}")
@@ -577,11 +582,8 @@ def _lmao_stop_stream_loop() -> None:
577582
logging.error(f"{name} error", exc_info=e)
578583
lmao_exceptions_queue.put(e)
579584

580-
# Release lock if needed and return the container
585+
# Return the container
581586
finally:
582-
if release_lock:
583-
request_lock.release()
584-
release_lock = False
585587
if request_response:
586588
lmao_response_queue.put(request_response)
587589

@@ -609,7 +611,7 @@ def _lmao_stop_stream_loop() -> None:
609611
logging.info(f"Trying to close {name}")
610612
try:
611613
# Request module close
612-
_request_wrapper(api_url, "delete", {"module": name_lmao}, cooldown_timer, request_lock, proxy, token)
614+
_request_wrapper(api_url, "close", {"module": name_lmao}, cooldown_timer, request_lock, proxy, token)
613615

614616
# Wait
615617
logging.info(f"Waiting for {name_lmao} module to close")

main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ def main():
166166
# "module_name": ModuleWrapperGlobal,
167167
# ...
168168
# }
169-
for module_name in config.get("modules").get("enabled"):
169+
for module_name in config.get("modules").get("enabled", []):
170170
logging.info(f"Trying to load and initialize {module_name} module")
171171
use_web = (
172172
module_name.startswith("lmao_")
173-
and module_name in config.get("modules").get("lmao_web_for_modules", [])
174-
and "lmao_web_api_url" in config.get("modules")
173+
and module_name in config.get("modules", {}).get("lmao_web_for_modules", [])
174+
and "lmao_web_api_url" in config.get("modules", [])
175175
)
176176
try:
177177
module = module_wrapper_global.ModuleWrapperGlobal(

0 commit comments

Comments
 (0)