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,94 @@
import { proxy } from "@odoo/owl";
import { _t } from "@web/core/l10n/translation";
import { patch } from "@web/core/utils/patch";
import { rpc } from "@web/core/network/rpc";
import { useService } from "@web/core/utils/hooks";
import { AddPageConfirmDialog } from "@website/components/dialog/add_page_dialog";
patch(AddPageConfirmDialog.prototype, {
setup() {
super.setup();
this.notification = useService("notification");
this.state = proxy({
...this.state,
instructions: "",
tone: "",
generateText: false,
loading: false,
});
this.tones = {
concise: {
title: _t("Concise"),
description: "Keep it short and to the point",
},
professional: {
title: _t("Professional"),
description: "Use a formal, professional tone",
},
friendly: {
title: _t("Friendly"),
description: "Keep it relaxed and easygoing",
},
persuasive: {
title: _t("Persuasive"),
description: "Make it more action-oriented",
},
informative: {
title: _t("Informative"),
description: "Make it clear and explanatory",
},
};
},
onChangeGenerateText(value) {
this.state.generateText = value;
},
onToneSelect(ev) {
const tone = ev.target.dataset.tone;
if (tone === this.state.tone) {
this.state.tone = "";
return;
}
this.state.tone = tone;
},
get buttonTitle() {
if (this.state.generateText) {
return _t("Create with AI");
}
return _t("Create");
},
async processSectionsArch() {
if (this.state.sectionsArch) {
const aiGeneratedContent = await rpc("/ai_website/generate_page", {
instructions: this.state.instructions || "",
name: this.state.name,
sectionsArch: this.state.sectionsArch,
tone: this.state.tone ? this.tones[this.state.tone] : "",
templateId: this.state.templateId || "",
});
if (aiGeneratedContent && aiGeneratedContent.html) {
if (aiGeneratedContent.error) {
this.notification.add(aiGeneratedContent.error, {
type: "danger",
sticky: true,
});
return false;
}
this.state.sectionsArch = aiGeneratedContent.html;
}
}
},
async addPage() {
if (this.state.generateText) {
this.state.loading = true;
await this.processSectionsArch();
}
await super.addPage();
this.state.loading = false;
},
});

View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="ai_website.AddPageConfirmDialog" t-inherit="website.AddPageConfirmDialog" t-inherit-mode="extension">
<xpath expr="//WebsiteDialog" position="attributes">
<attribute name="primaryTitle">this.buttonTitle</attribute>
</xpath>
<xpath expr="//div[@class='row gy-4']" position="attributes">
<attribute name="t-att-class">this.state.loading ? 'row gy-4 opacity-50 pe-none' : 'row gy-4'</attribute>
</xpath>
<xpath expr="//div[@class='row gy-4']" position="inside">
<div t-if="this.state.loading" class="d-flex justify-content-center w-100 position-absolute" style="bottom: 160px;">
<div class="text-muted">
<i class="oi oi-spin me-2" data-icon="progress_activity"></i>
Generating content...
</div>
</div>
</xpath>
<xpath expr="//Switch[contains(@label.translate, 'Add to menu')]" position="after">
<div t-if="!!this.state.sectionsArch" class="offset-md-3 col-md-9">
<Switch extraClasses="'d-inline-flex align-items-center text-start'"
value="this.state.generateText"
onChange="(checked) => this.onChangeGenerateText(checked)"
labelIcon="'o_ai_icon'"
labelIconClass="'oi-lg o_ai_icon_color'"
label.translate="Generate text"/>
</div>
<t t-if="this.state.generateText">
<label class="col-form-label col-md-3">
Instructions
</label>
<div class="col-md-9">
<textarea t-model.proxy="this.state.instructions"
class="form-control"
rows="4"
placeholder="Describe the page with keywords..."></textarea>
</div>
<div class="btn-group offset-md-3 col-md-9 text-start" role="group" aria-label="Tone Selection">
<t t-foreach="Object.keys(this.tones)" t-as="toneOption" t-key="toneOption">
<button
t-att-class="'btn btn-sm me-1 rounded-pill ' + (this.state.tone === toneOption ? 'btn-primary' : 'btn-outline-secondary')"
t-att-title="this.tones[toneOption].title"
t-on-click="this.onToneSelect"
t-att-data-tone="toneOption"
>
<t t-out="this.tones[toneOption].title"/>
</button>
</t>
</div>
</t>
</xpath>
</t>
</templates>

View File

@@ -0,0 +1,99 @@
import { rpc } from "@web/core/network/rpc";
import { useService } from "@web/core/utils/hooks";
import { patch } from "@web/core/utils/patch";
import { TitleDescription, OptimizeSEODialog, seoContext } from "@website/components/dialog/seo";
const SCHEMA = {
type: "object",
properties: {
title: {
type: "string",
description: "The title of the web page.",
minLength: 50,
},
description: {
type: "string",
description: "The description of the web page.",
minLength: 100,
},
keywords: {
type: "array",
description: "A list of relevant keyword based on the web page content.",
items: {
type: "string",
},
maxItems: 10,
},
},
required: ["title", "description", "keywords"],
additionalProperties: false,
};
const getAISeo = async (component, onlyKeywords = false) => {
/** @type{HTMLElement} */
const pageTextContentEl = component.website.pageDocument.documentElement.querySelector("main");
// Filtering down on actual interesting elements for seo
// getting rid of noise for the agent
const textElements = pageTextContentEl.querySelectorAll("h1, h2, h3, a, p");
// We only keep elements that are visible to the user
// since it would most likely be what the user would
// use if he had to do SEO manually
let pageContentStr = "";
textElements.forEach((element) => {
const isVisible = element.checkVisibility({
checkOpacity: true,
checkVisibilityCSS: true,
});
if (!isVisible) {
return;
}
pageContentStr += `\n\n${element.outerHTML}`;
});
const seoOutput = await rpc("/ai/get_direct_response", {
interface_key: "website_seo_ai",
prompt: JSON.stringify({
website_name: component.website.currentWebsite.name,
language: component.website.currentWebsite.metadata.lang,
website_content: pageContentStr,
}),
schema: SCHEMA,
});
const parsedOutput = JSON.parse(seoOutput);
if (parsedOutput.keywords.length > 0) {
component.seoContext.keywords = parsedOutput.keywords;
}
if (!onlyKeywords) {
component.seoContext.title = parsedOutput.title;
component.seoContext.description = parsedOutput.description;
}
};
patch(TitleDescription.prototype, {
setup() {
super.setup();
this.orm = useService("orm");
},
async autoFill() {
await getAISeo(this);
},
});
patch(OptimizeSEODialog.prototype, {
async save() {
const metaImageSrc = seoContext.metaImage;
if (metaImageSrc) {
const match = metaImageSrc.match(/\/web\/image\/(\d+)-/);
if (match) {
await this.orm.call("ai.attachment.vacuum", "mark_attachments_used", [], {
attachment_ids: [parseInt(match[1])],
});
}
}
await super.save();
},
});

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="ai_website.TitleDescription" t-inherit="website.TitleDescription" t-inherit-mode="extension">
<xpath expr="//span[contains(text(), 'Need help to optimize?')]/following-sibling::button/span" position="replace">
<span><span class="oi-fw o_ai_icon_color d-inline-block align-top me-1" data-icon="o_ai_icon"/> Update with AI</span>
</xpath>
<xpath expr="//span[contains(text(), 'Need help to optimize?')]/following-sibling::button/i" position="replace">
</xpath>
</t>
</templates>