|
| 1 | +import logging |
| 2 | +from odoo import _, api, fields, models |
| 3 | +from odoo.tools import safe_eval |
| 4 | + |
| 5 | +_logger = logging.getLogger(__name__) |
| 6 | + |
| 7 | +class BaseAutomation(models.Model): |
| 8 | + _inherit = "base.automation" |
| 9 | + |
| 10 | + # We need to extend the selection field properly |
| 11 | + # Cannot directly use selection_add because it needs the original selection |
| 12 | + # Instead, we'll override the field completely |
| 13 | + state = fields.Selection(selection=[ |
| 14 | + ('code', 'Execute Python Code'), |
| 15 | + ('object_create', 'Create a new Record'), |
| 16 | + ('object_write', 'Update the Record'), |
| 17 | + ('mail_post', 'Post a Message'), |
| 18 | + ('followers', 'Add Followers'), |
| 19 | + ('next_activity', 'Create Next Activity'), |
| 20 | + ('llm_update', 'Update LLM Document'), |
| 21 | + ], string='Action To Do', |
| 22 | + default='code', required=True, copy=True, |
| 23 | + help="Type of server action") |
| 24 | + |
| 25 | + llm_collection_id = fields.Many2one( |
| 26 | + "llm.document.collection", |
| 27 | + string="LLM Collection", |
| 28 | + ondelete="cascade", |
| 29 | + help="LLM Collection that this automated action is linked to", |
| 30 | + ) |
| 31 | + |
| 32 | + llm_auto_process = fields.Boolean( |
| 33 | + string="Auto Process Documents", |
| 34 | + default=True, |
| 35 | + help="Automatically process documents through the RAG pipeline", |
| 36 | + ) |
| 37 | + |
| 38 | + @api.model |
| 39 | + def _get_states(self): |
| 40 | + """Add llm_update to the states dictionary.""" |
| 41 | + states = super()._get_states() |
| 42 | + states["llm_update"] = _("Update related LLM document") |
| 43 | + return states |
| 44 | + |
| 45 | + def _process_llm_update(self, records): |
| 46 | + """Process the update LLM document action.""" |
| 47 | + self.ensure_one() |
| 48 | + |
| 49 | + if not self.llm_collection_id: |
| 50 | + _logger.error("Cannot execute LLM Update action without a collection") |
| 51 | + return False |
| 52 | + |
| 53 | + collection = self.llm_collection_id |
| 54 | + model_id = self.model_id.id |
| 55 | + |
| 56 | + # Apply filter_domain to get matched records |
| 57 | + domain = self.filter_domain or "[]" |
| 58 | + matched_records = records |
| 59 | + |
| 60 | + # If this isn't on_create, we need to filter the records |
| 61 | + if self.trigger != "on_create" and domain != "[]": |
| 62 | + eval_context = self._get_eval_context() |
| 63 | + domain_result = safe_eval.safe_eval(domain, eval_context) |
| 64 | + matched_records = records.filtered_domain(domain_result) |
| 65 | + |
| 66 | + # Process matched records: either create new or add to collection |
| 67 | + for record in matched_records: |
| 68 | + # Try to find existing document |
| 69 | + existing_doc = self.env["llm.document"].search([ |
| 70 | + ("model_id", "=", model_id), |
| 71 | + ("res_id", "=", record.id) |
| 72 | + ], limit=1) |
| 73 | + |
| 74 | + if existing_doc: |
| 75 | + # If it exists but not in this collection, add it |
| 76 | + if collection.id not in existing_doc.collection_ids.ids: |
| 77 | + existing_doc.write({"collection_ids": [(4, collection.id)]}) |
| 78 | + else: |
| 79 | + # Create a new document |
| 80 | + # Get a meaningful name |
| 81 | + if hasattr(record, "display_name") and record.display_name: |
| 82 | + name = record.display_name |
| 83 | + elif hasattr(record, "name") and record.name: |
| 84 | + name = record.name |
| 85 | + else: |
| 86 | + name = f"{self.model_id.name} #{record.id}" |
| 87 | + |
| 88 | + # Create the document and add to collection |
| 89 | + doc = self.env["llm.document"].create({ |
| 90 | + "name": name, |
| 91 | + "model_id": model_id, |
| 92 | + "res_id": record.id, |
| 93 | + "collection_ids": [(4, collection.id)], |
| 94 | + }) |
| 95 | + |
| 96 | + # Process the document if auto_process is enabled |
| 97 | + if self.llm_auto_process: |
| 98 | + doc.process_document() |
| 99 | + |
| 100 | + # For on_write and on_unlink, handle records that no longer match the domain |
| 101 | + if self.trigger in ["on_write", "on_unlink"]: |
| 102 | + unmatched_records = records - matched_records |
| 103 | + |
| 104 | + for record in unmatched_records: |
| 105 | + # Find document |
| 106 | + doc = self.env["llm.document"].search([ |
| 107 | + ("model_id", "=", model_id), |
| 108 | + ("res_id", "=", record.id) |
| 109 | + ], limit=1) |
| 110 | + |
| 111 | + if doc and collection.id in doc.collection_ids.ids: |
| 112 | + # Remove from this collection |
| 113 | + doc.write({"collection_ids": [(3, collection.id)]}) |
| 114 | + |
| 115 | + # If document doesn't belong to any collection, remove it |
| 116 | + if not doc.collection_ids: |
| 117 | + doc.unlink() |
| 118 | + |
| 119 | + return True |
| 120 | + |
| 121 | + def _process(self, records, domain_post=None): |
| 122 | + """Override _process to handle the llm_update state.""" |
| 123 | + # Handle standard behavior for other states |
| 124 | + if self.state != "llm_update": |
| 125 | + return super()._process(records, domain_post=domain_post) |
| 126 | + |
| 127 | + # Filter out the records on which self has already been done |
| 128 | + action_done = self._context.get("__action_done") or {} |
| 129 | + records_done = action_done.get(self, records.browse()) |
| 130 | + records -= records_done |
| 131 | + if not records: |
| 132 | + return |
| 133 | + |
| 134 | + # Mark the remaining records as done (to avoid recursive processing) |
| 135 | + action_done = dict(action_done) |
| 136 | + action_done[self] = records_done + records |
| 137 | + self = self.with_context(__action_done=action_done) |
| 138 | + records = records.with_context(__action_done=action_done) |
| 139 | + |
| 140 | + # Update document modification date if field exists |
| 141 | + values = {} |
| 142 | + if "date_action_last" in records._fields: |
| 143 | + values["date_action_last"] = fields.Datetime.now() |
| 144 | + if values: |
| 145 | + records.write(values) |
| 146 | + |
| 147 | + # Process the LLM document update |
| 148 | + self._process_llm_update(records) |
0 commit comments