diff --git a/AgentReact/utils/InterruptPayload.py b/AgentReact/utils/InterruptPayload.py index e02a0eb..d76c236 100644 --- a/AgentReact/utils/InterruptPayload.py +++ b/AgentReact/utils/InterruptPayload.py @@ -1,4 +1,5 @@ from typing import Dict, List +import json class InterruptPayload(): """ @@ -9,9 +10,9 @@ class InterruptPayload(): #EDITED = 2 DENIED = 3 - def __init__(self, fields:Dict): + def __init__(self, fields:Dict, state:int=0): self.__fields = fields - self.__state = None + self.__state = state def __displayKeys(self, keys:List[str]): for i,field in enumerate(keys): @@ -65,6 +66,34 @@ class InterruptPayload(): def isAccepted(self)->bool: return self.__state == InterruptPayload.ACCEPTED + + def toJSON(self, indent:int=None)->str: # Vient de https://github.com/LJ5O/Assistant/blob/main/modules/Brain/src/Json/Types.py + """ + Exporter cet objet vers une String JSON. Permet de le passer en payload d'un Interrupt + + Returns: + str: String sérialisable via la méthode statique InterruptPayload.strImport(string) + """ + return '{"state":'+ str(self.__state) +', "fields": ' + json.dumps(self.__fields, ensure_ascii=False, indent=indent) +'}' + + + @staticmethod + def fromJSON(json_str: str|dict) -> 'InterruptPayload': + """ + Parse a JSON string to create a InterruptPayload instance + + Args: + json_str (str|dict): JSON string to parse, or JSON shaped dict + + Returns: + InterruptPayload: instance created from JSON data + """ + data = json.loads(json_str) if type(json_str) is str else json_str + + state_ = data.get("state", 0) + fields_ = data.get("fields", {}) + + return InterruptPayload(fields=fields_, state=state_) @@ -76,6 +105,12 @@ if __name__ == "__main__": 'Funny_SQL_request': "SELECT * FROM users WHERE username='xX_UsErNaMe_Xx'; DROP TABLE user;--' AND password='1234';" }) - test.humanDisplay() # Et une fois arrivé dans la boucle de gestion des interuptions, cette méthode est appelée + print("AVANT MODIF : " + test.toJSON(3)) + + test2 = InterruptPayload.fromJSON(test.toJSON()) # Import export JSON + + test2.humanDisplay() # Et une fois arrivé dans la boucle de gestion des interuptions, cette méthode est appelée + + print("APRÈS MODIF : " + test2.toJSON(3))