Implement AI workspace and mitigation workflow
This commit is contained in:
@@ -8,7 +8,6 @@ from django.urls import reverse
|
||||
from signalbot import Command, Context, SignalBot
|
||||
|
||||
from core.clients import ClientBase, signalapi
|
||||
from core.lib.prompts.functions import delete_messages, truncate_and_summarize
|
||||
from core.messaging import ai, history, natural, replies, utils
|
||||
from core.models import Chat, Manipulation, PersonIdentifier, QueuedMessage
|
||||
from core.util import logs
|
||||
@@ -25,11 +24,90 @@ SIGNAL_PORT = 8080
|
||||
|
||||
SIGNAL_URL = f"{SIGNAL_HOST}:{SIGNAL_PORT}"
|
||||
|
||||
|
||||
def _get_nested(payload, path):
|
||||
current = payload
|
||||
for key in path:
|
||||
if not isinstance(current, dict):
|
||||
return None
|
||||
current = current.get(key)
|
||||
return current
|
||||
|
||||
|
||||
def _looks_like_signal_attachment(entry):
|
||||
return isinstance(entry, dict) and (
|
||||
"id" in entry or "attachmentId" in entry or "contentType" in entry
|
||||
)
|
||||
|
||||
|
||||
def _normalize_attachment(entry):
|
||||
attachment_id = entry.get("id") or entry.get("attachmentId")
|
||||
if attachment_id is None:
|
||||
return None
|
||||
return {
|
||||
"id": attachment_id,
|
||||
"content_type": entry.get("contentType", "application/octet-stream"),
|
||||
"filename": entry.get("filename") or str(attachment_id),
|
||||
"size": entry.get("size") or 0,
|
||||
"width": entry.get("width"),
|
||||
"height": entry.get("height"),
|
||||
}
|
||||
|
||||
|
||||
def _extract_attachments(raw_payload):
|
||||
envelope = raw_payload.get("envelope", {})
|
||||
candidate_paths = [
|
||||
("dataMessage", "attachments"),
|
||||
("syncMessage", "sentMessage", "attachments"),
|
||||
("syncMessage", "editMessage", "dataMessage", "attachments"),
|
||||
]
|
||||
results = []
|
||||
seen = set()
|
||||
|
||||
for path in candidate_paths:
|
||||
found = _get_nested(envelope, path)
|
||||
if not isinstance(found, list):
|
||||
continue
|
||||
for entry in found:
|
||||
normalized = _normalize_attachment(entry)
|
||||
if not normalized:
|
||||
continue
|
||||
key = str(normalized["id"])
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
results.append(normalized)
|
||||
|
||||
# Fallback: scan for attachment-shaped lists under envelope.
|
||||
if not results:
|
||||
stack = [envelope]
|
||||
while stack:
|
||||
node = stack.pop()
|
||||
if isinstance(node, dict):
|
||||
for value in node.values():
|
||||
stack.append(value)
|
||||
elif isinstance(node, list):
|
||||
if node and all(_looks_like_signal_attachment(item) for item in node):
|
||||
for entry in node:
|
||||
normalized = _normalize_attachment(entry)
|
||||
if not normalized:
|
||||
continue
|
||||
key = str(normalized["id"])
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
results.append(normalized)
|
||||
else:
|
||||
for value in node:
|
||||
stack.append(value)
|
||||
return results
|
||||
|
||||
|
||||
class NewSignalBot(SignalBot):
|
||||
def __init__(self, ur, service, config):
|
||||
self.ur = ur
|
||||
self.service = service
|
||||
self.signal_rest = config["signal_service"] # keep your own copy
|
||||
self.signal_rest = config["signal_service"] # keep your own copy
|
||||
self.phone_number = config["phone_number"]
|
||||
super().__init__(config)
|
||||
self.log = logs.get_logger("signalI")
|
||||
@@ -46,7 +124,9 @@ class NewSignalBot(SignalBot):
|
||||
try:
|
||||
resp = await session.get(uri)
|
||||
if resp.status != 200:
|
||||
self.log.error(f"contacts lookup failed: {resp.status} {await resp.text()}")
|
||||
self.log.error(
|
||||
f"contacts lookup failed: {resp.status} {await resp.text()}"
|
||||
)
|
||||
return None
|
||||
|
||||
contacts_data = await resp.json()
|
||||
@@ -95,6 +175,7 @@ class HandleMessage(Command):
|
||||
self.ur = ur
|
||||
self.service = service
|
||||
return super().__init__(*args, **kwargs)
|
||||
|
||||
async def handle(self, c: Context):
|
||||
msg = {
|
||||
"source": c.message.source,
|
||||
@@ -106,10 +187,15 @@ class HandleMessage(Command):
|
||||
"group": c.message.group,
|
||||
"reaction": c.message.reaction,
|
||||
"mentions": c.message.mentions,
|
||||
"raw_message": c.message.raw_message
|
||||
"raw_message": c.message.raw_message,
|
||||
}
|
||||
raw = json.loads(c.message.raw_message)
|
||||
dest = raw.get("envelope", {}).get("syncMessage", {}).get("sentMessage", {}).get("destinationUuid")
|
||||
dest = (
|
||||
raw.get("envelope", {})
|
||||
.get("syncMessage", {})
|
||||
.get("sentMessage", {})
|
||||
.get("destinationUuid")
|
||||
)
|
||||
|
||||
account = raw.get("account", "")
|
||||
source_name = raw.get("envelope", {}).get("sourceName", "")
|
||||
@@ -125,9 +211,9 @@ class HandleMessage(Command):
|
||||
is_from_bot = source_uuid == c.bot.bot_uuid
|
||||
is_to_bot = dest == c.bot.bot_uuid or dest is None
|
||||
|
||||
reply_to_self = same_recipient and is_from_bot # Reply
|
||||
reply_to_others = is_to_bot and not same_recipient # Reply
|
||||
is_outgoing_message = is_from_bot and not is_to_bot # Do not reply
|
||||
reply_to_self = same_recipient and is_from_bot # Reply
|
||||
reply_to_others = is_to_bot and not same_recipient # Reply
|
||||
is_outgoing_message = is_from_bot and not is_to_bot # Do not reply
|
||||
|
||||
# Determine the identifier to use
|
||||
identifier_uuid = dest if is_from_bot else source_uuid
|
||||
@@ -135,20 +221,8 @@ class HandleMessage(Command):
|
||||
log.warning("No Signal identifier available for message routing.")
|
||||
return
|
||||
|
||||
# Handle attachments
|
||||
attachments = raw.get("envelope", {}).get("syncMessage", {}).get("sentMessage", {}).get("attachments", [])
|
||||
if not attachments:
|
||||
attachments = raw.get("envelope", {}).get("dataMessage", {}).get("attachments", [])
|
||||
attachment_list = []
|
||||
for attachment in attachments:
|
||||
attachment_list.append({
|
||||
"id": attachment["id"],
|
||||
"content_type": attachment["contentType"],
|
||||
"filename": attachment["filename"],
|
||||
"size": attachment["size"],
|
||||
"width": attachment.get("width"),
|
||||
"height": attachment.get("height"),
|
||||
})
|
||||
# Handle attachments across multiple Signal payload variants.
|
||||
attachment_list = _extract_attachments(raw)
|
||||
|
||||
# Get users/person identifiers for this Signal sender/recipient.
|
||||
identifiers = await sync_to_async(list)(
|
||||
@@ -160,9 +234,16 @@ class HandleMessage(Command):
|
||||
xmpp_attachments = []
|
||||
|
||||
# Asynchronously fetch all attachments
|
||||
tasks = [signalapi.fetch_signal_attachment(att["id"]) for att in attachment_list]
|
||||
fetched_attachments = await asyncio.gather(*tasks)
|
||||
log.info(f"ATTACHMENT LIST {attachment_list}")
|
||||
if attachment_list:
|
||||
tasks = [
|
||||
signalapi.fetch_signal_attachment(att["id"]) for att in attachment_list
|
||||
]
|
||||
fetched_attachments = await asyncio.gather(*tasks)
|
||||
else:
|
||||
envelope = raw.get("envelope", {})
|
||||
log.info(f"No attachments found. Envelope keys: {list(envelope.keys())}")
|
||||
fetched_attachments = []
|
||||
|
||||
for fetched, att in zip(fetched_attachments, attachment_list):
|
||||
if not fetched:
|
||||
@@ -170,12 +251,14 @@ class HandleMessage(Command):
|
||||
continue
|
||||
|
||||
# Attach fetched file to XMPP
|
||||
xmpp_attachments.append({
|
||||
"content": fetched["content"],
|
||||
"content_type": fetched["content_type"],
|
||||
"filename": fetched["filename"],
|
||||
"size": fetched["size"],
|
||||
})
|
||||
xmpp_attachments.append(
|
||||
{
|
||||
"content": fetched["content"],
|
||||
"content_type": fetched["content_type"],
|
||||
"filename": fetched["filename"],
|
||||
"size": fetched["size"],
|
||||
}
|
||||
)
|
||||
|
||||
# Forward incoming Signal messages to XMPP and apply mutate rules.
|
||||
for identifier in identifiers:
|
||||
@@ -200,7 +283,9 @@ class HandleMessage(Command):
|
||||
)
|
||||
log.info("Running Signal mutate prompt")
|
||||
result = await ai.run_prompt(prompt, manip.ai)
|
||||
log.info(f"Sending {len(xmpp_attachments)} attachments from Signal to XMPP.")
|
||||
log.info(
|
||||
f"Sending {len(xmpp_attachments)} attachments from Signal to XMPP."
|
||||
)
|
||||
await self.ur.xmpp.client.send_from_external(
|
||||
user,
|
||||
identifier,
|
||||
@@ -209,7 +294,9 @@ class HandleMessage(Command):
|
||||
attachments=xmpp_attachments,
|
||||
)
|
||||
else:
|
||||
log.info(f"Sending {len(xmpp_attachments)} attachments from Signal to XMPP.")
|
||||
log.info(
|
||||
f"Sending {len(xmpp_attachments)} attachments from Signal to XMPP."
|
||||
)
|
||||
await self.ur.xmpp.client.send_from_external(
|
||||
user,
|
||||
identifier,
|
||||
@@ -219,9 +306,7 @@ class HandleMessage(Command):
|
||||
)
|
||||
|
||||
# TODO: Permission checks
|
||||
manips = await sync_to_async(list)(
|
||||
Manipulation.objects.filter(enabled=True)
|
||||
)
|
||||
manips = await sync_to_async(list)(Manipulation.objects.filter(enabled=True))
|
||||
session_cache = {}
|
||||
stored_messages = set()
|
||||
for manip in manips:
|
||||
@@ -233,7 +318,9 @@ class HandleMessage(Command):
|
||||
person__in=manip.group.people.all(),
|
||||
)
|
||||
except PersonIdentifier.DoesNotExist:
|
||||
log.warning(f"{manip.name}: Message from unknown identifier {identifier_uuid}.")
|
||||
log.warning(
|
||||
f"{manip.name}: Message from unknown identifier {identifier_uuid}."
|
||||
)
|
||||
continue
|
||||
|
||||
# Find/create ChatSession once per user/person.
|
||||
@@ -241,7 +328,9 @@ class HandleMessage(Command):
|
||||
if session_key in session_cache:
|
||||
chat_session = session_cache[session_key]
|
||||
else:
|
||||
chat_session = await history.get_chat_session(manip.user, person_identifier)
|
||||
chat_session = await history.get_chat_session(
|
||||
manip.user, person_identifier
|
||||
)
|
||||
session_cache[session_key] = chat_session
|
||||
|
||||
# Store each incoming/outgoing event once per session.
|
||||
@@ -270,10 +359,7 @@ class HandleMessage(Command):
|
||||
elif manip.mode in ["active", "notify", "instant"]:
|
||||
await utils.update_last_interaction(chat_session)
|
||||
prompt = replies.generate_reply_prompt(
|
||||
msg,
|
||||
person_identifier.person,
|
||||
manip,
|
||||
chat_history
|
||||
msg, person_identifier.person, manip, chat_history
|
||||
)
|
||||
|
||||
log.info("Running context prompt")
|
||||
@@ -307,14 +393,13 @@ class HandleMessage(Command):
|
||||
custom_author="BOT",
|
||||
)
|
||||
|
||||
await delete_messages(existing_queue)
|
||||
await history.delete_queryset(existing_queue)
|
||||
qm = await history.store_own_message(
|
||||
session=chat_session,
|
||||
text=result,
|
||||
ts=ts + 1,
|
||||
manip=manip,
|
||||
queue=True,
|
||||
|
||||
)
|
||||
accept = reverse(
|
||||
"message_accept_api", kwargs={"message_id": qm.id}
|
||||
@@ -333,9 +418,6 @@ class HandleMessage(Command):
|
||||
else:
|
||||
log.error(f"Mode {manip.mode} is not implemented")
|
||||
|
||||
# Manage truncation & summarization
|
||||
await truncate_and_summarize(chat_session, manip.ai)
|
||||
|
||||
await sync_to_async(Chat.objects.update_or_create)(
|
||||
source_uuid=source_uuid,
|
||||
defaults={
|
||||
@@ -353,9 +435,10 @@ class SignalClient(ClientBase):
|
||||
ur,
|
||||
self.service,
|
||||
{
|
||||
"signal_service": SIGNAL_URL,
|
||||
"phone_number": "+447490296227",
|
||||
})
|
||||
"signal_service": SIGNAL_URL,
|
||||
"phone_number": "+447490296227",
|
||||
},
|
||||
)
|
||||
|
||||
self.client.register(HandleMessage(self.ur, self.service))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user