This commit is contained in:
2026-11-08 20:08:53 +08:00
parent 7526a5fe4d
commit 9b924747c5
2692 changed files with 394867 additions and 0 deletions

View File

@@ -0,0 +1 @@
from .ai_documents_sort import *

View File

@@ -0,0 +1,166 @@
# 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")

View File

@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="ai_documents_sort_view_form" model="ir.ui.view">
<field name="name">ai_documents.sort.view.form</field>
<field name="model">ai_documents.sort</field>
<field name="mode">primary</field>
<field name="arch" type="xml">
<form string="AI Sort">
<sheet>
<button
type="button"
class="btn-close float-end"
aria-label="Close"
special="cancel"
close="1"
string=""
/>
<div class="d-flex flex-column o_ai_documents_sort pb-4">
<strong>
What do you want the AI to do?
</strong>
<field name="folder_id" invisible="1" force_save="1" context="{'default_type': 'folder'}"/> <!-- Force saving -->
<field name="model" invisible="1"/> <!-- Used by the `ai_prompt` widget -->
<field name="relation" invisible="1"/> <!-- Used by the `ai_prompt` widget -->
<field
name="ai_sort_prompt"
widget="ai_sort_documents_prompt"
nolabel="1"
placeholder="eg:
If the documents is an invoice, move it to Finance,
if it's an image, move it to Photos,
otherwise, create an activity 'Document to sort' for Admin
"
options="{
'model_reference_field': 'model',
'record_selector_relation_field': 'relation',
'record_selector_domain': '[(\'type\', \'=\', \'folder\')]',
'show_missing_records_warning': 'true'
}"/>
<strong class="mt-3">What actions can the AI take?</strong>
<field name="ai_tool_ids"
widget="many2many_tags"
options="{'no_quick_create': True, 'on_tag_click': 'open_form'}"
context="{'default_model_id': context.get('default_model_id'), 'default_use_in_ai': True}"
nolabel="1"/>
</div>
</sheet>
<footer>
<button class="btn btn-primary" type="object" name="action_setup_folder" close="1">Save</button>
<button class="btn btn-secondary" special="cancel" close="1"/>
<button
class="btn btn-secondary float-end ms-auto"
icon="delete"
icon_class="oi-filled"
type="object"
name="action_delete"
close="1"
invisible="not is_ai_sort_prompt_set">Delete</button>
</footer>
</form>
</field>
</record>
<record id="ai_documents_sort_action" model="ir.actions.act_window">
<field name="name">AI Sort</field>
<field name="res_model">ai_documents.sort</field>
<field name="view_mode">form</field>
<field name="view_id" ref="ai_documents_sort_view_form"/>
<field name="target">new</field>
<field name="context" eval="{
'header': False,
'default_model_id': ref('documents.model_documents_document'),
}"/>
</record>
</odoo>