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,14 @@
import { _t } from "@web/core/l10n/translation";
import { registry } from "@web/core/registry";
import { aiPrompt } from "@ai/ai_prompt/ai_prompt_field";
class AiSortDocumentsPromptField extends aiPrompt.component {
get missingRecordsMessage() {
return _t("⚠️ Tell the AI where to sort your documents with /record.");
}
}
registry.category("fields").add("ai_sort_documents_prompt", {
...aiPrompt,
component: AiSortDocumentsPromptField,
});

View File

@@ -0,0 +1,25 @@
import { patch } from "@web/core/utils/patch";
import { DocumentsCogMenu } from "@documents/views/cog_menu/documents_cog_menu";
import { DocumentsCogMenuItemAiAutoSort } from "./documents_cog_menu_item_ai_auto_sort";
import { STATIC_COG_GROUP_ACTION_PIN } from "@documents/views/cog_menu/documents_cog_menu_group";
import { useService } from "@web/core/utils/hooks";
import { user } from "@web/core/user";
patch(DocumentsCogMenu.prototype, {
setup() {
this.documentService = useService("document.document");
return super.setup(...arguments);
},
async _registryItems() {
const ret = await super._registryItems();
if (user.hasGroup("base.group_system")) {
ret.push({
Component: DocumentsCogMenuItemAiAutoSort,
groupNumber: STATIC_COG_GROUP_ACTION_PIN,
key: DocumentsCogMenuItemAiAutoSort.name,
});
}
return ret;
},
});

View File

