Skip to content

Commit

Permalink
update: add delta content into realtime data response
Browse files Browse the repository at this point in the history
  • Loading branch information
Maplemx committed Oct 9, 2024
1 parent c55dbb2 commit 91e87b8
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions Agently/plugins/agent_component/Realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self, agent: object):
self._streamed_keys = []
self._ongoing_key = None
self._ongoing_value = None
self._last_value = None

def use_realtime(self):
self._is_enable = True
Expand All @@ -39,6 +40,21 @@ async def __scan_realtime_value(self, realtime_value:dict, prefix:str="", output
else:
if self._ongoing_key == (prefix + key):
self._ongoing_value = value
if self._last_value and isinstance(self._ongoing_value, str):
delta = self._ongoing_value.replace(self._last_value, "")
else:
delta = value
if delta != "":
await self.agent.call_event_listeners(
"response:realtime",
{
"key": self._ongoing_key,
"value": self._ongoing_value,
"delta": delta,
"complete_value": realtime_value,
},
)
self._last_value = value
if self._ongoing_key != prefix + key:
if (prefix + key) not in self._streamed_keys and value not in (None, ""):
if self._ongoing_key != None:
Expand All @@ -47,6 +63,21 @@ async def __scan_realtime_value(self, realtime_value:dict, prefix:str="", output
await self.agent.call_event_listeners(f"key_start:{ self._ongoing_key }", None)
self._streamed_keys.append(prefix + key)
self._ongoing_value = value
if self._last_value and isinstance(self._ongoing_value, str):
delta = self._ongoing_value.replace(self._last_value, "")
else:
delta = value
if delta != "":
await self.agent.call_event_listeners(
"response:realtime",
{
"key": self._ongoing_key,
"value": self._ongoing_value,
"delta": delta,
"complete_value": realtime_value,
},
)
self._last_value = value
if (prefix + key) in self._streamed_keys or value in (None, ""):
continue

Expand All @@ -67,26 +98,24 @@ async def _suffix(self, event:str, data:any):
lexer.append_string(realtime_json_str)
realtime_value = json5.loads(lexer.complete_json())
if self._output_data_frame == None:
await self.agent.call_event_listeners(
"response:realtime",
{
"key": None,
"value": realtime_value,
"complete_value": realtime_value,
},
)
if self._last_value and isinstance(realtime_value, str):
delta = realtime_value.replace(self._last_value, "")
else:
delta = realtime_value
if delta != "":
await self.agent.call_event_listeners(
"response:realtime",
{
"key": None,
"value": realtime_value,
"delta": delta,
"complete_value": realtime_value,
},
)
self._last_value = realtime_value
else:
if isinstance(realtime_value, dict):
await self.__scan_realtime_value(realtime_value, "", self._output_data_frame)
if self._ongoing_key != None:
await self.agent.call_event_listeners(
"response:realtime",
{
"key": self._ongoing_key,
"value": self._ongoing_value,
"complete_value": realtime_value,
},
)

def export(self):
return {
Expand Down

0 comments on commit 91e87b8

Please sign in to comment.