diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index 1221ec037..748abfd00 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -982,6 +982,23 @@ def run_workflows( "triggers, ignoring", extra={"group": logging_group}, ) + elif ( + action.type + == WorkflowAction.WorkflowActionType.APPLY_AI_SUGGESTIONS + ): + if use_overrides: + # The document has not been parsed yet, so there is no + # content for the LLM to make suggestions from + logger.debug( + "Apply AI suggestions action does not apply to " + "consumption triggers, ignoring", + extra={"group": logging_group}, + ) + else: + # Queued rather than run sync + from documents.tasks import apply_ai_suggestions + + apply_ai_suggestions.delay(action.pk, document.pk) if not use_overrides: # limit title to 128 characters diff --git a/src/documents/tasks.py b/src/documents/tasks.py index b0344053c..182dbd2bd 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -713,6 +713,38 @@ def llmindex_index( ) +@shared_task +def apply_ai_suggestions(action_id: int, document_id: int) -> None: + """ + Deferred "apply AI suggestions" workflow action. + """ + from documents.models import WorkflowAction + from documents.workflows.ai import apply_ai_suggestions_to_document + + try: + action = WorkflowAction.objects.get(pk=action_id) + document = Document.objects.select_related("owner").get(pk=document_id) + except (WorkflowAction.DoesNotExist, Document.DoesNotExist): + logger.warning( + "Workflow action %s or document %s no longer exists, " + "not applying AI suggestions", + action_id, + document_id, + ) + return + + if not apply_ai_suggestions_to_document(action, document): + return + + # No document_updated signal to avoid loop + clear_document_caches(document.pk) + index_document.delay(document.pk) + + ai_config = AIConfig() + if ai_config.llm_index_enabled: + update_document_in_llm_index.apply_async(kwargs={"document": document}) + + @shared_task def update_document_in_llm_index(document) -> None: llm_index_add_or_update_document(document)