@@ -0,0 +1,25 @@
import { FolderAction } from "@documents/views/action/folder_action";
export class DocumentsCogMenuItemAiAutoSort extends FolderAction {
static template = "ai_documents.DocumentsCogMenuItemAiAutoSort";
setup() {
super.setup();
this.folder = this.env.searchModel.getSelectedFolder();
}
async onSelected() {
if (!this.folder || typeof this.folder.id !== "number") {
return;
}
await this.action.doAction("ai_documents.ai_documents_sort_action", {
additionalContext: {
default_folder_id: this.folder.id,
},
onClose: async () => {
await this.env.searchModel._reloadSearchPanel();
},
});
}
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<templates id="template" xml:space="preserve">
<t t-name="ai_documents.DocumentsCogMenuItemAiAutoSort">
<DropdownItem closingMode="'all'" onSelected.bind="this.onSelected">
<label for="ai_documents_auto_sort" class="d-flex justify-content-between">
<span>
<img src="/ai/static/description/icon.png" width="18"/>
Auto-sort
<i t-if="this.folder.ai_sort_prompt" class="oi ps-1" data-icon="check"/>
</span>
</label>
</DropdownItem>
</t>
</templates>

View File

@@ -0,0 +1,23 @@
import { DocumentService } from "@documents/core/document_service";
import { markup } from "@odoo/owl";
import { patch } from "@web/core/utils/patch";
patch(DocumentService.prototype, {
async start() {
await super.start(...arguments);
this.busService.subscribe(
"ai_documents.auto_sort_notification",
({ message, type, document_name, document_access_url }) => {
const link = markup`<a href="${document_access_url}?documents_init_open_preview=">${document_name}</a>`;
this.notification.add(
markup`<img class="mb-1 me-2" src="/ai/static/description/icon.png" height="20" width="20"/>${link} - ${message}`,
{
type: type,
autocloseDelay: 10000,
}
);
}
);
this.busService.start();
},
});

View File

@@ -0,0 +1,25 @@
import { FileViewer as WebFileViewer } from "@web/core/file_viewer/file_viewer";
import { _t } from "@web/core/l10n/translation";
import { patch } from "@web/core/utils/patch";
patch(WebFileViewer.prototype, {
async openAIChat() {
if (this.documentService) {
if (this.state.file.isVideo || this.state.file.isUrlYoutube) {
this.notification.add(_t("Oops, AI cannot process videos!"), { type: "warning" });
return;
}
const fileName = this.state.file.name || 'File Attachment';
await this.aiChatLauncher.launchAIChat({
interfaceKey: "file_viewer_ai_button",
recordModel: "ir.attachment",
recordId: this.state.file.documentData.attachment_id.id,
channelTitle: fileName,
aiChatSourceId: this.state.file.documentData.attachment_id.id,
});
} else {
return super.openAIChat();
}
},
});

View File

@@ -0,0 +1,3 @@
.o_ai_documents_sort .o_field_ai_prompt .odoo-editor-editable {
min-height: 100px;
}

View File

@@ -0,0 +1,5 @@
import { patch } from "@web/core/utils/patch";
import { DocumentsActivityController } from "@documents/views/activity/documents_activity_controller";
import { AiDocumentsControllerMixin } from "../documents_controller_mixin";
patch(DocumentsActivityController.prototype, AiDocumentsControllerMixin());

View File

@@ -0,0 +1,33 @@
import { STATIC_COG_GROUP_ACTION_BASE } from "@documents/views/cog_menu/documents_cog_menu_group";
import { _t } from "@web/core/l10n/translation";
export const AiDocumentsControllerMixin = () => ({
async onClickAutoSort() {
const documentsIds = this.aiSortableDocuments().map((r) => r.data.id);
const action = await this.orm.call("documents.document", "action_ai_sort", [documentsIds]);
if (action) {
this.actionService.doAction(action);
}
this.documentService.reload();
},
aiSortableDocuments() {
return this.model.targetRecords.filter((r) => r.data.ai_sortable);
},
getTopBarActionMenuItems() {
const menuItems = super.getTopBarActionMenuItems();
return {
...menuItems,
aiAutoSort: {
isAvailable: () =>
this.env.searchModel.getSelectedFolder()?.ai_has_sort_prompt &&
this.aiSortableDocuments().length,
sequence: 0,
description: _t("Sort With AI"),
callback: async () => this.onClickAutoSort(),
groupNumber: STATIC_COG_GROUP_ACTION_BASE,
},
};
},
});

View File

@@ -0,0 +1,5 @@
import { patch } from "@web/core/utils/patch";
import { DocumentsKanbanController } from "@documents/views/kanban/documents_kanban_controller";
import { AiDocumentsControllerMixin } from "../documents_controller_mixin";
patch(DocumentsKanbanController.prototype, AiDocumentsControllerMixin());

View File

@@ -0,0 +1,5 @@
import { patch } from "@web/core/utils/patch";
import { DocumentsListController } from "@documents/views/list/documents_list_controller";
import { AiDocumentsControllerMixin } from "../documents_controller_mixin";
patch(DocumentsListController.prototype, AiDocumentsControllerMixin());

View File

@@ -0,0 +1,52 @@
import { user } from "@web/core/user";
import { describe, expect, test } from "@odoo/hoot";
import { animationFrame, waitFor } from "@odoo/hoot-dom";
import {
contains,
defineModels,
patchWithCleanup,
} from "@web/../tests/web_test_helpers";
import {
DocumentsModels,
getDocumentsTestServerModelsData,
} from "@documents/../tests/helpers/data";
import { makeDocumentsMockEnv } from "@documents/../tests/helpers/model";
import { mountDocumentsKanbanView } from "@documents/../tests/helpers/views/kanban";
describe.current.tags("desktop");
defineModels(DocumentsModels);
test("Admin user should be able to see the Auto-sort item under the cog menu", async function () {
const serverData = getDocumentsTestServerModelsData();
const { name: folder1Name } = serverData["documents.document"][0];
await makeDocumentsMockEnv({ serverData });
patchWithCleanup(user, {
hasGroup: () => true,
});
await mountDocumentsKanbanView();
await waitFor(".o_kanban_renderer");
await contains(`.o_kanban_record:contains(${folder1Name})`).click();
await waitFor(`.o_last_breadcrumb_item:contains('${folder1Name}')`);
await contains(".o_cp_action_menus [data-icon='more_vert']").click();
await waitFor(`.o-dropdown-item`);
expect(".o-dropdown-item:contains('Auto-sort')").toHaveCount(1);
});
test("Basic user shouldn't be able to see the Auto-sort item under the cog menu", async function () {
const serverData = getDocumentsTestServerModelsData();
const { name: folder1Name } = serverData["documents.document"][0];
patchWithCleanup(user, {
hasGroup: (group) => group === "base.group_user",
});
await makeDocumentsMockEnv({ serverData });
await mountDocumentsKanbanView();
await waitFor(".o_kanban_renderer");
await contains(`.o_kanban_record:contains(${folder1Name})`).click();
await waitFor(`.o_last_breadcrumb_item:contains('${folder1Name}')`);
await animationFrame();
expect(".o_cp_action_menus [data-icon='more_vert']").toHaveCount(0);
expect(".o-dropdown-item:contains('Auto-sort')").toHaveCount(0);
});