error handling, message sanitation
This commit is contained in:
parent
d82288279f
commit
043077658d
2 changed files with 42 additions and 8 deletions
48
__init__.py
48
__init__.py
|
@ -1,5 +1,3 @@
|
|||
# Copyright 2025 Rudis Muiznieks
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
|
@ -15,6 +13,7 @@
|
|||
from ovos_workshop.skills.fallback import FallbackSkill
|
||||
import requests
|
||||
import json
|
||||
import re
|
||||
|
||||
|
||||
class KagiSkill(FallbackSkill):
|
||||
|
@ -25,7 +24,7 @@ class KagiSkill(FallbackSkill):
|
|||
def handle_fallback_kagi(self, message):
|
||||
if "kagiApiKey" not in self.settings:
|
||||
self.log.error(
|
||||
"Kagi not configured yet, please set your kagiApiKey in %s",
|
||||
"kagi not configured yet, please set your kagiApiKey in %s",
|
||||
self.settings.path,
|
||||
)
|
||||
return False # not configured yet
|
||||
|
@ -41,9 +40,44 @@ class KagiSkill(FallbackSkill):
|
|||
data=js_data,
|
||||
headers=header_content,
|
||||
verify=False)
|
||||
self.log.error(f'received: {response.text}')
|
||||
json_data = json.loads(response.text)
|
||||
dirty_response = json_data["data"]["output"]
|
||||
# TODO: remove [n] reference tokens
|
||||
self.speak(dirty_response)
|
||||
dirty_response = self.get_kagi_response(json_data)
|
||||
if dirty_response is None:
|
||||
self.speak("I wasn't able to retrieve an answer.")
|
||||
else:
|
||||
clean_response = self.clean_string(dirty_response)
|
||||
self.speak(clean_response)
|
||||
return True
|
||||
|
||||
def clean_string(self, text):
|
||||
# Remove asterisks and underscores
|
||||
text = text.replace('*', '').replace('_', '')
|
||||
|
||||
# Remove numbers in square brackets (e.g., [1], [12])
|
||||
text = re.sub(r'\[\d+\]', '', text)
|
||||
|
||||
return text
|
||||
|
||||
def get_kagi_response(self, json_data):
|
||||
try:
|
||||
# Check if "data" exists and is a dict
|
||||
if not isinstance(json_data.get("data"), dict):
|
||||
self.log.error("'data' missing from kagi response")
|
||||
return None
|
||||
|
||||
# Check if "output" exists in data
|
||||
if "output" not in json_data["data"]:
|
||||
self.log.error("'output' missing from kagi response")
|
||||
return None
|
||||
|
||||
# Check if the output value is a string
|
||||
output_value = json_data["data"]["output"]
|
||||
if not isinstance(output_value, str):
|
||||
self.log.error(f"'output' was not a string in kagi response (found {type(output_value).__name__})")
|
||||
return None
|
||||
|
||||
return output_value
|
||||
|
||||
except Exception as e:
|
||||
self.log.error(f"error parsing kagi response: {str(e)}")
|
||||
return None
|
||||
|
|
2
setup.py
2
setup.py
|
@ -4,7 +4,7 @@ import os
|
|||
from os import walk, path
|
||||
|
||||
PYPI_NAME = "skill-ovos-fallback-kagi" # pip install PYPI_NAME
|
||||
VERSION = "0.0.3"
|
||||
VERSION = "0.0.4"
|
||||
URL = f"https://code.sitosis.com/rudism/{PYPI_NAME}"
|
||||
SKILL_CLAZZ = "KagiSkill" # needs to match __init__.py class name
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue