Files
dn20/ai_documents/wizard/ai_documents_sort.py
2026-11-08 20:08:53 +08:00

167 lines
6.6 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, Command, api, fields, models
from odoo.tools import is_html_empty
from markupsafe import Markup
class AiDocumentsSort(models.TransientModel):
"""Wizard responsible to create the Automation Rule and the server action to auto sort files."""
_name = "ai_documents.sort"
_description = "Ai Documents Sort"
@api.model
def default_get(self, fields):
values = super().default_get(fields)
tool_actions = self.env["ir.actions.server"]
if "folder_id" in values and "ai_tool_ids" in fields:
existing_ir_action = self.env["ir.actions.server"].search(
[("ai_autosort_folder_id", "=", values["folder_id"])],
limit=1,
)
if existing_ir_action:
# The folder is auto-sorted
values["ai_sort_prompt"] = self.env["documents.document"].browse(values["folder_id"]).ai_sort_prompt
values["ai_tool_ids"] = [Command.set(existing_ir_action.ai_tool_ids.ids)]
return values
# By default, add all pinned action that can be used as AI tools
folder = self.env["documents.document"].browse(values["folder_id"])
tool_actions = self._get_folder_actions(folder)
if "ai_tool_ids" in fields:
tools = ["ai_documents.ir_actions_server_move_in_folder", "ai_documents.ir_actions_server_rename_file"]
for tool_name in tools:
if tool := self.env.ref(tool_name, raise_if_not_found=False):
tool_actions |= tool
values["ai_tool_ids"] = [Command.set(tool_actions.ids)]
finance_folder = self.env.ref("documents.document_finance_folder", raise_if_not_found=False)
if "ai_sort_prompt" not in fields or not finance_folder:
return values
ai_sort_prompts = [
_("Before starting, analyze the content, metadata, and messages."),
_("Rename the file using: [ORIGIN]-[SENDER]-[DOC_TYPE]-[DATE]."),
"",
_(
"If the document is an invoice, send it to %s",
self.env["documents.document"]._ai_folder_insert(finance_folder.id),
),
]
values["ai_sort_prompt"] = Markup("<br/>").join(ai_sort_prompts)
return values
@api.model
def _activate_demo_prompt(self, folder_id):
"""Activate the demo prompt on the given folder."""
prompt_lines, ai_tool_ids = self._get_demo_prompt_data()
if not prompt_lines:
return
self.create({
"folder_id": folder_id,
"ai_sort_prompt": Markup("<div>%s</div>") % Markup("<br/>").join(prompt_lines),
"ai_tool_ids": ai_tool_ids.ids,
}).action_setup_folder()
@api.model
def _get_demo_prompt_data(self):
"""Return demo prompt lines and tool IDs.
:return: tuple(list of prompt line strings, list of ir.actions.server)
"""
finance_folder = self.env.ref("documents.document_finance_folder", raise_if_not_found=False)
prompt = [
_("Before starting, analyze the content, metadata, and messages."),
_("Rename the file using: [ORIGIN]-[SENDER]-[DOC_TYPE]-[DATE]."),
"",
]
if finance_folder:
prompt.append(
_(
"If the document is an invoice, send it to %s",
self.env["documents.document"]._ai_folder_insert(finance_folder.id),
)
)
ai_tool_ids = self.env["ir.actions.server"]
tools = ["ai_documents.ir_actions_server_move_in_folder", "ai_documents.ir_actions_server_rename_file"]
for tool_name in tools:
if tool := self.env.ref(tool_name, raise_if_not_found=False):
ai_tool_ids |= tool
return prompt, ai_tool_ids
folder_id = fields.Many2one(
"documents.document",
string="Folder",
domain=[("type", "=", "folder"), ("shortcut_document_id", "=", False)],
required=True,
)
ai_sort_prompt = fields.Html(string="AI Folder Sort Prompt")
ai_tool_ids = fields.Many2many(
"ir.actions.server",
string="Additional Tools",
domain="[('id', 'in', allowed_tools_ids)]",
)
allowed_tools_ids = fields.Many2many(
"ir.actions.server",
string="Allowed Tools",
compute="_compute_allowed_tools_ids",
)
# UI fields
model = fields.Char(compute="_compute_ui_fields")
relation = fields.Char(compute="_compute_ui_fields")
is_ai_sort_prompt_set = fields.Boolean("Is Prompt Set", compute="_compute_is_prompt_set")
def _compute_ui_fields(self):
# Because the widget is generic for all models / fields
# we simply create "UI" field instead of overwriting the widget
self.model = "documents.document"
self.relation = "documents.document"
@api.depends("ai_sort_prompt")
def _compute_is_prompt_set(self):
for record in self:
record.is_ai_sort_prompt_set = not is_html_empty(record.folder_id.ai_sort_prompt)
@api.depends("folder_id")
def _compute_allowed_tools_ids(self):
documents_tools = self.env["ir.actions.server"].search([
("model_id.model", "=", "documents.document"),
("usage", "in", ["documents_embedded", "ir_actions_server"]),
])
documents_tools = documents_tools.filtered(lambda d: d.ai_tool_is_candidate)
self.allowed_tools_ids = documents_tools
def action_setup_folder(self):
self.folder_id.ai_sort_prompt = self.ai_sort_prompt
self.folder_id._ai_setup_sort_actions(self.ai_tool_ids.ids)
def action_delete(self):
self.folder_id.ai_sort_prompt = False
self.folder_id._ai_setup_sort_actions(self.ai_tool_ids.ids)
def _get_folder_actions(self, folder):
"""Return the actions that can be turned into AI tools."""
embedded_server_actions = (
self.env["documents.document"]
._get_folder_embedded_actions(folder.ids)
.get(folder.id)
)
if not embedded_server_actions:
return self.env["ir.actions.server"]
tool_actions = embedded_server_actions.action_id.filtered(lambda a: a.type == "ir.actions.server")
# Convert the `ir.actions.actions` to `ir.actions.server`
# (those models share the same ids index, see `base/data/base_data.sql`)
tool_actions = self.env["ir.actions.server"].browse(tool_actions.ids)
return tool_actions.filtered("ai_tool_is_candidate").filtered("use_in_ai")