Spaces:
Sleeping
Sleeping
File size: 7,630 Bytes
1b7e88c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
import json
from omagent_core.clients.base import CallbackBase
from omagent_core.services.connectors.redis import RedisConnector
from omagent_core.utils.logger import logging
from omagent_core.utils.registry import registry
from .schemas import ConversationEvent, InteractionType, MessageType
@registry.register_component()
class AaasCallback(CallbackBase):
redis_stream_client: RedisConnector
bot_id: str = ""
def _create_output_data(
self,
event='',
conversation_id='',
chat_id='',
agent_id='',
status='',
contentType='',
content='',
type='',
is_finish=True
):
data = {
'content': json.dumps({
'event': event,
'data': {
'conversationId': conversation_id,
'chatId': chat_id,
'agentId': agent_id,
'createTime': None,
'endTime': None,
'status': status,
'contentType': contentType,
'content': content,
'type': type,
'isFinish': is_finish
}
}, ensure_ascii=False)
}
return data
def send_base_message(
self,
event='',
conversation_id='',
chat_id='',
agent_id='',
status='',
contentType='',
content='',
type='',
is_finish=True
):
stream_name = f"agent_os:conversation:output:{conversation_id}"
group_name = "OmAaasAgentConsumerGroup" # replace with your consumer group name
message = self._create_output_data(
event=event,
conversation_id=conversation_id,
chat_id=chat_id,
agent_id=agent_id,
status=status,
contentType=contentType,
content=content,
type=type,
is_finish=is_finish
)
self.send_to_group(stream_name, group_name, message)
def send_to_group(self, stream_name, group_name, data):
logging.info(f"Stream: {stream_name}, Group: {group_name}, Data: {data}")
self.redis_stream_client._client.xadd(stream_name, data)
try:
self.redis_stream_client._client.xgroup_create(
stream_name, group_name, id="0"
)
except Exception as e:
logging.debug(f"Consumer group may already exist: {e}")
@staticmethod
def _parse_workflow_instance_id(data: str):
split_data = data.split('|')
if not split_data:
return {}
result = {}
keys = [
'workflow_instance_id',
'agent_id',
'conversation_id',
'chat_id',
]
for index, value in enumerate(split_data):
if index + 1 <= len(keys):
result.setdefault(keys[index], value)
return result
def send_incomplete(
self,
agent_id,
msg,
took=0,
msg_type=MessageType.TEXT.value,
prompt_tokens=0,
output_tokens=0,
filter_special_symbols=True,
):
result = self._parse_workflow_instance_id(agent_id)
agent_id = result.get('agent_id', '')
conversation_id = result.get('conversation_id', '')
chat_id = result.get('chat_id', '')
self.send_base_message(
event=ConversationEvent.MESSAGE_DELTA.value,
conversation_id=conversation_id,
chat_id=chat_id,
agent_id=agent_id,
status='delta',
contentType=msg_type,
content=msg,
type='answer',
is_finish=False
)
def send_block(
self,
agent_id,
msg,
took=0,
msg_type=MessageType.TEXT.value,
interaction_type=InteractionType.DEFAULT.value,
prompt_tokens=0,
output_tokens=0,
filter_special_symbols=True,
):
result = self._parse_workflow_instance_id(agent_id)
agent_id = result.get('agent_id', '')
conversation_id = result.get('conversation_id', '')
chat_id = result.get('chat_id', '')
if interaction_type == InteractionType.DEFAULT.INPUT:
self.send_base_message(
event=ConversationEvent.MESSAGE_DELTA.value,
conversation_id=conversation_id,
chat_id=chat_id,
agent_id=agent_id,
status='completed',
contentType=msg_type,
content=msg,
type='ask_complete',
is_finish=True
)
return
self.send_base_message(
event=ConversationEvent.MESSAGE_DELTA.value,
conversation_id=conversation_id,
chat_id=chat_id,
agent_id=agent_id,
status='completed',
contentType=msg_type,
content=msg,
type='answer',
is_finish=True
)
def send_answer(
self,
agent_id,
msg,
took=0,
msg_type=MessageType.TEXT.value,
prompt_tokens=0,
output_tokens=0,
filter_special_symbols=True,
):
result = self._parse_workflow_instance_id(agent_id)
agent_id = result.get('agent_id', '')
conversation_id = result.get('conversation_id', '')
chat_id = result.get('chat_id', '')
self.send_base_message(
event=ConversationEvent.MESSAGE_COMPLETED.value,
conversation_id=conversation_id,
chat_id=chat_id,
agent_id=agent_id,
status='completed',
contentType=msg_type,
content=msg,
type='answer',
is_finish=True
)
def info(
self,
agent_id,
msg,
msg_type=MessageType.TEXT.value,
):
result = self._parse_workflow_instance_id(agent_id)
agent_id = result.get('agent_id', '')
conversation_id = result.get('conversation_id', '')
chat_id = result.get('chat_id', '')
self.send_base_message(
event=ConversationEvent.MESSAGE_DELTA.value,
conversation_id=conversation_id,
chat_id=chat_id,
agent_id=agent_id,
status='completed',
contentType=msg_type,
content=msg,
type='runner_output',
is_finish=True
)
def error(
self,
agent_id,
msg,
msg_type=MessageType.TEXT.value,
):
result = self._parse_workflow_instance_id(agent_id)
agent_id = result.get('agent_id', '')
conversation_id = result.get('conversation_id', '')
chat_id = result.get('chat_id', '')
self.send_base_message(
event=ConversationEvent.MESSAGE_ERROR.value,
conversation_id=conversation_id,
chat_id=chat_id,
agent_id=agent_id,
status='completed',
contentType=msg_type,
content=msg,
type='runner_output',
is_finish=True
)
def finish(self, agent_id, took, type, msg, prompt_tokens=0, output_tokens=0):
self.send_answer(
agent_id,
msg=msg
)
|