Continue AI features and improve protocol support

This commit is contained in:
2026-02-15 16:57:32 +00:00
parent 2d3b8fdac6
commit 85e97e895d
62 changed files with 5472 additions and 441 deletions

View File

@@ -4,12 +4,12 @@ from typing import Annotated, Optional
from uuid import UUID
from asgiref.sync import sync_to_async
from django.conf import settings
from django.utils import timezone as dj_timezone
from pydantic import BaseModel, ValidationError
from core.clients import signal, signalapi
from core.lib.prompts.functions import delete_messages
from core.models import Message, PersonIdentifier, QueuedMessage, User
from core.clients import serviceapi
from core.messaging import natural
from core.models import Message, PersonIdentifier, QueuedMessage
from core.util import logs
log = logs.get_logger("deferred")
@@ -34,12 +34,23 @@ class DeferredRequest(BaseModel):
async def send_message(db_obj):
recipient_uuid = db_obj.session.identifier.identifier
identifier = db_obj.session.identifier
recipient_uuid = identifier.identifier
service = identifier.service
text = db_obj.text
send = lambda x: signalapi.send_message_raw(recipient_uuid, x) # returns ts
start_t = lambda: signalapi.start_typing(recipient_uuid)
stop_t = lambda: signalapi.stop_typing(recipient_uuid)
async def send(value):
return await serviceapi.send_message_raw(
service,
recipient_uuid,
value,
) # returns ts
async def start_t():
return await serviceapi.start_typing(service, recipient_uuid)
async def stop_t():
return await serviceapi.stop_typing(service, recipient_uuid)
tss = await natural.natural_send_message(
text,
@@ -52,13 +63,17 @@ async def send_message(db_obj):
result = [x for x in tss if x] # all trueish ts
if result: # if at least one message was sent
ts1 = result.pop() # pick a time
log.info(f"signal message create {text}")
if isinstance(ts1, bool):
ts1 = int(dj_timezone.now().timestamp() * 1000)
log.info("Stored outbound message for %s: %s", service, text)
await sync_to_async(Message.objects.create)(
user=db_obj.session.user,
session=db_obj.session,
custom_author="BOT",
text=text,
ts=ts1, # use that time in db
delivered_ts=ts1,
read_source_service=service,
)
@@ -86,12 +101,7 @@ async def process_deferred(data: dict, **kwargs):
log.info(f"Didn't get message from {message_id}")
return
if message.session.identifier.service == "signal":
await send_message(message)
else:
log.warning(f"Protocol not supported: {message.session.identifier.service}")
return
await send_message(message)
elif method == "xmpp": # send xmpp message
xmpp = kwargs.get("xmpp")
service = validated_data.service
@@ -107,12 +117,16 @@ async def process_deferred(data: dict, **kwargs):
# attachments = []
# Asynchronously fetch all attachments
tasks = [signalapi.fetch_signal_attachment(att["id"]) for att in attachments]
tasks = [serviceapi.fetch_attachment(service, att) for att in attachments]
fetched_attachments = await asyncio.gather(*tasks)
for fetched, att in zip(fetched_attachments, attachments):
if not fetched:
log.warning(f"Failed to fetch attachment {att['id']} from Signal.")
log.warning(
"Failed to fetch attachment %s from %s.",
att.get("id"),
service,
)
continue
# Attach fetched file to XMPP
@@ -129,7 +143,9 @@ async def process_deferred(data: dict, **kwargs):
user = identifier.user
log.info(
f"Sending {len(xmpp_attachments)} attachments from Signal to XMPP."
"Sending %s attachments from %s to XMPP.",
len(xmpp_attachments),
service,
)
await xmpp.send_from_external(
user,